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

匹配函数新增验证:同权重玩家不可同CLASS配对

修改玩家匹配函数,禁止同CLASS玩家配对

已实现按相同weight配对玩家的功能,现在需要新增验证规则:拥有相同CLASS的玩家不能配对(即两个玩家的class数组存在交集时,不能分到同一组)。

修改后的完整代码

const source = [
    {
        entryID: 1,
        entryName: "player1",
        weight: 1900,
        class: ['a', 'b'],
    },
    {
        entryID: 2,
        entryName: "player2",
        weight: 1900,
        class: ['a', 'b'],
    },
    {
        entryID: 3,
        entryName: "player3",
        weight: 1900,
        class: ['c', 'd'],
    },
    {
        entryID: 4,
        entryName: "player4",
        weight: 1900,
        class: ['c', 'd'],
    },
];

console.log(combine(source))

function combine(data = [], different = 0, maxGroupSize = 2) {
    const groups = [], related = [], sortedData = [...data].sort((a, b) => a.weight - b.weight),
        alreadyInRela = (setX, eName) => {
            let list = [...setX, eName]
            return related.some(rela => list.every(l => rela.has(l)))
        };
    
    sortedData.forEach((el, indx) => {
        let place = groups.findIndex( // 为当前玩家寻找合适的组
            g => g.names.size < maxGroupSize              // 组未满员
                && !g.names.has(el.entryName)             // 当前玩家不在该组内
                && (el.weight - g.weight) <= different
                && !alreadyInRela(g.names, el.entryName) // 该玩家组合未被使用过
                // 新增CLASS校验:组内所有玩家与当前玩家无共同CLASS
                && !g.indxs.some(groupInd => {
                    const groupPlayer = sortedData[groupInd];
                    return el.class.some(c => groupPlayer.class.includes(c));
                })
        )

        if (place < 0) { // 未找到合适组,创建新组
            let names = new Set().add(el.entryName)
            groups.push({ names, indxs: [indx], weight: el.weight })
            related.push(names)
        } else { // 加入找到的目标组
            groups[place].names.add(el.entryName)
            groups[place].indxs.push(indx)
        }
    });

    return groups.reduce((r, g, i) => {
        if (g.indxs.length > 1) {
            let key = `${i}_` + g.indxs.map(x => sortedData[x].weight).join('_')
            r[key] = []
            g.indxs.forEach(x => r[key].push(sortedData[x]))
        }
        return r
    }, {})
}

核心修改说明

在findIndex的组筛选条件中新增了CLASS校验逻辑:

  • 遍历目标组内的所有玩家索引,从排序后的数组中取出对应玩家数据
  • 检查当前玩家的class数组是否与组内任意玩家的class数组存在交集
  • 如果存在交集,则跳过该组,确保同一组内的玩家不会有共同CLASS

运行结果

修改后的代码执行后,输出与目标完全一致:

{
    0_1900_1900: [
        {
            class: ["a", "b"],
            entryID: 1,
            entryName: "player1",
            weight: 1900
        },
        {
            class: ["c", "d"],
            entryID: 3,
            entryName: "player3",
            weight: 1900
        }
    ],
    1_1900_1900: [
        {
            class: ["a", "b"],
            entryID: 2,
            entryName: "player2",
            weight: 1900
        },
        {
            class: ["c", "d"],
            entryID: 4,
            entryName: "player4",
            weight: 1900
        }
    ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 11:55:26