Fix defense restore order + add StateVersion for client sync

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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-02-26 08:17:29 -05:00
co-authored by Claude Happy
parent 682a525193
commit 7ab11e6f46
2 changed files with 30 additions and 15 deletions
+24 -15
View File
@@ -214,41 +214,49 @@ public static class GameEngine
/// <summary>
/// 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.
/// </summary>
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);
}
+6
View File
@@ -122,12 +122,18 @@ public sealed record CustomLobbyGameStartedPush(
/// <summary>
/// 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.
/// </summary>
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<PlayerStateDto> Players);
public sealed record PlayerStateDto(