From 469e72b0f6cbc9e689ebc63d73e3451bbc2a8eab Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 11 May 2026 21:05:14 -0400 Subject: [PATCH] Fix GamePlayCardHandler card lookup and visual target fallback Medium: replace .First() with .FirstOrDefault() ?? throw new GameException("CARD_NOT_IN_HAND") so a bad/stale CardInstanceId produces a normal game error instead of a raw InvalidOperationException. Low/Medium: ComputeVisualTargets fallback now uses GameState.GetEnemyIds which filters by different TeamIndex and !IsEliminated, preventing the placeholder projectile from targeting an ally or an already-eliminated player in team and multiplayer modes. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../SOR.Game/Handlers/GamePlayCardHandler.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Server/src/SOR.Game/Handlers/GamePlayCardHandler.cs b/Server/src/SOR.Game/Handlers/GamePlayCardHandler.cs index 5b1f9db..de52a6b 100644 --- a/Server/src/SOR.Game/Handlers/GamePlayCardHandler.cs +++ b/Server/src/SOR.Game/Handlers/GamePlayCardHandler.cs @@ -44,7 +44,9 @@ internal sealed class GamePlayCardHandler : GameHandlerBase, IMessageHandler var state = GameEngine.Deserialize(prevStateJson); // Capture card data before ChannelCard moves it from hand. - var card = state.Players[actorId].Hand.First(c => c.InstanceId == req.CardInstanceId); + // Use FirstOrDefault so a bad InstanceId throws the same GameException the engine would. + var card = state.Players[actorId].Hand.FirstOrDefault(c => c.InstanceId == req.CardInstanceId) + ?? throw new GameException("CARD_NOT_IN_HAND"); // Build the coordinate list for the engine. // TargetTiles (multi-tile) takes priority; fall back to TargetTile (single-tile). @@ -106,8 +108,9 @@ internal sealed class GamePlayCardHandler : GameHandlerBase, IMessageHandler /// represent a player-chosen target and are valid for all ability types that /// require explicit targeting. /// 2. Player-style targets sent as InstanceId (account IDs for player-zone actions). - /// 3. Fallback: the first non-actor opponent's ArchSpirit account ID, so the - /// projectile always has somewhere to travel to for targetless abilities. + /// 3. Fallback: the first live enemy ArchSpirit (different team, not eliminated) + /// via GameState.GetEnemyIds, so the projectile always has somewhere to travel + /// for targetless or server-resolved abilities without hitting an ally. /// private static IReadOnlyList ComputeVisualTargets( IReadOnlyList? clientTargets, @@ -124,11 +127,12 @@ internal sealed class GamePlayCardHandler : GameHandlerBase, IMessageHandler return usable; } - // Targetless / server-resolved: aim at the first live opponent ArchSpirit. - var opponentArch = state.Players.Values - .Where(p => p.AccountId.ToString() != actorId && p.ArchSpirit is not null) - .Select(p => p.ArchSpirit!) - .FirstOrDefault(); + // Targetless / server-resolved: aim at the first live enemy ArchSpirit. + // GetEnemyIds filters by different TeamIndex and !IsEliminated, covering both + // FFA and team modes without accidentally targeting an ally or a dead player. + var opponentArch = state.GetEnemyIds(actorId) + .Select(id => state.Players[id].ArchSpirit) + .FirstOrDefault(a => a is not null); if (opponentArch is not null) return [new AbilityTarget(ActionIndex: 0, InstanceId: opponentArch.InstanceId)];