Fix: abilitiesJson null crash, battlefield popup, ExactSelf ability filter

Client (game_screen.gd):
- Fix crash when clicking a unit with null abilitiesJson: server sends the key
  with a null value, so dict.get(key,default) returns null — guard both uses
  (_show_ability_panel and _on_hold_timer_fired) with explicit null check.
- Add double-click on battlefield units to open a full CardDetailPopup showing
  name, affinity, stats, and ability descriptions. Popup is instantiated once
  in _ready() and reused; aliases movementPoints/abilities keys as expected.

Server (Filters.cs / AbilitySystem.cs):
- IsSelf filter now matches by CardDefinitionId (definition-level: any copy of
  the same card). This is the appropriate semantic for "self" as a type scope.
- IsExactSelf filter added: matches by InstanceId (instance-level: only this
  specific unit on the battlefield). Use this to prevent duplicate-instance
  trigger bleed where two copies of a card both fire when only one acted.
- TriggerMatches now accepts the evaluating FieldCard and forwards its
  InstanceId to Own() for all ownership checks.
- Own() gains "ExactSelf" scope: fires only when occ.Data.SourceId matches the
  evaluating unit's InstanceId — eliminating the definition-level bleed bug.

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-03-08 16:28:06 -04:00
co-authored by Claude Happy
parent 94f39be8a7
commit 11ebdc40d7
3 changed files with 93 additions and 33 deletions
@@ -64,6 +64,7 @@ enum Mode {
var count : int = 0
var _last_click_frame : int = -1
var _last_click_iid : String = ""
var _card_detail_popup : Control = null
# ── Affinity colors ───────────────────────────────────────────────────────────
const AFFINITY_COLORS : Dictionary = {
@@ -92,7 +93,8 @@ const AFFINITY_COLORS : Dictionary = {
"GatewayField": Color(0.52, 0.60, 0.22),
}
const _CARD_WIDGET_SCENE := preload("res://scenes/collection/components/CardWidget.tscn")
const _CARD_WIDGET_SCENE := preload("res://scenes/collection/components/CardWidget.tscn")
const _CARD_DETAIL_POPUP_SCENE := preload("res://scenes/collection/components/CardDetailPopup.tscn")
const HI_GREEN := Color(0.20, 0.90, 0.20, 0.40)
const HI_RED := Color(1.00, 0.25, 0.25, 0.50)
@@ -159,6 +161,11 @@ func _ready() -> void:
get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn"))
_hold_timer.timeout.connect(_on_hold_timer_fired)
# Card detail popup (shared overlay — reused across multiple unit clicks).
_card_detail_popup = _CARD_DETAIL_POPUP_SCENE.instantiate()
_hud.add_child(_card_detail_popup)
_card_detail_popup.hide()
Game.state_updated.connect(_on_state_updated)
Game.game_over.connect(_on_game_over)
@@ -221,6 +228,12 @@ func _unhandled_input(event: InputEvent) -> void:
_last_mouse = mb.position
elif mb.button_index == MOUSE_BUTTON_LEFT:
if mb.pressed:
# Double-click on a battlefield unit → show full card detail popup.
if mb.double_click:
var dbl_unit_id := _mouse_to_unit_id(mb.position)
if not dbl_unit_id.is_empty():
_show_unit_detail_popup(dbl_unit_id)
return
_last_mouse = mb.position
_drag_active = false
_press_was_on_board = true # unhandled press = board area, not a UI card
@@ -986,7 +999,10 @@ func _show_card_picker(title: String, cards: Array, on_pick: Callable) -> void:
func _show_ability_panel(unit_id: String, unit: Dictionary) -> void:
_hide_ability_panel()
var abil_json : String = unit.get("abilitiesJson", "")
# Null trap: server sends "abilitiesJson": null for units with no abilities.
# dict.get(key, default) returns null when key exists with null value.
var _raw_abil_ap = unit.get("abilitiesJson")
var abil_json : String = "" if _raw_abil_ap == null else str(_raw_abil_ap)
if abil_json.is_empty(): return
var parsed = JSON.parse_string(abil_json)
if not parsed is Array: return
@@ -1038,6 +1054,18 @@ func _hide_ability_panel() -> void:
_ability_panel.queue_free()
_ability_panel = null
func _show_unit_detail_popup(unit_id: String) -> void:
var unit := Game.get_unit_by_id(unit_id)
if unit.is_empty(): return
# Build a dict with the aliases card_detail_popup expects.
var data := unit.duplicate()
data["movementPoints"] = unit.get("maxMp", null)
# Null-safe abilitiesJson → abilities alias (popup reads "abilities" key).
var _raw_abil_udp = unit.get("abilitiesJson")
data["abilities"] = "" if _raw_abil_udp == null else str(_raw_abil_udp)
_card_detail_popup.show_card(data, data)
# ── Hand card ArchSpirit routing ──────────────────────────────────────────────
func _on_hand_card_clicked(iid: String, _panel: PanelContainer) -> void:
@@ -1291,7 +1319,8 @@ func _on_hold_timer_fired() -> void:
int(unit.get("currentDefense", 0)), int(unit.get("maxDefense", 0)),
int(unit.get("damage", 0))]
var abil_str := ""
var parsed = JSON.parse_string(unit.get("abilitiesJson", "[]"))
var _raw_abil_hud = unit.get("abilitiesJson")
var parsed = JSON.parse_string("[]" if _raw_abil_hud == null else str(_raw_abil_hud))
if parsed is Array:
for ab: Dictionary in parsed:
abil_str += "• %s\n" % str(ab.get("name", ""))
+50 -29
View File
@@ -128,7 +128,7 @@ public static class AbilitySystem
var def = abilities[i];
if (def.Type != AbilityType.Triggered) continue;
if (def.TriggerEvent is null) continue;
if (!TriggerMatches(def.TriggerEvent, occurrence, state, ownerId)) continue;
if (!TriggerMatches(def.TriggerEvent, occurrence, state, ownerId, unit)) continue;
var ctx = new AbilityContext
{
@@ -1387,7 +1387,10 @@ public static class AbilitySystem
_ => false,
},
TierIsFilter tif => card.Tier == tif.Tier,
IsSelfFilter => ctx.Source is not null && card.InstanceId == ctx.Source.InstanceId,
// IsSelf: definition-level match (any instance of the same card definition).
IsSelfFilter => ctx.Source is not null && card.CardDefinitionId == ctx.Source.CardDefinitionId,
// IsExactSelf: instance-level match (only this specific unit on the battlefield).
IsExactSelfFilter => ctx.Source is not null && card.InstanceId == ctx.Source.InstanceId,
HealthBelowFilter hbf => (card.CurrentHealth ?? 0) < hbf.N,
HealthAboveFilter haf => (card.CurrentHealth ?? 0) > haf.N,
_ => true,
@@ -1395,91 +1398,109 @@ public static class AbilitySystem
// ── Trigger matching ──────────────────────────────────────────────────────
/// <param name="evaluatingUnit">
/// The specific battlefield unit whose trigger is being evaluated.
/// Required for the "ExactSelf" ownership scope — fire only when this unit
/// is the source of the event (occ.Data.SourceId == evaluatingUnit.InstanceId).
/// </param>
private static bool TriggerMatches(
TriggerEvent trigger, TriggerOccurrence occurrence, GameState state, string ownerId)
=> trigger switch
TriggerEvent trigger, TriggerOccurrence occurrence, GameState state, string ownerId,
FieldCard? evaluatingUnit = null)
{
var uid = evaluatingUnit?.InstanceId;
return trigger switch
{
OnTurnStartTrigger t =>
occurrence.Kind == TriggerEventKind.TurnStart
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnTurnEndTrigger t =>
occurrence.Kind == TriggerEventKind.TurnEnd
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnUnitDeathTrigger t =>
occurrence.Kind == TriggerEventKind.UnitDeath
&& UnitType(t.UnitType, occurrence.UnitType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnDamageDealtTrigger =>
occurrence.Kind == TriggerEventKind.DamageDealt,
OnUnitEnterBattlefieldTrigger t =>
occurrence.Kind == TriggerEventKind.UnitEnterBattlefield
&& UnitType(t.UnitType, occurrence.UnitType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnUnitLeaveBattlefieldTrigger t =>
occurrence.Kind == TriggerEventKind.UnitLeaveBattlefield
&& UnitType(t.UnitType, occurrence.UnitType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnSPGainedTrigger t =>
occurrence.Kind == TriggerEventKind.SPGained
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnSPSpentTrigger t =>
occurrence.Kind == TriggerEventKind.SPSpent
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnSTGainedTrigger t =>
occurrence.Kind == TriggerEventKind.STGained
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnSTSpentTrigger t =>
occurrence.Kind == TriggerEventKind.STSpent
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnCardDrawnTrigger t =>
occurrence.Kind == TriggerEventKind.CardDrawn
&& Own(t.Player, occurrence, ownerId, state),
&& Own(t.Player, occurrence, ownerId, state, uid),
OnUnitMoveTrigger t =>
occurrence.Kind == TriggerEventKind.UnitMove
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnUnitAttackTrigger t =>
occurrence.Kind == TriggerEventKind.UnitAttack
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnSpiritSacrificedTrigger t =>
occurrence.Kind == TriggerEventKind.SpiritSacrificed
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnAscendTrigger t =>
occurrence.Kind == TriggerEventKind.Ascend
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnTransitionTrigger t =>
occurrence.Kind == TriggerEventKind.Transition
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnChanneledTrigger t =>
occurrence.Kind == TriggerEventKind.Channeled
&& CardType(t.CardType, occurrence.CardType)
&& Own(t.Ownership, occurrence, ownerId, state),
&& Own(t.Ownership, occurrence, ownerId, state, uid),
OnEnterAuraRangeTrigger eat =>
occurrence.Kind == TriggerEventKind.EnterAuraRange
&& Own(eat.AuraOwnership, occurrence, ownerId, state)
&& Own(eat.AuraOwnership, occurrence, ownerId, state, uid)
&& UnitOwnerMatches(eat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId),
OnLeaveAuraRangeTrigger lat =>
occurrence.Kind == TriggerEventKind.LeaveAuraRange
&& Own(lat.AuraOwnership, occurrence, ownerId, state)
&& Own(lat.AuraOwnership, occurrence, ownerId, state, uid)
&& UnitOwnerMatches(lat.UnitOwnership, occurrence.Data?.TargetId, state, ownerId),
OnRuinActivationTrigger rat =>
occurrence.Kind == TriggerEventKind.RuinActivation
&& Own(rat.RuinOwnership, occurrence, ownerId, state)
&& Own(rat.RuinOwnership, occurrence, ownerId, state, uid)
&& (rat.OccupantOwnership is null
|| UnitOwnerMatches(rat.OccupantOwnership, occurrence.Data?.TargetId, state, ownerId)),
OnUnitOccupyRuinTrigger uor =>
occurrence.Kind == TriggerEventKind.UnitOccupyRuin
&& Own(uor.UnitOwnership, occurrence, ownerId, state),
&& Own(uor.UnitOwnership, occurrence, ownerId, state, uid),
_ => false,
};
}
private static bool Own(string? scope, TriggerOccurrence occ, string unitOwnerId, GameState _)
/// <param name="evalUnitId">
/// Instance ID of the unit whose trigger is being evaluated.
/// Only used for the "ExactSelf" scope: the trigger fires only when this unit
/// is the specific unit that caused the event (occ.Data.SourceId == evalUnitId).
/// </param>
private static bool Own(string? scope, TriggerOccurrence occ, string unitOwnerId, GameState _,
string? evalUnitId = null)
=> (scope?.ToLowerInvariant()) switch
{
null or "any" => true,
null or "any" => true,
"own" or "self" or "owner" => occ.ActorId == unitOwnerId,
"enemy" => occ.ActorId != unitOwnerId,
"ally" => occ.ActorId == unitOwnerId,
_ => true,
"enemy" => occ.ActorId != unitOwnerId,
"ally" => occ.ActorId == unitOwnerId,
// ExactSelf: fires only for the specific unit that performed the action.
// Requires occ.Data.SourceId to carry the acting unit's instance ID.
"exactself" => evalUnitId is not null && occ.Data?.SourceId == evalUnitId,
_ => true,
};
private static bool UnitOwnerMatches(
@@ -26,6 +26,7 @@ using SOR.Database.Enums;
[JsonDerivedType(typeof(CardSTCostRangeFilter), "CardSTCostRange")]
[JsonDerivedType(typeof(WithinRangeFilter), "WithinRange")]
[JsonDerivedType(typeof(IsSelfFilter), "IsSelf")]
[JsonDerivedType(typeof(IsExactSelfFilter), "IsExactSelf")]
// ── Tile filters ─────────────────────────────────────────────────────────────
[JsonDerivedType(typeof(TileAffinityMatchesFilter), "TileAffinityMatches")]
[JsonDerivedType(typeof(TileIsOccupiedFilter), "TileIsOccupied")]
@@ -86,9 +87,18 @@ public sealed class CardSTCostRangeFilter : Filter { public int Min { get; set;
/// <summary>Unit must be within Min..Max Manhattan distance of the ability's source unit.</summary>
public sealed class WithinRangeFilter : Filter { public int Min { get; set; } public int Max { get; set; } }
/// <summary>Matches only the exact unit that owns this ability (not all friendly units).</summary>
/// <summary>
/// Matches any unit that shares the same card definition as the ability source.
/// Use for abilities that affect "all copies of this card type".
/// </summary>
public sealed class IsSelfFilter : Filter { }
/// <summary>
/// Matches only the exact unit instance that owns this ability (by InstanceId).
/// Use this instead of IsSelf when only the specific unit on the battlefield should be affected.
/// </summary>
public sealed class IsExactSelfFilter : Filter { }
// ── Tile filter implementations ───────────────────────────────────────────────
public sealed class TileAffinityMatchesFilter : Filter