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. ## Seconds before we give up waiting for version.check and proceed to sync. const VERSION_CHECK_TIMEOUT_SEC := 8.0 ## Seconds of no sync progress before we give up and show the retry button. const SYNC_TIMEOUT_SEC := 20.0 @onready var status_label : Label = %StatusLabel @onready var progress_bar : ProgressBar = %ProgressBar @onready var update_panel : Control = %UpdatePanel @onready var update_msg : Label = %UpdateMsg @onready var retry_btn : Button = %RetryBtn # ── State ───────────────────────────────────────────────────────────────────── ## Incremented each time _start() runs so stale timeout callbacks are ignored. var _version_gen : int = 0 ## Upper bound on expected total definitions; tracks running max across pages. var _expected_total : int = 1 ## Tween used for the indeterminate pulse while checking for updates. var _pulse_tween : Tween = null ## SceneTree timer that fires if the sync hangs with no progress. var _sync_timeout : SceneTreeTimer = null ## True only while we are actively waiting for sync signals from ContentCache. ## Cleared by _on_sync_failed so that a late sync_complete/sync_progress from a ## timed-out run cannot navigate the player or restart the watchdog. var _sync_accepting : bool = false # ── Lifecycle ───────────────────────────────────────────────────────────────── func _ready() -> void: ContentCache.apply_scene_background(self) update_panel.visible = false retry_btn.visible = false progress_bar.value = 0.0 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 _version_gen += 1 var gen := _version_gen _set_status("Checking for updates…") _start_pulse() 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) ) # Fallback: if the server receives the request but never replies, don't hang. get_tree().create_timer(VERSION_CHECK_TIMEOUT_SEC).timeout.connect( func() -> void: if _version_gen != gen or not is_inside_tree(): return push_warning("InitScreen: version.check timed out — proceeding to sync") _version_gen += 1 # invalidate further timeout callbacks from this gen _do_sync(), CONNECT_ONE_SHOT) func _on_version_ok(payload: Variant) -> void: _version_gen += 1 # disarm timeout var p := payload as Dictionary var status := p.get("status", "Ok") as String match status: "UpdateRequired": _stop_pulse() 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: _version_gen += 1 # disarm timeout if code == "DISCONNECTED": # Lost connection before we got a reply — wait for reconnect and retry. _stop_pulse() _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: _stop_pulse() _sync_accepting = true _expected_total = max(ContentCache.definition_count(), 1) var cached := ContentCache.definition_count() if cached > 0: _set_status("Syncing card database…") else: _set_status("Downloading card database…") progress_bar.value = 5.0 _start_sync_timeout() ContentCache.start_sync() BoardTemplateCache.fetch() # parallel fire-and-forget; ready before lobby screen opens # ── ContentCache signal handlers ────────────────────────────────────────────── func _on_sync_progress(fetched_total: int) -> void: if not _sync_accepting: return _expected_total = max(_expected_total, fetched_total) var pct := 5.0 + 80.0 * float(fetched_total) / float(_expected_total) progress_bar.value = clampf(pct, 5.0, 85.0) _start_sync_timeout() # reset on each page received so we time out from last activity func _on_sync_complete(_total: int) -> void: if not _sync_accepting: return _sync_accepting = false _sync_timeout = null progress_bar.value = 90.0 _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_raw = p.get("sessionId") # may be null when server has no active session # Treat null or zero UUID as "no active session". if sid_raw == null: _go_to_main() return var sid : String = str(sid_raw) if sid.is_empty() or sid == "00000000-0000-0000-0000-000000000000": _go_to_main() return var can_rejoin_raw = p.get("canRejoinAsPlayer") if can_rejoin_raw != null and not bool(can_rejoin_raw): # Player is already eliminated/conceded in the active match. # Don't prompt to rejoin as a player; they can spectate manually from Active Games. _go_to_main() return var phase_raw = p.get("phase") var phase : String = str(phase_raw) if phase_raw != null else "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: progress_bar.value = 100.0 get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn") func _on_sync_failed(error: String) -> void: _sync_accepting = false _sync_timeout = null _stop_pulse() _set_status("Sync failed: %s" % error) retry_btn.visible = true # ── Helpers ─────────────────────────────────────────────────────────────────── func _set_status(text: String) -> void: status_label.text = text ## Pulse the progress bar back and forth to signal indeterminate activity. func _start_pulse() -> void: _stop_pulse() progress_bar.value = 0.0 _pulse_tween = create_tween().set_loops() _pulse_tween.tween_property(progress_bar, "value", 12.0, 0.8) \ .set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_SINE) _pulse_tween.tween_property(progress_bar, "value", 0.0, 0.8) \ .set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_SINE) func _stop_pulse() -> void: if _pulse_tween: _pulse_tween.kill() _pulse_tween = null ## Start (or restart) the sync watchdog. If no progress arrives within ## SYNC_TIMEOUT_SEC, surface the retry button so the user isn't stranded. func _start_sync_timeout() -> void: var t := get_tree().create_timer(SYNC_TIMEOUT_SEC) _sync_timeout = t t.timeout.connect(func() -> void: if not is_inside_tree() or _sync_timeout != t: return # stale timer — a newer one has already replaced us _sync_timeout = null push_warning("InitScreen: sync timed out — showing retry") _on_sync_failed("Timed out — tap Retry to try again."), CONNECT_ONE_SHOT)