Roller: expand refs and variables through RollContext, save pipe results
Entry text is expanded by TextExpander with a shared RollContext; inline reference rolls become children marked inline, pipes with save_as save the child's export BBCode. MAX_DEPTH is 32. Steps carry BBCode text, depth and inline/reshuffle flags. Adds tests/test_integration.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f3a6964044
commit
38e3819898
3 changed files with 372 additions and 28 deletions
169
core/roller.gd
169
core/roller.gd
|
|
@ -1,38 +1,99 @@
|
|||
class_name Roller
|
||||
extends RefCounted
|
||||
## Rolls a table and follows its pipes, producing a result tree.
|
||||
## Rolls a table and follows its pipes and inline references, producing a
|
||||
## result tree.
|
||||
##
|
||||
## Each node is a Dictionary:
|
||||
## table: RollTable, value: int (die value), index: int, entry: TableEntry,
|
||||
## children: Array of nodes, and "stop": String when the chain couldn't continue.
|
||||
## summary: String (the entry text with [[refs]] and {vars} expanded, as
|
||||
## BBCode with TextExpander's internal [url] markers),
|
||||
## children: Array of nodes (inline reference rolls first, marked
|
||||
## "inline": true, then pipe rolls in pipe order),
|
||||
## and "stop": String when the chain couldn't continue (its summary then
|
||||
## explains why).
|
||||
|
||||
const MAX_DEPTH := 12
|
||||
## Deepest level a chain can reach before it stops (matches RollContext).
|
||||
const MAX_DEPTH := RollContext.DEFAULT_MAX_DEPTH
|
||||
|
||||
|
||||
## Rolls `table_id` and everything its pipes lead to. `decks` maps table id to
|
||||
## DeckState for card-mode tables; missing decks are created in it on demand.
|
||||
## Extra node keys: "save_as" (on nodes reached through a pipe that saves),
|
||||
## "reshuffled" (card deck was refilled for this draw).
|
||||
static func roll(board: TowerBoard, table_id: String, rng: RandomNumberGenerator, depth: int = 0, decks: Dictionary = {}) -> Dictionary:
|
||||
## A RollContext wired to this board: it resolves [[names]] against the board's
|
||||
## tables and rolls inline references through the roller. `persistent` is the
|
||||
## collection's persistent variables (name without "@" -> {value, source}),
|
||||
## shared, not copied. `decks` maps table id to DeckState for card-mode tables,
|
||||
## also shared.
|
||||
static func new_context(board: TowerBoard, rng: RandomNumberGenerator, persistent: Dictionary = {}, decks: Dictionary = {}) -> RollContext:
|
||||
var ctx := RollContext.new(persistent, rng)
|
||||
ctx.max_depth = MAX_DEPTH
|
||||
ctx.set_meta("decks", decks)
|
||||
# A weak reference, so the context doesn't keep itself alive through its hook.
|
||||
var wr: WeakRef = weakref(ctx)
|
||||
ctx.resolve_table = func(name_or_id: String) -> String:
|
||||
return resolve(board, name_or_id)
|
||||
ctx.roll_table = func(id: String, depth: int) -> Dictionary:
|
||||
var c: RollContext = wr.get_ref()
|
||||
return {} if c == null else _roll(board, id, depth, c)
|
||||
return ctx
|
||||
|
||||
|
||||
## Table id for a [[reference]]: an exact id, or a name matched
|
||||
## case-insensitively without BBCode. "" if there's no such table.
|
||||
static func resolve(board: TowerBoard, name_or_id: String) -> String:
|
||||
var key := name_or_id.strip_edges()
|
||||
if board.get_table(key) != null:
|
||||
return key
|
||||
var want := RollContext.normalize_name(key)
|
||||
if want == "":
|
||||
return ""
|
||||
for t in board.tables:
|
||||
if RollContext.normalize_name(t.name) == want:
|
||||
return t.id
|
||||
return ""
|
||||
|
||||
|
||||
## Rolls `table_id` and everything its pipes and references lead to.
|
||||
## `decks` maps table id to DeckState for card-mode tables; missing decks are
|
||||
## created in it on demand. `ctx` carries the variables for the whole chain.
|
||||
## Without one, a fresh context (no persistent variables) is made around
|
||||
## `decks`; a context from new_context() brings its own decks and `decks` is
|
||||
## then ignored. Extra node keys: "save_as" (on nodes reached through a pipe
|
||||
## that saves), "reshuffled" (card deck was refilled for this draw), "inline"
|
||||
## (rolled by a [[reference]] in the parent's text).
|
||||
static func roll(board: TowerBoard, table_id: String, rng: RandomNumberGenerator, depth: int = 0, decks: Dictionary = {}, ctx: RollContext = null) -> Dictionary:
|
||||
if ctx == null:
|
||||
ctx = new_context(board, rng, {}, decks)
|
||||
elif not ctx.has_meta("decks"):
|
||||
ctx.set_meta("decks", decks)
|
||||
return _roll(board, table_id, depth, ctx)
|
||||
|
||||
|
||||
static func _stop(t: RollTable, why: String, extra: Dictionary = {}) -> Dictionary:
|
||||
var node := {"table": t, "stop": why, "summary": why, "children": []}
|
||||
node.merge(extra, true)
|
||||
return node
|
||||
|
||||
|
||||
static func _roll(board: TowerBoard, table_id: String, depth: int, ctx: RollContext) -> Dictionary:
|
||||
var t := board.get_table(table_id)
|
||||
if t == null:
|
||||
return {}
|
||||
var rng := ctx.rng
|
||||
if depth > MAX_DEPTH:
|
||||
return {"table": t, "stop": "Chain stopped after %d tables" % MAX_DEPTH, "children": []}
|
||||
return _stop(t, "Chain stopped after %d tables" % MAX_DEPTH, {"summary": TextExpander.deep_marker(t.id)})
|
||||
if t.entries.is_empty():
|
||||
return {"table": t, "stop": "This table has no results", "children": []}
|
||||
return _stop(t, "This table has no results")
|
||||
var index: int
|
||||
var value: int
|
||||
var reshuffled := false
|
||||
if t.card_mode:
|
||||
var decks: Dictionary = ctx.get_meta("decks", {})
|
||||
if not decks.has(t.id):
|
||||
decks[t.id] = DeckState.new()
|
||||
var deck: DeckState = decks[t.id]
|
||||
if deck.cards_left(t).y <= 0:
|
||||
return {"table": t, "stop": "This table has no results", "children": []}
|
||||
return _stop(t, "This table has no results")
|
||||
if deck.cards_left(t).x <= 0:
|
||||
if t.deck_empty == "stop":
|
||||
return {"table": t, "stop": "Deck empty", "children": []}
|
||||
return _stop(t, "Deck empty")
|
||||
deck.reshuffle()
|
||||
reshuffled = true
|
||||
var card := deck.draw(t, rng)
|
||||
|
|
@ -42,24 +103,32 @@ static func roll(board: TowerBoard, table_id: String, rng: RandomNumberGenerator
|
|||
value = t.dice_obj().roll(rng)
|
||||
index = t.entry_index_for(value)
|
||||
if index < 0:
|
||||
return {"table": t, "value": value, "stop": "No result for %s" % t.fmt(value), "children": []}
|
||||
return _stop(t, "No result for %s" % t.fmt(value), {"value": value})
|
||||
else:
|
||||
index = _pick_weighted(t, rng)
|
||||
if index < 0:
|
||||
return {"table": t, "stop": "No result can come up", "children": []}
|
||||
return _stop(t, "No result can come up")
|
||||
value = index + 1
|
||||
var e := t.entries[index]
|
||||
ctx.depth = depth + 1
|
||||
var expanded := TextExpander.expand(e.text, ctx)
|
||||
var children: Array = []
|
||||
for c: Dictionary in expanded["children"]:
|
||||
c["inline"] = true
|
||||
children.append(c)
|
||||
for p in e.pipes:
|
||||
var save := e.save_for(p)
|
||||
for k in e.count_for(p):
|
||||
var child := roll(board, p, rng, depth + 1, decks)
|
||||
var child := _roll(board, p, depth + 1, ctx)
|
||||
if child.is_empty():
|
||||
break
|
||||
if save != "":
|
||||
child["save_as"] = save
|
||||
if not child.has("stop"):
|
||||
ctx.save(save, Formatter.to_export_bbcode(str(child.get("summary", ""))), p)
|
||||
children.append(child)
|
||||
var node := {"table": t, "index": index, "value": value, "entry": e, "children": children}
|
||||
var node := {"table": t, "index": index, "value": value, "entry": e, "children": children,
|
||||
"summary": expanded["text"]}
|
||||
if reshuffled:
|
||||
node["reshuffled"] = true
|
||||
return node
|
||||
|
|
@ -122,26 +191,39 @@ static func _can_finish(t: RollTable, board: TowerBoard, done: Dictionary) -> bo
|
|||
return true
|
||||
|
||||
|
||||
## One line of the result tray for a single node.
|
||||
static func step_of(n: Dictionary) -> Dictionary:
|
||||
## One line of the result tray for a single node. Plain data only, because it
|
||||
## is saved in the roll history. "text" is BBCode with internal markers.
|
||||
static func step_of(n: Dictionary, depth: int = 0) -> Dictionary:
|
||||
var t: RollTable = n["table"]
|
||||
var text: String = n["stop"] if n.has("stop") else (n["entry"] as TableEntry).text
|
||||
return {
|
||||
var step := {
|
||||
"table": t.name,
|
||||
"table_id": t.id,
|
||||
"num": t.fmt(n["value"]) if n.has("value") else "",
|
||||
"die": t.die_size(),
|
||||
"dice": t.dice_label(),
|
||||
"text": text,
|
||||
"text": str(n.get("summary", n.get("stop", ""))),
|
||||
"depth": depth,
|
||||
}
|
||||
if n.has("entry"):
|
||||
step["entry_id"] = (n["entry"] as TableEntry).id
|
||||
if n.has("stop"):
|
||||
step["stop"] = true
|
||||
if n.get("inline", false):
|
||||
step["inline"] = true
|
||||
if n.get("reshuffled", false):
|
||||
step["reshuffled"] = true
|
||||
if n.has("save_as"):
|
||||
step["save_as"] = n["save_as"]
|
||||
return step
|
||||
|
||||
|
||||
## The whole chain in the order it was rolled.
|
||||
static func flatten(n: Dictionary, out: Array = []) -> Array:
|
||||
static func flatten(n: Dictionary, out: Array = [], depth: int = 0) -> Array:
|
||||
if n.is_empty():
|
||||
return out
|
||||
out.append(step_of(n))
|
||||
out.append(step_of(n, depth))
|
||||
for c in n["children"]:
|
||||
flatten(c, out)
|
||||
flatten(c, out, depth + 1)
|
||||
return out
|
||||
|
||||
|
||||
|
|
@ -156,16 +238,18 @@ static func reaches(n: Dictionary, table_id: String) -> bool:
|
|||
return false
|
||||
|
||||
|
||||
## Marks every entry and pipe along the chain as lit.
|
||||
static func collect_lit(n: Dictionary, entries: Dictionary, pipes: Dictionary) -> void:
|
||||
## Marks every entry, pipe and reference pipe along the chain as lit. When
|
||||
## given, `texts` gets each lit entry's expanded text (entry key -> BBCode).
|
||||
static func collect_lit(n: Dictionary, entries: Dictionary, pipes: Dictionary, texts: Dictionary = {}) -> void:
|
||||
if n.is_empty() or n.has("stop"):
|
||||
return
|
||||
var t: RollTable = n["table"]
|
||||
var e: TableEntry = n["entry"]
|
||||
entries[entry_key(t.id, e.id)] = true
|
||||
texts[entry_key(t.id, e.id)] = str(n.get("summary", e.text))
|
||||
for c in n["children"]:
|
||||
pipes[pipe_key(t.id, e.id, (c["table"] as RollTable).id)] = true
|
||||
collect_lit(c, entries, pipes)
|
||||
pipes[link_key(t.id, e.id, c)] = true
|
||||
collect_lit(c, entries, pipes, texts)
|
||||
|
||||
|
||||
static func entry_key(table_id: String, entry_id: String) -> String:
|
||||
|
|
@ -174,3 +258,32 @@ static func entry_key(table_id: String, entry_id: String) -> String:
|
|||
|
||||
static func pipe_key(table_id: String, entry_id: String, to_id: String) -> String:
|
||||
return "%s:%s>%s" % [table_id, entry_id, to_id]
|
||||
|
||||
|
||||
## Key of a reference pipe: a [[reference]] in an entry's text to another table.
|
||||
static func ref_key(table_id: String, entry_id: String, to_id: String) -> String:
|
||||
return "%s:%s~%s" % [table_id, entry_id, to_id]
|
||||
|
||||
|
||||
static func is_ref_key(key: String) -> bool:
|
||||
return key.contains("~")
|
||||
|
||||
|
||||
## The pipe or reference-pipe key that led from an entry to child node `c`.
|
||||
static func link_key(table_id: String, entry_id: String, c: Dictionary) -> String:
|
||||
var to := (c["table"] as RollTable).id
|
||||
return ref_key(table_id, entry_id, to) if c.get("inline", false) else pipe_key(table_id, entry_id, to)
|
||||
|
||||
|
||||
## Ids of the tables an entry's text references, resolved against the board,
|
||||
## without duplicates or missing tables.
|
||||
static func ref_targets(board: TowerBoard, text: String) -> Array[String]:
|
||||
var out: Array[String] = []
|
||||
if not text.contains("[[") and not text.contains("|"):
|
||||
return out
|
||||
var res := func(name: String) -> String: return resolve(board, name)
|
||||
for name in TextExpander.references(text, res):
|
||||
var id := resolve(board, name)
|
||||
if id != "" and not out.has(id):
|
||||
out.append(id)
|
||||
return out
|
||||
|
|
|
|||
230
tests/test_integration.gd
Normal file
230
tests/test_integration.gd
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
extends SceneTree
|
||||
## Headless checks for the roller working with RollContext, TextExpander,
|
||||
## pipes that save variables, card decks and StateStore.
|
||||
## Run: godot --headless --path . -s res://tests/test_integration.gd
|
||||
|
||||
var _failures := 0
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
_test_refs_and_vars()
|
||||
_test_pipe_save()
|
||||
_test_persistent_across_rolls()
|
||||
_test_depth_limit()
|
||||
_test_missing_and_unset()
|
||||
_test_decks_through_state()
|
||||
_test_lit_and_steps()
|
||||
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
||||
quit(1 if _failures else 0)
|
||||
|
||||
|
||||
func _entry(id: String, text: String, weight: int = 1) -> TableEntry:
|
||||
var e := TableEntry.new(id, text)
|
||||
e.weight = weight
|
||||
return e
|
||||
|
||||
|
||||
func _table(id: String, name: String, texts: Array) -> RollTable:
|
||||
var t := RollTable.new(id, name)
|
||||
for i in texts.size():
|
||||
t.entries.append(_entry("%s%d" % [id, i], texts[i]))
|
||||
return t
|
||||
|
||||
|
||||
func _board(tables: Array) -> TowerBoard:
|
||||
var b := TowerBoard.new()
|
||||
b.id = "test-coll"
|
||||
for t in tables:
|
||||
b.tables.append(t)
|
||||
return b
|
||||
|
||||
|
||||
func _rng(seed_value: int) -> RandomNumberGenerator:
|
||||
var r := RandomNumberGenerator.new()
|
||||
r.seed = seed_value
|
||||
return r
|
||||
|
||||
|
||||
## A quest board: Quest uses refs, chain vars and a persistent var.
|
||||
func _quest_board() -> TowerBoard:
|
||||
var quest := _table("quest", "Quest Hook", ["[[Names -> hero]] seeks [[Relics]] in {@town|Towns}. {hero} is [b]sure[/b] of it."])
|
||||
var names := _table("names", "Names", ["Aldric"])
|
||||
var relics := _table("relics", "[i]Relics[/i]", ["the Crown"])
|
||||
var towns := _table("towns", "Towns", ["Riverton"])
|
||||
return _board([quest, names, relics, towns])
|
||||
|
||||
|
||||
func _test_refs_and_vars() -> void:
|
||||
var b := _quest_board()
|
||||
var n := Roller.roll(b, "quest", _rng(1))
|
||||
var summary: String = n["summary"]
|
||||
_check(summary.contains("[url=table:names]Aldric[/url]"), "inline [[Names -> hero]] expands: %s" % summary)
|
||||
_check(summary.contains("[url=table:relics]the Crown[/url]"), "reference matches a name with BBCode")
|
||||
_check(summary.contains("[url=var:@town]Riverton[/url]"), "{@town|Towns} rolls and inserts")
|
||||
_check(summary.contains("[url=var:hero]Aldric[/url]"), "{hero} reuses the chain variable")
|
||||
_eq(Formatter.strip_tags(summary), "Aldric seeks the Crown in Riverton. Aldric is sure of it.", "plain text of the result")
|
||||
var kids: Array = n["children"]
|
||||
_check(kids.size() == 3, "three inline children (names, relics, towns), got %d" % kids.size())
|
||||
var all_inline := true
|
||||
for c: Dictionary in kids:
|
||||
all_inline = all_inline and c.get("inline", false)
|
||||
_check(all_inline, "inline children are marked inline")
|
||||
var steps := Roller.flatten(n, [])
|
||||
_check(steps.size() == 4 and steps[1].get("inline", false) and not steps[0].has("inline"), "steps mark inline rolls")
|
||||
_check(int(steps[1]["depth"]) == 1 and int(steps[0]["depth"]) == 0, "steps carry their depth")
|
||||
_check((steps[0]["text"] as String).contains("[b]sure[/b]"), "steps carry BBCode text")
|
||||
_check(Roller.ref_targets(b, b.get_table("quest").entries[0].text) == ["names", "relics", "towns"], "ref_targets resolves names: %s" % str(Roller.ref_targets(b, b.get_table("quest").entries[0].text)))
|
||||
|
||||
|
||||
func _test_pipe_save() -> void:
|
||||
var src := _table("src", "Source", ["Go {foe}, then {@pal?}"])
|
||||
var dst := _table("dst", "Foes", ["[b]Ogre[/b]"])
|
||||
var pals := _table("pals", "Pals", ["Mira"])
|
||||
src.entries[0].add_pipe("dst", 2, "foe")
|
||||
src.entries[0].add_pipe("pals", 1, "@pal")
|
||||
var b := _board([src, dst, pals])
|
||||
var store := {}
|
||||
var ctx := Roller.new_context(b, _rng(3), store, {})
|
||||
var n := Roller.roll(b, "src", ctx.rng, 0, {}, ctx)
|
||||
# Text is expanded before pipes roll, so {foe} isn't set yet in the same entry.
|
||||
_check((n["summary"] as String).contains("{foe?}"), "pipes roll after the entry's text")
|
||||
_eq(ctx.lookup("foe"), "[b]Ogre[/b]", "pipe save_as stores the export BBCode in a chain var")
|
||||
_check(not (ctx.lookup("foe") as String).contains("url="), "saved value has no internal markers")
|
||||
_check(store.has("pal") and store["pal"]["value"] == "Mira" and store["pal"]["source"] == "pals", "@ save goes to persistent vars with source: %s" % str(store))
|
||||
_check(ctx.persistent_changed, "persistent_changed after a pipe save")
|
||||
# A later table in the same chain sees the chain var.
|
||||
var a := _table("a", "A", ["start"])
|
||||
var c := _table("c", "C", ["uses {foe}"])
|
||||
a.entries[0].add_pipe("dst", 1, "foe")
|
||||
a.entries[0].add_pipe("c")
|
||||
var b2 := _board([a, c, dst])
|
||||
var n2 := Roller.roll(b2, "a", _rng(4))
|
||||
var used: String = (n2["children"] as Array)[1]["summary"]
|
||||
_eq(used, "uses [url=var:foe][b]Ogre[/b][/url]", "chain var from a pipe is used by a later pipe")
|
||||
|
||||
|
||||
func _test_persistent_across_rolls() -> void:
|
||||
var b := _quest_board()
|
||||
var store := {}
|
||||
var decks := {}
|
||||
var r := _rng(5)
|
||||
var ctx := Roller.new_context(b, r, store, decks)
|
||||
Roller.roll(b, "quest", r, 0, {}, ctx)
|
||||
_check(store.has("town") and store["town"]["value"] == "Riverton", "first roll saves @town")
|
||||
# Change the town by hand; the next roll uses it instead of rolling.
|
||||
store["town"]["value"] = "Hilltop"
|
||||
var ctx2 := Roller.new_context(b, r, store, decks)
|
||||
var n := Roller.roll(b, "quest", r, 0, {}, ctx2)
|
||||
_check((n["summary"] as String).contains("Hilltop"), "persistent var survives into the next roll")
|
||||
_check(ctx2.chain_vars.has("hero") and not ctx2.chain_vars.has("town"), "chain vars are fresh per roll")
|
||||
var inline_tables := []
|
||||
for c: Dictionary in n["children"]:
|
||||
inline_tables.append((c["table"] as RollTable).id)
|
||||
_check(not inline_tables.has("towns"), "no reroll of Towns while @town is set")
|
||||
# Through StateStore dictionaries (names without "@").
|
||||
var dir := "user://test_integration_state"
|
||||
var st := StateStore.new(dir)
|
||||
st.reload()
|
||||
for k in store:
|
||||
st.set_variable(b.id, k, store[k]["value"], store[k]["source"])
|
||||
st.save()
|
||||
var st2 := StateStore.new(dir)
|
||||
var back := st2.get_variables(b.id)
|
||||
_check(back.has("town") and back["town"]["value"] == "Hilltop" and back["town"]["source"] == "towns", "persistent vars round-trip through StateStore")
|
||||
var ctx3 := Roller.new_context(b, r, back, {})
|
||||
_eq(ctx3.lookup("@town"), "Hilltop", "RollContext reads StateStore's variables")
|
||||
DirAccess.remove_absolute(st.state_path())
|
||||
DirAccess.remove_absolute(dir.path_join("state"))
|
||||
DirAccess.remove_absolute(dir)
|
||||
|
||||
|
||||
func _test_depth_limit() -> void:
|
||||
_check(Roller.MAX_DEPTH == 32, "depth limit is 32")
|
||||
var loop := _table("loop", "Loop", ["again [[Loop]]"])
|
||||
var b := _board([loop])
|
||||
var n := Roller.roll(b, "loop", _rng(6))
|
||||
var steps := Roller.flatten(n, [])
|
||||
_check(steps.size() == Roller.MAX_DEPTH + 1, "self-reference stops at the depth limit (%d steps)" % steps.size())
|
||||
var deepest: Dictionary = steps[steps.size() - 1]
|
||||
_check((deepest["text"] as String).contains(TextExpander.DEEP_TEXT), "deepest text shows the chain-too-deep marker")
|
||||
_check(int(deepest["depth"]) == Roller.MAX_DEPTH, "deepest step is at the limit")
|
||||
# Mixed: pipes and references count the same depth.
|
||||
var ta := _table("ta", "TA", ["[[TB]]"])
|
||||
var tb := _table("tb", "TB", ["pipe"])
|
||||
tb.entries[0].add_pipe("ta")
|
||||
var b2 := _board([ta, tb])
|
||||
var n2 := Roller.roll(b2, "ta", _rng(7))
|
||||
var worst := 0
|
||||
for s in Roller.flatten(n2, []):
|
||||
worst = maxi(worst, int(s["depth"]))
|
||||
_check(worst <= Roller.MAX_DEPTH + 1, "mixed pipes and refs never go past the limit (%d)" % worst)
|
||||
|
||||
|
||||
func _test_missing_and_unset() -> void:
|
||||
var t := _table("t", "T", ["[[Nowhere]] and {ghost}"])
|
||||
var n := Roller.roll(_board([t]), "t", _rng(8))
|
||||
_eq(n["summary"], "[url=missing:Nowhere]⚠ Nowhere[/url] and [url=unset:ghost]{ghost?}[/url]", "missing and unset markers")
|
||||
_eq(Formatter.strip_tags(n["summary"]), "⚠ Nowhere (missing table) and {ghost?}", "markers read as text")
|
||||
|
||||
|
||||
func _test_decks_through_state() -> void:
|
||||
var deck := _table("deck", "Deck", ["A", "B", "C"])
|
||||
deck.card_mode = true
|
||||
var b := _board([deck])
|
||||
var dir := "user://test_integration_decks"
|
||||
var st := StateStore.new(dir)
|
||||
st.reload()
|
||||
var r := _rng(9)
|
||||
var seen := {}
|
||||
# Each roll loads the deck from the store and saves it back, like Main does.
|
||||
for i in 3:
|
||||
var decks := {}
|
||||
var saved := st.get_deck(b.id, "deck")
|
||||
if not saved.is_empty():
|
||||
decks["deck"] = DeckState.from_dict(saved)
|
||||
var n := Roller.roll(b, "deck", r, 0, decks)
|
||||
seen[(n["entry"] as TableEntry).text] = true
|
||||
_check(not n.has("reshuffled"), "no reshuffle while cards are left")
|
||||
st.set_deck(b.id, "deck", (decks["deck"] as DeckState).to_dict())
|
||||
st.save()
|
||||
_check(seen.size() == 3, "three rolls through the store deal three different cards")
|
||||
var st2 := StateStore.new(dir)
|
||||
var restored := DeckState.from_dict(st2.get_deck(b.id, "deck"))
|
||||
_check(restored.cards_left(deck) == Vector2i(0, 3), "a reloaded store remembers the empty deck")
|
||||
var decks2 := {"deck": restored}
|
||||
var n4 := Roller.roll(b, "deck", r, 0, decks2)
|
||||
_check(n4.get("reshuffled", false), "empty deck from the store reshuffles")
|
||||
_check(Roller.step_of(n4).get("reshuffled", false), "step notes the reshuffle")
|
||||
st2.forget_table(b.id, "deck")
|
||||
_check(st2.get_deck(b.id, "deck").is_empty(), "forget_table drops the deck")
|
||||
DirAccess.remove_absolute(st.state_path())
|
||||
DirAccess.remove_absolute(dir.path_join("state"))
|
||||
DirAccess.remove_absolute(dir)
|
||||
|
||||
|
||||
func _test_lit_and_steps() -> void:
|
||||
var b := _quest_board()
|
||||
b.get_table("quest").entries[0].add_pipe("names")
|
||||
var n := Roller.roll(b, "quest", _rng(10))
|
||||
var le := {}
|
||||
var lp := {}
|
||||
var lt := {}
|
||||
Roller.collect_lit(n, le, lp, lt)
|
||||
_check(lp.has(Roller.ref_key("quest", "quest0", "names")), "reference pipe is lit")
|
||||
_check(lp.has(Roller.pipe_key("quest", "quest0", "names")), "real pipe is lit too")
|
||||
_check(Roller.is_ref_key(Roller.ref_key("a", "b", "c")) and not Roller.is_ref_key(Roller.pipe_key("a", "b", "c")), "ref keys are told apart")
|
||||
_check((lt[Roller.entry_key("quest", "quest0")] as String).contains("Aldric"), "lit texts hold the expanded text")
|
||||
# Steps survive JSON (history is saved as JSON).
|
||||
var steps := Roller.flatten(n, [])
|
||||
var back: Variant = JSON.parse_string(JSON.stringify(steps))
|
||||
_check(back is Array and (back as Array).size() == steps.size() and back[0]["text"] == steps[0]["text"], "steps round-trip through JSON")
|
||||
|
||||
|
||||
func _eq(got: Variant, want: Variant, what: String) -> void:
|
||||
_check(got == want, "%s\n got: %s\n want: %s" % [what, str(got), str(want)])
|
||||
|
||||
|
||||
func _check(ok: bool, what: String) -> void:
|
||||
if not ok:
|
||||
_failures += 1
|
||||
print(" ✗ ", what)
|
||||
1
tests/test_integration.gd.uid
Normal file
1
tests/test_integration.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b8wmtk471t5hv
|
||||
Loading…
Reference in a new issue