From 81f38a804471f40e6b4dcc557256295d81f9cddb Mon Sep 17 00:00:00 2001 From: oclaim Date: Tue, 8 Jul 2025 14:22:58 -0400 Subject: [PATCH] Ensured that an error midstep would be catched by expansionsManager --- src/NoPELib/expansionsLib.py | 49 ++++++++++++++++++++-------------- src/NoPELib/player_settings.py | 8 +++--- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/NoPELib/expansionsLib.py b/src/NoPELib/expansionsLib.py index 30fb68f..7bee9f4 100644 --- a/src/NoPELib/expansionsLib.py +++ b/src/NoPELib/expansionsLib.py @@ -37,30 +37,45 @@ class Expansion: players (tuple of str): tags (tuple of flag): config: The config of the expansion. Can't be modified during execution. - + Methods: + __call__: Execute a given action with the expansion + + Subclass methods: step: reset: close: Signals: - onError: + midStepError: """ def __init__(self, ID: str, expansionsManager: "ExpansionsManager"): - """ - Initialise the creation of an expansion and raise an error if not - available. - """ self._ID = ID + self._closed = False self._manager = expansionsManager - self.onError = Hook() + self.midStepError = Hook() + self.midStepError.connect(expansionsManager._midStepError(ID)) + + def __call__(self, action): + try: + assert not self._closed, "Can't use an expansion that has been closed." + step_info = self.step(action) + except Exception as e: + step_info = { + "expansionID": self._ID, + "showOnScreen": None, + "error": e, + "done": False + } + self.midStepError() + return step_info @property def config(self): """ The config of the expansion. - NOTE Can't be modified during execution. + Can't be modified during execution. """ return copy.deepcopy(self._manager.config[self._ID]["config"]) @@ -71,11 +86,11 @@ class Expansion: @players.setter def players(self, newPlayers): - self._manager.expansionPlayersChanged(self._ID, newPlayers) + self._manager.expansionPlayersChange(self._ID, newPlayers) @players.deleter def players(self): - self._manager.expansionPlayersChanged(self._ID, ()) + self._manager.expansionPlayersChange(self._ID, ()) @property def tags(self): @@ -129,22 +144,16 @@ class serialShocker(Expansion): a positive float that defines the duration a float within the range [0.0, 1.0] that defines the intensity """ + # Execute the step vibrateInstead, duration, intensity = action callFunc = self.shocker.vibrate if vibrateInstead else self.shocker.shock - # Try to execute the step - try: - callFunc(duration=duration, intensity=intensity) - error = None - done = True - except Exception as e: - error = e - done = False + callFunc(duration=duration, intensity=intensity) # Return additionnal info step_info = { "expansionID": self._ID, "showOnScreen": None, - "error": error, - "done": done + "error": None, + "done": True } return step_info diff --git a/src/NoPELib/player_settings.py b/src/NoPELib/player_settings.py index ab4d8fb..1d4c23c 100644 --- a/src/NoPELib/player_settings.py +++ b/src/NoPELib/player_settings.py @@ -239,7 +239,8 @@ class ExpansionsManager: Container of the relevent expansions Methods: - expansionPlayersChanged + expansionPlayersChange: Change the assigned players of an expansion + lookupPlayer: Provide a list of expansion available to a player """ defaultExpansionConfig = {"players": (), "types": ()} keysConvert2Tuple = ("players", "types") @@ -339,7 +340,6 @@ class ExpansionsManager: # Remove the expansion expansion = self._activeExpansions.pop(expansionID) expansion.close() - del expansion def _includeChanged(self): """ @@ -388,10 +388,10 @@ class ExpansionsManager: for expansionID in filteredRemoval: self._removeExpansion(expansionID) - def _errorOccured(self, expansionID): + def _midStepError(self, expansionID): self._removeExpansion(expansionID) - def expansionPlayersChanged(self, expansionID, newPlayers): + def expansionPlayersChange(self, expansionID, newPlayers): """ Method that allows changing the assigned players of an expansion.