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)];