为权重匹配的对战匹配函数添加参赛间隔验证需求
对战匹配函数添加参赛场次间隔限制需求
我正在开发一个对战匹配函数,该函数会将拥有相同weight的两名玩家配对,目前已实现基于相同weight的匹配功能。现在需要添加参赛场次间隔限制:
- 若player1参加了第1场比赛,那么player1的下一场比赛应安排在第4场或第6场。即同一玩家两次参赛之间,需间隔最少3场、最多6场比赛。
当前实现代码
const source = [ { entryID: 1, entryName: "player1", weight: 1900, class:[], }, { entryID: 2, entryName: "player1", weight: 1900, class:[], }, { entryID: 3, entryName: "player2", weight: 1900, class:[], }, { entryID: 4, entryName: "player3", weight: 1900, class:[], }, { entryID: 5, entryName: "player4", weight: 1900, class:[], }, { entryID: 6, entryName: "player5", weight: 1900, class:[], }, { entryID: 7, entryName: "player6", weight: 1900, class:[], }, { entryID: 8, entryName: "player7", weight: 1900, class:[], }, { entryID: 9, entryName: "player8", weight: 1900, class:[], }, { entryID: 10, entryName: "player9", weight: 1900, class:[], }, { entryID: 11, entryName: "player10", weight: 1900, class:[], }, { entryID: 12, entryName: "player11", weight: 1900, class:[], }, { entryID: 13, entryName: "player12", weight: 1900, class:[], }, { entryID: 14, entryName: "player1", weight: 1900, class:[], }, ]; 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( // find a place in a group forEach element, use indx as track g => g.names.size < maxGroupSize // is the group incomplete ? && !g.names.has(el.entryName) // is entryName not in the group list (names Set) ? && (el.weight - g.weight) <= different && !alreadyInRela(g.names, el.entryName) // is (entryName + group list) does not already used ? && el.class.every(c => !g.usedClasses.has(c)) // check class ) if (place < 0) { // not found -> create new group let names = new Set().add(el.entryName) // create new group groups.push({ names, indxs: [indx], weight: el.weight, usedClasses: new Set(el.class) }) // group constitutive info related.push(names) // keep track of group list } else { // find a place in a group groups[place].names.add(el.entryName) // related list is also updated el.class.forEach(c => groups[place].usedClasses.add(c)) // add classes groups[place].indxs.push(indx) // add indx to retreive element in sortedData } }); return groups.reduce((r, g, i) => { // build result 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 }, {}) }
当前输出结果
{ "0_1900_1900": [{ // 第1场比赛 "class": [], "entryID": 1, "entryName": "player1", "weight": 1900 }, { "class": [], "entryID": 3, "entryName": "player2", "weight": 1900 }], "1_1900_1900": [{ // 第2场比赛(此处player1再次被匹配,不符合间隔要求) "class": [], "entryID": 2, "entryName": "player1", "weight": 1900 }, { "class": [], "entryID": 4, "entryName": "player3", "weight": 1900 }], "2_1900_1900": [{ // 第3场比赛 "class": [], "entryID": 5, "entryName": "player4", "weight": 1900 }, { "class": [], "entryID": 6, "entryName": "player5", "weight": 1900 }], "3_1900_1900": [{ // 第4场比赛 "class": [], "entryID": 7, "entryName": "player6", "weight": 1900 }, { "class": [], "entryID": 8, "entryName": "player7", "weight": 1900 }], "4_1900_1900": [{ // 第5场比赛 "class": [], "entryID": 9, "entryName": "player8", "weight": 1900 }, { "class": [], "entryID": 10, "entryName": "player9", "weight": 1900 }], "5_1900_1900": [{ // 第6场比赛 "class": [], "entryID": 11, "entryName": "player10", "weight": 1900 }, { "class": [], "entryID": 12, "entryName": "player11", "weight": 1900 }], "6_1900_1900": [{ // 第7场比赛 "class": [], "entryID": 13, "entryName": "player12", "weight": 1900 }, { "class": [], "entryID": 14, "entryName": "player1", "weight": 1900 }] }
目标输出结果
{ "0_1900_1900": [{// 第1场比赛 "class": [], "entryID": 1, "entryName": "player1", "weight": 1900 }, { "class": [], "entryID": 3, "entryName": "player2", "weight": 1900 }], "1_1900_1900": [{ //第2场比赛 "class": [], "entryID": 4, "entryName": "player3", "weight": 1900 }, { "class": [], "entryID": 5, "entryName": "player4", "weight": 1900 }], "2_1900_1900": [{ //第3场比赛 "class": [], "entryID": 6, "entryName": "player5", "weight": 1900 }, { "class": [], "entryID": 7, "entryName": "player6", "weight": 1900 }], "3_1900_1900": [{ //第4场比赛(此处player1间隔2场后再次匹配,符合要求) "class": [], "entryID": 2, "entryName": "player1", "weight": 1900 }, { "class": [], "entryID": 8, "entryName": "player7", "weight": 1900 }], "4_1900_1900": [{ //第5场比赛 "class": [], "entryID": 9, "entryName": "player8", "weight": 1900 }, { "class": [], "entryID": 10, "entryName": "player9", "weight": 1900 }], "5_1900_1900": [{ //第6场比赛 "class": [], "entryID": 11, "entryName": "player10", "weight": 1900 }, { "class": [], "entryID": 12, "entryName": "player11", "weight": 1900 }], "6_1900_1900": [{ //第7场比赛 "class": [], "entryID": 14, "entryName": "player1", "weight": 1900 }, { "class": [], "entryID": 13, "entryName": "player12", "weight": 1900 }] }
内容的提问来源于stack exchange,提问作者Ulquiorra Schiffer
相关产品推荐
相关产品推荐

