From 5482dedcbecc8d622d1ce4fed26873d8ac2df604 Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 5 May 2026 15:58:45 -0400 Subject: [PATCH] fix(game): centralize death resolution, reorder UnitAttack trigger, clear Position on zone transitions - FireTrigger(UnitAttack) now fires BEFORE ApplyDamage so triggered abilities can modify attacker/target stats before damage lands - AbilitySystem.KillUnit is now leader-aware: killing an ArchSpirit (in field or ArchSpirit slot) calls GameEngine.EliminatePlayer instead of silently failing - MoveToDiscard and RemoveFromAllZones both clear Position = null on zone transitions - RemoveFromAllZones guards against direct ArchSpirit removal (throws InvalidOperationException) - EliminatePlayer made internal for cross-class access from AbilitySystem - Client: improved JSON parsing error handling in Network.gd; debug_output enabled in Game.gd Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Client/origin-client/autoloads/Game.gd | 2 +- Client/origin-client/autoloads/Network.gd | 14 ++++++++- .../src/SOR.Game/Abilities/AbilitySystem.cs | 25 +++++++++++++++- Server/src/SOR.Game/GameEngine.cs | 29 ++++++++++--------- 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/Client/origin-client/autoloads/Game.gd b/Client/origin-client/autoloads/Game.gd index 0c147de..fc319d5 100644 --- a/Client/origin-client/autoloads/Game.gd +++ b/Client/origin-client/autoloads/Game.gd @@ -89,7 +89,7 @@ var session_id : String = "" var state : Dictionary = {} var opponent_name : String = "" var is_spectating : bool = false -var debug_output : bool = false +var debug_output : bool = true var _state_version : int = -1 # ── Lifecycle ───────────────────────────────────────────────────────────────── diff --git a/Client/origin-client/autoloads/Network.gd b/Client/origin-client/autoloads/Network.gd index e5386ca..87190e6 100644 --- a/Client/origin-client/autoloads/Network.gd +++ b/Client/origin-client/autoloads/Network.gd @@ -234,10 +234,22 @@ func _poll_packets() -> void: _last_rx_time = _now() _ping_sent_at = -1.0 # Any received packet resets the ping watchdog. + if raw.size() == 0 or text.strip_edges().is_empty(): + if debug_log_packets: + print("[Net] << ") + continue + if debug_log_packets: print("[Net] << %s" % text) - var parsed: Variant = JSON.parse_string(text) + var json := JSON.new() + var err := json.parse(text) + if err != OK: + push_warning("Network: invalid JSON packet (%s) — %s" + % [json.get_error_message(), text]) + continue + + var parsed: Variant = json.data #print("[Parsed Type] ", type_string(typeof(parsed))) if typeof(parsed) != TYPE_DICTIONARY: if (parsed != null): diff --git a/Server/src/SOR.Game/Abilities/AbilitySystem.cs b/Server/src/SOR.Game/Abilities/AbilitySystem.cs index e6d183a..6ca6f0a 100644 --- a/Server/src/SOR.Game/Abilities/AbilitySystem.cs +++ b/Server/src/SOR.Game/Abilities/AbilitySystem.cs @@ -3559,7 +3559,22 @@ public static class AbilitySystem private static void KillUnit(CardInstance unit, AbilityContext ctx, string? killerInstanceId = null) { var owner = FindCardOwner(ctx.State, unit.InstanceId); - if (owner is null) return; + if (owner is null) + { + // The unit may be an ArchSpirit, which lives in PlayerState.ArchSpirit (not in regular zones). + var archOwner = ctx.State.Players.Values + .FirstOrDefault(p => p.ArchSpirit?.InstanceId == unit.InstanceId); + if (archOwner is null) return; + GameEngine.EliminatePlayer(ctx.State, archOwner, out _); + return; + } + + // If the killed unit is a leader (ArchSpirit on the field), eliminate the player. + if (unit is FieldCard { IsLeader: true }) + { + GameEngine.EliminatePlayer(ctx.State, owner, out _); + return; + } // Pre-death replacement/prevention window. FireTrigger(ctx.State, new TriggerOccurrence @@ -3713,6 +3728,11 @@ public static class AbilitySystem private static void RemoveFromAllZones(PlayerState player, CardInstance card, Board? board) { + // Guard: ArchSpirit must be eliminated via EliminatePlayer, not removed directly. + if (player.ArchSpirit?.InstanceId == card.InstanceId) + throw new InvalidOperationException( + $"RemoveFromAllZones must not be called on an ArchSpirit ({card.InstanceId}); use EliminatePlayer instead."); + if (board is not null) { board.RemoveUnit(card.InstanceId); @@ -3722,6 +3742,9 @@ public static class AbilitySystem player.Deck.Remove(card); player.Field.Remove(card); player.Discard.Remove(card); + + // Clear board position so stale coords are never observed after a zone transition. + if (card is FieldCard fc) fc.Position = null; } private static PlayerState? FindCardOwner(GameState state, string instanceId) diff --git a/Server/src/SOR.Game/GameEngine.cs b/Server/src/SOR.Game/GameEngine.cs index 95448b5..28864ef 100644 --- a/Server/src/SOR.Game/GameEngine.cs +++ b/Server/src/SOR.Game/GameEngine.cs @@ -723,6 +723,20 @@ public static class GameEngine throw new GameException("TARGET_OUT_OF_RANGE"); } + // Fire UnitAttack trigger before damage so triggered abilities can affect + // attacker/target stats (e.g. bonus damage, damage reduction) before they land. + AbilitySystem.FireTrigger(state, new TriggerOccurrence + { + Kind = TriggerEventKind.UnitAttack, + ActorId = actorId, + UnitType = attacker.CardType, + Data = new TriggerEventData + { + SourceId = attackerInstanceId, + TargetId = targetInstanceId, + }, + }); + // Attacker hits target. ApplyDamage(target, attacker.Damage ?? 0); @@ -756,18 +770,6 @@ public static class GameEngine bool gameOver = false; var activePlayer = state.Players[actorId]; - AbilitySystem.FireTrigger(state, new TriggerOccurrence - { - Kind = TriggerEventKind.UnitAttack, - ActorId = actorId, - UnitType = attacker.CardType, - Data = new TriggerEventData - { - SourceId = attackerInstanceId, - TargetId = targetInstanceId, - }, - }); - if (attacker.CurrentHealth <= 0) { if (attacker.IsLeader) @@ -1385,6 +1387,7 @@ public static class GameEngine private static void MoveToDiscard(PlayerState player, FieldCard card) { player.Field.Remove(card); + card.Position = null; player.Discard.Add(card); } @@ -1408,7 +1411,7 @@ public static class GameEngine /// players share the same team (game over) or the game continues. /// When gameOver = true the caller should finalize the session. /// - private static bool EliminatePlayer(GameState state, PlayerState victim, out bool gameOver) + internal static bool EliminatePlayer(GameState state, PlayerState victim, out bool gameOver) { victim.IsEliminated = true; RemovePlayerBoardPresence(state, victim.AccountId);