extends Control ## Post-auth initialization screen. ## ## Runs on every launch after a successful authentication: ## 1. version.check — block on UpdateRequired; soft banner on UpdateAvailable ## 2. content.sync — incremental definition sync via ContentCache ## 3. Transition — change scene to MainScreen ## ## If the network is not yet up when this screen opens, we wait for the ## Network.connected signal before starting. const CLIENT_VERSION := "0.1.0" const PLATFORM := "web" # TODO: detect at runtime for native builds. @onready var status_label : Label = %StatusLabel @onready var update_panel : Control = %UpdatePanel @onready var update_msg : Label = %UpdateMsg @onready var retry_btn : Button = %RetryBtn # ── Lifecycle ───────────────────────────────────────────────────────────────── func _ready() -> void: update_panel.visible = false retry_btn.visible = false ContentCache.sync_progress.connect(_on_sync_progress) ContentCache.sync_complete.connect(_on_sync_complete) ContentCache.sync_failed.connect(_on_sync_failed) retry_btn.pressed.connect(_start) if Network.is_online(): _start() else: _set_status("Connecting to server…") Network.connected.connect(_start, CONNECT_ONE_SHOT) func _exit_tree() -> void: if ContentCache.sync_progress.is_connected(_on_sync_progress): ContentCache.sync_progress.disconnect(_on_sync_progress) if ContentCache.sync_complete.is_connected(_on_sync_complete): ContentCache.sync_complete.disconnect(_on_sync_complete) if ContentCache.sync_failed.is_connected(_on_sync_failed): ContentCache.sync_failed.disconnect(_on_sync_failed) # ── Initialization sequence ─────────────────────────────────────────────────── func _start() -> void: update_panel.visible = false retry_btn.visible = false _set_status("Checking for updates…") Network.request( "version.check", { "clientVersion": CLIENT_VERSION, "platform": PLATFORM }, func(_type: String, p: Variant): _on_version_ok(p), func(code: String, _msg: String): _on_version_error(code) ) func _on_version_ok(payload: Variant) -> void: var p := payload as Dictionary var status := p.get("status", "Ok") as String match status: "UpdateRequired": var url : String = p.get("downloadUrl", "") as String var notes: String = p.get("releaseNotes", "") as String update_panel.visible = true if url.is_empty(): update_msg.text = ( "A required update is available.\n" + "Please refresh your browser to continue." ) else: update_msg.text = ( "A required update is available.\n%s\n\nRefresh your browser or visit:\n%s" % [notes, url] ) # Block here — do not proceed to sync. "UpdateAvailable": # Non-blocking soft notice, then continue. _set_status("Update available (not required) — starting…") _do_sync() _: # "Ok" or anything unrecognised _do_sync() func _on_version_error(code: String) -> void: if code == "DISCONNECTED": # Lost connection before we got a reply — wait for reconnect and retry. _set_status("Connection lost. Waiting to reconnect…") Network.connected.connect(_start, CONNECT_ONE_SHOT) return # Any other error (server-side, unknown type, etc.): don't block the player. push_warning("InitScreen: version check failed (%s) — proceeding anyway" % code) _do_sync() func _do_sync() -> void: var cached := ContentCache.definition_count() if cached > 0: _set_status("Syncing card database… (%d cached)" % cached) else: _set_status("Downloading card database…") ContentCache.start_sync() # ── ContentCache signal handlers ────────────────────────────────────────────── func _on_sync_progress(fetched_total: int) -> void: _set_status("Syncing card database… (%d cards)" % fetched_total) func _on_sync_complete(_total: int) -> void: _check_active_session() func _check_active_session() -> void: _set_status("Checking for active games…") Network.request( "game.get_active_session", {}, func(_type: String, p: Variant): _on_active_session(p), func(_code: String, _msg: String): _go_to_main() ) func _on_active_session(payload: Variant) -> void: var p := payload as Dictionary var sid : String = str(p.get("sessionId", "")) # A null or zero UUID means no active session. if sid.is_empty() or sid == "00000000-0000-0000-0000-000000000000": _go_to_main() return var phase : String = str(p.get("phase", "unknown")) var dialog := ConfirmationDialog.new() dialog.title = "Active Game Found" dialog.dialog_text = "You have an active game in progress (%s phase).\nWould you like to rejoin?" % phase dialog.ok_button_text = "Rejoin" dialog.cancel_button_text = "Concede" add_child(dialog) dialog.confirmed.connect(func() -> void: Game.session_id = sid get_tree().change_scene_to_file("res://scenes/game/GameScreen.tscn") ) dialog.canceled.connect(func() -> void: _set_status("Conceding game…") Network.request( "game.concede", { "sessionId": sid }, func(_t: String, _rp: Variant): _go_to_main(), func(_c: String, _m: String): _go_to_main() ) ) dialog.popup_centered() func _go_to_main() -> void: get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn") func _on_sync_failed(error: String) -> void: _set_status("Sync failed: %s" % error) retry_btn.visible = true # ── Helpers ─────────────────────────────────────────────────────────────────── func _set_status(text: String) -> void: status_label.text = text