TableTower/core/ledger_members.gd
RobinFiredrake edb49fce27 Ledger entries are saved on the card and editable on the spread
The entries were only saved values of the spread, so copies, exports and
imports lost them. Now a Ledger card lists its entries (name and starting
value) in the spread file; current values stay saved values. Build mode
edits them on the card too. Older spreads take in the entries made on the
card when they open.

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

115 lines
4.5 KiB
GDScript

class_name LedgerMembers
extends RefCounted
## A Ledger card's entries (in code: a collection variable card's `members`).
## The card lists each entry's name and starting value, so they are saved in
## the spread file and travel with copies, exports and imports. The entry's
## current value is a saved value of the spread, @<stem>_<name>, like any
## other value: these functions keep the two in step.
##
## A member is {"name": "climbing", "start": "0"}: the name is a slug (no
## spaces) and the start is text (a number or words).
## Cleans a "members" value from JSON or the UI: blank and repeated names are
## dropped, names become slugs, starts become text.
static func clean(value: Variant) -> Array:
var out: Array = []
if not (value is Array):
return out
var seen := {}
for item: Variant in value:
if not (item is Dictionary):
continue
var member_name := JsonIO.slug(str(item.get("name", "")), "")
if member_name == "" or seen.has(member_name):
continue
seen[member_name] = true
var start: Variant = item.get("start", "0")
out.append({"name": member_name, "start": "" if start == null else Expr.format_value(start)})
return out
## The saved-value name of a card's member: "skill_climbing".
static func value_name(card: RollTable, member_name: String) -> String:
return "%s_%s" % [card.var_name, member_name]
## Index of the member called `member_name` on `card`, or -1.
static func index_of(card: RollTable, member_name: String) -> int:
for i in card.members.size():
if str(card.members[i]["name"]) == member_name:
return i
return -1
## The starting value of a member, as a number when it looks like one.
static func start_value(start: String) -> Variant:
return Expr.to_value(start) if Expr.is_number(start) else start
## The Ledger card that lists the saved value `name` ("skill_climbing" or
## "@skill_climbing") and the entry's starting value: {"card", "start"}, or
## {} when no Ledger card lists it.
static func find(board: TowerBoard, name: String) -> Dictionary:
if board == null:
return {}
var bare := name.strip_edges().trim_prefix("@")
for card in board.tables:
if not card.is_collection() or card.var_name == "":
continue
for member: Dictionary in card.members:
if value_name(card, str(member["name"])) == bare:
return {"card": card, "start": start_value(str(member["start"]))}
return {}
## Older spreads kept a Ledger's entries only as saved values. A Ledger card
## with no members takes in the saved values that were made on it (source =
## the card), with their current value as the start. Values that rolls added
## (another source) are left as they are. Returns true when a card changed.
static func adopt(board: TowerBoard, persistent: Dictionary) -> bool:
if board == null:
return false
var changed := false
for card in board.tables:
if not card.is_collection() or card.var_name == "" or not card.members.is_empty():
continue
var prefix := card.var_name + "_"
var names: Array = persistent.keys()
names.sort_custom(func(left: String, right: String) -> bool: return left.naturalnocasecmp_to(right) < 0)
for saved_name: String in names:
var saved: Variant = persistent[saved_name]
if not saved_name.begins_with(prefix) or saved_name.length() <= prefix.length():
continue
if not (saved is Dictionary) or str(saved.get("source", "")) != card.id:
continue
card.members.append({"name": saved_name.substr(prefix.length()), "start": Expr.format_value(saved.get("value"))})
changed = true
return changed
## Brings the saved values in line with the Ledger cards: a member with no
## saved value gets its start, and a value made on the card (source = the
## card) whose member is gone is removed. Returns true when anything changed.
static func sync(board: TowerBoard, persistent: Dictionary) -> bool:
if board == null:
return false
var changed := false
for card in board.tables:
if not card.is_collection() or card.var_name == "":
continue
var listed := {}
for member: Dictionary in card.members:
var saved_name := value_name(card, str(member["name"]))
listed[saved_name] = true
if not persistent.has(saved_name):
persistent[saved_name] = {"value": start_value(str(member["start"])), "source": card.id}
changed = true
if card.members.is_empty():
continue
for saved_name: String in persistent.keys():
var saved: Variant = persistent[saved_name]
if saved_name.begins_with(card.var_name + "_") and not listed.has(saved_name) and saved is Dictionary and str(saved.get("source", "")) == card.id:
persistent.erase(saved_name)
changed = true
return changed