TableTower/ui/batch_roll.gd

79 lines
3.1 KiB
GDScript3
Raw Normal View History

class_name BatchRoll
extends RefCounted
## Batches: roll a card's whole chain several times ("5 NPCs") and get every
## result at the end, in one window with Copy all. Questions on the way are
## asked each time. Values and decks change as they would for single rolls.
## Each roll goes into the tray's Earlier list (and the saved history), and
## the batch plays one roll sound.
const MAX := 100
## Asks how many, then runs the batch.
static func ask(m: Main, tid: String) -> void:
var t := m.data.get_table(tid)
if t == null:
return
m._add_modal(ModalCard.ask_text("Roll several times", "How many times should %s roll?" % Formatter.strip_tags(t.name), "5", "Roll",
func(s: String) -> void:
if not s.strip_edges().is_valid_int() or int(s) < 1:
m.toast("Type a number from 1 to %d" % MAX)
return
run(m, tid, mini(int(s), MAX))))
## Rolls `n` times and shows the results. Returns the result texts.
static func run(m: Main, tid: String, n: int) -> Array[String]:
var t := m.data.get_table(tid)
var texts: Array[String] = []
if m.sound:
m.sound.play("roll")
m.rolling._archive_current()
for i in n:
var ctx := Roller.new_context(m.data, m.rng, m.persistent_vars, m.decks)
ctx.prompt = m.prompt_hook
var tree: Dictionary = await Roller.roll_async(m.data, tid, ctx)
if ctx.halted == Roller.CANCELLED:
break
var steps := Roller.flatten(tree, [])
texts.append(m.rolling.result_text(steps, ResultTray.COPY_TEXT))
var rec := {"collection": m.data.id, "root": tid, "root_name": t.name, "time": Time.get_unix_time_from_system(),
"steps": steps, "all": steps, "done": true, "archived": true, "batch": "%d of %d" % [i + 1, n]}
m.history.push_front(rec)
if m.history.size() > Main.HISTORY_SIZE:
m.history.resize(Main.HISTORY_SIZE)
if m.state:
m.local_state._store_decks()
if ctx.persistent_changed:
m.local_state._store_variables()
m.state.add_history({"collection": m.data.id, "root": tid, "root_name": t.name,
"time": Time.get_unix_time_from_system(), "steps": steps, "chain_vars": ctx.chain_vars.duplicate()})
if m.state:
m.state.save()
m.refresh_live()
m.tray.render()
if not texts.is_empty():
m._add_modal(dialog(t, texts))
return texts
static func dialog(t: RollTable, texts: Array[String]) -> ModalCard:
var d := ModalCard.new("%s × %d" % [Formatter.strip_tags(t.name), texts.size()], 620.0)
for i in texts.size():
var box := PanelContainer.new()
box.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD_SOFT, 8, [12, 8, 12, 8]), 1, Style.RULE))
var v := VBoxContainer.new()
box.add_child(v)
v.add_child(Style.make_label("#%d" % (i + 1), Style.body_bold(), 13, Style.TRIM_DARK))
var l := Style.make_label(texts[i], Style.body(), 15, Style.INK)
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
v.add_child(l)
d.body.add_child(box)
var parts := PackedStringArray()
for i in texts.size():
parts.append("#%d\n%s" % [i + 1, texts[i]])
var all := "\n\n".join(parts)
d.add_button("Copy all", "plain", func() -> void: DisplayServer.clipboard_set(all))
d.add_button("Close", "primary", d.close)
return d