extends Node ## CustomLobby autoload — manages free-play lobby state and server communication. ## ## Maintains local state for the lobby the player is currently in (as host or guest), ## handles incoming server pushes from the lobby room, and exposes clean methods for ## all lobby actions (create, join, leave, ready, unready, change deck, update rules, ## preset management). ## ## Usage: ## CustomLobby.lobby_id ← current lobby GUID string ## CustomLobby.lobby ← latest lobby details dict ## CustomLobby.is_host ← bool ## CustomLobby.i_am_ready ← bool ## CustomLobby.partner_ready ← bool # ── Signals ─────────────────────────────────────────────────────────────────── ## Pushed when a guest joins the host's open lobby. signal guest_joined(guest_account_id: String) ## Pushed when the host updates lobby rules (or a preset vote is approved). signal lobby_rules_updated(new_rules: Dictionary) ## Pushed when a participant changes their selected deck. signal lobby_deck_changed(account_id: String, deck_id: String) ## Pushed when any participant's ready status changes. signal ready_status_changed(host_ready: bool, guest_ready: bool) ## Pushed when both players are ready and the game session has been created. ## Navigate to GameScreen.tscn after receiving this. signal game_starting(session_id: String, opponent_account_id: String) ## Pushed when the host proposes loading a preset; guests should show a vote banner. signal preset_vote_requested(payload: Dictionary) ## Pushed when a preset vote is rejected. signal preset_vote_rejected(lobby_id: String, preset_id: String) ## Emitted locally when the player has left or cancelled the lobby. signal lobby_left # ── State ───────────────────────────────────────────────────────────────────── var lobby_id : String = "" var lobby : Dictionary = {} var my_deck_id : String = "" var is_host : bool = false var i_am_ready : bool = false var partner_ready : bool = false var first_turn_pref: String = "random" # ── Lifecycle ───────────────────────────────────────────────────────────────── func _ready() -> void: Network.message_received.connect(_on_server_push) Auth.session_expired.connect(clear_state) func clear_state() -> void: lobby_id = "" lobby = {} my_deck_id = "" is_host = false i_am_ready = false partner_ready = false first_turn_pref = "random" # ── Public actions ──────────────────────────────────────────────────────────── ## Create a new lobby. On success, lobby_id and is_host are set; navigate to LobbyRoomScreen. func create(lobby_name: String, deck_id: String, password: String, rules_override: Dictionary, on_success: Callable = Callable(), on_error: Callable = Callable()) -> void: my_deck_id = deck_id var payload := { "lobbyName": lobby_name, "deckId": deck_id, } if password != "": payload["password"] = password if not rules_override.is_empty(): payload["rules"] = rules_override Network.request("game.custom.create", payload, func(_t: String, p: Variant) -> void: var d := p as Dictionary lobby_id = str(d.get("lobbyId", "")) lobby = d is_host = true if on_success.is_valid(): on_success.call(d), func(code: String, msg: String) -> void: print("[CustomLobby] ERR create ", code, " ", msg) if on_error.is_valid(): on_error.call(code, msg)) ## Join an existing open lobby. On success, lobby_id and is_host=false are set. func join(lid: String, deck_id: String, password: String, on_success: Callable = Callable(), on_error: Callable = Callable()) -> void: my_deck_id = deck_id var payload := { "lobbyId": lid, "deckId": deck_id } if password != "": payload["password"] = password Network.request("game.custom.join", payload, func(_t: String, p: Variant) -> void: var d := p as Dictionary lobby_id = str(d.get("lobbyId", "")) lobby = d is_host = false if on_success.is_valid(): on_success.call(d), func(code: String, msg: String) -> void: print("[CustomLobby] ERR join ", code, " ", msg) if on_error.is_valid(): on_error.call(code, msg)) ## Leave or cancel the current lobby. func leave(on_done: Callable = Callable()) -> void: if lobby_id.is_empty(): return Network.request("game.custom.leave", { "lobbyId": lobby_id }, func(_t: String, _p: Variant) -> void: clear_state() lobby_left.emit() if on_done.is_valid(): on_done.call(), func(code: String, msg: String) -> void: print("[CustomLobby] ERR leave ", code, " ", msg)) ## Mark the caller as ready with their final deck and first-turn preference. func set_ready(deck_id: String, pref: String) -> void: my_deck_id = deck_id first_turn_pref = pref Network.request("game.custom.lobby.ready", { "lobbyId": lobby_id, "deckId": deck_id, "firstTurnPref": pref }, func(_t: String, _p: Variant) -> void: i_am_ready = true, func(code: String, msg: String) -> void: print("[CustomLobby] ERR set_ready ", code, " ", msg)) ## Withdraw the caller's ready status. func unready() -> void: Network.request("game.custom.lobby.unready", { "lobbyId": lobby_id }, func(_t: String, _p: Variant) -> void: i_am_ready = false, func(code: String, msg: String) -> void: print("[CustomLobby] ERR unready ", code, " ", msg)) ## Change the caller's deck selection (only when not yet ready). func change_deck(deck_id: String) -> void: my_deck_id = deck_id Network.request("game.custom.lobby.change_deck", { "lobbyId": lobby_id, "deckId": deck_id }, func(_t: String, _p: Variant) -> void: pass, func(code: String, msg: String) -> void: print("[CustomLobby] ERR change_deck ", code, " ", msg)) ## Host-only: update lobby rule overrides (only when neither player is ready). func update_rules(rules_override: Dictionary) -> void: Network.request("game.custom.update_rules", { "lobbyId": lobby_id, "rules": rules_override }, func(_t: String, _p: Variant) -> void: pass, func(code: String, msg: String) -> void: print("[CustomLobby] ERR update_rules ", code, " ", msg)) ## Save the current lobby's rules as a named preset. func save_preset(preset_name: String) -> void: Network.request("game.custom.preset.save", { "lobbyId": lobby_id, "presetName": preset_name }, func(_t: String, _p: Variant) -> void: pass, func(code: String, msg: String) -> void: print("[CustomLobby] ERR save_preset ", code, " ", msg)) ## List all presets owned by the local player. func list_presets(callback: Callable) -> void: Network.request("game.custom.preset.list", {}, func(_t: String, p: Variant) -> void: var d := p as Dictionary callback.call(d.get("presets", [])), func(code: String, msg: String) -> void: print("[CustomLobby] ERR list_presets ", code, " ", msg)) ## Host proposes loading a preset; triggers a yes/no vote for the guest. func propose_preset(preset_id: String) -> void: Network.request("game.custom.preset.propose", { "lobbyId": lobby_id, "presetId": preset_id }, func(_t: String, _p: Variant) -> void: pass, func(code: String, msg: String) -> void: print("[CustomLobby] ERR propose_preset ", code, " ", msg)) ## Cast a yes/no vote on the active preset proposal. func vote_preset(accept: bool) -> void: Network.request("game.custom.preset.vote", { "lobbyId": lobby_id, "accept": accept }, func(_t: String, _p: Variant) -> void: pass, func(code: String, msg: String) -> void: print("[CustomLobby] ERR vote_preset ", code, " ", msg)) # ── Private ─────────────────────────────────────────────────────────────────── func _on_server_push(type: String, payload: Variant) -> void: if type.begins_with("game.custom.lobby"): print("[CustomLobby] PUSH ", type, " ", JSON.stringify(payload).substr(0, 200)) match type: "game.custom.lobby.guest_joined": var d := payload as Dictionary var gid : String = str(d.get("guestAccountId", "")) guest_joined.emit(gid) "game.custom.lobby_updated": var d := payload as Dictionary var rules := d.get("newRules", {}) as Dictionary lobby_rules_updated.emit(rules) "game.custom.lobby.deck_changed": var d := payload as Dictionary var aid := str(d.get("accountId", "")) var did := str(d.get("deckId", "")) lobby_deck_changed.emit(aid, did) "game.custom.lobby.ready_status": var d := payload as Dictionary var hr : bool = d.get("hostReady", false) var gr : bool = d.get("guestReady", false) if is_host: i_am_ready = hr partner_ready = gr else: i_am_ready = gr partner_ready = hr ready_status_changed.emit(hr, gr) "game.custom.started": var d := payload as Dictionary var sid : String = str(d.get("sessionId", "")) var opp : String = str(d.get("opponentAccountId", "")) Game.session_id = sid Game.state = {} game_starting.emit(sid, opp) "game.custom.preset.vote_requested": preset_vote_requested.emit(payload as Dictionary) "game.custom.preset.vote_rejected": var d := payload as Dictionary var lid := str(d.get("lobbyId", "")) var pid := str(d.get("presetId", "")) preset_vote_rejected.emit(lid, pid)