Files
sor/Client/origin-client/autoloads/ContentCache.gd
T
6a33e26237 Godot client: Collection screen — cards grid, deck management, card widget
- CardWidget (146×204px): affinity-coloured border + art placeholder,
  stats with tooltips, foil/serial indicators, selection highlight
- DeckListItem: inline rename, clone/delete signals to parent
- CardsView: paginated + filterable grid (search debounce, type/affinity
  filters, responsive columns, Load More pagination)
- DecksView: deck list with New Deck form (ArchSpirit card picker),
  clone/rename/delete wired to API, ConfirmationDialog for delete
- CollectionScreen: pill-tab container (Cards / Decks), background
  incremental content sync, back button → MainScreen
- MainScreen: wire CollectionBtn → CollectionScreen
- ContentCache: expose is_syncing() public method

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-02-28 07:48:07 -05:00

146 lines
4.9 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()
## True while a sync is in progress.
func is_syncing() -> bool:
return _is_syncing
## 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()