Ensured that an error midstep would be catched by expansionsManager

This commit is contained in:
2025-07-08 14:22:58 -04:00
parent ed7cdd21c1
commit 81f38a8044
2 changed files with 33 additions and 24 deletions

View File

@ -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

View File

@ -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.