TableTower/ui/batch_roll.gd
RobinFiredrake a527bf545c Batches: Roll several times… on a card's menu
Rolls the card's whole chain a chosen number of times (up to 100) and
shows every result together, with Copy all. Each run asks its questions,
changes values and decks, and goes into Earlier like a normal roll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:07:37 -06:00

67 lines
2.5 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.
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] = []
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))
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()
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