Fix _apply_delta crash on null array/dict fields in delta

dict.get(key, default) returns null (not the default) when the key exists
with a null value. Assigning null to a typed Array or Dictionary variable
throws a type error in GDScript 4. Replace all typed var declarations with
untyped vars guarded by 'is Array' / 'is Dictionary' checks so a fully-null
delta (e.g. a stats-only update that sends changedTiles: null) no longer
crashes every game action.

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-21 10:25:03 -04:00
co-authored by Claude Happy
parent b812372514
commit 88f395e032
+30 -26
View File
@@ -313,9 +313,12 @@ func _player_by_id(account_id: String) -> Dictionary:
func _apply_delta(delta: Dictionary) -> void:
# Helper: dict.get(key, default) returns null (not default) when the key exists with value null.
# Use 'is Array' / 'is Dictionary' guards so typed assignment never receives null.
# 1. Changed tiles
var changed_tiles: Array = delta.get("changedTiles", [])
if changed_tiles != null:
var changed_tiles = delta.get("changedTiles")
if changed_tiles is Array:
var tiles: Array = state.get("board", {}).get("tiles", [])
for td: Dictionary in changed_tiles:
var tx := int(td.get("x", -1))
@@ -335,8 +338,8 @@ func _apply_delta(delta: Dictionary) -> void:
break
# 2. Added units (new units entering the field)
var added_units: Array = delta.get("addedUnits", [])
if added_units != null:
var added_units = delta.get("addedUnits")
if added_units is Array:
for unit: Dictionary in added_units:
var owner_id := str(unit.get("ownerId", "")) # not on CardInstanceDto, use position to find by ownerId?
# CardInstanceDto doesn't carry ownerId; find the right player by checking which player
@@ -366,8 +369,8 @@ func _apply_delta(delta: Dictionary) -> void:
my_p.get("field", []).append(unit)
# 3. Removed unit IDs
var removed_ids: Array = delta.get("removedUnitIds", [])
if removed_ids != null:
var removed_ids = delta.get("removedUnitIds")
if removed_ids is Array:
for iid: String in removed_ids:
for p: Dictionary in state.get("players", []):
var field: Array = p.get("field", [])
@@ -377,8 +380,8 @@ func _apply_delta(delta: Dictionary) -> void:
break
# 4. Changed units (merge non-null UnitDelta fields)
var changed_units: Array = delta.get("changedUnits", [])
if changed_units != null:
var changed_units = delta.get("changedUnits")
if changed_units is Array:
for ud: Dictionary in changed_units:
var iid := str(ud.get("instanceId", ""))
var unit := get_unit_by_id(iid)
@@ -386,14 +389,14 @@ func _apply_delta(delta: Dictionary) -> void:
_merge_unit_delta(unit, ud)
# 5. Hand added / removed (own player only)
var hand_added: Array = delta.get("handAdded", [])
if hand_added != null and not hand_added.is_empty():
var hand_added = delta.get("handAdded")
if hand_added is Array and not hand_added.is_empty():
var my_hand: Array = get_my_player().get("hand", [])
for card: Dictionary in hand_added:
my_hand.append(card)
var hand_removed: Array = delta.get("handRemovedIds", [])
if hand_removed != null:
var hand_removed = delta.get("handRemovedIds")
if hand_removed is Array:
var my_hand: Array = get_my_player().get("hand", [])
for iid: String in hand_removed:
for i in range(my_hand.size() - 1, -1, -1):
@@ -402,36 +405,36 @@ func _apply_delta(delta: Dictionary) -> void:
break
# 6. Discard added
var discard_added: Array = delta.get("discardAdded", [])
if discard_added != null and not discard_added.is_empty():
var discard_added = delta.get("discardAdded")
if discard_added is Array and not discard_added.is_empty():
var my_discard: Array = get_my_player().get("discard", [])
for card: Dictionary in discard_added:
my_discard.append(card)
# 7. Opponent hand / deck counts
var opp_hand_count = delta.get("opponentHandCount", null)
var opp_deck_count = delta.get("opponentDeckCount", null)
var opp_hand_count = delta.get("opponentHandCount")
var opp_deck_count = delta.get("opponentDeckCount")
var opp := get_opponent()
if not opp.is_empty():
if opp_hand_count != null: opp["handCount"] = int(opp_hand_count)
if opp_deck_count != null: opp["deckCount"] = int(opp_deck_count)
# 8. ArchSpirits
var own_arch_delta: Dictionary = delta.get("ownArchSpiritDelta", {})
if own_arch_delta != null and not own_arch_delta.is_empty():
var own_arch_delta = delta.get("ownArchSpiritDelta")
if own_arch_delta is Dictionary and not own_arch_delta.is_empty():
var my_arch: Dictionary = get_my_player().get("archSpirit", {})
if not my_arch.is_empty():
_merge_unit_delta(my_arch, own_arch_delta)
var opp_arch_delta: Dictionary = delta.get("opponentArchSpiritDelta", {})
if opp_arch_delta != null and not opp_arch_delta.is_empty():
var opp_arch_delta = delta.get("opponentArchSpiritDelta")
if opp_arch_delta is Dictionary and not opp_arch_delta.is_empty():
var opp_arch: Dictionary = get_opponent().get("archSpirit", {})
if not opp_arch.is_empty():
_merge_unit_delta(opp_arch, opp_arch_delta)
# 9. Player stats
var own_stats: Dictionary = delta.get("ownStatsDelta", {})
if own_stats != null and not own_stats.is_empty():
var own_stats = delta.get("ownStatsDelta")
if own_stats is Dictionary and not own_stats.is_empty():
var my_p := get_my_player()
if not my_p.is_empty():
if own_stats.has("currentSp") and own_stats["currentSp"] != null: my_p["currentSp"] = int(own_stats["currentSp"])
@@ -440,8 +443,8 @@ func _apply_delta(delta: Dictionary) -> void:
if own_stats.has("handCount") and own_stats["handCount"] != null: my_p["handCount"] = int(own_stats["handCount"])
if own_stats.has("discardCount") and own_stats["discardCount"] != null: my_p["discardCount"] = int(own_stats["discardCount"])
var opp_stats: Dictionary = delta.get("opponentStatsDelta", {})
if opp_stats != null and not opp_stats.is_empty():
var opp_stats = delta.get("opponentStatsDelta")
if opp_stats is Dictionary and not opp_stats.is_empty():
var opp_p := get_opponent()
if not opp_p.is_empty():
if opp_stats.has("currentSp") and opp_stats["currentSp"] != null: opp_p["currentSp"] = int(opp_stats["currentSp"])
@@ -451,8 +454,8 @@ func _apply_delta(delta: Dictionary) -> void:
if opp_stats.has("discardCount") and opp_stats["discardCount"] != null: opp_p["discardCount"] = int(opp_stats["discardCount"])
# 10. Meta
var meta: Dictionary = delta.get("metaDelta", {})
if meta != null and not meta.is_empty():
var meta = delta.get("metaDelta")
if meta is Dictionary and not meta.is_empty():
if meta.has("phase") and meta["phase"] != null: state["phase"] = meta["phase"]
if meta.has("activePlayerAccountId") and meta["activePlayerAccountId"] != null: state["activePlayerAccountId"] = meta["activePlayerAccountId"]
if meta.has("turnNumber") and meta["turnNumber"] != null: state["turnNumber"] = int(meta["turnNumber"])
@@ -525,6 +528,7 @@ func _on_server_push(type: String, payload: Variant) -> void:
% [_state_version, v])
_fetch_state()
return
print("[Delta] ", delta)
_apply_delta(delta)
"game.over":