TableTower/ui/spread_loader.gd
RobinFiredrake 2ed91e7acd Spreads can roll cards from other spreads: [[Spread::Card]]
A result's text can name a card in another spread, as "[[Treasure::Loot]]".
It rolls there (its own threads and references resolve in that spread)
and its changes apply to this spread's values. The inspector has
"+ Roll a card from another spread…" to pick one. Links show as
"Treasure › Loot" chips; nodes from there are marked so this spread's
cards aren't lit for them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 14:45:01 -06:00

40 lines
1.2 KiB
GDScript

class_name SpreadLoader
extends RefCounted
## Finds spreads for [[Spread::Card]] links (SpreadLinks.loader): by id or by
## name (any capitals). The open spread is used as it is in memory; others are
## read from disk and kept until their file changes.
var m: Main
## id -> [modified time, TowerBoard]
var _cache: Dictionary = {}
func _init(p_main: Main) -> void:
m = p_main
func find(key: String) -> TowerBoard:
if m.data and (key == m.data.id or RollContext.normalize_name(key) == RollContext.normalize_name(m.data.name)):
return m.data
if m.library == null:
return null
var id := key if m.library.exists(key) else _id_by_name(key)
if id == "":
return null
var stamp := FileAccess.get_modified_time(m.library.path_for(id))
var kept: Array = _cache.get(id, [])
if kept.is_empty() or int(kept[0]) != stamp:
var board := m.library.load_collection(id)
if board == null:
return null
kept = [stamp, board]
_cache[id] = kept
return kept[1]
func _id_by_name(spread_name: String) -> String:
var want := RollContext.normalize_name(spread_name)
for entry in m.library.list():
if RollContext.normalize_name(str(entry["name"])) == want:
return str(entry["id"])
return ""