adding date tools
This commit is contained in:
parent
ffc7be0f36
commit
f258129063
|
@ -4,14 +4,14 @@ from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from telisar.reckoning import telisaran
|
from reckoning import telisaran
|
||||||
|
|
||||||
|
|
||||||
def _string_to_date(date):
|
def string_to_date(date):
|
||||||
return telisaran.datetime.from_expression(f"on {date}", timeline={})
|
return telisaran.datetime.from_expression(f"on {date}", timeline={})
|
||||||
|
|
||||||
|
|
||||||
def _date_to_string(date):
|
def date_to_string(date):
|
||||||
return date.numeric
|
return date.numeric
|
||||||
|
|
||||||
|
|
||||||
|
@ -47,8 +47,8 @@ def save(campaign, path='.', name='dnd_campaign'):
|
||||||
target = Path(f"{savepath}.000")
|
target = Path(f"{savepath}.000")
|
||||||
shutil.move(savepath, target)
|
shutil.move(savepath, target)
|
||||||
|
|
||||||
campaign['date'] = _date_to_string(campaign['date'])
|
campaign['date'] = date_to_string(campaign['date'])
|
||||||
campaign['start_date'] = _date_to_string(campaign['start_date'])
|
campaign['start_date'] = date_to_string(campaign['start_date'])
|
||||||
savepath.write_text(yaml.safe_dump(dict(campaign)))
|
savepath.write_text(yaml.safe_dump(dict(campaign)))
|
||||||
return savepath, (backup_count or 0) + 2
|
return savepath, (backup_count or 0) + 2
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ def save(campaign, path='.', name='dnd_campaign'):
|
||||||
def load(path=".", name='dnd_campaign', start_date='', backup=None, console=None):
|
def load(path=".", name='dnd_campaign', start_date='', backup=None, console=None):
|
||||||
ext = "" if backup is None else f".{backup:03d}"
|
ext = "" if backup is None else f".{backup:03d}"
|
||||||
|
|
||||||
default_date = _string_to_date(start_date)
|
default_date = string_to_date(start_date)
|
||||||
campaign = defaultdict(str)
|
campaign = defaultdict(str)
|
||||||
campaign['start_date'] = default_date
|
campaign['start_date'] = default_date
|
||||||
campaign['date'] = default_date
|
campaign['date'] = default_date
|
||||||
|
@ -67,8 +67,8 @@ def load(path=".", name='dnd_campaign', start_date='', backup=None, console=None
|
||||||
target = Path(path).expanduser() / f"{name}.yaml{ext}"
|
target = Path(path).expanduser() / f"{name}.yaml{ext}"
|
||||||
with open(target, 'rb') as f:
|
with open(target, 'rb') as f:
|
||||||
loaded = yaml.safe_load(f)
|
loaded = yaml.safe_load(f)
|
||||||
loaded['start_date'] = _string_to_date(loaded['start_date'])
|
loaded['start_date'] = string_to_date(loaded['start_date'])
|
||||||
loaded['date'] = _string_to_date(loaded['date'])
|
loaded['date'] = string_to_date(loaded['date'])
|
||||||
campaign.update(loaded)
|
campaign.update(loaded)
|
||||||
if console:
|
if console:
|
||||||
console.print(f"Successfully loaded Campaign {name} from {target}!")
|
console.print(f"Successfully loaded Campaign {name} from {target}!")
|
||||||
|
|
|
@ -11,6 +11,8 @@ from site_tools import campaign
|
||||||
|
|
||||||
from npc.generator.base import generate_npc
|
from npc.generator.base import generate_npc
|
||||||
from reckoning.calendar import TelisaranCalendar
|
from reckoning.calendar import TelisaranCalendar
|
||||||
|
from reckoning.telisaran import Day
|
||||||
|
from reckoning.telisaran import ReckoningError
|
||||||
|
|
||||||
BINDINGS = KeyBindings()
|
BINDINGS = KeyBindings()
|
||||||
|
|
||||||
|
@ -64,40 +66,88 @@ class DMShell(BasePrompt):
|
||||||
self.npc()
|
self.npc()
|
||||||
|
|
||||||
@self.key_bindings.add("f4")
|
@self.key_bindings.add("f4")
|
||||||
def calendar(event):
|
def date(event):
|
||||||
self.calendar()
|
self.date()
|
||||||
|
|
||||||
@self.key_bindings.add("f8")
|
@self.key_bindings.add("f8")
|
||||||
def save(event):
|
def save(event):
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
@command(usage="""
|
@command(usage="""
|
||||||
[title]Calendar[/title]
|
[title]Date[/title]
|
||||||
|
|
||||||
Print the Telisaran calendar, including the current date.
|
Work with the Telisaran calendar, including the current campaign date.
|
||||||
|
|
||||||
[title]calendar[/title]
|
[title]USAGE[/title]
|
||||||
|
|
||||||
[link]> calendar [season][/link]
|
[link]> date [COMMAND[, ARGS]][/link]
|
||||||
|
|
||||||
|
COMMAND Description
|
||||||
|
|
||||||
|
season Print the spans of the current season, highlighting today
|
||||||
|
year Print the full year's calendar, highlighting today.
|
||||||
|
inc N Increment the current date by N days; defaults to 1.
|
||||||
|
dec N Decrement the current date by N days; defaults to 1.
|
||||||
|
set DATE Set the current date to DATE, in numeric format, such as
|
||||||
|
[link]2.1125.1.45[/link].
|
||||||
""", completer=WordCompleter(
|
""", completer=WordCompleter(
|
||||||
[
|
[
|
||||||
'season',
|
'season',
|
||||||
|
'year',
|
||||||
|
'inc',
|
||||||
|
'dec',
|
||||||
|
'set',
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
def calendar(self, parts=[]):
|
def date(self, parts=[]):
|
||||||
|
|
||||||
if not self.cache['calendar']:
|
if not self.cache['calendar']:
|
||||||
self.cache['calendar'] = TelisaranCalendar(today=self._campaign['start_date'])
|
self.cache['calendar'] = TelisaranCalendar(today=self._campaign['date'])
|
||||||
|
|
||||||
if not parts:
|
if not parts:
|
||||||
self.console.print(self.cache['calendar'].__doc__)
|
self.console.print(f"Today is {self._campaign['date'].short} ({self._campaign['date'].numeric})")
|
||||||
self.console.print(f"Today is {self._campaign['date'].short}")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if parts[0] == 'season':
|
cmd = parts[0]
|
||||||
self.console.print(self.cache['calendar'].season)
|
try:
|
||||||
|
val = parts[1]
|
||||||
|
except IndexError:
|
||||||
|
val = None
|
||||||
|
|
||||||
|
print(cmd)
|
||||||
|
|
||||||
|
handler = getattr(self, f"_handler_date_{cmd}", None)
|
||||||
|
if not handler:
|
||||||
|
self.console.error(f"Unsupported command: {cmd}. Try 'help date'.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
return handler(val)
|
||||||
|
|
||||||
|
def _handler_date_season(self, *args):
|
||||||
|
self.console.print(self.cache['calendar'].season)
|
||||||
|
|
||||||
|
def _handler_date_year(self, *args):
|
||||||
|
self.console.print(self.cache['calendar'].calendar)
|
||||||
|
|
||||||
|
def _handler_date_inc(self, days):
|
||||||
|
offset = int(days or 1) * Day.length_in_seconds
|
||||||
|
self._campaign['date'] = self._campaign['date'] + offset
|
||||||
|
return self.date()
|
||||||
|
|
||||||
|
def _handler_date_dec(self, days):
|
||||||
|
offset = int(days or 1) * Day.length_in_seconds
|
||||||
|
self._campaign['date'] = self._campaign['date'] - offset
|
||||||
|
return self.date()
|
||||||
|
|
||||||
|
def _handler_date_set(self, new_date):
|
||||||
|
try:
|
||||||
|
self._campaign['date'] = campaign.string_to_date(new_date)
|
||||||
|
except ReckoningError as e:
|
||||||
|
self.console.error(str(e))
|
||||||
|
self.console.error("Invalid date. Use numeric formats; see 'help date' for more.")
|
||||||
|
self.cache['calendar'] = TelisaranCalendar(today=self._campaign['date'])
|
||||||
|
return self.date()
|
||||||
|
|
||||||
@command(usage="""
|
@command(usage="""
|
||||||
[title]Save[/title]
|
[title]Save[/title]
|
||||||
|
|
||||||
|
@ -194,21 +244,6 @@ class DMShell(BasePrompt):
|
||||||
super().help(parts)
|
super().help(parts)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@command(usage="""
|
|
||||||
[title]INCREMENT DATE[/title]
|
|
||||||
|
|
||||||
[b]id[/b] Increments the calendar date by one day.
|
|
||||||
|
|
||||||
[title]USAGE[/title]
|
|
||||||
|
|
||||||
[link]id[/link]
|
|
||||||
""")
|
|
||||||
def id(self, parts=[]):
|
|
||||||
"""
|
|
||||||
Increment the date by one day.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
@command(
|
@command(
|
||||||
usage="""
|
usage="""
|
||||||
[title]LOCATION[/title]
|
[title]LOCATION[/title]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user