New helpers: BoardGroups (group nesting and membership), BoardFileIO (migrate, atomic writes), RollNodes (result-node helpers), RollLive (live values and starting values) and RollTableFactory (new cards). RollPipes now holds the pipe and pause frames, RollChoice reads options_from, and RollTableIO cleans inputs. The original files keep one-line delegators. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
212 lines
7.8 KiB
GDScript
212 lines
7.8 KiB
GDScript
class_name RollTableIO
|
|
extends RefCounted
|
|
## RollTable to and from its JSON Dictionary (RollTable.to_dict/from_dict
|
|
## delegate here). Keys at their defaults are left out.
|
|
|
|
|
|
static func to_dict(t: RollTable) -> Dictionary:
|
|
var list: Array = []
|
|
for e in t.entries:
|
|
list.append(e.to_dict())
|
|
var d := {"id": t.id, "name": t.name, "x": roundi(t.position.x), "y": roundi(t.position.y), "entries": list}
|
|
if t.kind != RollTable.KIND_ROLL:
|
|
d["kind"] = t.kind
|
|
if t.collapsed:
|
|
d["collapsed"] = true
|
|
if t.details != "":
|
|
d["details"] = t.details
|
|
if not t.tags.is_empty():
|
|
d["tags"] = t.tags.duplicate()
|
|
if t.color != "":
|
|
d["color"] = t.color
|
|
if t.roll_floor.strip_edges() != "":
|
|
d["roll_floor"] = t.roll_floor.strip_edges()
|
|
if t.roll_ceiling.strip_edges() != "":
|
|
d["roll_ceiling"] = t.roll_ceiling.strip_edges()
|
|
if t.width > 0.0:
|
|
d["w"] = roundi(t.width)
|
|
if t.list_height > 0.0:
|
|
d["list_h"] = roundi(t.list_height)
|
|
if t.is_ranged():
|
|
d["dice"] = t.dice
|
|
if t.has_mod():
|
|
d["dice_mod"] = t.dice_mod.strip_edges()
|
|
if t.card_mode:
|
|
d["card_mode"] = true
|
|
if t.deck_empty != "reshuffle":
|
|
d["deck_empty"] = t.deck_empty
|
|
if t.deck_empty_text != "":
|
|
d["deck_empty_text"] = t.deck_empty_text
|
|
if not t.columns.is_empty():
|
|
d["columns"] = clean_columns(t.columns)
|
|
if t.column_rolls != RollTable.COLUMNS_ROW:
|
|
d["column_rolls"] = t.column_rolls
|
|
if t.column_join != "":
|
|
d["column_join"] = t.column_join
|
|
if t.kind == RollTable.KIND_CHOICE:
|
|
if t.choice != "pick":
|
|
d["choice"] = t.choice
|
|
if t.prompt != "":
|
|
d["prompt"] = t.prompt
|
|
if t.save_as != "":
|
|
d["save_as"] = t.save_as
|
|
if t.options_from.strip_edges() != "":
|
|
d["options_from"] = t.options_from.strip_edges()
|
|
if t.options_where.strip_edges() != "":
|
|
d["options_where"] = t.options_where.strip_edges()
|
|
if t.min_expr != "0":
|
|
d["min"] = t.min_expr
|
|
if t.max_expr != "99":
|
|
d["max"] = t.max_expr
|
|
elif t.kind == RollTable.KIND_LOOP:
|
|
if t.loop_mode != "times":
|
|
d["loop"] = t.loop_mode
|
|
if t.loop_expr != "":
|
|
d["loop_expr"] = t.loop_expr
|
|
elif t.kind == RollTable.KIND_VARIABLE:
|
|
if t.var_name != "":
|
|
d["var_name"] = t.var_name
|
|
if t.var_type != "number":
|
|
d["var_type"] = t.var_type
|
|
if t.initial != "":
|
|
d["initial"] = t.initial
|
|
if t.var_type == RollTable.VAR_SCALE and not t.steps.is_empty():
|
|
d["steps"] = t.steps.duplicate()
|
|
elif t.kind == RollTable.KIND_CHECK:
|
|
if t.check_dice != RollTable.CHECK_DEFAULT_DICE:
|
|
d["check_dice"] = t.check_dice
|
|
if t.check_mod != "":
|
|
d["check_mod"] = t.check_mod
|
|
if t.check_cmp != "ge":
|
|
d["check_cmp"] = t.check_cmp
|
|
if t.check_target != RollTable.CHECK_DEFAULT_TARGET:
|
|
d["check_target"] = t.check_target
|
|
if not t.inputs.is_empty():
|
|
var list2: Array = []
|
|
for inp: Dictionary in t.inputs:
|
|
var o := {"name": str(inp.get("name", ""))}
|
|
if str(inp.get("label", "")) != "":
|
|
o["label"] = str(inp["label"])
|
|
if str(inp.get("default", "")) != "":
|
|
o["default"] = str(inp["default"])
|
|
list2.append(o)
|
|
d["inputs"] = list2
|
|
return d
|
|
|
|
|
|
static func from_dict(d: Dictionary) -> RollTable:
|
|
var t := RollTable.new(str(d.get("id", "")), str(d.get("name", "")))
|
|
t.position = Vector2(float(d.get("x", 0)), float(d.get("y", 0)))
|
|
t.collapsed = bool(d.get("collapsed", false))
|
|
t.details = str(d.get("details", ""))
|
|
for tag: Variant in d.get("tags", []):
|
|
t.tags.append(str(tag))
|
|
t.color = str(d.get("color", ""))
|
|
t.roll_floor = str(d.get("roll_floor", ""))
|
|
t.roll_ceiling = str(d.get("roll_ceiling", ""))
|
|
t.width = maxf(0.0, float(d.get("w", 0)))
|
|
t.list_height = maxf(0.0, float(d.get("list_h", 0)))
|
|
if d.has("dice"):
|
|
t.dice = str(d["dice"])
|
|
elif int(d.get("die", 0)) > 1:
|
|
t.dice = "1d%d" % int(d["die"])
|
|
t.dice_mod = RollTable.expr_text(d.get("dice_mod", ""), "").strip_edges()
|
|
t.card_mode = bool(d.get("card_mode", false))
|
|
t.deck_empty = "stop" if str(d.get("deck_empty", "reshuffle")) == "stop" else "reshuffle"
|
|
t.deck_empty_text = str(d.get("deck_empty_text", ""))
|
|
t.columns = clean_columns(d.get("columns", []))
|
|
t.column_join = str(d.get("column_join", "")) if d.get("column_join") != null else ""
|
|
t.column_rolls = RollTable.COLUMNS_EACH if str(d.get("column_rolls", "")) == RollTable.COLUMNS_EACH else RollTable.COLUMNS_ROW
|
|
var k := str(d.get("kind", RollTable.KIND_ROLL))
|
|
t.kind = k if RollTable.KINDS.has(k) else RollTable.KIND_ROLL
|
|
var c := str(d.get("choice", "pick"))
|
|
t.choice = c if RollTable.CHOICES.has(c) else "pick"
|
|
t.prompt = str(d.get("prompt", ""))
|
|
t.save_as = str(d.get("save_as", ""))
|
|
t.options_from = str(d.get("options_from", "")).strip_edges() if d.get("options_from") != null else ""
|
|
t.options_where = RollTable.expr_text(d.get("options_where", ""), "").strip_edges()
|
|
t.min_expr = RollTable.expr_text(d.get("min", "0"), "0")
|
|
t.max_expr = RollTable.expr_text(d.get("max", "99"), "99")
|
|
var lm := str(d.get("loop", "times"))
|
|
t.loop_mode = lm if RollTable.LOOPS.has(lm) else "times"
|
|
t.loop_expr = RollTable.expr_text(d.get("loop_expr", ""), "")
|
|
t.var_name = str(d.get("var_name", "")).strip_edges().trim_prefix("@")
|
|
var vt := str(d.get("var_type", "number"))
|
|
t.var_type = vt if RollTable.VAR_TYPES.has(vt) else "number"
|
|
t.initial = RollTable.expr_text(d.get("initial", ""), "")
|
|
t.steps = Scales.clean(d.get("steps", []))
|
|
t.check_dice = RollTable.expr_text(d.get("check_dice", RollTable.CHECK_DEFAULT_DICE), RollTable.CHECK_DEFAULT_DICE).strip_edges()
|
|
if t.check_dice == "":
|
|
t.check_dice = RollTable.CHECK_DEFAULT_DICE
|
|
t.check_mod = RollTable.expr_text(d.get("check_mod", ""), "").strip_edges()
|
|
var cc := str(d.get("check_cmp", "ge"))
|
|
t.check_cmp = cc if RollTable.CHECK_CMPS.has(cc) else "ge"
|
|
t.check_target = RollTable.expr_text(d.get("check_target", RollTable.CHECK_DEFAULT_TARGET), RollTable.CHECK_DEFAULT_TARGET).strip_edges()
|
|
if t.check_target == "":
|
|
t.check_target = RollTable.CHECK_DEFAULT_TARGET
|
|
t.inputs = RollTable.clean_inputs(d.get("inputs", []))
|
|
for ed: Variant in d.get("entries", []):
|
|
if ed is Dictionary:
|
|
t.entries.append(TableEntry.from_dict(ed))
|
|
return t
|
|
|
|
|
|
## Cleans a "columns" value from JSON or the UI: Array of {name, save_as?,
|
|
## when?} with blank names dropped and empty keys left out.
|
|
static func clean_columns(v: Variant) -> Array:
|
|
var out: Array = []
|
|
if not (v is Array):
|
|
return out
|
|
for item: Variant in v:
|
|
var nm := ""
|
|
var save := ""
|
|
var when := ""
|
|
if item is Dictionary:
|
|
nm = str(item.get("name", "")) if item.get("name") != null else ""
|
|
save = str(item.get("save_as", "")) if item.get("save_as") != null else ""
|
|
when = str(item.get("when", "")) if item.get("when") != null else ""
|
|
elif item is String:
|
|
nm = item
|
|
nm = nm.strip_edges()
|
|
if nm == "":
|
|
continue
|
|
var o := {"name": nm}
|
|
if save.strip_edges() != "":
|
|
o["save_as"] = save.strip_edges()
|
|
if when.strip_edges() != "":
|
|
o["when"] = when.strip_edges()
|
|
out.append(o)
|
|
return out
|
|
|
|
|
|
## Cleans an "inputs" value from JSON or the UI: Array of {name, label,
|
|
## default} with names trimmed (a leading "@" dropped) and empty names left
|
|
## out. Numbers become expression text.
|
|
static func clean_inputs(value: Variant) -> Array:
|
|
var out: Array = []
|
|
if not (value is Array):
|
|
return out
|
|
for item: Variant in value:
|
|
var input_name := ""
|
|
var label := ""
|
|
var default_text := ""
|
|
if item is Dictionary:
|
|
input_name = str(item.get("name", "")) if item.get("name") != null else ""
|
|
label = str(item.get("label", "")) if item.get("label") != null else ""
|
|
default_text = expr_text(item.get("default", ""), "")
|
|
elif item is String:
|
|
input_name = item
|
|
input_name = input_name.strip_edges().trim_prefix("@")
|
|
if input_name == "":
|
|
continue
|
|
out.append({"name": input_name, "label": label.strip_edges(), "default": default_text.strip_edges()})
|
|
return out
|
|
|
|
|
|
## Expression fields may arrive as JSON numbers: 3.0 -> "3".
|
|
static func expr_text(value: Variant, fallback: String) -> String:
|
|
if value == null:
|
|
return fallback
|
|
if value is float or value is int:
|
|
return Expr.format_value(value)
|
|
return str(value)
|