Client flow after auth is now:
LoginScreen → auth_success → InitScreen → MainScreen
autoloads/ContentCache.gd
- Cursor-based incremental sync via content.sync (PageSize=200)
- Merges definition DTOs in-memory (id → Dictionary)
- Persists to user://content/defs.json on sync completion
- Incremental on repeat launches: 0 round trips when nothing changed
- Signals: sync_progress(fetched_total), sync_complete(total), sync_failed(error)
scenes/init/InitScreen.tscn + init_screen.gd
- Step 1: version.check (clientVersion="0.1.0", platform="web")
UpdateRequired → show "please refresh" panel, block
UpdateAvailable → soft banner, continue
Ok → continue
DISCONNECTED → wait for Network.connected (one-shot), retry
other error → warn + proceed (don't block player on non-critical check)
- Step 2: ContentCache.start_sync()
Shows running card count while pages arrive
On sync_complete: transition to MainScreen
On sync_failed: show error + Retry button
scenes/login/login_screen.gd
- _go_to_main() now routes to InitScreen instead of MainScreen directly
project.godot
- ContentCache registered as autoload (loads before Auth)
- pointing/emulate_touch_from_mouse=true for desktop browser testing
Caching architecture decision (see plan):
- Layer 1 (definitions/text): always sync all eagerly — no privacy concern
- Layer 2 (2D art): lazy by definition ID, CDN fetch on demand
- Layer 3 (3D models/animations): set-bundle granularity + server-injected
noise during match pre-warm to prevent cache oracle attack
(Layer 2/3 implementation is future work)
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>
141 lines
4.8 KiB
GDScript
141 lines
4.8 KiB
GDScript
extends Node
|
|
## Local card-definition cache.
|
|
##
|
|
## Syncs all published card definitions from the server using the
|
|
## content.sync message (cursor-based, incremental). Definitions are text
|
|
## only (stats, CDN URLs for art/models) — no binary assets are stored here.
|
|
##
|
|
## Storage layout:
|
|
## user://content/defs.json — { "version": int, "defs": { id: dto } }
|
|
##
|
|
## Sync is incremental: on the first launch SinceVersion=0 fetches everything;
|
|
## on subsequent launches only cards changed since the stored cursor are fetched
|
|
## (usually 0 pages if nothing changed — instant).
|
|
##
|
|
## Usage:
|
|
## ContentCache.start_sync()
|
|
## ContentCache.get_definition("some-guid") → Dictionary or {}
|
|
## ContentCache.sync_complete.connect(...)
|
|
|
|
const CACHE_DIR := "user://content"
|
|
const CACHE_PATH := "user://content/defs.json"
|
|
const PAGE_SIZE := 200
|
|
|
|
signal sync_progress(fetched_total: int) ## Running count of total definitions in memory.
|
|
signal sync_complete(total: int) ## Emitted once sync finishes.
|
|
signal sync_failed(error: String) ## Emitted if sync cannot complete.
|
|
|
|
var _defs: Dictionary = {} ## definitionId (String) → definition Dictionary
|
|
var _sync_version: int = 0 ## Stored DefinitionVersion cursor.
|
|
var _is_syncing: bool = false
|
|
|
|
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
|
|
|
func _ready() -> void:
|
|
_load_from_disk()
|
|
|
|
# ── Public API ────────────────────────────────────────────────────────────────
|
|
|
|
## Return the cached definition dictionary, or {} if not found.
|
|
func get_definition(id: String) -> Dictionary:
|
|
return _defs.get(id, {})
|
|
|
|
|
|
## True if the definition is cached.
|
|
func has_definition(id: String) -> bool:
|
|
return _defs.has(id)
|
|
|
|
|
|
## Returns all cached definition IDs.
|
|
func get_all_definition_ids() -> Array:
|
|
return _defs.keys()
|
|
|
|
|
|
## The current sync-version cursor (0 = never synced).
|
|
func get_sync_version() -> int:
|
|
return _sync_version
|
|
|
|
|
|
## Total number of definitions currently in memory.
|
|
func definition_count() -> int:
|
|
return _defs.size()
|
|
|
|
|
|
## Start (or resume) the incremental sync.
|
|
## Emits sync_progress during, sync_complete or sync_failed when done.
|
|
func start_sync() -> void:
|
|
if _is_syncing:
|
|
return
|
|
_is_syncing = true
|
|
_fetch_page(_sync_version)
|
|
|
|
# ── Sync loop ─────────────────────────────────────────────────────────────────
|
|
|
|
func _fetch_page(since_version: int) -> void:
|
|
Network.request(
|
|
"content.sync",
|
|
{ "sinceVersion": since_version, "pageSize": PAGE_SIZE },
|
|
func(_type: String, p: Variant): _on_page(p),
|
|
func(code: String, msg: String): _on_error(code, msg)
|
|
)
|
|
|
|
|
|
func _on_page(payload: Variant) -> void:
|
|
var p := payload as Dictionary
|
|
var cards : Array = p.get("cards", [])
|
|
var next_ver : int = p.get("currentVersion", _sync_version)
|
|
var has_more : bool = p.get("hasMore", false)
|
|
|
|
for card in cards:
|
|
var id: String = str(card.get("id", ""))
|
|
if not id.is_empty():
|
|
_defs[id] = card
|
|
|
|
_sync_version = next_ver
|
|
sync_progress.emit(_defs.size())
|
|
|
|
if has_more:
|
|
_fetch_page(next_ver)
|
|
else:
|
|
_is_syncing = false
|
|
_save_to_disk()
|
|
sync_complete.emit(_defs.size())
|
|
|
|
|
|
func _on_error(code: String, msg: String) -> void:
|
|
_is_syncing = false
|
|
var detail := msg if not msg.is_empty() else code
|
|
push_warning("ContentCache: sync failed — %s" % detail)
|
|
sync_failed.emit(detail)
|
|
|
|
# ── Persistence ───────────────────────────────────────────────────────────────
|
|
|
|
func _load_from_disk() -> void:
|
|
if not FileAccess.file_exists(CACHE_PATH):
|
|
return
|
|
var file := FileAccess.open(CACHE_PATH, FileAccess.READ)
|
|
if not file:
|
|
push_warning("ContentCache: could not open cache file")
|
|
return
|
|
var text := file.get_as_text()
|
|
file.close()
|
|
var parsed: Variant = JSON.parse_string(text)
|
|
if typeof(parsed) != TYPE_DICTIONARY:
|
|
push_warning("ContentCache: cache file is malformed — starting fresh")
|
|
return
|
|
var root := parsed as Dictionary
|
|
_sync_version = int(root.get("version", 0))
|
|
var defs: Variant = root.get("defs", {})
|
|
if typeof(defs) == TYPE_DICTIONARY:
|
|
_defs = defs as Dictionary
|
|
|
|
|
|
func _save_to_disk() -> void:
|
|
DirAccess.make_dir_recursive_absolute(CACHE_DIR)
|
|
var file := FileAccess.open(CACHE_PATH, FileAccess.WRITE)
|
|
if not file:
|
|
push_warning("ContentCache: could not write cache file")
|
|
return
|
|
file.store_string(JSON.stringify({ "version": _sync_version, "defs": _defs }))
|
|
file.close()
|