0
\$\begingroup\$

There's a lot of multiplayer games out there that uses countdown timers to set a time limit in their game sessions, but how is it actually implemented?

For example, in a chess multiplayer game based on web, where the server is the source of truth and I've to keep a countdown timer for each player, should I store the time left for each player in a database like redis and afterwards send events to update the client timer or there's a better solution for that?

\$\endgroup\$
5
  • 3
    \$\begingroup\$ You don't need to store "time left" - that's a value that's constantly changing. It suffices to store "timestamp when turn started" - then you can calculate the time remaining at any future moment with a simple subtraction, without constantly updating the stored value. \$\endgroup\$
    – DMGregory
    Commented Nov 1, 2022 at 16:59
  • \$\begingroup\$ hmm, makes much more sense that way. Another doubt I have is about the match session, as each game match must have it's own game state, is it worth to handle multiple matches in just one websocket server or its better to have one server instantiated for each match? I'm pretty new in multiplayer games, so I'm still confused about that part... anyway, thank you so much for your time! \$\endgroup\$ Commented Nov 1, 2022 at 17:21
  • 1
    \$\begingroup\$ Please ask one question per post. If you have a different question, create a new post. \$\endgroup\$
    – DMGregory
    Commented Nov 1, 2022 at 17:24
  • 1
    \$\begingroup\$ And the client can just render the countdown timer based on the time stamp. Your server just needs to validate if a move was made in the allowed time frame, regardless what the client said if it was allowed or not \$\endgroup\$
    – Zibelas
    Commented Nov 1, 2022 at 18:37
  • \$\begingroup\$ Awesome, now it's much more clear for me, thanks a lot!! \$\endgroup\$ Commented Nov 1, 2022 at 18:42

1 Answer 1

1
\$\begingroup\$

As I mentioned in a comment, this can be implemented as a timestamp.

When an opponent reports their move to the server, the server records the time on the server clock as the last move time for that game. Then it relays that information to the player - immediately or when they next log on and query the current game state.

The player's game client can now render the time remaining on screen, and update it each frame in its game loop, without any work for the server.

When the player makes their move, they report it to the server. The server checks the last move time stored in the database, subtracts to find how much time has elapsed, and confirms whether the move from the player was received in time to count. If so, the cycle continues. If not, you fall back to your "player timed out" behaviour. You can also trigger this the next time the player or their opponent log on / refresh the current game state, if the state turns out to be "a player has timed out".

In between answering these queries, there's nothing your server has to do to maintain the state of the game, so you can support a large number of concurrent games with very modest server requirements.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .