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>
58 lines
2.1 KiB
C#
58 lines
2.1 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.sacrifice — active player sacrifices one of their Spirits adjacent to their ArchSpirit.
|
|
///
|
|
/// Sacrifice is a free action (§6.15): it does not consume an attack or ability use.
|
|
/// The Spirit must be orthogonally adjacent to the player's ArchSpirit.
|
|
/// ST earned = SacrificeSystem.GetSTReturn(currentST, spiritSacValue).
|
|
/// </summary>
|
|
internal sealed class GameSacrificeHandler : GameHandlerBase, IMessageHandler
|
|
{
|
|
private readonly IMessageSerializer _serializer;
|
|
private readonly GameDbContext _gameDb;
|
|
private readonly IClientPush _clientPush;
|
|
|
|
public GameSacrificeHandler(
|
|
IMessageSerializer serializer,
|
|
GameDbContext gameDb,
|
|
IClientPush clientPush)
|
|
{
|
|
_serializer = serializer;
|
|
_gameDb = gameDb;
|
|
_clientPush = clientPush;
|
|
}
|
|
|
|
public string MessageType => "game.sacrifice";
|
|
|
|
public async Task<IServerMessage?> HandleAsync(
|
|
IClientMessage message,
|
|
AuthenticatedConnection connection,
|
|
CancellationToken ct)
|
|
{
|
|
var req = _serializer.DeserializePayload<GameSacrificeRequest>(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.Sacrifice(state, actorId, req.UnitInstanceId);
|
|
|
|
var dto = GameEngine.ToDto(newState, actorId);
|
|
await SaveStateAsync(_gameDb, session, newState, actorId, "game.sacrifice",
|
|
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
|
|
|
|
await PushStateToPlayersAsync(_clientPush, session, newState, ct, prevStateJson);
|
|
|
|
return ServerMessage.Reply("game.sacrifice.ack", message.Seq,
|
|
new GameSacrificeResponse(dto));
|
|
}
|
|
}
|