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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-05-11 21:05:14 -04:00
co-authored by Claude Happy
parent a26c99ce4a
commit 469e72b0f6
@@ -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.
/// </summary>
private static IReadOnlyList<AbilityTarget> ComputeVisualTargets(
IReadOnlyList<AbilityTarget>? 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)];