嗨,我有一排球员,从这一排球员中,我得到了一个符合两个条件的球员
const condition = (5 / 100) * playerOne.mmr + playerOne.mmr
const condition2 = playerOne.mmr - ((5 / 100) * playerOne.mmr);找一个比我的玩家的MMR值低5%到高5%的玩家
然后我就这么做了
searching(playerOne) {
const condition = (5 / 100) * playerOne.mmr + playerOne.mmr
const condition2 = playerOne.mmr - ((5 / 100) * playerOne.mmr);
const player = playerOne;
const playerTwo = this.players.find((playerTwo) => playerTwo.mmr < condition && playerTwo.mmr > condition2 && playerTwo.id != playerOne.id);
while(!this.players.find((playerTwo) => playerTwo.mmr < condition && playerTwo.mmr > condition2 && playerTwo.id != playerOne.id)){
const playerTwo = this.players.find((playerTwo) => playerTwo.mmr < condition && playerTwo.mmr > condition2 && playerTwo.id != playerOne.id);
console.log(this.players);
}
const matchedPlayers = [
player,
playerTwo
]
// remove matched players from this.players
this.removePlayers(matchedPlayers);
// return new Match with matched players
return matchedPlayers;
}
}如果我找不到兼容的mmr,那就太好了,我会一直走下去
甚至在我的服务器上,我将一个玩家添加到队列中
在我的队列中,虽然我的队列中继续有相同的球员,但我添加到队列中的新球员没有出现
queue.addPlayer(new Player(1,'spt',970));
queue.addPlayer(new Player(2,'test2',1000));
queue.addPlayer(new Player(3,'test3',1050));
queue.addPlayer(new Player(4,'test4',70));
const playerOne = queue.players.find((playerOne) => playerOne.mmr === 70);
const players = queue.searching(playerOne);
queue.addPlayer(new Player(5,'test6',75));我在调用我的搜索函数后添加了我的5个玩家,同时我在队列中放入了console.log,但是只有4个玩家出现了,而不是我的5个玩家,所以它处于无限循环中,我不知道我哪里出了错,我不知道如何修复,或者我的逻辑是否非常失败
[
Player { id: 1, name: 'spt', mmr: 970 },
Player { id: 2, name: 'test2', mmr: 1000 },
Player { id: 3, name: 'test3', mmr: 1050 },
Player { id: 4, name: 'test4', mmr: 70 }
]发布于 2019-11-28 04:53:20
您需要以异步方式进行搜索。有很多方法可以做到这一点。就我个人而言,我是一个承诺的粉丝。下面是一个基本的方法,创建一种等待的方式,直到玩家被添加到范围中。我还添加了取消搜索的功能。
const players = [
{ id: 1, name: 'spt', mmr: 970 },
{ id: 2, name: 'test2', mmr: 1000 },
{ id: 3, name: 'test3', mmr: 1050 },
{ id: 4, name: 'test4', mmr: 70 }
]
const makeMatch = (id) => {
let timer
let promiseResolve
let promiseReject
const promise = new Promise((resolve, reject) => {
promiseResolve = resolve
promiseReject = reject
});
const playerDetails = players.find(p => p.id == id)
const { mmr } = playerDetails
const findMatchup = () => {
const secondPlayer = players.find(p =>
p.id !== id &&
Math.abs(p.mmr - mmr) <= 10)
if (secondPlayer) {
console.log("match!", id)
promiseResolve({ player1: playerDetails, player2: secondPlayer })
} else {
console.log("no matches", id)
timer = window.setTimeout(findMatchup, 1000)
}
}
findMatchup()
return {
kill: () => {
timer && window.clearTimeout(timer)
promiseReject('cancelled')
},
promise
}
}
// Make up a match with player 4
// There are no matches so it will loop for awhile
var matchMaking = makeMatch(4)
matchMaking.promise.then( ({ player1, player2 }) => {
console.log(player1, player2)
})
// Act like a new player signed on, push in the player
// We should be a match after this happens
window.setTimeout(() => {
console.log("Player is added");
players.push({ id: 5, name: 'test5', mmr: 75 })
}, 5500)
//Example showing killing off
// Make up a match with player 1
// There are no matches so it will loop for awhile
var matchMaking2 = makeMatch(1)
matchMaking2.promise.then( ({ player1, player2 }) => {
console.log(player1, player2)
}).catch((details) => {
console.log(details);
})
// Acting like user clicked cancel button to stop playing
window.setTimeout(() => {
console.log("Player has cancelled");
matchMaking2.kill()
}, 10000)
https://stackoverflow.com/questions/59077587
复制相似问题