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
@@ -41,15 +41,16 @@ internal sealed class GameAscendHandler : GameHandlerBase, IMessageHandler
var req = _serializer.DeserializePayload<GameAscendRequest>(message.Payload);
var actorId = connection.UserId;
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
var state = GameEngine.Deserialize(session.StateJson!);
var newState = GameEngine.Ascend(state, actorId, req.IncomingCardInstanceId);
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
var prevStateJson = session.StateJson!;
var state = GameEngine.Deserialize(prevStateJson);
var newState = GameEngine.Ascend(state, actorId, req.IncomingCardInstanceId);
var dto = GameEngine.ToDto(newState, actorId);
await SaveStateAsync(_gameDb, session, newState, actorId, "game.ascend",
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
await PushStateToPlayersAsync(_clientPush, session, newState, ct, state);
await PushStateToPlayersAsync(_clientPush, session, newState, ct, prevStateJson);
return ServerMessage.Reply("game.ascend.ack", message.Seq,
new GameAscendResponse(dto));