Files
sor/Server/src/SOR.Game/Handlers/GameTransitionHandler.cs
T
6650561051 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>
2026-03-21 10:50:15 -04:00

59 lines
2.2 KiB
C#

namespace SOR.Game.Handlers;
using System.Text.Json;
using SOR.Database.Contexts;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Push;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
/// <summary>
/// game.transition — active player swaps their ArchSpirit with a Tier 1 ArchSpirit
/// from hand (§6.7.1).
///
/// The outgoing ArchSpirit goes to discard; the incoming enters at the same board
/// position with full HP, Defense, and MP. Cost is the incoming ArchSpirit's SP/ST cost.
/// The incoming ArchSpirit may move, attack, and use abilities the turn it enters.
/// </summary>
internal sealed class GameTransitionHandler : GameHandlerBase, IMessageHandler
{
private readonly IMessageSerializer _serializer;
private readonly GameDbContext _gameDb;
private readonly IClientPush _clientPush;
public GameTransitionHandler(
IMessageSerializer serializer,
GameDbContext gameDb,
IClientPush clientPush)
{
_serializer = serializer;
_gameDb = gameDb;
_clientPush = clientPush;
}
public string MessageType => "game.transition";
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken ct)
{
var req = _serializer.DeserializePayload<GameTransitionRequest>(message.Payload);
var actorId = connection.UserId;
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
var prevStateJson = session.StateJson!;
var state = GameEngine.Deserialize(prevStateJson);
var newState = GameEngine.Transition(state, actorId, req.IncomingCardInstanceId);
var dto = GameEngine.ToDto(newState, actorId);
await SaveStateAsync(_gameDb, session, newState, actorId, "game.transition",
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
await PushStateToPlayersAsync(_clientPush, session, newState, ct, prevStateJson);
return ServerMessage.Reply("game.transition.ack", message.Seq,
new GameTransitionResponse(dto));
}
}