From 3282610c09a3081e51c368efbbeb627610a5bff7 Mon Sep 17 00:00:00 2001 From: rr- Date: Mon, 5 Nov 2018 18:38:30 +0100 Subject: [PATCH] ui: add cheats --- pqcli/__main__.py | 10 +++++++++- pqcli/ui/__init__.py | 6 ++++-- pqcli/ui/game_view.py | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/pqcli/__main__.py b/pqcli/__main__.py index a3522bf..76f3d34 100644 --- a/pqcli/__main__.py +++ b/pqcli/__main__.py @@ -1,3 +1,4 @@ +import argparse from pathlib import Path import xdg @@ -8,11 +9,18 @@ from pqcli.ui import Ui SAVE_PATH = Path(xdg.XDG_CONFIG_HOME) / "pqcli" / "save.dat" +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(prog="pqcli") + parser.add_argument("--cheats", help="enable cheats", action="store_true") + return parser.parse_args() + + def main() -> None: + args = parse_args() roster = Roster.load(SAVE_PATH) try: - Ui(roster).run() + Ui(roster, args).run() finally: roster.save(SAVE_PATH) diff --git a/pqcli/ui/__init__.py b/pqcli/ui/__init__.py index c851fe0..32eca41 100644 --- a/pqcli/ui/__init__.py +++ b/pqcli/ui/__init__.py @@ -1,3 +1,4 @@ +import argparse import typing as T import urwid @@ -43,10 +44,11 @@ def bind_commands() -> None: class Ui: - def __init__(self, roster: Roster) -> None: + def __init__(self, roster: Roster, args: argparse.Namespace) -> None: bind_commands() self.roster = roster + self.args = args self.loop = urwid.MainLoop( None, PALETTE, unhandled_input=self.unhandled_input ) @@ -98,7 +100,7 @@ class Ui: def switch_to_game_view(self, player_idx: int) -> None: player = self.roster.players[player_idx] - self.loop.widget = GameView(self.loop, player) + self.loop.widget = GameView(self.loop, player, self.args) self._connect("cancel", self.switch_to_roster_view) def switch_to_delete_player_view(self, player_idx: int) -> None: diff --git a/pqcli/ui/game_view.py b/pqcli/ui/game_view.py index d29d8df..03f4bcc 100644 --- a/pqcli/ui/game_view.py +++ b/pqcli/ui/game_view.py @@ -1,3 +1,4 @@ +import argparse import datetime import typing as T @@ -5,7 +6,13 @@ import urwid from pqcli.config import EquipmentType, StatType from pqcli.lingo import act_name, to_roman -from pqcli.mechanic import InventoryItem, Player, Simulation, Spell +from pqcli.mechanic import ( + InventoryItem, + Player, + SignalMixin, + Simulation, + Spell, +) from pqcli.ui.custom_line_box import CustomLineBox from pqcli.ui.custom_progress_bar import CustomProgressBar from pqcli.ui.read_only_check_box import ReadOnlyCheckBox @@ -292,9 +299,12 @@ class TaskView(urwid.Pile): class GameView(urwid.Pile): signals = ["cancel"] - def __init__(self, loop: urwid.MainLoop, player: Player) -> None: + def __init__( + self, loop: urwid.MainLoop, player: Player, args: argparse.Namespace + ) -> None: self.loop = loop self.player = player + self.args = args self.simulation = Simulation(player) self.last_tick = datetime.datetime.now() @@ -352,4 +362,30 @@ class GameView(urwid.Pile): self.cancel() return True + if self.args.cheats: + if key == "e": + self.simulation.player.exp_bar.increment(1) + return True + + fast_forward = {"t": 1, "T": 100, "ctrl t": 10000} + if key in fast_forward: + # hack to suppress UI updates + old = SignalMixin.emit + SignalMixin.emit = lambda *_: None + + iterations = fast_forward[key] + for i in range(iterations): + self.simulation.tick() + + self.character_sheet_view.sync() + self.experience_view.sync() + self.spell_book_view.sync() + self.equipment_view.sync() + self.inventory_view.sync() + self.plot_view.sync() + self.task_view.sync() + + SignalMixin.emit = old + return True + return False