From 7ab11e6f46b571a79ce49c6673882f7ceb9185e8 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 26 Feb 2026 08:17:29 -0500 Subject: [PATCH] Fix defense restore order + add StateVersion for client sync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Defense restore bug fix: - EndTurn previously restored defense for the ENDING player's units (wrong) - Per §6.0 start-of-turn order: defense restores for the player whose turn is STARTING - IsExhausted still resets for the ending player (they finished acting) - This prevents opponents attacking into fully-restored defense every turn StateVersion on GameStateDto: - NextActionSequence from GameState exposed as StateVersion in every state push - Clients track lastSeen StateVersion; if push arrives with version > lastSeen+1, client calls game.get_state for a full board refresh - game.get_state already returns full state — no additional server work needed Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Server/src/SOR.Game/GameEngine.cs | 39 ++++++++++++++++++----------- Server/src/SOR.Game/GameMessages.cs | 6 +++++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Server/src/SOR.Game/GameEngine.cs b/Server/src/SOR.Game/GameEngine.cs index 93b25d9..ed721c4 100644 --- a/Server/src/SOR.Game/GameEngine.cs +++ b/Server/src/SOR.Game/GameEngine.cs @@ -214,41 +214,49 @@ public static class GameEngine /// /// End the active player's turn. - /// - Resets IsExhausted on all active player field cards. - /// - Restores CurrentDefense to MaxDefense for all active player field cards (§6.9.3). - /// - Switches the active player. - /// - Accumulates SP for the next player (+SpAccumulationPerTurn; never resets). - /// - Sets phase to Draw for the next player's turn. + /// + /// Per §6.0 (start-of-turn order): + /// 1. MP restore (not yet implemented — no positional grid in v1) + /// 2. Defense restoration for the NEXT player's units (§6.9.3) + /// 3. SP gain for the next player + /// 4. Draw phase + /// + /// Note: IsExhausted resets for the ENDING player's units (they finished acting). + /// Defense restores for the STARTING player's units (at the top of their turn). + /// These apply to different players — restoring defense for the wrong player + /// would let the opponent attack into fully-restored defense every single turn. /// public static GameState EndTurn(GameState state, string actorId) { ValidateActivePlayer(state, actorId); ValidatePhase(state, GamePhase.Main); - // Refresh active player's field cards at end of their turn. + // End of active player's turn: reset exhausted status only. + // Defense does NOT restore here — it restores at start of that player's NEXT turn. var active = state.Players[actorId]; foreach (var card in active.Field) - { card.IsExhausted = false; - if (card.MaxDefense.HasValue) - card.CurrentDefense = card.MaxDefense; - } if (active.ArchSpirit is not null) - { active.ArchSpirit.IsExhausted = false; - if (active.ArchSpirit.MaxDefense.HasValue) - active.ArchSpirit.CurrentDefense = active.ArchSpirit.MaxDefense; - } // Switch active player. state.ActivePlayerAccountId = GetOpponentId(state, actorId); - // Next player's turn: advance counter, accumulate SP. + // Start of next player's turn: restore their defense (§6.9.3), accumulate SP. var next = state.Players[state.ActivePlayerAccountId]; var rules = state.Rules; state.TurnNumber++; + // Defense restores for the player whose turn is starting. + foreach (var card in next.Field) + { + if (card.MaxDefense.HasValue) + card.CurrentDefense = card.MaxDefense; + } + if (next.ArchSpirit is not null && next.ArchSpirit.MaxDefense.HasValue) + next.ArchSpirit.CurrentDefense = next.ArchSpirit.MaxDefense; + // SP accumulates — never resets between turns. next.CurrentSp += rules.SpAccumulationPerTurn; if (rules.SpHasCap && next.CurrentSp > rules.SpCap) @@ -295,6 +303,7 @@ public static class GameEngine Phase : state.Phase.ToString(), ActivePlayerAccountId : state.ActivePlayerAccountId, TurnNumber : state.TurnNumber, + StateVersion : state.NextActionSequence, Players : players); } diff --git a/Server/src/SOR.Game/GameMessages.cs b/Server/src/SOR.Game/GameMessages.cs index ad2f346..16d467f 100644 --- a/Server/src/SOR.Game/GameMessages.cs +++ b/Server/src/SOR.Game/GameMessages.cs @@ -122,12 +122,18 @@ public sealed record CustomLobbyGameStartedPush( /// /// Client-facing game state — opponent's private zones (hand, deck) are filtered. +/// +/// StateVersion is a monotonically-increasing counter incremented after every game action. +/// Clients should track the last received StateVersion. If a GameStateUpdatePush arrives +/// with StateVersion > lastSeen + 1, the client missed a push and should call game.get_state +/// to request a full board refresh. /// public sealed record GameStateDto( Guid SessionId, string Phase, // Draw | Main | End | GameOver string ActivePlayerAccountId, int TurnNumber, + int StateVersion, // monotonic; use for client mismatch detection IEnumerable Players); public sealed record PlayerStateDto(