首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >队列逻辑和一段时间

队列逻辑和一段时间
EN

Stack Overflow用户
提问于 2019-11-28 03:52:16
回答 1查看 24关注 0票数 2

嗨,我有一排球员,从这一排球员中,我得到了一个符合两个条件的球员

代码语言:javascript
复制
const condition = (5 / 100) * playerOne.mmr + playerOne.mmr
const condition2 = playerOne.mmr - ((5 / 100) * playerOne.mmr);

找一个比我的玩家的MMR值低5%到高5%的玩家

然后我就这么做了

代码语言:javascript
复制
  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,那就太好了,我会一直走下去

甚至在我的服务器上,我将一个玩家添加到队列中

在我的队列中,虽然我的队列中继续有相同的球员,但我添加到队列中的新球员没有出现

代码语言:javascript
复制
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个玩家,所以它处于无限循环中,我不知道我哪里出了错,我不知道如何修复,或者我的逻辑是否非常失败

代码语言:javascript
复制
[
  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 }
]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-28 04:53:20

您需要以异步方式进行搜索。有很多方法可以做到这一点。就我个人而言,我是一个承诺的粉丝。下面是一个基本的方法,创建一种等待的方式,直到玩家被添加到范围中。我还添加了取消搜索的功能。

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59077587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档