Fix delta diffs always empty: capture prevStateJson before engine mutation

All GameEngine.* action methods mutate the state object in place and return
the same reference. Passing that reference as both 'prev' and 'next' to
GameStateDiffer produced all-null deltas — so phase, activePlayer, turnNumber,
unit stats, hand/discard, and tile changes were never sent via delta, requiring
clients to request a full resync after every action.

Fix: capture prevStateJson = session.StateJson! before calling the engine
method (before mutation occurs), then pass it to PushStateToPlayersAsync.
PushStateToPlayersAsync now accepts string? prevStateJson and deserializes it
internally for the diff computation, giving a genuine before/after snapshot.

Affects all 10 action handlers: EndTurn, Draw, Attack, MoveUnit, PlayCard,
ActivateAbility, Sacrifice, Ascend, Transition, PlaceArchSpirit.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-21 10:50:15 -04:00
co-authored by Claude Happy
parent 88f395e032
commit 6650561051
11 changed files with 53 additions and 40 deletions
@@ -108,22 +108,25 @@ internal abstract class GameHandlerBase
/// <summary>
/// Push the updated game state to both players and any current spectators.
/// When <paramref name="prevState"/> is supplied a structured delta (<c>game.state_delta</c>)
/// When <paramref name="prevStateJson"/> is supplied a structured delta (<c>game.state_delta</c>)
/// is sent instead of the full state, reducing payload size.
/// Falls back to a full <c>game.state_update</c> push when prevState is null.
/// Falls back to a full <c>game.state_update</c> push when prevStateJson is null.
/// IMPORTANT: prevStateJson must be captured from session.StateJson BEFORE SaveStateAsync
/// overwrites it and BEFORE the engine method mutates the state object.
/// </summary>
internal static async Task PushStateToPlayersAsync(
IClientPush clientPush,
GameSession session,
GameState state,
CancellationToken ct,
GameState? prevState = null)
string? prevStateJson = null)
{
var p1Id = session.PlayerOneAccountId.ToString();
var p2Id = session.PlayerTwoAccountId.ToString();
if (prevState != null)
if (prevStateJson != null)
{
var prevState = GameEngine.Deserialize(prevStateJson);
var d1 = GameStateDiffer.ComputeDelta(prevState, state, p1Id);
var d2 = GameStateDiffer.ComputeDelta(prevState, state, p2Id);