2026-11-10 21:12:47 -06:00
|
|
|
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
|
2026-11-16 10:41:13 -06:00
|
|
|
if not t.outlet.pipes.is_empty():
|
|
|
|
|
d["card_threads"] = t.outlet.to_dict()["pipes"]
|
2026-11-10 21:12:47 -06:00
|
|
|
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
|
2026-11-14 14:23:07 -06:00
|
|
|
if t.cost_value.strip_edges() != "" and t.cost_amount.strip_edges() != "":
|
|
|
|
|
d["cost"] = {"value": t.cost_value.strip_edges(), "amount": t.cost_amount.strip_edges()}
|
2026-11-10 21:12:47 -06:00
|
|
|
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
|
2026-11-15 12:29:52 -06:00
|
|
|
d["ranged"] = true
|
|
|
|
|
if t.range_edit == RangeWeights.WEIGHTS:
|
|
|
|
|
d["range_edit"] = RangeWeights.WEIGHTS
|
|
|
|
|
if not t.auto_sort:
|
|
|
|
|
d["auto_sort"] = false
|
2026-11-14 14:05:09 -06:00
|
|
|
if t.has_mod():
|
|
|
|
|
d["dice_mod"] = t.dice_mod.strip_edges()
|
2026-11-10 21:12:47 -06:00
|
|
|
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
|
2026-11-11 09:06:55 -06:00
|
|
|
if t.column_join != "":
|
|
|
|
|
d["column_join"] = t.column_join
|
2026-11-10 21:12:47 -06:00
|
|
|
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
|
2026-11-29 16:52:41 -06:00
|
|
|
elif t.kind == RollTable.KIND_SHEET:
|
|
|
|
|
if t.sheet_id != "":
|
|
|
|
|
d["sheet"] = t.sheet_id
|
|
|
|
|
if t.sheet_prefix != "":
|
|
|
|
|
d["prefix"] = t.sheet_prefix
|
2026-11-10 21:12:47 -06:00
|
|
|
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()
|
2026-11-14 13:40:20 -06:00
|
|
|
if t.var_type == "list" and t.list_repeats:
|
|
|
|
|
d["repeats"] = true
|
2026-11-14 13:36:03 -06:00
|
|
|
if t.is_collection() and not t.members.is_empty():
|
|
|
|
|
d["members"] = t.members.duplicate(true)
|
2026-11-10 21:12:47 -06:00
|
|
|
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()
|
2026-11-15 12:29:52 -06:00
|
|
|
t.ranged = bool(d.get("ranged", false))
|
|
|
|
|
t.range_edit = RangeWeights.WEIGHTS if str(d.get("range_edit", "")) == RangeWeights.WEIGHTS else RangeWeights.RANGES
|
|
|
|
|
t.auto_sort = bool(d.get("auto_sort", true))
|
2026-11-10 21:12:47 -06:00
|
|
|
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", []))
|
2026-11-11 09:06:55 -06:00
|
|
|
t.column_join = str(d.get("column_join", "")) if d.get("column_join") != null else ""
|
2026-11-10 21:12:47 -06:00
|
|
|
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", ""), "")
|
2026-11-29 16:52:41 -06:00
|
|
|
t.sheet_id = str(d.get("sheet", ""))
|
|
|
|
|
t.sheet_prefix = str(d.get("prefix", ""))
|
2026-11-10 21:12:47 -06:00
|
|
|
t.steps = Scales.clean(d.get("steps", []))
|
2026-11-14 13:36:03 -06:00
|
|
|
t.members = LedgerMembers.clean(d.get("members", []))
|
2026-11-14 13:40:20 -06:00
|
|
|
t.list_repeats = bool(d.get("repeats", false))
|
2026-11-14 14:23:07 -06:00
|
|
|
var cost: Variant = d.get("cost")
|
|
|
|
|
if cost is Dictionary:
|
|
|
|
|
t.cost_value = str(cost.get("value", ""))
|
|
|
|
|
t.cost_amount = str(cost.get("amount", ""))
|
2026-11-10 21:12:47 -06:00
|
|
|
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))
|
2026-11-16 10:41:13 -06:00
|
|
|
if d.get("card_threads") is Array and not (d["card_threads"] as Array).is_empty():
|
|
|
|
|
t.outlet = TableEntry.from_dict({"id": RollTable.OUTLET_ID, "text": "", "pipes": d["card_threads"]})
|
2026-11-10 21:12:47 -06:00
|
|
|
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
|
2026-11-14 13:19:54 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## 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)
|