游戏的每回合超时逻辑

Timeout-per-turn logic for a game

我正在用电报机器人创建游戏,现在我已经达到 "PLAYING" 状态。最困难的部分是 timeout-per-turn 算法(对我来说)。

细目如下:

Game starting.. Got 4 players

The game objective is to answer the question. The question is : Mention the names of animal.

The order to answer is : Player C --> Player B --> Player D --> Player A.

Each player has the maximum 5 seconds to answer (can be less).

Okay now, the game will begin in 3.. 2.. 1.. GO!!

Player C answer: dog (1 sec)

Player B answer : cat (1 sec)

Player D answer : deer (1 sec)

Player A answer : bird (2 sec)

And when it goes back to Player C, it will be 5 secs, and my logic is to check whether the oldTurnId (Player C) === currentTurnId (Player C). In this case, player C will get notified that the time is out, and he will be sent off, but player C is about to answer for the 2nd time.

I have this columns on my table : previousTurnId, currentTurnId, nextTurnId.

但是我找不到解决这个问题的方向。

仅供参考,我正在使用 node.js 来创建这个游戏,主要部分是我大量使用 setTimeOut 来处理每轮超时逻辑。

谁有解决这个问题的更好主意?

非常感谢你们, 我们将不胜感激。

谢谢

听起来你想要的是 clearTimeout。基本上,如果玩家在接下来的五秒内回答,您将清除超时,否则,您将照常将玩家踢出游戏。您可以从 setTimeout.

中获取超时 ID 作为 return 值

我对你的代码做了一个假设,但基本上是这样的:

var playerTimeout = setTimeout(function () {
  removePlayer(player);
  startNextPlayersTurn();
}, 5000);

player.on('answer', function() {
  clearTimeout(playerTimeout);
  startNextPlayersTurn();
});