dnd/deadsands/site_tools/shell/interactive_shell.py

151 lines
4.0 KiB
Python
Raw Normal View History

2023-07-03 14:14:03 -07:00
from pathlib import Path
2023-07-04 10:59:51 -07:00
from prompt_toolkit.application import get_app
2023-07-03 14:14:03 -07:00
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.key_binding import KeyBindings
2023-07-04 10:59:51 -07:00
from rich.table import Table
from rolltable.tables import RollTable
2023-07-03 14:14:03 -07:00
2023-07-04 10:59:51 -07:00
from site_tools.shell.base import BasePrompt, command
2023-07-03 14:14:03 -07:00
2023-07-04 10:45:10 -07:00
BINDINGS = KeyBindings()
2023-07-03 14:14:03 -07:00
2023-07-04 10:45:10 -07:00
class DMShell(BasePrompt):
def __init__(self, cache={}):
super().__init__(cache)
self._name = "DM Shell"
2023-07-04 10:59:51 -07:00
self._prompt = ["dm"]
self._toolbar = [("class:bold", " DMSH ")]
2023-07-04 10:45:10 -07:00
self._key_bindings = BINDINGS
2023-07-03 14:14:03 -07:00
self._register_subshells()
self._register_keybindings()
def _register_keybindings(self):
2023-07-04 10:59:51 -07:00
self._toolbar.extend(
[
("", " [H]elp "),
("", " [W]ild Magic Table "),
("", " [Q]uit "),
]
)
@self.key_bindings.add("c-q")
@self.key_bindings.add("c-d")
2023-07-03 14:14:03 -07:00
def quit(event):
self.quit()
2023-07-04 10:59:51 -07:00
@self.key_bindings.add("c-h")
2023-07-03 14:14:03 -07:00
def help(event):
self.help()
2023-07-04 10:59:51 -07:00
@self.key_bindings.add("c-w")
2023-07-03 14:14:03 -07:00
def wmt(event):
self.wmt()
@command(usage="""
[title]QUIT[/title]
The [b]quit[/b] command exits dmsh.
[title]USAGE[/title]
[link]> quit|^D|<ENTER>[/link]
""")
def quit(self, *parts):
"""
Quit dmsh.
"""
2023-07-04 10:45:10 -07:00
try:
get_app().exit()
finally:
raise SystemExit("")
2023-07-03 14:14:03 -07:00
@command(usage="""
[title]HELP FOR THE HELP LORD[/title]
The [b]help[/b] command will print usage information for whatever you're currently
doing. You can also ask for help on any command currently available.
[title]USAGE[/title]
[link]> help [COMMAND][/link]
""")
2023-07-04 10:45:10 -07:00
def help(self, parts=[]):
2023-07-03 14:14:03 -07:00
"""
Display the help message.
"""
super().help(parts)
return True
@command(usage="""
[title]INCREMENT DATE[/title]
[b]id[/b] Increments the calendar date by one day.
[title]USAGE[/title]
[link]id[/link]
""")
2023-07-04 10:45:10 -07:00
def id(self, parts=[]):
2023-07-03 14:14:03 -07:00
"""
Increment the date by one day.
"""
raise NotImplementedError()
2023-07-04 10:59:51 -07:00
@command(
usage="""
2023-07-03 14:14:03 -07:00
[title]LOCATION[/title]
[b]loc[/b] sets the party's location to the specified region of the Sahwat Desert.
[title]USAGE[/title]
[link]loc LOCATION[/link]
""",
2023-07-04 10:59:51 -07:00
completer=WordCompleter(["The Blooming Wastes", "Dust River Canyon", "Gopher Gulch", "Calamity Ridge"]),
)
2023-07-04 10:45:10 -07:00
def loc(self, parts=[]):
2023-07-03 14:14:03 -07:00
"""
Move the party to a new region of the Sahwat Desert.
"""
if parts:
2023-07-04 10:59:51 -07:00
self.cache["location"] = " ".join(parts)
2023-07-04 10:45:10 -07:00
self.console.print(f"The party is in {self.cache['location']}.")
2023-07-03 14:14:03 -07:00
@command(usage="""
[title]OVERLAND TRAVEL[/title]
[b]ot[/b]
[title]USAGE[/title]
[link]ot in[/link]
""")
2023-07-04 10:45:10 -07:00
def ot(self, parts=[]):
2023-07-03 14:14:03 -07:00
"""
Increment the date by one day and record
"""
raise NotImplementedError()
@command(usage="""
[title]WILD MAGIC TABLE[/title]
2023-07-04 10:45:10 -07:00
[b]wmt[/b] Generates a d20 wild magic surge roll table. The table will be cached for the cache.
2023-07-03 14:14:03 -07:00
[title]USAGE[/title]
[link]> wmt[/link]
[title]CLI[/title]
[link]roll-table \\
sources/sahwat_magic_table.yaml \\
--frequency default --die 20[/link]
""")
2023-07-04 10:59:51 -07:00
def wmt(self, *parts, source="sahwat_magic_table.yaml"):
2023-07-03 14:14:03 -07:00
"""
Generate a Wild Magic Table for resolving spell effects.
"""
2023-07-04 10:59:51 -07:00
if "wmt" not in self.cache:
2023-07-03 14:14:03 -07:00
rt = RollTable(
2023-07-04 10:45:10 -07:00
[Path(f"{self.cache['table_sources_path']}/{source}").read_text()],
2023-07-04 10:59:51 -07:00
frequency="default",
2023-07-03 14:14:03 -07:00
die=20,
)
table = Table(*rt.expanded_rows[0])
for row in rt.expanded_rows[1:]:
table.add_row(*row)
2023-07-04 10:59:51 -07:00
self.cache["wmt"] = table
self.console.print(self.cache["wmt"])