fix(ability): apply cost.Filters to interactive pick candidates and resume validation
PayCosts now filters rawCandidates through FiltersMatch before building CandidatesJson, so only eligible cards are offered to the picker. ResumeCostPick now deserialises FiltersJson, resolves the source unit early for a complete filter context, and validates each client-selected card against the stored filters before executing the cost — rejecting invalid selections with INVALID_COST_PICK_SELECTION. 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:
@@ -516,7 +516,11 @@ public static class AbilitySystem
|
||||
player.Field.OfType<FieldCard>().Where(f => f.CardType == "Spirit").Cast<CardInstance>(),
|
||||
_ => Enumerable.Empty<CardInstance>()
|
||||
};
|
||||
var candidateItems = rawCandidates
|
||||
// Apply cost filters so only eligible cards are offered to the picker.
|
||||
var filteredCandidates = cost.Filters is not null && cost.Filters.Length > 0
|
||||
? rawCandidates.Where(c => FiltersMatch(cost.Filters, c, ctx))
|
||||
: rawCandidates;
|
||||
var candidateItems = filteredCandidates
|
||||
.Select(c => new { instanceId = c.InstanceId, name = c.Name, cardType = c.CardType })
|
||||
.ToArray();
|
||||
|
||||
@@ -778,6 +782,17 @@ public static class AbilitySystem
|
||||
|
||||
var player = state.Players[pending.ActorId];
|
||||
|
||||
// Resolve source early so the filter context can evaluate source-relative filters
|
||||
// (IsSelf, IsAdjacentToSource, etc.). Source may have left the field — that's OK;
|
||||
// those spatial filters simply return false and thus never apply to hand/field cost picks.
|
||||
var earlySource = state.Players.Values
|
||||
.SelectMany(p => p.Field).OfType<FieldCard>()
|
||||
.FirstOrDefault(f => f.InstanceId == pending.SourceInstanceId);
|
||||
var filterCtx = new AbilityContext { State = state, ActorId = pending.ActorId, Source = earlySource };
|
||||
var pickFilters = pending.FiltersJson is not null
|
||||
? JsonSerializer.Deserialize<Filter[]>(pending.FiltersJson, _jsonOpts)
|
||||
: null;
|
||||
|
||||
// Validate and pay the interactive cost using the client-selected IDs.
|
||||
switch (pending.CostType)
|
||||
{
|
||||
@@ -790,6 +805,8 @@ public static class AbilitySystem
|
||||
.ToList();
|
||||
if (toDiscard.Count < pending.Amount)
|
||||
throw new GameException("INVALID_COST_PICK_SELECTION");
|
||||
if (pickFilters is not null && toDiscard.Any(c => !FiltersMatch(pickFilters, c, filterCtx)))
|
||||
throw new GameException("INVALID_COST_PICK_SELECTION");
|
||||
foreach (var c in toDiscard) { player.Hand.Remove(c); player.Discard.Add(c); }
|
||||
break;
|
||||
}
|
||||
@@ -800,6 +817,8 @@ public static class AbilitySystem
|
||||
var spirit = player.Field.OfType<FieldCard>()
|
||||
.FirstOrDefault(f => f.InstanceId == picked)
|
||||
?? throw new GameException("INVALID_COST_PICK_SELECTION");
|
||||
if (pickFilters is not null && !FiltersMatch(pickFilters, spirit, filterCtx))
|
||||
throw new GameException("INVALID_COST_PICK_SELECTION");
|
||||
state.Board?.RemoveUnit(spirit.InstanceId);
|
||||
player.Field.Remove(spirit);
|
||||
player.Discard.Add(spirit);
|
||||
@@ -812,6 +831,8 @@ public static class AbilitySystem
|
||||
var spirit = player.Field.OfType<FieldCard>()
|
||||
.FirstOrDefault(f => f.InstanceId == picked)
|
||||
?? throw new GameException("INVALID_COST_PICK_SELECTION");
|
||||
if (pickFilters is not null && !FiltersMatch(pickFilters, spirit, filterCtx))
|
||||
throw new GameException("INVALID_COST_PICK_SELECTION");
|
||||
// Temporary context for damage application.
|
||||
var dmgCtx = new AbilityContext { State = state, ActorId = pending.ActorId };
|
||||
ApplyDamageToUnit(spirit, pending.Amount, bypassDefense: false, dmgCtx);
|
||||
|
||||
Reference in New Issue
Block a user