You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改匹配系统代码 实现基于自定义体重差值的选手匹配

选手体重差值阈值匹配功能修改方案

1. HTML结构调整

新增阈值输入控件,默认值设为0兼容原有完全匹配逻辑:

<!-- 新增体重差值阈值输入区域 -->
<div class="match-control">
  <label for="weight-diff-threshold">允许最大体重差:</label>
  <input type="number" id="weight-diff-threshold" min="0" step="1" placeholder="请输入阈值" value="0">
  <button id="start-match">开始匹配</button>
</div>

属性min="0"可直接禁止用户输入负数值,避免逻辑异常。

2. Ajax请求逻辑修改

在原有请求参数中新增阈值字段,将用户输入的阈值同步传给后端:

// 匹配按钮点击事件
$('#start-match').on('click', function () {
  // 读取阈值,非数字输入默认取0兼容旧逻辑
  const threshold = Number($('#weight-diff-threshold').val()) || 0;
  $.ajax({
    url: "/api/player/match",
    type: "POST",
    data: {
      // 原有选手列表等参数保持不变
      player_list: getCurrentPlayerList(),
      // 新增阈值参数
      weight_threshold: threshold
    },
    success: function (res) {
      // 原有匹配结果渲染逻辑保持不变
      renderMatchResult(res);
    },
    error: function (err) {
      // 原有错误处理逻辑保持不变
      console.error("匹配失败", err);
    }
  });
})

3. 核心匹配逻辑修改

原有完全相等的判断替换为体重差绝对值判断,优化后的匹配逻辑如下:

  • 先将选手列表按体重升序排序,减少无效对比次数
  • 匹配判断规则改为:两选手体重差的绝对值 小于等于 阈值即判定为匹配
    逻辑通用,可适配任意后端语言,Python示例如下:
def match_players(player_list, weight_threshold):
    matched_groups = []
    used = set()
    # 按体重升序排序
    player_list.sort(key=lambda x: x["weight"])
    for i in range(len(player_list)):
        if i in used:
            continue
        current_group = [player_list[i]]
        used.add(i)
        # 仅和后续选手对比,已排序情况下差值超过阈值即可跳出循环
        for j in range(i+1, len(player_list)):
            if j in used:
                continue
            weight_diff = abs(player_list[j]["weight"] - player_list[i]["weight"])
            if weight_diff <= weight_threshold:
                current_group.append(player_list[j])
                used.add(j)
            else:
                break
        # 仅保留至少2人的有效匹配组
        if len(current_group) >= 2:
            matched_groups.append(current_group)
    return matched_groups

4. 边界场景优化

  • 输入框额外增加非数字输入拦截,避免参数异常
  • 若体重为小数,计算差值前先统一转整数(如乘100保留两位小数精度)后再对比,避免浮点精度问题
  • 阈值为0时逻辑和原有完全匹配逻辑100%一致,无需额外做兼容处理

内容的提问来源于stack exchange,提问作者Dark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 18:36:04