Files
sor/Client/origin-client/scenes/init/init_screen.gd
T
a46be8e06c Init screen: loading bar + version check hang fix
Replace the sync card count with a ProgressBar that pulses
indeterminately during version check, then fills 5→85% during
content sync, 90% during active-session check, 100% on done.

Add an 8-second timeout on version.check so a silent server-side
hang (server receives packet but never replies) auto-proceeds to
sync instead of freezing the init screen indefinitely.

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>
2026-03-23 12:14:42 -04:00

236 lines
8.1 KiB
GDScript

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
@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
# ── Lifecycle ─────────────────────────────────────────────────────────────────
func _ready() -> void:
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()
_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
ContentCache.start_sync()
# ── ContentCache signal handlers ──────────────────────────────────────────────
func _on_sync_progress(fetched_total: int) -> void:
_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)
func _on_sync_complete(_total: int) -> void:
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 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:
_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