I'm trying to make a simple text turn-based game in node.js using the socket.io module. The player must have a timeout to send the text during his turn. If he fails to do so, he loses the opportunity and now is the turn of the next player.
The timeout is my real problem here. I did some simple setTimeout
to advance the turns, like this:
var playerIndex = 0;
var timer = setTimeout(advancePlayer, turnTime);
function advancePlayer() {
playerIndex = (playerIndex + 1) % numPlayers;
// Warns the players about whose turn is now:
aknowledgeTurn(playerIndex);
timer = setTimeout(advancePlayer, turnTime);
}
The problem is that when the player send his text, I need to check first that it's his turn (and that is a related issue, as I'm not sure how to share the game instance/data across all players, and for now I'm using a database) and this could take enough time to the timer run out and call the next turn, making the move invalid or maybe jumping the next player's turn, because as soon as the text is received, the timer should stop and the turn should advance.
If I just clear the timeout before checking if the move is valid, I don't know how to resume the timer in case of an invalid message (i.e. the player sent a text outside his turn, maybe by fiddling with the client code/console).
How can I close that timeout when the player sends the message without falling into these problems?