Description
Background:
Previously, I had opened issue #20, PR #25, and then issue #24, all because of an incorrect assumption: I thought that the Gist forking/editing logic depended on the state of the turn system (the previous and current player), but it's only indirectly related. The real condition to check is simply whether the current player owns the Gist or not!
Scenario illustrating the bug if multiple players disconnect on their turns:
-
Three players are in the game, and Player 1 completes their turn. (Gist owner: Player 1)
-
Player 2 disconnects before their turn ends. (Gist owner: Player 1)
-
Player 3 also disconnects before their turn ends. (Gist owner: Player 1)
-
Player 1 begins their turn, and they should not fork the Gist since they own it! But the game state looks like this: previous player is Player 3, current player is Player 1, and since they don't match, Player 1 should fork and edit the Gist. And that leads to an error from GitHub, because a user can't fork their own Gist!
Instead of starting from first principles and looking at the root of the other actions in this system, I got caught up in the tangled mess of secondary effects, treating the symptoms instead of the cause! I think the situation can be summed up well this way: instead of thinking of my app as one complicated state machine, it's actually more like two or three smaller state machines that communicate with each other. My code should reflect that!
Ultimate solution:
The best solution would be to close issue #21 ("Server should fork/edit Gists on behalf of disconnected users"), so that not only is this bug resolved, but there would also be the extra benefit of accurately giving each player credit for their work even if they're disconnected. (A pure client-side solution still can't solve that issue!)
Tasks for this issue (intermediary solution):
- Close issue CANCELLED: Game state should track previous and current player for Gist forking/editing logic #20 and CANCELLED: If current player disconnects, next turn can trigger unwanted Gist fork #24, noting that they've been replaced with this issue
- Throw out and close PR Close #20, track previous/current players for Gist logic #25
- Check out to access the GitHub username of the current Gist's owner
- Add Gist owner to the client's game state
- Update Gist owner whenever client creates a new Gist (via
handleCreateNewGist
orforkAndEditGist
) - Update the client-side Gist-forking condition to now check if current player is the current Gist owner
- Update events list and flowchart to reflect these new changes