1

我正在节点和套接字中创建绘图/猜测游戏。简单地说:我有一个类RoomGame(扩展了 Room)和一个类Round(每场 10 个)。每轮一个用户被指定为抽屉,猜测者有45 秒的时间来猜测这个词。

在第一次猜测计时器是否高于 20 秒时,计时器将减少到 20 秒

我不确定,但这就是我开始的方式:

班级回合:

function Round(id, game){
  var ROUNDTIME = 45,
      timer = new Date();
  this.id = id;
  this.game = game;
  this.endTime = timer.setSeconds(timer.getSeconds() + ROUNDTIME);
  ...
}

类游戏:

function Game(){
  this.MAX_ROUNDS = 10;
  this.rounds = [];
  this.currentRound = null;
  ...
}

Game.prototype.start = function(){
  this.nextRound;
}

Game.prototype.nextRound = function(){
  if(this.rounds.length <= this.MAX_ROUNDS){
    this.currentRound = new Round(this.rounds.length + 1, this);
    ...
  } else {
    this.endGame();
  }
}

Game.prototype.endGame = function(){
  ...
}

之后,我有一个 Round 函数,每次提交答案/消息时都会检查答案。在第一次回答时,我将 endTime 减少到剩下 20 秒。

所以有2个问题:

  1. 这是一种正确/良好的实用方法吗?
  2. 我应该如何将它进一步实施到应用程序本身?(setInterval of 1 seconds with emit?还是在达到 endDate 时简单地发出?还是其他方式?)
4

1 回答 1

1

你可以做类似的事情

function Round(id, game,loseCallbackFn){
var ROUNDTIME = 45, startTime = new Date();
this.id = id;
this.game = game;
this.endTime = startTime.getTime()+45*1000;
this.timerId = setTimeout(loseCallbackFn,45*1000);
...
}
...
Round.onWrongGuess(){
    if((this.endTime-(new Date()).getTime())>20*1000) {// he has more than 20s left
        clearTimeout(this.timerId);
        this.timerId = setTimeout(loseCallbackFn,20*1000);
        ...
    }else{
        loseCallbackFn();
    }
}
...
于 2014-10-15T06:59:57.903 回答