Fix SummonToken tile-filter validation: reject bad picks before costs are paid

Three bugs fixed for abilities like Demonate Leviathan (SummonToken + TileFilter):

1. Server: AbilityUsesThisTurn was incremented (and costs paid) before effect-level
   TileFilter validation ran. Added PreValidateClientTileTargets() called before the
   use-count increment so an invalid tile pick wastes no uses and no sacrificed tokens.

2. Server: SummonToken silently break'd when TileFilter or token-affinity check failed,
   giving the client no error. Changed both to throw GameException("INVALID_TILE").

3. Client: tile_passes_filters() only checked the target selector's filters, not the
   effect-level TileFilter (e.g. SummonToken.tileFilter). Added _collect_effect_tile_filters()
   and TileAffinityMatches handling so invalid tiles are never highlighted or accepted.

Also includes: Art2dUrl/ModelUrl deltas in GameStateDiffer/GameMessages/GameEngine,
unit_mesh_builder token-model URL fallback + shader/tint helpers, ModelCache v3 bump,
Game.gd unit delta merging for art2dUrl/modelUrl.

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-12 07:06:54 -04:00
co-authored by Claude Happy
parent 3bbf419836
commit f3e5363c13
10 changed files with 210 additions and 37 deletions
+48 -3
View File
@@ -126,6 +126,10 @@ public static class AbilitySystem
if (!ValidateSourceActivationConditions(def, ctx, source))
throw new GameException("ABILITY_CONDITION_NOT_MET");
// Pre-validate client tile targets against effect-level TileFilters BEFORE
// incrementing the use count or paying any costs, so a bad pick wastes nothing.
PreValidateClientTileTargets(def, clientTargets, ctx);
source.AbilityUsesThisTurn[abilityIndex] = usedThisTurn + 1;
var effectiveActions = GetEffectiveActions(def, ctx, abilityIndex, def.Range, def.Duration);
@@ -191,6 +195,42 @@ public static class AbilitySystem
return true;
}
/// <summary>
/// Checks client-supplied tile targets against any effect-level <c>TileFilter</c> constraints
/// (e.g. <see cref="SummonTokenEffect.TileFilter"/>). Called before the use-count is
/// incremented and before costs are paid, so a rejected pick costs the player nothing.
/// </summary>
private static void PreValidateClientTileTargets(
AbilityDefinition def,
IReadOnlyList<AbilityTarget>? clientTargets,
AbilityContext ctx)
{
if (clientTargets is null || ctx.State.Board is null) return;
// Iterate the flat action list in the top-level AND group (the common case).
// OR/XOR groups with pending interactive picks are handled after the pick resolves
// and never reach this path with client-provided tile coords.
var actions = def.ActionGroup?.Actions ?? def.Actions ?? [];
for (int ai = 0; ai < actions.Length; ai++)
{
var action = actions[ai];
if (action.Target.Type != TargetType.Tile) continue;
foreach (var effect in action.Effects)
{
if (effect is not SummonTokenEffect ste || ste.TileFilter is not { Length: > 0 }) continue;
var ct = clientTargets.FirstOrDefault(t => t.ActionIndex == ai && t.Coord is not null);
if (ct is null) continue;
var coord = new Coord(ct.Coord!.X, ct.Coord!.Y);
var tile = ctx.State.Board.GetTile(coord);
if (tile is null || !TileMatchesFilters(ste.TileFilter, tile, ctx))
throw new GameException("INVALID_TILE");
}
}
}
/// <summary>
/// Scans every battlefield unit (and, when SourceZoneGate is set, hand/discard/deck cards)
/// for Triggered abilities matching the occurrence and fires each matching one.
@@ -2837,12 +2877,17 @@ public static class AbilitySystem
var proto = JsonSerializer.Deserialize<CardInstance>(ste.TokenDefJson, _jsonOpts);
if (proto is null) break;
// Enforce tile affinity: a token with a non-Void affinity may only be
// placed on a tile whose resolved affinity is compatible with the token.
// Enforce explicit TileFilter (e.g. TileAffinityMatches) — throw so the client
// receives an error rather than silently swallowing a bad pick.
if (ste.TileFilter is { Length: > 0 } && !TileMatchesFilters(ste.TileFilter, tile, ctx))
throw new GameException("INVALID_TILE");
// Enforce tile affinity derived from the token's own Affinity field: a token
// with a non-Void affinity may only be placed on a compatible tile.
if (!string.IsNullOrEmpty(proto.Affinity) && proto.Affinity != "Void"
&& Enum.TryParse<Affinity>(proto.Affinity, out var tokenAffinity)
&& !AffinitySystem.IsAffinityValid(tile, tokenAffinity))
break;
throw new GameException("INVALID_TILE");
proto.InstanceId = Guid.NewGuid().ToString("N")[..8];
proto.CardType = "SpiritToken";
+3 -1
View File
@@ -1701,7 +1701,9 @@ public static class GameEngine
AttackRequiresLos : c.RequiresLos,
ExtraAttacksRemaining : c.ExtraAttacksRemaining,
CanBeAttacked : c.CanBeAttacked,
AbilitiesJson : c.AbilitiesJson);
AbilitiesJson : c.AbilitiesJson,
Art2dUrl : c.Art2dUrl,
ModelUrl : c.ModelUrl);
}
private static BoardDto ToBoardDto(Board board)
+6 -2
View File
@@ -556,7 +556,9 @@ public sealed record UnitDelta(
int? ExtraAttacksRemaining,
CoordDto? Position,
string? AbilitiesJson,
string? AbilityUsesJson);
string? AbilityUsesJson,
string? Art2dUrl,
string? ModelUrl);
public sealed record PublicCountsDelta(
int? DeckCount,
@@ -725,7 +727,9 @@ public sealed record CardInstanceDto(
bool AttackRequiresLos,
int ExtraAttacksRemaining,
bool CanBeAttacked,
string? AbilitiesJson);
string? AbilitiesJson,
string? Art2dUrl,
string? ModelUrl);
public sealed record BoardDto(
int Width,
+5 -1
View File
@@ -251,6 +251,8 @@ internal static class GameStateDiffer
prevC.ExtraAttacksRemaining != nextC.ExtraAttacksRemaining ||
prevFc?.Position != nextFc?.Position ||
prevC.AbilitiesJson != nextC.AbilitiesJson ||
prevC.Art2dUrl != nextC.Art2dUrl ||
prevC.ModelUrl != nextC.ModelUrl ||
abilityUsesChanged;
if (!anyChange) return null;
@@ -280,7 +282,9 @@ internal static class GameStateDiffer
ExtraAttacksRemaining : prevC.ExtraAttacksRemaining != nextC.ExtraAttacksRemaining ? nextC.ExtraAttacksRemaining : null,
Position : positionDelta,
AbilitiesJson : prevC.AbilitiesJson != nextC.AbilitiesJson ? nextC.AbilitiesJson : null,
AbilityUsesJson : abilityUsesJson);
AbilityUsesJson : abilityUsesJson,
Art2dUrl : prevC.Art2dUrl != nextC.Art2dUrl ? nextC.Art2dUrl : null,
ModelUrl : prevC.ModelUrl != nextC.ModelUrl ? nextC.ModelUrl : null);
}
private static PlayerStatsDelta? DiffPlayerStats(PlayerState prev, PlayerState next)