mirror of
https://github.com/rr-/pq-cli.git
synced 2026-11-12 22:40:36 -07:00
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:
2 files changed
+25
-7
No files matched your search
@@ -173,6 +173,11 @@ class QuestBook(SignalMixin):
|
|||||||
return self._quests[-1]
|
return self._quests[-1]
|
||||||
return None
|
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:
|
def increment_act(self) -> None:
|
||||||
self._act += 1
|
self._act += 1
|
||||||
self.emit("start_act", self._act)
|
self.emit("start_act", self._act)
|
||||||
@@ -210,6 +215,14 @@ class Inventory(SignalMixin):
|
|||||||
def __getitem__(self, idx: int) -> InventoryItem:
|
def __getitem__(self, idx: int) -> InventoryItem:
|
||||||
return self._items[idx]
|
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:
|
def add_gold(self, quantity: int) -> None:
|
||||||
logger.info(
|
logger.info(
|
||||||
"%s %s",
|
"%s %s",
|
||||||
@@ -294,6 +307,10 @@ class SpellBook(SignalMixin):
|
|||||||
def __len__(self) -> int:
|
def __len__(self) -> int:
|
||||||
return len(self._spells)
|
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:
|
def add(self, spell_name: str, level: int) -> None:
|
||||||
for spell in self._spells:
|
for spell in self._spells:
|
||||||
if spell.name == spell_name:
|
if spell.name == spell_name:
|
||||||
|
|||||||
+8
-7
@@ -241,8 +241,9 @@ def import_game(
|
|||||||
player.quest_book.plot_bar.reset(*_bar(game, "PlotBar"))
|
player.quest_book.plot_bar.reset(*_bar(game, "PlotBar"))
|
||||||
player.quest_book.quest_bar.reset(*_bar(game, "QuestBar"))
|
player.quest_book.quest_bar.reset(*_bar(game, "QuestBar"))
|
||||||
|
|
||||||
player.quest_book._act = _int(game, "act")
|
player.quest_book.restore(
|
||||||
player.quest_book._quests = [str(q) for q in _field(game, "Quests")]
|
_int(game, "act"), [str(q) for q in _field(game, "Quests")]
|
||||||
|
)
|
||||||
player.quest_book.monster = _parse_monster(
|
player.quest_book.monster = _parse_monster(
|
||||||
str(game.get("questmonster", ""))
|
str(game.get("questmonster", ""))
|
||||||
)
|
)
|
||||||
@@ -256,13 +257,13 @@ def import_game(
|
|||||||
rows = _field(game, "Inventory")
|
rows = _field(game, "Inventory")
|
||||||
if not rows or rows[0][0] != "Gold":
|
if not rows or rows[0][0] != "Gold":
|
||||||
raise PqwError("field Inventory does not start with Gold")
|
raise PqwError("field Inventory does not start with Gold")
|
||||||
player.inventory._gold = _int(game, "Inventory", 0, 1)
|
player.inventory.restore(
|
||||||
player.inventory._items = [
|
_int(game, "Inventory", 0, 1),
|
||||||
InventoryItem(str(row[0]), int(row[1])) for row in rows[1:]
|
[InventoryItem(str(row[0]), int(row[1])) for row in rows[1:]],
|
||||||
]
|
)
|
||||||
player.inventory.encum_bar.reset(*_bar(game, "EncumBar"))
|
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.task = _parse_task(game)
|
||||||
player.queue = [_parse_queue_entry(e) for e in _field(game, "queue")]
|
player.queue = [_parse_queue_entry(e) for e in _field(game, "queue")]
|
||||||
|
|||||||
Reference in new issue
Block a user