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.
This commit is contained in:
Dash committed 2026-10-17 19:14:46 +02:00
1 parent 7573807808
commit 2fef41ca5e
2 files changed
+25 -7

No files matched your search

+17
View File
@@ -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:
+8 -7
View File
@@ -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")]