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; /// /// 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). /// 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 HandleAsync( IClientMessage message, AuthenticatedConnection connection, CancellationToken ct) { var req = _serializer.DeserializePayload(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)); } }