Size audit 2: split table_entry and describe
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f47f659ff4
commit
b604553364
8 changed files with 330 additions and 248 deletions
127
core/describe.gd
127
core/describe.gd
|
|
@ -14,10 +14,6 @@ extends RefCounted
|
|||
## Longest phrase before falling back to the original text (or a cut).
|
||||
const MAX_LEN := 90
|
||||
const MINUS := "−"
|
||||
const _CHAIN_LABELS := {"answer": "Answer", "i": "Pass", "nat": "Natural roll", "total": "Total",
|
||||
"effect": "Effect", "option": "Chosen option", "option_label": "Chosen option",
|
||||
"option_value": "Chosen value"}
|
||||
const _OPS := {"*": "×", "/": "÷", "-": MINUS, "==": "=", "!=": "≠", ">=": "≥", "<=": "≤"}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- labels
|
||||
|
|
@ -54,65 +50,13 @@ static func label(name: String, board: TowerBoard) -> String:
|
|||
return _label(name, _labels(board))
|
||||
|
||||
|
||||
## Label table for a board: {"vars": {name: label}, "stems": [[stem, label]]
|
||||
## longest first, "inputs": {name: label}, "scales": {name: steps}}.
|
||||
## Label table for a board (see DescribeLabels.build).
|
||||
static func _labels(board: TowerBoard) -> Dictionary:
|
||||
var vars := {}
|
||||
var scales := {}
|
||||
var stems: Array = []
|
||||
var inputs := {}
|
||||
if board != null:
|
||||
for t in board.tables:
|
||||
if t.kind == RollTable.KIND_VARIABLE and t.var_name != "":
|
||||
var lab := Formatter.strip_tags(t.name).strip_edges()
|
||||
if t.var_type == RollTable.VAR_COLLECTION:
|
||||
stems.append([t.var_name, lab if lab != "" else RollContext.pretty_name(t.var_name)])
|
||||
elif lab != "" and not vars.has(t.var_name):
|
||||
vars[t.var_name] = lab
|
||||
if t.is_scale() and not scales.has(t.var_name):
|
||||
scales[t.var_name] = t.steps
|
||||
for inp: Dictionary in t.inputs:
|
||||
var il := str(inp.get("label", "")).strip_edges()
|
||||
if il != "" and not inputs.has(str(inp.get("name", ""))):
|
||||
inputs[str(inp.get("name", ""))] = il
|
||||
stems.sort_custom(func(a: Array, b: Array) -> bool: return str(a[0]).length() > str(b[0]).length())
|
||||
return {"vars": vars, "stems": stems, "inputs": inputs, "scales": scales}
|
||||
return DescribeLabels.build(board)
|
||||
|
||||
|
||||
static func _label(name: String, L: Dictionary) -> String:
|
||||
var n := name.strip_edges()
|
||||
if n.begins_with("@["):
|
||||
return "Chosen value"
|
||||
if n.begins_with("@"):
|
||||
n = n.substr(1).to_lower()
|
||||
if n.ends_with("*"):
|
||||
var stem := n.trim_suffix("*").trim_suffix("_")
|
||||
for sp: Array in L["stems"]:
|
||||
if sp[0] == stem:
|
||||
return sp[1]
|
||||
return "%s values" % RollContext.pretty_name(stem)
|
||||
if L["vars"].has(n):
|
||||
return L["vars"][n]
|
||||
for sp: Array in L["stems"]:
|
||||
var pre := str(sp[0]) + "_"
|
||||
if n.begins_with(pre) and n.length() > pre.length():
|
||||
return RollContext.pretty_name(n.substr(pre.length()))
|
||||
return RollContext.pretty_name(n)
|
||||
if L["inputs"].has(n):
|
||||
return L["inputs"][n]
|
||||
if _CHAIN_LABELS.has(n):
|
||||
return _CHAIN_LABELS[n]
|
||||
return RollContext.pretty_name(n)
|
||||
|
||||
|
||||
## The collection a value belongs to ({"stem", "label"}) or {}.
|
||||
static func _collection_of(name: String, L: Dictionary) -> Dictionary:
|
||||
var n := name.strip_edges().trim_prefix("@").to_lower()
|
||||
for sp: Array in L["stems"]:
|
||||
var pre := str(sp[0]) + "_"
|
||||
if n.begins_with(pre):
|
||||
return {"stem": sp[0], "label": sp[1]}
|
||||
return {}
|
||||
return DescribeLabels.lookup(name, L)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- effects
|
||||
|
|
@ -307,70 +251,7 @@ static func dice(expr: String, board: TowerBoard) -> String:
|
|||
|
||||
|
||||
static func _expr(expr: String, L: Dictionary) -> String:
|
||||
var src := expr.strip_edges()
|
||||
if src == "":
|
||||
return ""
|
||||
var toks := Builder.tokens(src)
|
||||
if toks.is_empty():
|
||||
return src
|
||||
var pieces: Array = [] # [kind, text]; kinds: val fn op neg ( ) ,
|
||||
var k := 0
|
||||
while k < toks.size():
|
||||
var tk: Dictionary = toks[k]
|
||||
var t := str(tk["t"])
|
||||
var s := str(tk["s"])
|
||||
var prevk := str(pieces.back()[0]) if not pieces.is_empty() else ""
|
||||
# dm(x) with one value inside: "X modifier".
|
||||
if t == "name" and s == "dm" and k + 3 < toks.size() and toks[k + 1]["s"] == "(" and toks[k + 3]["s"] == ")" \
|
||||
and ["var", "name", "num"].has(str(toks[k + 2]["t"])):
|
||||
pieces.append(["val", _value(str(toks[k + 2]["s"]), L) + " modifier"])
|
||||
k += 4
|
||||
continue
|
||||
k += 1
|
||||
match t:
|
||||
"dice":
|
||||
var dp := Builder.dice_parts(s)
|
||||
if int(dp[0]) == 1:
|
||||
pieces.append(["val", "d%d" % int(dp[1])])
|
||||
else:
|
||||
pieces.append(["val", "%dd%d" % [int(dp[0]), int(dp[1])]])
|
||||
"var", "prefix":
|
||||
pieces.append(["val", _label(s, L)])
|
||||
"name":
|
||||
if k < toks.size() and toks[k]["s"] == "(":
|
||||
pieces.append(["fn", s])
|
||||
else:
|
||||
pieces.append(["val", _value(s, L)])
|
||||
"num":
|
||||
pieces.append(["val", s])
|
||||
"str":
|
||||
pieces.append(["val", Builder.unquote(s)])
|
||||
"kw":
|
||||
pieces.append(["op", s])
|
||||
_:
|
||||
if s == "(" or s == ")" or s == ",":
|
||||
pieces.append([s, s])
|
||||
elif s == "-" and (prevk == "" or prevk == "op" or prevk == "(" or prevk == "," or prevk == "neg"):
|
||||
pieces.append(["neg", MINUS])
|
||||
else:
|
||||
pieces.append(["op", _OPS.get(s, s)])
|
||||
var out := ""
|
||||
var prev := ""
|
||||
for pc: Array in pieces:
|
||||
var kind := str(pc[0])
|
||||
var space := false
|
||||
match kind:
|
||||
"op":
|
||||
space = out != ""
|
||||
"val", "fn":
|
||||
space = prev in ["op", ",", "val", ")"]
|
||||
"(", "neg":
|
||||
space = prev in ["op", ","]
|
||||
if space:
|
||||
out += " "
|
||||
out += str(pc[1])
|
||||
prev = kind
|
||||
return _short(out, src)
|
||||
return DescribeDice.words(expr, L)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- helpers
|
||||
|
|
|
|||
90
core/describe_dice.gd
Normal file
90
core/describe_dice.gd
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
class_name DescribeDice
|
||||
extends RefCounted
|
||||
## Turns a dice or count expression into plain words for Describe:
|
||||
## "2d6 + dm(@str)" -> "2d6 + Strength modifier", "1d6 + tier" -> "d6 + Tier".
|
||||
## It reads the expression as tokens (small pieces such as "2d6", "+", "@str"),
|
||||
## swaps each piece for its words, then joins them with tidy spacing.
|
||||
## Describe.dice() and Describe._expr() delegate here.
|
||||
|
||||
## Maths signs shown as their print forms ("−" is Describe.MINUS).
|
||||
const OPS := {"*": "×", "/": "÷", "-": "−", "==": "=", "!=": "≠", ">=": "≥", "<=": "≤"}
|
||||
|
||||
|
||||
## The expression in words, using a label table from DescribeLabels.build().
|
||||
## Text that can't be read comes back as it is.
|
||||
static func words(expr: String, labels: Dictionary) -> String:
|
||||
var source := expr.strip_edges()
|
||||
if source == "":
|
||||
return ""
|
||||
var tokens := Builder.tokens(source)
|
||||
if tokens.is_empty():
|
||||
return source
|
||||
return Describe._short(_join(_pieces(tokens, labels)), source)
|
||||
|
||||
|
||||
## Each token as a [kind, text] piece; kinds: val fn op neg ( ) ,
|
||||
static func _pieces(tokens: Array, labels: Dictionary) -> Array:
|
||||
var pieces: Array = []
|
||||
var index := 0
|
||||
while index < tokens.size():
|
||||
var token: Dictionary = tokens[index]
|
||||
var token_type := str(token["t"])
|
||||
var token_text := str(token["s"])
|
||||
var prev_kind := str(pieces.back()[0]) if not pieces.is_empty() else ""
|
||||
# dm(x) with one value inside: "X modifier".
|
||||
if token_type == "name" and token_text == "dm" and index + 3 < tokens.size() \
|
||||
and tokens[index + 1]["s"] == "(" and tokens[index + 3]["s"] == ")" \
|
||||
and ["var", "name", "num"].has(str(tokens[index + 2]["t"])):
|
||||
pieces.append(["val", Describe._value(str(tokens[index + 2]["s"]), labels) + " modifier"])
|
||||
index += 4
|
||||
continue
|
||||
index += 1
|
||||
match token_type:
|
||||
"dice":
|
||||
var dice_parts := Builder.dice_parts(token_text)
|
||||
if int(dice_parts[0]) == 1:
|
||||
pieces.append(["val", "d%d" % int(dice_parts[1])])
|
||||
else:
|
||||
pieces.append(["val", "%dd%d" % [int(dice_parts[0]), int(dice_parts[1])]])
|
||||
"var", "prefix":
|
||||
pieces.append(["val", DescribeLabels.lookup(token_text, labels)])
|
||||
"name":
|
||||
if index < tokens.size() and tokens[index]["s"] == "(":
|
||||
pieces.append(["fn", token_text])
|
||||
else:
|
||||
pieces.append(["val", Describe._value(token_text, labels)])
|
||||
"num":
|
||||
pieces.append(["val", token_text])
|
||||
"str":
|
||||
pieces.append(["val", Builder.unquote(token_text)])
|
||||
"kw":
|
||||
pieces.append(["op", token_text])
|
||||
_:
|
||||
if token_text == "(" or token_text == ")" or token_text == ",":
|
||||
pieces.append([token_text, token_text])
|
||||
elif token_text == "-" and prev_kind in ["", "op", "(", ",", "neg"]:
|
||||
pieces.append(["neg", "−"])
|
||||
else:
|
||||
pieces.append(["op", OPS.get(token_text, token_text)])
|
||||
return pieces
|
||||
|
||||
|
||||
## Joins the pieces with a space where a reader expects one.
|
||||
static func _join(pieces: Array) -> String:
|
||||
var out := ""
|
||||
var prev := ""
|
||||
for piece: Array in pieces:
|
||||
var kind := str(piece[0])
|
||||
var space := false
|
||||
match kind:
|
||||
"op":
|
||||
space = out != ""
|
||||
"val", "fn":
|
||||
space = prev in ["op", ",", "val", ")"]
|
||||
"(", "neg":
|
||||
space = prev in ["op", ","]
|
||||
if space:
|
||||
out += " "
|
||||
out += str(piece[1])
|
||||
prev = kind
|
||||
return out
|
||||
1
core/describe_dice.gd.uid
Normal file
1
core/describe_dice.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://coms1uaipk7wn
|
||||
73
core/describe_labels.gd
Normal file
73
core/describe_labels.gd
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
class_name DescribeLabels
|
||||
extends RefCounted
|
||||
## Finds the plain-words name ("label") of a value for Describe. It builds a
|
||||
## lookup table from a board's cards once, then answers "what is @skill_pilot
|
||||
## called?" from it. Describe keeps one-line delegators, so callers use
|
||||
## Describe.label() and Describe._labels() as before.
|
||||
|
||||
## Names for values that come from the roll itself rather than a card.
|
||||
const CHAIN_LABELS := {"answer": "Answer", "i": "Pass", "nat": "Natural roll", "total": "Total",
|
||||
"effect": "Effect", "option": "Chosen option", "option_label": "Chosen option",
|
||||
"option_value": "Chosen value"}
|
||||
|
||||
|
||||
## Label table for a board: {"vars": {name: label}, "stems": [[stem, label]]
|
||||
## longest first, "inputs": {name: label}, "scales": {name: steps}}.
|
||||
static func build(board: TowerBoard) -> Dictionary:
|
||||
var vars := {}
|
||||
var scales := {}
|
||||
var stems: Array = []
|
||||
var inputs := {}
|
||||
if board != null:
|
||||
for table in board.tables:
|
||||
if table.kind == RollTable.KIND_VARIABLE and table.var_name != "":
|
||||
var card_label := Formatter.strip_tags(table.name).strip_edges()
|
||||
if table.var_type == RollTable.VAR_COLLECTION:
|
||||
stems.append([table.var_name, card_label if card_label != "" else RollContext.pretty_name(table.var_name)])
|
||||
elif card_label != "" and not vars.has(table.var_name):
|
||||
vars[table.var_name] = card_label
|
||||
if table.is_scale() and not scales.has(table.var_name):
|
||||
scales[table.var_name] = table.steps
|
||||
for input: Dictionary in table.inputs:
|
||||
var input_label := str(input.get("label", "")).strip_edges()
|
||||
if input_label != "" and not inputs.has(str(input.get("name", ""))):
|
||||
inputs[str(input.get("name", ""))] = input_label
|
||||
stems.sort_custom(func(first: Array, second: Array) -> bool: return str(first[0]).length() > str(second[0]).length())
|
||||
return {"vars": vars, "stems": stems, "inputs": inputs, "scales": scales}
|
||||
|
||||
|
||||
## The label of one value name, looked up in a table from build().
|
||||
static func lookup(name: String, labels: Dictionary) -> String:
|
||||
var value_name := name.strip_edges()
|
||||
if value_name.begins_with("@["):
|
||||
return "Chosen value"
|
||||
if value_name.begins_with("@"):
|
||||
value_name = value_name.substr(1).to_lower()
|
||||
if value_name.ends_with("*"):
|
||||
var stem := value_name.trim_suffix("*").trim_suffix("_")
|
||||
for stem_pair: Array in labels["stems"]:
|
||||
if stem_pair[0] == stem:
|
||||
return stem_pair[1]
|
||||
return "%s values" % RollContext.pretty_name(stem)
|
||||
if labels["vars"].has(value_name):
|
||||
return labels["vars"][value_name]
|
||||
for stem_pair: Array in labels["stems"]:
|
||||
var prefix := str(stem_pair[0]) + "_"
|
||||
if value_name.begins_with(prefix) and value_name.length() > prefix.length():
|
||||
return RollContext.pretty_name(value_name.substr(prefix.length()))
|
||||
return RollContext.pretty_name(value_name)
|
||||
if labels["inputs"].has(value_name):
|
||||
return labels["inputs"][value_name]
|
||||
if CHAIN_LABELS.has(value_name):
|
||||
return CHAIN_LABELS[value_name]
|
||||
return RollContext.pretty_name(value_name)
|
||||
|
||||
|
||||
## The collection a value belongs to ({"stem", "label"}) or {}.
|
||||
static func collection_of(name: String, labels: Dictionary) -> Dictionary:
|
||||
var value_name := name.strip_edges().trim_prefix("@").to_lower()
|
||||
for stem_pair: Array in labels["stems"]:
|
||||
var prefix := str(stem_pair[0]) + "_"
|
||||
if value_name.begins_with(prefix):
|
||||
return {"stem": stem_pair[0], "label": stem_pair[1]}
|
||||
return {}
|
||||
1
core/describe_labels.gd.uid
Normal file
1
core/describe_labels.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ccf5jv2wu5gto
|
||||
|
|
@ -277,134 +277,15 @@ func remove_pipe(target: String) -> void:
|
|||
pipe_reroll.erase(target)
|
||||
|
||||
|
||||
## The entry as a Dictionary for the board file (TableEntryIO).
|
||||
func to_dict() -> Dictionary:
|
||||
var d := {"id": id, "text": text}
|
||||
if details != "":
|
||||
d["details"] = details
|
||||
if weight != 1:
|
||||
d["weight"] = weight
|
||||
if effects != "":
|
||||
d["effects"] = effects
|
||||
if when.strip_edges() != "":
|
||||
d["when"] = when.strip_edges()
|
||||
if show_if.strip_edges() != "":
|
||||
d["show_if"] = show_if.strip_edges()
|
||||
if not pipes.is_empty():
|
||||
var list: Array = []
|
||||
for p in pipes:
|
||||
var expr := count_expr(p)
|
||||
var n := count_for(p)
|
||||
var s := save_for(p)
|
||||
var w := when_for(p)
|
||||
var other := is_otherwise(p)
|
||||
var wv: Variant = pipe_with.get(p)
|
||||
var col := str(pipe_color.get(p, ""))
|
||||
var comb := combine_for(p)
|
||||
var paused := pipe_pause.has(p)
|
||||
var dice := dice_for(p)
|
||||
var again := reroll_for(p)
|
||||
if expr == "" and n == 1 and s == "" and w == "" and not other and wv == null and col == "" and comb == "all" and not paused and dice == "" and again.is_empty():
|
||||
list.append(p)
|
||||
else:
|
||||
var po := {"table": p}
|
||||
if expr != "":
|
||||
po["count"] = expr
|
||||
elif n != 1:
|
||||
po["count"] = n
|
||||
if s != "":
|
||||
po["save_as"] = s
|
||||
if w != "":
|
||||
po["when"] = w
|
||||
elif other:
|
||||
po["otherwise"] = true
|
||||
if wv is Dictionary and not (wv as Dictionary).is_empty():
|
||||
po["with"] = (wv as Dictionary).duplicate()
|
||||
if col != "":
|
||||
po["color"] = col
|
||||
if comb != "all":
|
||||
po["combine"] = comb
|
||||
if paused:
|
||||
po["pause"] = true
|
||||
if dice != "":
|
||||
po["dice"] = dice
|
||||
if not again.is_empty():
|
||||
po["reroll"] = again
|
||||
list.append(po)
|
||||
d["pipes"] = list
|
||||
if has_range:
|
||||
d["range"] = [range_lo, range_hi]
|
||||
if not cells.is_empty():
|
||||
d["cells"] = cells.duplicate()
|
||||
if not repeat.is_empty():
|
||||
d["repeat"] = repeat.duplicate(true)
|
||||
if page != "":
|
||||
d["page"] = page
|
||||
if image != "":
|
||||
d["image"] = image
|
||||
if cost_value.strip_edges() != "":
|
||||
d["cost"] = {"value": cost_value.strip_edges(), "amount": cost_amount.strip_edges()}
|
||||
return d
|
||||
return TableEntryIO.to_dict(self)
|
||||
|
||||
|
||||
## A "repeat" value from JSON or the UI: {count >= 1, reroll?: [ids]}, or {}.
|
||||
static func clean_repeat(v: Variant) -> Dictionary:
|
||||
if not (v is Dictionary):
|
||||
return {}
|
||||
var n := int(JsonIO._num((v as Dictionary).get("count", 2), 2))
|
||||
if n < 1:
|
||||
return {}
|
||||
var out := {"count": n}
|
||||
var ids: Array[String] = []
|
||||
if (v as Dictionary).get("reroll") is Array:
|
||||
for x: Variant in v["reroll"]:
|
||||
if str(x) != "" and not ids.has(str(x)):
|
||||
ids.append(str(x))
|
||||
if not ids.is_empty():
|
||||
out["reroll"] = ids
|
||||
return out
|
||||
static func clean_repeat(value: Variant) -> Dictionary:
|
||||
return TableEntryIO.clean_repeat(value)
|
||||
|
||||
|
||||
static func from_dict(d: Dictionary) -> TableEntry:
|
||||
var e := TableEntry.new(str(d.get("id", "")), str(d.get("text", "")))
|
||||
e.details = str(d.get("details", ""))
|
||||
e.weight = maxi(0, int(d.get("weight", 1)))
|
||||
e.effects = str(d.get("effects", ""))
|
||||
e.page = str(d.get("page", ""))
|
||||
e.image = str(d.get("image", ""))
|
||||
var cost: Variant = d.get("cost")
|
||||
if cost is Dictionary:
|
||||
e.cost_value = str(cost.get("value", ""))
|
||||
e.cost_amount = str(cost.get("amount", ""))
|
||||
e.when = str(d.get("when", "")).strip_edges() if d.get("when") != null else ""
|
||||
e.show_if = str(d.get("show_if", "")).strip_edges() if d.get("show_if") != null else ""
|
||||
for p: Variant in d.get("pipes", []):
|
||||
if p is Dictionary:
|
||||
var target := str(p.get("table", ""))
|
||||
if target != "":
|
||||
var c: Variant = p.get("count", 1)
|
||||
e.add_pipe(target, c if c is String else int(c), str(p.get("save_as", "")))
|
||||
e.set_pipe_when(target, str(p.get("when", "")) if p.get("when") != null else "")
|
||||
e.set_pipe_otherwise(target, bool(p.get("otherwise", false)))
|
||||
if p.get("with") is Dictionary:
|
||||
e.set_pipe_with(target, p["with"])
|
||||
if str(p.get("color", "")) != "":
|
||||
e.pipe_color[target] = str(p["color"])
|
||||
if RollCombine.MODES.has(str(p.get("combine", "all"))) and str(p.get("combine", "all")) != "all":
|
||||
e.pipe_combine[target] = str(p["combine"])
|
||||
if bool(p.get("pause", false)):
|
||||
e.pipe_pause[target] = true
|
||||
e.set_pipe_dice(target, str(p.get("dice", "")) if p.get("dice") != null else "")
|
||||
if p.get("reroll") is Array:
|
||||
e.set_pipe_reroll(target, p["reroll"])
|
||||
elif str(p) != "":
|
||||
e.add_pipe(str(p))
|
||||
if d.get("cells") is Array:
|
||||
for c: Variant in d["cells"]:
|
||||
e.cells.append(str(c) if c != null else "")
|
||||
e.repeat = clean_repeat(d.get("repeat"))
|
||||
var r: Variant = d.get("range")
|
||||
if r is Array and r.size() == 2:
|
||||
e.has_range = true
|
||||
e.range_lo = int(r[0])
|
||||
e.range_hi = int(r[1])
|
||||
return e
|
||||
static func from_dict(data: Dictionary) -> TableEntry:
|
||||
return TableEntryIO.from_dict(data)
|
||||
|
|
|
|||
154
core/table_entry_io.gd
Normal file
154
core/table_entry_io.gd
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
class_name TableEntryIO
|
||||
extends RefCounted
|
||||
## Saves a TableEntry (one result on a card) to a plain Dictionary for the
|
||||
## board file, and builds one back from such a Dictionary. Only values that
|
||||
## differ from the defaults are written, so files stay small. TableEntry keeps
|
||||
## to_dict(), from_dict() and clean_repeat() as one-line delegators.
|
||||
|
||||
|
||||
## The entry as a Dictionary (the board file's form).
|
||||
static func to_dict(entry: TableEntry) -> Dictionary:
|
||||
var data := {"id": entry.id, "text": entry.text}
|
||||
if entry.details != "":
|
||||
data["details"] = entry.details
|
||||
if entry.weight != 1:
|
||||
data["weight"] = entry.weight
|
||||
if entry.effects != "":
|
||||
data["effects"] = entry.effects
|
||||
if entry.when.strip_edges() != "":
|
||||
data["when"] = entry.when.strip_edges()
|
||||
if entry.show_if.strip_edges() != "":
|
||||
data["show_if"] = entry.show_if.strip_edges()
|
||||
if not entry.pipes.is_empty():
|
||||
var list: Array = []
|
||||
for target in entry.pipes:
|
||||
list.append(_pipe_to_value(entry, target))
|
||||
data["pipes"] = list
|
||||
if entry.has_range:
|
||||
data["range"] = [entry.range_lo, entry.range_hi]
|
||||
if not entry.cells.is_empty():
|
||||
data["cells"] = entry.cells.duplicate()
|
||||
if not entry.repeat.is_empty():
|
||||
data["repeat"] = entry.repeat.duplicate(true)
|
||||
if entry.page != "":
|
||||
data["page"] = entry.page
|
||||
if entry.image != "":
|
||||
data["image"] = entry.image
|
||||
if entry.cost_value.strip_edges() != "":
|
||||
data["cost"] = {"value": entry.cost_value.strip_edges(), "amount": entry.cost_amount.strip_edges()}
|
||||
return data
|
||||
|
||||
|
||||
## One pipe as saved: just the target id when every option is the default,
|
||||
## else a Dictionary with "table" and the options that are set.
|
||||
static func _pipe_to_value(entry: TableEntry, target: String) -> Variant:
|
||||
var expr := entry.count_expr(target)
|
||||
var count := entry.count_for(target)
|
||||
var save_as := entry.save_for(target)
|
||||
var condition := entry.when_for(target)
|
||||
var otherwise := entry.is_otherwise(target)
|
||||
var with_values: Variant = entry.pipe_with.get(target)
|
||||
var color := str(entry.pipe_color.get(target, ""))
|
||||
var combine := entry.combine_for(target)
|
||||
var paused := entry.pipe_pause.has(target)
|
||||
var dice := entry.dice_for(target)
|
||||
var reroll := entry.reroll_for(target)
|
||||
if expr == "" and count == 1 and save_as == "" and condition == "" and not otherwise \
|
||||
and with_values == null and color == "" and combine == "all" and not paused \
|
||||
and dice == "" and reroll.is_empty():
|
||||
return target
|
||||
var options := {"table": target}
|
||||
if expr != "":
|
||||
options["count"] = expr
|
||||
elif count != 1:
|
||||
options["count"] = count
|
||||
if save_as != "":
|
||||
options["save_as"] = save_as
|
||||
if condition != "":
|
||||
options["when"] = condition
|
||||
elif otherwise:
|
||||
options["otherwise"] = true
|
||||
if with_values is Dictionary and not (with_values as Dictionary).is_empty():
|
||||
options["with"] = (with_values as Dictionary).duplicate()
|
||||
if color != "":
|
||||
options["color"] = color
|
||||
if combine != "all":
|
||||
options["combine"] = combine
|
||||
if paused:
|
||||
options["pause"] = true
|
||||
if dice != "":
|
||||
options["dice"] = dice
|
||||
if not reroll.is_empty():
|
||||
options["reroll"] = reroll
|
||||
return options
|
||||
|
||||
|
||||
## A "repeat" value from JSON or the UI: {count >= 1, reroll?: [ids]}, or {}.
|
||||
static func clean_repeat(value: Variant) -> Dictionary:
|
||||
if not (value is Dictionary):
|
||||
return {}
|
||||
var count := int(JsonIO._num((value as Dictionary).get("count", 2), 2))
|
||||
if count < 1:
|
||||
return {}
|
||||
var out := {"count": count}
|
||||
var ids: Array[String] = []
|
||||
if (value as Dictionary).get("reroll") is Array:
|
||||
for item: Variant in value["reroll"]:
|
||||
if str(item) != "" and not ids.has(str(item)):
|
||||
ids.append(str(item))
|
||||
if not ids.is_empty():
|
||||
out["reroll"] = ids
|
||||
return out
|
||||
|
||||
|
||||
## Builds an entry from its saved Dictionary.
|
||||
static func from_dict(data: Dictionary) -> TableEntry:
|
||||
var entry := TableEntry.new(str(data.get("id", "")), str(data.get("text", "")))
|
||||
entry.details = str(data.get("details", ""))
|
||||
entry.weight = maxi(0, int(data.get("weight", 1)))
|
||||
entry.effects = str(data.get("effects", ""))
|
||||
entry.page = str(data.get("page", ""))
|
||||
entry.image = str(data.get("image", ""))
|
||||
var cost: Variant = data.get("cost")
|
||||
if cost is Dictionary:
|
||||
entry.cost_value = str(cost.get("value", ""))
|
||||
entry.cost_amount = str(cost.get("amount", ""))
|
||||
entry.when = str(data.get("when", "")).strip_edges() if data.get("when") != null else ""
|
||||
entry.show_if = str(data.get("show_if", "")).strip_edges() if data.get("show_if") != null else ""
|
||||
for pipe: Variant in data.get("pipes", []):
|
||||
if pipe is Dictionary:
|
||||
_read_pipe(entry, pipe)
|
||||
elif str(pipe) != "":
|
||||
entry.add_pipe(str(pipe))
|
||||
if data.get("cells") is Array:
|
||||
for cell: Variant in data["cells"]:
|
||||
entry.cells.append(str(cell) if cell != null else "")
|
||||
entry.repeat = clean_repeat(data.get("repeat"))
|
||||
var range_value: Variant = data.get("range")
|
||||
if range_value is Array and range_value.size() == 2:
|
||||
entry.has_range = true
|
||||
entry.range_lo = int(range_value[0])
|
||||
entry.range_hi = int(range_value[1])
|
||||
return entry
|
||||
|
||||
|
||||
## Adds one saved pipe Dictionary ({"table": id, options...}) to `entry`.
|
||||
static func _read_pipe(entry: TableEntry, pipe: Dictionary) -> void:
|
||||
var target := str(pipe.get("table", ""))
|
||||
if target == "":
|
||||
return
|
||||
var count: Variant = pipe.get("count", 1)
|
||||
entry.add_pipe(target, count if count is String else int(count), str(pipe.get("save_as", "")))
|
||||
entry.set_pipe_when(target, str(pipe.get("when", "")) if pipe.get("when") != null else "")
|
||||
entry.set_pipe_otherwise(target, bool(pipe.get("otherwise", false)))
|
||||
if pipe.get("with") is Dictionary:
|
||||
entry.set_pipe_with(target, pipe["with"])
|
||||
if str(pipe.get("color", "")) != "":
|
||||
entry.pipe_color[target] = str(pipe["color"])
|
||||
if RollCombine.MODES.has(str(pipe.get("combine", "all"))) and str(pipe.get("combine", "all")) != "all":
|
||||
entry.pipe_combine[target] = str(pipe["combine"])
|
||||
if bool(pipe.get("pause", false)):
|
||||
entry.pipe_pause[target] = true
|
||||
entry.set_pipe_dice(target, str(pipe.get("dice", "")) if pipe.get("dice") != null else "")
|
||||
if pipe.get("reroll") is Array:
|
||||
entry.set_pipe_reroll(target, pipe["reroll"])
|
||||
1
core/table_entry_io.gd.uid
Normal file
1
core/table_entry_io.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://xmi7f46bxkv1
|
||||
Loading…
Reference in a new issue