Skip to content

fix(reconnect): preserve game state across disconnects#20

Draft
stephenkirk wants to merge 2 commits intomainfrom
bug/fix-lives-and-hands
Draft

fix(reconnect): preserve game state across disconnects#20
stephenkirk wants to merge 2 commits intomainfrom
bug/fix-lives-and-hands

Conversation

@stephenkirk
Copy link
Member

Warning

This has not been tested yet. Needs manual verification before merging.

Why

When a player's TCP connection drops mid-game, the server creates a 60-second grace period for reconnection. The reconnect slot stored the player's token and role. It did not store anything about the game.

On reconnect, a fresh Client object is created by the socket handler — lives = 5, score = 0, ante = 1, the full factory-default experience. The rejoin logic slotted this blank client into the lobby without transferring any state from the old one. The server then authoritatively told both players the wrong life count on the next PvP resolution.

Observed in the wild: player at 1 life disconnects, reconnects, server thinks they have 5. They lose a PvP round. Server sends playerInfo lives: 4. Three bonus lives, on the house.

What this does

Instead of storing just a token in the DisconnectedSlot, the server now keeps the entire old Client object. On rejoin, oldClient.replaceConnection(newClient) swaps the socket onto the original client — all game state survives by construction, not by copying a field list that someone has to remember to maintain.

main.ts reassigns its client binding to the restored object so all future messages on that socket route to the right place. rejoin() returns the restored client (or null on failure) instead of a boolean. After reconnection, both sides get an immediate playerInfo/enemyInfo re-sync.

Four files touched: Client.ts, Lobby.ts, actionHandlers.ts, main.ts.

Test plan

  • Player disconnects mid-game, reconnects within 60s — lives, score, ante, skips all match pre-disconnect values
  • Opponent sees correct enemy lives after the reconnect (not 5, not the default)
  • Reconnect token expiry (60s) still triggers a full leave
  • Voluntary leave (not a disconnect) still works normally
  • Double disconnect/reconnect in quick succession doesn't corrupt state

On disconnect, preserve the old Client object (with all game state) in
the reconnect slot. On rejoin, swap the new socket onto the old client
via replaceConnection instead of creating a fresh client with default
lives = 5. Re-sync both sides with playerInfo/enemyInfo after rejoin.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant