fix(reconnect): preserve game state across disconnects#20
Draft
stephenkirk wants to merge 2 commits intomainfrom
Draft
fix(reconnect): preserve game state across disconnects#20stephenkirk wants to merge 2 commits intomainfrom
stephenkirk wants to merge 2 commits intomainfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Clientobject 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 oldClientobject. 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.tsreassigns itsclientbinding to the restored object so all future messages on that socket route to the right place.rejoin()returns the restored client (ornullon failure) instead of a boolean. After reconnection, both sides get an immediateplayerInfo/enemyInfore-sync.Four files touched:
Client.ts,Lobby.ts,actionHandlers.ts,main.ts.Test plan