游戏的每转超时逻辑 [英] Timeout-per-turn logic for a game

查看:71
本文介绍了游戏的每转超时逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用电报机器人创建游戏,现在我已经达到正在播放状态。最困难的部分是每转超时算法(对我而言)。



以下是详细信息:


游戏开始了。. 4玩家



游戏的目的是回答问题。问题是:
提到动物的名字。



答案的顺序是:玩家C->玩家B- ->玩家D->玩家
A。



每个玩家最多有5秒的回答时间(可以更少)。



现在,游戏将开始于3 .. 2 .. 1 .. GO !!



玩家C回答:(1秒)



玩家B回答:(1秒)



播放器D答案:鹿(1秒)



播放器A答案:( 2秒)



回到播放器C时,将是5秒,我的逻辑是
来检查oldTurnId(玩家C) === currentTurnId(玩家
C)。在这种情况下,玩家C将收到通知,通知时间到了,他将被送走,但
玩家C将第二次应答。


我的桌子上有此列:previousTurnId,currentTurnId,nextTurnId



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



仅供参考,我正在使用node.js来创建此游戏,主要部分是我'

每个人都有更好的主意来解决这个问题吗?



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



谢谢

解决方案

类似您想要的声音是 clearTimeout 。基本上,如果玩家在接下来的五秒钟内回答,您将清除超时,否则,您将玩家按正常情况踢出游戏。您可以从 setTimeout 中获取超时ID作为返回值。



我正在对您的代码进行假设,但基本上基本上是这样:

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

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


I'm creating a game in telegram bot and now i already reached the "PLAYING" state. The most difficult part is the timeout-per-turn algorithm (for me).

Here's the breakdown:

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.

But i can't find any direction to tackle this problem.

FYI, I'm using node.js to create this game and the main part is I'm using setTimeOut a lot for this timeout-per-turn logic.

Anyone has better idea for tackling this problem?

Thanks a lot guys, All help will be appreciated.

Thanks

解决方案

Sounds like what you want is a clearTimeout. Basically, you would clear the timeout if the player answers within the next five seconds, otherwise, you carry on with kicking the player out of the game as normal. You can get the timeout ID as a return value from setTimeout.

I'm making an assumption on your code, but this would basically be how it would go:

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

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

这篇关于游戏的每转超时逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆