Cut over game.state_delta to account-scoped multiplayer schema

This commit is contained in:
Shard
2026-03-27 12:42:57 +00:00
parent f07ea4135a
commit cb024c70e2
5 changed files with 260 additions and 257 deletions
+83 -90
View File
@@ -397,38 +397,27 @@ func _apply_delta(delta: Dictionary) -> void:
break
# 2. Added units (new units entering the field)
var added_units = delta.get("addedUnits")
var added_units = delta.get("unitsAdded")
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
# doesn't already have this unit. We append to the player who owns the tile.
# Fallback: use the unit's position coord to find the tile's occupant owner.
var placed := false
for p: Dictionary in state.get("players", []):
var field: Array = p.get("field", [])
# Check if this player already has the unit (shouldn't happen)
var found := false
for u: Dictionary in field:
if u.get("instanceId", "") == unit.get("instanceId", ""):
found = true
break
if not found and not placed:
# Determine ownership from the tile's occupantOwner matching this player's accountId
var pos: Dictionary = unit.get("position", {})
if not pos.is_empty():
var tile := get_tile(int(pos.get("x", -1)), int(pos.get("y", -1)))
if str(tile.get("occupantOwner", "")).to_lower() == str(p.get("accountId", "")).to_lower():
field.append(unit)
placed = true
# Fallback if we couldn't place via tile: append to active player's field
if not placed:
var my_p := get_my_player()
if not my_p.is_empty():
my_p.get("field", []).append(unit)
for add_entry: Dictionary in added_units:
var owner_id := str(add_entry.get("ownerAccountId", ""))
var unit := add_entry.get("unit", {}) as Dictionary
if unit.is_empty():
continue
var target := _player_by_id(owner_id)
if target.is_empty():
continue
var field: Array = target.get("field", [])
var exists := false
for u: Dictionary in field:
if str(u.get("instanceId", "")) == str(unit.get("instanceId", "")):
exists = true
break
if not exists:
field.append(unit)
# 3. Removed unit IDs
var removed_ids = delta.get("removedUnitIds")
var removed_ids = delta.get("unitsRemoved")
if removed_ids is Array:
for iid: String in removed_ids:
for p: Dictionary in state.get("players", []):
@@ -439,7 +428,7 @@ func _apply_delta(delta: Dictionary) -> void:
break
# 4. Changed units (merge non-null UnitDelta fields)
var changed_units = delta.get("changedUnits")
var changed_units = delta.get("unitsChanged")
if changed_units is Array:
for ud: Dictionary in changed_units:
var iid := str(ud.get("instanceId", ""))
@@ -447,72 +436,76 @@ func _apply_delta(delta: Dictionary) -> void:
if not unit.is_empty():
_merge_unit_delta(unit, ud)
# 5. Hand added / removed (own player only)
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)
# 5. Viewer-private zones (own hand/discard only)
var private_delta = delta.get("privateByViewer")
if private_delta is Dictionary and not private_delta.is_empty():
var hand_added = private_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 = 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):
if my_hand[i].get("instanceId", "") == iid:
my_hand.remove_at(i)
break
var hand_removed = private_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):
if my_hand[i].get("instanceId", "") == iid:
my_hand.remove_at(i)
break
# 6. Discard added
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)
var discard_added = private_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")
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)
# 6. Public counts by account
var public_counts = delta.get("publicCountsByAccount")
if public_counts is Dictionary:
for aid_v: Variant in public_counts.keys():
var aid := str(aid_v)
var counts := public_counts.get(aid, {}) as Dictionary
var player := _player_by_id(aid)
if player.is_empty() or counts.is_empty():
continue
if counts.has("handCount") and counts["handCount"] != null: player["handCount"] = int(counts["handCount"])
if counts.has("deckCount") and counts["deckCount"] != null: player["deckCount"] = int(counts["deckCount"])
if counts.has("discardCount") and counts["discardCount"] != null: player["discardCount"] = int(counts["discardCount"])
# 8. ArchSpirits
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)
# 7. ArchSpirit deltas by account
var arch_by_account = delta.get("archSpiritByAccount")
if arch_by_account is Dictionary:
for aid_v: Variant in arch_by_account.keys():
var aid := str(aid_v)
var arch_delta := arch_by_account.get(aid, {}) as Dictionary
if arch_delta.is_empty():
continue
var player := _player_by_id(aid)
if player.is_empty():
continue
var arch: Dictionary = player.get("archSpirit", {})
if not arch.is_empty():
_merge_unit_delta(arch, arch_delta)
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)
# 8. Player stats by account
var stats_by_account = delta.get("playerStatsByAccount")
if stats_by_account is Dictionary:
for aid_v: Variant in stats_by_account.keys():
var aid := str(aid_v)
var stats := stats_by_account.get(aid, {}) as Dictionary
if stats.is_empty():
continue
var player := _player_by_id(aid)
if player.is_empty():
continue
if stats.has("currentSp") and stats["currentSp"] != null: player["currentSp"] = int(stats["currentSp"])
if stats.has("currentSt") and stats["currentSt"] != null: player["currentSt"] = int(stats["currentSt"])
if stats.has("deckCount") and stats["deckCount"] != null: player["deckCount"] = int(stats["deckCount"])
if stats.has("handCount") and stats["handCount"] != null: player["handCount"] = int(stats["handCount"])
if stats.has("discardCount") and stats["discardCount"] != null: player["discardCount"] = int(stats["discardCount"])
# 9. Player stats
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"])
if own_stats.has("currentSt") and own_stats["currentSt"] != null: my_p["currentSt"] = int(own_stats["currentSt"])
if own_stats.has("deckCount") and own_stats["deckCount"] != null: my_p["deckCount"] = int(own_stats["deckCount"])
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 = 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"])
if opp_stats.has("currentSt") and opp_stats["currentSt"] != null: opp_p["currentSt"] = int(opp_stats["currentSt"])
if opp_stats.has("deckCount") and opp_stats["deckCount"] != null: opp_p["deckCount"] = int(opp_stats["deckCount"])
if opp_stats.has("handCount") and opp_stats["handCount"] != null: opp_p["handCount"] = int(opp_stats["handCount"])
if opp_stats.has("discardCount") and opp_stats["discardCount"] != null: opp_p["discardCount"] = int(opp_stats["discardCount"])
# 10. Meta
# 9. Meta
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"]