135 lines
5 KiB
GDScript3
135 lines
5 KiB
GDScript3
|
|
class_name TrayText
|
||
|
|
extends RefCounted
|
||
|
|
## The words of ResultTray's chips: captions, effect lines and the sheet
|
||
|
|
## headline's step. Static, so they can be tested without a tray.
|
||
|
|
|
||
|
|
|
||
|
|
## The muted line above a chip's text: table name and how it got its result.
|
||
|
|
## With `plain`, rules read in plain words ("Loot · Tier 1 · rolled 4 on 1D +
|
||
|
|
## Tier", "Force the gate · rolled 7, +1 → 8"), labelled from `board`.
|
||
|
|
static func caption_for(step: Dictionary, board: TowerBoard = null, plain: bool = false) -> String:
|
||
|
|
var caption := Formatter.strip_tags(str(step["table"]))
|
||
|
|
var num: String = str(step.get("num", ""))
|
||
|
|
var ins: Variant = step.get("inputs")
|
||
|
|
if ins is Dictionary:
|
||
|
|
for nm: Variant in ins:
|
||
|
|
if plain:
|
||
|
|
caption += " · %s %s" % [Describe.label(str(nm), board), Expr.format_value(ins[nm])]
|
||
|
|
else:
|
||
|
|
caption += " · %s=%s" % [nm, Expr.format_value(ins[nm])]
|
||
|
|
match str(step.get("kind", "")):
|
||
|
|
RollTable.KIND_CHECK:
|
||
|
|
var c: Variant = step.get("check")
|
||
|
|
if c is Dictionary:
|
||
|
|
var mod := int(c.get("mod", 0))
|
||
|
|
var ms := ""
|
||
|
|
if mod != 0:
|
||
|
|
ms = ("+%d" % mod) if mod > 0 else (Describe.MINUS + str(-mod))
|
||
|
|
if plain:
|
||
|
|
caption += " · rolled %s%s → %s" % [Expr.format_value(c.get("nat")), (", " + ms) if ms != "" else "", Expr.format_value(c.get("total"))]
|
||
|
|
else:
|
||
|
|
caption += " · %s%s = %s vs %s" % [Expr.format_value(c.get("nat")), ms.replace(Describe.MINUS, "-"), Expr.format_value(c.get("total")), Expr.format_value(c.get("target"))]
|
||
|
|
RollTable.KIND_CHOICE:
|
||
|
|
if step.get("cancelled", false):
|
||
|
|
pass
|
||
|
|
elif step.has("answer"):
|
||
|
|
caption += " · answered"
|
||
|
|
elif step.get("picked", false):
|
||
|
|
caption += " · picked"
|
||
|
|
elif step.get("random", false):
|
||
|
|
caption += " · random"
|
||
|
|
if num != "":
|
||
|
|
caption += ", rolled %s on %s" % [num, step.get("dice", "d%d" % int(step.get("die", 0)))]
|
||
|
|
RollTable.KIND_LOOP:
|
||
|
|
if step.has("pass"):
|
||
|
|
caption += " · pass %s" % str(step["pass"])
|
||
|
|
elif step.get("after", false):
|
||
|
|
caption += " · afterwards"
|
||
|
|
RollTable.KIND_SHEET:
|
||
|
|
caption += " · sheet"
|
||
|
|
RollTable.KIND_ROUTER:
|
||
|
|
if step.has("case"):
|
||
|
|
var said := false
|
||
|
|
if plain and board != null:
|
||
|
|
var rt := board.get_table(str(step.get("table_id", "")))
|
||
|
|
var k := int(step["case"])
|
||
|
|
if rt != null and k >= 0 and k < rt.entries.size():
|
||
|
|
caption += " · " + RulesView.cut(RulesView.case_label(rt.entries[k], board, true), 44)
|
||
|
|
said = true
|
||
|
|
if not said:
|
||
|
|
caption += " · case %d" % (int(step["case"]) + 1)
|
||
|
|
_:
|
||
|
|
if num != "":
|
||
|
|
var dl := str(step.get("dice", "d%d" % int(step.get("die", 0))))
|
||
|
|
if plain and dl.contains(" "):
|
||
|
|
dl = RulesView.cut(Describe.dice(dl, board), 30)
|
||
|
|
caption += " · rolled %s on %s" % [num, dl]
|
||
|
|
if step.get("reshuffled", false):
|
||
|
|
caption += " · deck reshuffled"
|
||
|
|
if str(step.get("save_as", "")) != "":
|
||
|
|
if plain:
|
||
|
|
caption += " · saved as %s" % Describe.label(str(step["save_as"]), board)
|
||
|
|
else:
|
||
|
|
caption += " · saved as {%s}" % step["save_as"]
|
||
|
|
return caption
|
||
|
|
|
||
|
|
|
||
|
|
## A step's effect lines for its chip: in plain words, one per value that
|
||
|
|
## changed ("Luck 2 → 1", "Town = Millbrook", "Cleared all Skills (3)") from
|
||
|
|
## its why facts, then any errors from its log; else the log as it is.
|
||
|
|
static func effect_lines_for(step: Dictionary, board: TowerBoard, plain: bool) -> Array:
|
||
|
|
var fx: Array = step.get("effects", [])
|
||
|
|
if not plain or not (step.get("why") is Array):
|
||
|
|
return fx
|
||
|
|
var out: Array = []
|
||
|
|
for f: Variant in step["why"]:
|
||
|
|
if f is Dictionary and str(f.get("type", "")) == "effect":
|
||
|
|
out.append({"text": effect_fact_line(f, board)})
|
||
|
|
if out.is_empty():
|
||
|
|
return fx
|
||
|
|
for l: Variant in fx:
|
||
|
|
if l is Dictionary and (l as Dictionary).get("error", false):
|
||
|
|
out.append(l)
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
## "Luck 2 → 1", "Town = Millbrook", "Loot cleared", "Cleared all Skills (3)".
|
||
|
|
static func effect_fact_line(f: Dictionary, board: TowerBoard) -> String:
|
||
|
|
var lab := Describe.label(str(f.get("var", "")), board)
|
||
|
|
if f.has("count"):
|
||
|
|
return "Cleared all %s (%s)" % [lab, Expr.format_value(f["count"])]
|
||
|
|
var before: Variant = f.get("before")
|
||
|
|
var after: Variant = f.get("after")
|
||
|
|
if after == null:
|
||
|
|
return "%s cleared" % lab
|
||
|
|
var a := Expr.format_value(after)
|
||
|
|
if before == null or Expr.format_value(before) == "":
|
||
|
|
return "%s = %s" % [lab, a if a != "" else "(none)"]
|
||
|
|
return "%s %s → %s" % [lab, Expr.format_value(before), a if a != "" else "(none)"]
|
||
|
|
|
||
|
|
|
||
|
|
## The first line of a (sheet's) multi-line text, with "…" when there's more.
|
||
|
|
static func one_line(text: String) -> String:
|
||
|
|
var nl := text.find("\n")
|
||
|
|
if nl < 0:
|
||
|
|
return text
|
||
|
|
return Formatter.close_open_tags(text.substr(0, nl)) + " …"
|
||
|
|
|
||
|
|
|
||
|
|
## The last sheet step of a chain (the headline), or {}.
|
||
|
|
static func sheet_step(steps: Array) -> Dictionary:
|
||
|
|
for k in range(steps.size() - 1, -1, -1):
|
||
|
|
var s: Dictionary = steps[k]
|
||
|
|
if str(s.get("kind", "")) == RollTable.KIND_SHEET and not s.get("stop", false):
|
||
|
|
return s
|
||
|
|
return {}
|
||
|
|
|
||
|
|
|
||
|
|
static func thousands(n: int) -> String:
|
||
|
|
var s := str(n)
|
||
|
|
var out := ""
|
||
|
|
while s.length() > 3:
|
||
|
|
out = "," + s.substr(s.length() - 3) + out
|
||
|
|
s = s.substr(0, s.length() - 3)
|
||
|
|
return s + out
|