Model facing: GLB models exported from Blender arrive in Godot facing +Z, not -Z. Changed _facing_to_base_angle default case from 0 (north/-Z) to PI (south/+Z). Explicit "north" case added. Updated admin dropdown label from "Default (north)" to "Default (south / Blender GLB)" to match. Stale game state: when matchmaking.match_found is received, clear Game.state so the new GameScreen always calls send_ready() rather than rendering the previous (ended) game's state. Fixes same-players-re-match ending up in the already-completed session. 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>
292 lines
9.8 KiB
GDScript
292 lines
9.8 KiB
GDScript
extends Node
|
|
## Game autoload — manages all ranked-game state and server communication.
|
|
##
|
|
## Receives matchmaking and game server pushes, stores game state,
|
|
## and provides helpers for the GameScreen and MatchmakingScreen.
|
|
##
|
|
## Usage:
|
|
## Game.session_id ← current session GUID string
|
|
## Game.state ← latest GameStateDto dict
|
|
## Game.get_my_player() ← own PlayerStateDto dict
|
|
## Game.is_my_turn() ← bool
|
|
|
|
# ── Signals ───────────────────────────────────────────────────────────────────
|
|
|
|
## Emitted when matchmaking finds a game. Navigate to GameScreen.
|
|
signal match_found(session_id: String, opponent_name: String, opponent_rank: int)
|
|
|
|
## Emitted whenever the authoritative game state changes.
|
|
signal state_updated(state: Dictionary)
|
|
|
|
## Emitted when the game ends.
|
|
signal game_over(winner_account_id: String, win_condition: String,
|
|
winner_rank_delta: int, loser_rank_delta: int)
|
|
|
|
## Emitted when the server abandons an Initializing session (e.g. server restart).
|
|
signal session_abandoned(reason: String)
|
|
|
|
# ── State ─────────────────────────────────────────────────────────────────────
|
|
|
|
var session_id : String = ""
|
|
var state : Dictionary = {}
|
|
var opponent_name : String = ""
|
|
|
|
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
|
|
|
func _ready() -> void:
|
|
Network.message_received.connect(_on_server_push)
|
|
Auth.session_expired.connect(_clear_state)
|
|
|
|
|
|
func _clear_state() -> void:
|
|
session_id = ""
|
|
state = {}
|
|
opponent_name = ""
|
|
|
|
# ── Public helpers ────────────────────────────────────────────────────────────
|
|
|
|
## Returns the PlayerStateDto dict for the local player, or {}.
|
|
func get_my_player() -> Dictionary:
|
|
return _player_by_id(Auth.user_id)
|
|
|
|
|
|
## Returns the PlayerStateDto dict for the opponent, or {}.
|
|
func get_opponent() -> Dictionary:
|
|
for p: Dictionary in state.get("players", []):
|
|
if str(p.get("accountId", "")) != Auth.user_id:
|
|
return p
|
|
return {}
|
|
|
|
|
|
## Returns true when it is the local player's turn.
|
|
func is_my_turn() -> bool:
|
|
return str(state.get("activePlayerAccountId", "")) == Auth.user_id
|
|
|
|
|
|
## Returns the current phase string (Placement | Draw | Main | End | GameOver).
|
|
func get_phase() -> String:
|
|
return state.get("phase", "")
|
|
|
|
|
|
## Returns the BoardDto dict, or {}.
|
|
func get_board() -> Dictionary:
|
|
return state.get("board", {})
|
|
|
|
|
|
## Returns Array of TileDto dicts.
|
|
func get_tiles() -> Array:
|
|
return get_board().get("tiles", [])
|
|
|
|
|
|
## Returns the local player's hand (Array of CardInstanceDto dicts).
|
|
func get_hand() -> Array:
|
|
return get_my_player().get("hand", [])
|
|
|
|
|
|
## Returns the local player's field (Array of CardInstanceDto dicts).
|
|
func get_field() -> Array:
|
|
return get_my_player().get("field", [])
|
|
|
|
|
|
## Returns the opponent's field (Array of CardInstanceDto dicts).
|
|
func get_opponent_field() -> Array:
|
|
return get_opponent().get("field", [])
|
|
|
|
|
|
## Returns a TileDto dict by coord, or {}.
|
|
func get_tile(x: int, y: int) -> Dictionary:
|
|
for t: Dictionary in get_tiles():
|
|
var c: Dictionary = t.get("coord", {})
|
|
if c.get("x", -1) == x and c.get("y", -1) == y:
|
|
return t
|
|
return {}
|
|
|
|
|
|
## Returns a CardInstanceDto from any player's field by instanceId, or {}.
|
|
func get_unit_by_id(instance_id: String) -> Dictionary:
|
|
for p: Dictionary in state.get("players", []):
|
|
for u: Dictionary in p.get("field", []):
|
|
if u.get("instanceId", "") == instance_id:
|
|
return u
|
|
var arch: Dictionary = p.get("archSpirit", {})
|
|
if arch.get("instanceId", "") == instance_id:
|
|
return arch
|
|
return {}
|
|
|
|
|
|
## Returns the board width (default 8).
|
|
func board_width() -> int:
|
|
return get_board().get("width", 8)
|
|
|
|
|
|
## Returns the board height (default 8).
|
|
func board_height() -> int:
|
|
return get_board().get("height", 8)
|
|
|
|
# ── Server actions ────────────────────────────────────────────────────────────
|
|
|
|
func send_ready() -> void:
|
|
print("[Game] >>> game.ready sessionId=", session_id)
|
|
Network.request("game.ready",
|
|
{ "sessionId": session_id },
|
|
func(_t: String, p: Variant) -> void:
|
|
print("[Game] <<< game.ready.ack ", JSON.stringify(p))
|
|
var d := p as Dictionary
|
|
if d.get("status", "") == "AlreadyStarted":
|
|
_fetch_state(),
|
|
func(code: String, msg: String) -> void:
|
|
print("[Game] ERR game.ready code=", code, " msg=", msg))
|
|
|
|
|
|
func _fetch_state() -> void:
|
|
Network.request("game.get_state",
|
|
{ "sessionId": session_id },
|
|
func(_t: String, p: Variant) -> void: _apply_state_from_payload(p),
|
|
func(_c: String, _m: String) -> void: pass)
|
|
|
|
|
|
func send_draw() -> void:
|
|
_game_request("game.draw", {})
|
|
|
|
|
|
func send_place_arch_spirit(x: int, y: int) -> void:
|
|
_game_request("game.place_arch_spirit",
|
|
{ "position": { "x": x, "y": y } })
|
|
|
|
|
|
func send_play_card(card_instance_id: String, target_x: int, target_y: int) -> void:
|
|
_game_request("game.play_card", {
|
|
"cardInstanceId": card_instance_id,
|
|
"targetTile": { "x": target_x, "y": target_y }
|
|
})
|
|
|
|
|
|
func send_play_card_multi(card_instance_id: String, targets: Array) -> void:
|
|
_game_request("game.play_card", {
|
|
"cardInstanceId": card_instance_id,
|
|
"targetTiles": targets
|
|
})
|
|
|
|
|
|
func send_move_unit(unit_instance_id: String, target_x: int, target_y: int) -> void:
|
|
_game_request("game.move_unit", {
|
|
"unitInstanceId": unit_instance_id,
|
|
"targetPosition": { "x": target_x, "y": target_y }
|
|
})
|
|
|
|
|
|
func send_attack(attacker_id: String, target_id: String) -> void:
|
|
_game_request("game.attack", {
|
|
"attackerInstanceId": attacker_id,
|
|
"targetInstanceId": target_id
|
|
})
|
|
|
|
|
|
func send_sacrifice(unit_id: String) -> void:
|
|
_game_request("game.sacrifice", { "unitInstanceId": unit_id })
|
|
|
|
|
|
func send_end_turn() -> void:
|
|
_game_request("game.end_turn", {})
|
|
|
|
|
|
func send_concede() -> void:
|
|
_game_request("game.concede", {})
|
|
|
|
|
|
func send_ascend(incoming_card_instance_id: String) -> void:
|
|
_game_request("game.ascend",
|
|
{ "incomingCardInstanceId": incoming_card_instance_id })
|
|
|
|
|
|
func send_transition(incoming_card_instance_id: String) -> void:
|
|
_game_request("game.transition",
|
|
{ "incomingCardInstanceId": incoming_card_instance_id })
|
|
|
|
|
|
func send_activate_ability(source_id: String, ability_index: int,
|
|
targets: Array = []) -> void:
|
|
var payload := {
|
|
"sourceInstanceId": source_id,
|
|
"abilityIndex": ability_index
|
|
}
|
|
if not targets.is_empty():
|
|
payload["targets"] = targets
|
|
_game_request("game.activate_ability", payload)
|
|
|
|
# ── Private ───────────────────────────────────────────────────────────────────
|
|
|
|
func _game_request(msg_type: String, extra: Dictionary) -> void:
|
|
var payload := { "sessionId": session_id }
|
|
payload.merge(extra)
|
|
print("[Game] >>> ", msg_type, " ", JSON.stringify(extra))
|
|
Network.request(msg_type, payload,
|
|
func(_t: String, p: Variant) -> void:
|
|
print("[Game] <<< ", msg_type, ".ack payload=", JSON.stringify(p))
|
|
_apply_state_from_payload(p),
|
|
func(code: String, msg: String) -> void:
|
|
print("[Game] ERR ", msg_type, " code=", code, " msg=", msg))
|
|
|
|
|
|
func _apply_state_from_payload(p: Variant) -> void:
|
|
var d := p as Dictionary
|
|
var s := d.get("state", {}) as Dictionary
|
|
if s.is_empty():
|
|
print("[Game] _apply_state_from_payload: no 'state' key in payload ", JSON.stringify(p))
|
|
return
|
|
print("[Game] state applied phase=", s.get("phase","?"),
|
|
" turn=", s.get("turnNumber","?"),
|
|
" activePlayer=", str(s.get("activePlayerAccountId","?")).substr(0,8))
|
|
state = s
|
|
state_updated.emit(state)
|
|
|
|
|
|
func _player_by_id(account_id: String) -> Dictionary:
|
|
for p: Dictionary in state.get("players", []):
|
|
if str(p.get("accountId", "")).to_lower() == account_id.to_lower():
|
|
return p
|
|
return {}
|
|
|
|
|
|
func _on_server_push(type: String, payload: Variant) -> void:
|
|
if type.begins_with("game.") or type.begins_with("matchmaking."):
|
|
print("[Game] PUSH ", type, " ", JSON.stringify(payload).substr(0, 200))
|
|
match type:
|
|
"matchmaking.match_found":
|
|
var d := payload as Dictionary
|
|
var sid : String = str(d.get("gameSessionId", ""))
|
|
var oname : String = str(d.get("opponentDisplayName", "Opponent"))
|
|
var orank : int = int(d.get("opponentRankScore", 0))
|
|
state = {} # Clear any stale previous-game state before new session starts
|
|
session_id = sid
|
|
opponent_name = oname
|
|
match_found.emit(sid, oname, orank)
|
|
|
|
"game.state_update":
|
|
var d := payload as Dictionary
|
|
var s := d.get("state", {}) as Dictionary
|
|
if not s.is_empty():
|
|
print("[Game] PUSH state_update phase=", s.get("phase","?"),
|
|
" turn=", s.get("turnNumber","?"),
|
|
" activePlayer=", str(s.get("activePlayerAccountId","?")).substr(0,8))
|
|
state = s
|
|
state_updated.emit(state)
|
|
else:
|
|
print("[Game] PUSH state_update: no 'state' key")
|
|
|
|
"game.over":
|
|
var d := payload as Dictionary
|
|
var winner : String = str(d.get("winnerAccountId", ""))
|
|
var wc : String = str(d.get("winCondition", ""))
|
|
var wd : int = int(d.get("winnerRankDelta", 0))
|
|
var ld : int = int(d.get("loserRankDelta", 0))
|
|
state = {} # Clear stale state so a new GameScreen calls send_ready() on the next match
|
|
game_over.emit(winner, wc, wd, ld)
|
|
|
|
"game.session_abandoned":
|
|
var d := payload as Dictionary
|
|
var reason : String = str(d.get("reason", "unknown"))
|
|
print("[Game] session abandoned: ", reason)
|
|
_clear_state()
|
|
session_abandoned.emit(reason)
|