执行目标函数的另一个if语句



我正在制作一个匹配系统,其中2 players with the same weight will be matched。我的目标是当我有nofight时,玩家不应该与条目ID匹配。(更多解释如下(

注意:我的noFight是基于玩家的entryID

const source = [
{
entryID: 1,
entryName: "player1",
weight: 1900,
noFight: 2&3, //Will not be matched with entryID: 2 & 3

},
{
entryID: 2,
entryName: "player2",
weight: 1900,
noFight: 1&3&4, //Will not be matched with entryID: 1 & 2 & 3
},
{
entryID: 3,
entryName: "player3",
weight: 1900,
noFight: 1&2&4, //Will not be matched with entryID: 1 & 2 & 4
},
{
entryID: 4,
entryName: "player4",
weight: 1900,
noFight: 2&3&4, //Will not be matched with entryID: 2 & 3 & 4

},

];
function combine(
data = [],
different = 0,
maxGroupSize = 2,
sortedStatement = (a, b) => a.weight - b.weight
) {
const sortedData = [...data].sort(sortedStatement); // "weight" must be in ascending order
const dataGroups = sortedData.reduce((acc, item) => {
const findedAccItem = acc.find(
(accItem) =>
accItem.length < maxGroupSize && // if the array is not filled
accItem[0].weight + different >= item.weight && // and if the minimum is in the acceptable range
!accItem.find((obj) => obj.entryName === item.entryName /* || obj.entryID === item.nofight */) // I think i need to add more statement here. I have tried  obj.entryID === item.nofight but it is not working.
);
if (findedAccItem) {
findedAccItem.push(item);
} else {
acc.push([item]);
}
return acc;
}, []);
const namedDataGroups = dataGroups.reduce((acc, item, index) => {
// added an index as a prefix because the keys can match
const key = [index, ...item.map((item) => item.weight)].join("_");
acc[key] = item;
return acc;
}, {});
return namedDataGroups;
}
// default example
console.log("Example #1: ", combine(source));

我们可以在代码段上看到entryID: 1 & entryID: 2已经匹配的结果。与entryID: 3 & entryID: 4相同。

现在,如果我实现了无光的目标。结果是这样的:

Example #1:  {
"0_1900_1900": [
{
"entryID": 1,
"entryName": "player1",
"weight": 1900,
"noFight": 2&3,
},
{
"entryID": 4,
"entryName": "player4",
"weight": 1900,
"noFight": 2&3&4,
}
],
}

只匹配了entryID: 1 & entryID: 4

任何帮助都将不胜感激。谢谢

这可能是我的一个问题,但我从未见过在此上下文中使用的语法noFight: 2&3,所以我的第一步是将其更改为数组,而不是像noFight: [2,3]那样。

一旦完成,你所需要做的就是检查noFight数组是否包含当前被检查的战斗机。最后,在设置最终数据组时,您还需要检查这些组是否包含两名战士。由于上一节中的reduce的工作方式,您最终会得到一个数组,其中还包含未匹配的单个玩家。你可能会把这个逻辑推到reduce的某个地方,但它已经足够复杂了,这就完成了任务。

以下是更新后的代码以及对更改的注释。

const source = [
{
entryID: 1,
entryName: "player1",
weight: 1900,
noFight: [2,3], // changed these to arrays
},
{
entryID: 2,
entryName: "player2",
weight: 1900,
noFight: [1,3,4],
},
{
entryID: 3,
entryName: "player3",
weight: 1900,
noFight: [1,2,4],
},
{
entryID: 4,
entryName: "player4",
weight: 1900,
noFight: [2,3,4],
},
{ // added a new player that will fight anyone to test the logic
entryID: 5,
entryName: "player5",
weight: 1900,
noFight: [],
},
];
function combine(
data = [],
different = 0,
maxGroupSize = 2,
sortedStatement = (a, b) => a.weight - b.weight
) {
const sortedData = [...data].sort(sortedStatement);
const dataGroups = sortedData.reduce((acc, item) => {
const findedAccItem = acc.find(
(accItem) =>
accItem.length < maxGroupSize &&
accItem[0].weight + different >= item.weight &&
!accItem.find((obj) => obj.entryName === item.entryName || item.noFight.includes(obj.entryID)) // here we check if the fighter is in the noFight array or not
);

if (findedAccItem) {
findedAccItem.push(item);
} else {
acc.push([item]);
}

return acc;
}, []);
const namedDataGroups = dataGroups.reduce((acc, item, index) => {
if (item.length > 1) { // and finally we check that there are in fact two fighters in the grouping; without this you'll end up with  fighters that didn't get a match in the array as a result of how the reduce above works
const key = [index, ...item.map((item) => item.weight)].join("_");
acc[key] = item;
}
return acc;
}, {});
return namedDataGroups;
}
console.log("Example #1: ", combine(source));


编辑:关于偏好的快速最终思考。就我个人而言,我会完全删除第一个reduce,并选择自己遍历数组。我知道你会失去一些性能,但目前的方法非常难以阅读。当然,这只是我的2c,但如果你将来需要做出改变,这会让生活变得更轻松。

最新更新