Approach for Handling Unfinished Chess Games When Both Players Disconnect

I need to figure out what to do if both players leave the game without finishing it. In this case, I need a solution to preserve the current game state and determine when the game should be considered over.

Solution 1: Storing the Game in Redis Store the game state in Redis in case one of the players returns. When a player re-enters the game, check the state. If the time hasn't expired, continue the game; if it has, automatically end it.

Solution 2: Timer via Celery Set up a Celery task that starts a timer when both players leave. If neither player returns within the given time, the game is automatically considered finished, and the result is recorded.

I would appreciate any recommendations on how to solve this problem more effectively and would like to know how such situations are typically handled in production environments.

Upvotes: -1

Views: 28

Answers (1)

bman
bman

Reputation: 5235

Hybrid solution

You can also follow a hybird solution by combining both solutions.

You can store the game state in Redis, and schedule a Celery task when both players leave to handle the timer and finalization. When a player rejoins, you can check Redis for the state and cancel the Celery task if the game should resume.

Deterministic solution

You can also use a deterministic approach to decide when a game should end. For example, store the timestamp of the last action in Redis. When either player attempts to rejoin, check if the current time exceeds the timeout. If yes, finalize the game and purge the state.

Please note that Redis has TTL but it doesn't perform the notification. So if you require notification you need Celery to take care of that.

Upvotes: 0

Related Questions