From 2fef41ca5e2172a0706e30bf5eb5d5a70af778a1 Mon Sep 17 00:00:00 2001 From: Dash Date: Sat, 17 Oct 2026 19:14:46 +0200 Subject: [PATCH] pqw: restore state through public methods The importer assigned private attributes of QuestBook, Inventory and SpellBook. Each now has a restore() method that sets its state wholesale, and the importer uses those. --- pqcli/mechanic.py | 17 +++++++++++++++++ pqcli/pqw.py | 15 ++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/pqcli/mechanic.py b/pqcli/mechanic.py index d642d6a..2704bf5 100644 --- a/pqcli/mechanic.py +++ b/pqcli/mechanic.py @@ -173,6 +173,11 @@ class QuestBook(SignalMixin): return self._quests[-1] return None + def restore(self, act: int, quests: list[str]) -> None: + """Set the act and quest log wholesale, as when importing a save.""" + self._act = act + self._quests = list(quests) + def increment_act(self) -> None: self._act += 1 self.emit("start_act", self._act) @@ -210,6 +215,14 @@ class Inventory(SignalMixin): def __getitem__(self, idx: int) -> InventoryItem: return self._items[idx] + def restore(self, gold: int, items: list[InventoryItem]) -> None: + """Set the gold and items wholesale, as when importing a save. + + The encumbrance bar is left alone, so callers can restore it too. + """ + self._gold = gold + self._items = list(items) + def add_gold(self, quantity: int) -> None: logger.info( "%s %s", @@ -294,6 +307,10 @@ class SpellBook(SignalMixin): def __len__(self) -> int: return len(self._spells) + def restore(self, spells: list[Spell]) -> None: + """Set the spells wholesale, as when importing a save.""" + self._spells = list(spells) + def add(self, spell_name: str, level: int) -> None: for spell in self._spells: if spell.name == spell_name: diff --git a/pqcli/pqw.py b/pqcli/pqw.py index d7e9c39..a5ea23c 100644 --- a/pqcli/pqw.py +++ b/pqcli/pqw.py @@ -241,8 +241,9 @@ def import_game( player.quest_book.plot_bar.reset(*_bar(game, "PlotBar")) player.quest_book.quest_bar.reset(*_bar(game, "QuestBar")) - player.quest_book._act = _int(game, "act") - player.quest_book._quests = [str(q) for q in _field(game, "Quests")] + player.quest_book.restore( + _int(game, "act"), [str(q) for q in _field(game, "Quests")] + ) player.quest_book.monster = _parse_monster( str(game.get("questmonster", "")) ) @@ -256,13 +257,13 @@ def import_game( rows = _field(game, "Inventory") if not rows or rows[0][0] != "Gold": raise PqwError("field Inventory does not start with Gold") - player.inventory._gold = _int(game, "Inventory", 0, 1) - player.inventory._items = [ - InventoryItem(str(row[0]), int(row[1])) for row in rows[1:] - ] + player.inventory.restore( + _int(game, "Inventory", 0, 1), + [InventoryItem(str(row[0]), int(row[1])) for row in rows[1:]], + ) player.inventory.encum_bar.reset(*_bar(game, "EncumBar")) - player.spell_book._spells = _parse_spells(game) + player.spell_book.restore(_parse_spells(game)) player.task = _parse_task(game) player.queue = [_parse_queue_entry(e) for e in _field(game, "queue")]