Compare commits

..

7 Commits

3 changed files with 119 additions and 89 deletions

View File

@ -1,10 +1,11 @@
{ {
"expansions": "expansions":
{ {
"ShockColar1": {"players": ["Brosef"], "tags": ["shock"], "config": {"COM_port": "COM3", "shocker_ID": 24770}}, "ShockColar1": {"players": ["Brosef"], "tags": ["shock"], "class": "serialShocker", "config": {"COM_port": "COM3", "shocker_ID": 24770}},
"RobotBarman": {"players": ["Tango", "TRS_MML"], "tags": ["drink"], "config": {}}, "RobotBarman": {"players": ["Tango", "TRS_MML"], "tags": ["drink"], "class": "bar", "config": {}},
"ChallengeDB": {"players": ["TRS_MML"], "tags": ["challenge"], "config": {}}, "ChallengeDB": {"players": ["TRS_MML"], "tags": ["challenge"], "class": "challenge", "config": {}},
"SourCandy": {"players": [], "tags": ["food"], "config": {}} "SourCandy": {"players": [], "tags": ["food"], "class": "candy", "config": {}},
"Simple": {"players": ["Brosef"], "tags": [], "class": "simplest", "config": {}}
}, },
"players": { "players": {
"Brosef": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "gamesSave": {"gameID0": 2}}, "Brosef": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "gamesSave": {"gameID0": 2}},

View File

@ -6,7 +6,7 @@ import copy
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from NoPELib.player_settings import ExpansionsManager from .player_settings import ExpansionsManager
class Hook: class Hook:
@ -39,28 +39,43 @@ class Expansion:
config: The config of the expansion. Can't be modified during execution. config: The config of the expansion. Can't be modified during execution.
Methods: Methods:
__call__: Execute a given action with the expansion
Subclass methods:
step: step:
reset: reset:
close: close:
Signals: Signals:
onError: midStepError:
""" """
def __init__(self, ID: str, expansionsManager: "ExpansionsManager"): def __init__(self, ID: str, expansionsManager: "ExpansionsManager"):
"""
Initialise the creation of an expansion and raise an error if not
available.
"""
self._ID = ID self._ID = ID
self._closed = False
self._manager = expansionsManager self._manager = expansionsManager
self.onError = Hook() self.midStepError = Hook()
self.midStepError.connect(expansionsManager._midStepError)
def __call__(self, action):
try:
assert not self._closed, "Can't use an expansion that has been closed."
return self.step(action)
except Exception as e:
step_info = {
"expansionID": self._ID,
"showOnScreen": None,
"error": e,
"done": False
}
self.midStepError(self._ID)
return step_info
@property @property
def config(self): def config(self):
""" """
The config of the expansion. 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"]) return copy.deepcopy(self._manager.config[self._ID]["config"])
@ -71,11 +86,11 @@ class Expansion:
@players.setter @players.setter
def players(self, newPlayers): def players(self, newPlayers):
self._manager.expansionPlayersChanged(self._ID, newPlayers) self._manager.expansionPlayersChange(self._ID, newPlayers)
@players.deleter @players.deleter
def players(self): def players(self):
self._manager.expansionPlayersChanged(self._ID, ()) self._manager.expansionPlayersChange(self._ID, ())
@property @property
def tags(self): def tags(self):
@ -115,7 +130,7 @@ class serialShocker(Expansion):
super().__init__(ID, expansionsManager) super().__init__(ID, expansionsManager)
if self.config["COM_port"] not in serialShocker._api: if self.config["COM_port"] not in serialShocker._api:
self._assignApi() self._assignApi()
self.shocker = serialShocker._api[self.config["COM_port"]].shocker(["shocker_ID"]) self.shocker = serialShocker._api[self.config["COM_port"]].shocker(self.config["shocker_ID"])
def _assignApi(self): def _assignApi(self):
from pishock import SerialAPI from pishock import SerialAPI
@ -129,22 +144,16 @@ class serialShocker(Expansion):
a positive float that defines the duration a positive float that defines the duration
a float within the range [0.0, 1.0] that defines the intensity a float within the range [0.0, 1.0] that defines the intensity
""" """
# Execute the step
vibrateInstead, duration, intensity = action vibrateInstead, duration, intensity = action
callFunc = self.shocker.vibrate if vibrateInstead else self.shocker.shock callFunc = self.shocker.vibrate if vibrateInstead else self.shocker.shock
# Try to execute the step callFunc(duration=duration, intensity=intensity)
try:
callFunc(duration=duration, intensity=intensity)
error = None
done = True
except Exception as e:
error = e
done = False
# Return additionnal info # Return additionnal info
step_info = { step_info = {
"expansionID": self._ID, "expansionID": self._ID,
"showOnScreen": None, "showOnScreen": None,
"error": error, "error": None,
"done": done "done": True
} }
return step_info return step_info
@ -172,24 +181,18 @@ class simplest(Expansion):
a positive float that defines the duration a positive float that defines the duration
a float within the range [0.0, 1.0] that defines the intensity a float within the range [0.0, 1.0] that defines the intensity
""" """
# Execute the step
vibrateInstead, duration, intensity = action vibrateInstead, duration, intensity = action
values = f"duration={duration}, intensity={intensity}" values = f"duration={duration}, intensity={intensity}"
interact_type = "Vibrate" if vibrateInstead else "Shock" interact_type = "Vibrate" if vibrateInstead else "Shock"
# Try to execute the step message = f"{interact_type} with {values}"
try: print(message)
message = f"{interact_type} with {values}"
error = None
done = True
except Exception as e:
message = None
error = e
done = False
# Return additionnal info # Return additionnal info
step_info = { step_info = {
"expansionID": self._ID, "expansionID": self._ID,
"showOnScreen": message, "showOnScreen": message,
"error": error, "error": None,
"done": done "done": True
} }
return step_info return step_info

View File

@ -6,7 +6,7 @@ import copy
import json import json
import logging import logging
from pathlib import Path from pathlib import Path
import NoPELib.expansionsLib as expansionsLib from . import expansionsLib
_log = logging.getLogger('NoPE-Lib') _log = logging.getLogger('NoPE-Lib')
@ -233,21 +233,20 @@ class ExpansionsManager:
Manager of the availability of the expansions. Manager of the availability of the expansions.
Attributes: Attributes:
includedExpansions: tuple of str includeExpansions: tuple of str
Container of the expansions to try making available Container of the expansions to try making available
activeExpansions: dict of Expansion activeExpansions: dict of Expansion
Container of the relevent expansions Container of the relevent expansions
Methods: Methods:
expansionPlayersChanged expansionPlayersChange: Change the assigned players of an expansion
lookupPlayer: Provide a list of expansion available to a player
""" """
defaultExpansionConfig = {"players": (), "types": ()} defaultExpansionConfig = {"players": (), "types": ()}
keysConvert2Tuple = ("players", "types") keysConvert2Tuple = ("players", "tags")
tags = ["shock", "spice", "sour", "drink", "challenge"] tags = ["shock", "spice", "sour", "drink", "challenge"]
# (attributed players / types)
# TODO Allow to get a list of expansions with a specific tag/player
def __init__(self, playersManager: PlayersManager, includedExpansions: tuple[str]=None): def __init__(self, playersManager: PlayersManager, includeExpansions: tuple[str]=None):
""" """
Arguments: Arguments:
tryInclude: bool=False, tryActivate: bool=False tryInclude: bool=False, tryActivate: bool=False
@ -256,19 +255,23 @@ class ExpansionsManager:
self.playersManager = playersManager self.playersManager = playersManager
self._playersLookUp = {} self._playersLookUp = {}
self._cfg = playersManager.config["expansions"] self._cfg = playersManager.config["expansions"]
if includedExpansions is not None: if includeExpansions is not None:
self._includedExpansions = tuple(includedExpansions) self._includeExpansions = tuple(includeExpansions)
else: else:
self._includedExpansions = () self._includeExpansions = ()
# Convert the required lists into tuples # Convert the required lists into tuples
for expansionID in self._cfg["expansions"]: for expansionID in self._cfg:
for key in self.keysConvert2Tuple: for key in self.keysConvert2Tuple:
converted = tuple(self._cfg["expansions"][expansionID][key]) converted = tuple(self._cfg[expansionID][key])
self._cfg["expansions"][expansionID][key] = converted self._cfg[expansionID][key] = converted
# Compute the active expansions # Compute the active expansions
self._activeExpansions = {} self._activeExpansions = {}
for expansionID in self._listPossiblyValidExpansions(): for expansionID in self._listPossiblyValidExpansions():
self._createExpansion(expansionID) creation_out = self._createExpansion(expansionID)
if isinstance(creation_out, expansionsLib.Expansion):
self._activeExpansions[expansionID] = creation_out
else:
raise creation_out
# Connect the signals to the slots # Connect the signals to the slots
playersManager.onMadePlayerActive.connect(self._activePlayerAdded) playersManager.onMadePlayerActive.connect(self._activePlayerAdded)
@ -287,18 +290,18 @@ class ExpansionsManager:
return f"ExpansionsManager has {len(self._activeExpansions)} active expansions" return f"ExpansionsManager has {len(self._activeExpansions)} active expansions"
@property @property
def includedExpansions(self): def includeExpansions(self):
""" Dictionnary of the included expansions. """ """ Dictionnary of the included expansions. """
return self._includedExpansions return self._includeExpansions
@includedExpansions.setter @includeExpansions.setter
def includedExpansions(self, newIncluded: tuple[str]): def includeExpansions(self, newIncluded: tuple[str]):
self._includedExpansions = tuple(newIncluded) self._includeExpansions = tuple(newIncluded)
self._includeChanged() self._includeChanged()
@includedExpansions.deleter @includeExpansions.deleter
def includedExpansions(self): def includeExpansions(self):
self._includedExpansions = () self._includeExpansions = ()
@property @property
def config(self): def config(self):
@ -306,7 +309,10 @@ class ExpansionsManager:
return self._cfg return self._cfg
def _getPlayersFillMissing(self, expansionID): def _getPlayersFillMissing(self, expansionID):
return set(self._cfg["expansions"].get(expansionID, {}).get("players", [])) return set(self._cfg.get(expansionID, {}).get("players", []))
def _getClassFillMissing(self, expansionID):
return self._cfg.get(expansionID, {}).get("class", "")
def _listPossiblyValidExpansions(self): def _listPossiblyValidExpansions(self):
""" """
@ -315,20 +321,28 @@ class ExpansionsManager:
""" """
activePlayers = set(self.playersManager.keys()) activePlayers = set(self.playersManager.keys())
possiblyValidExpansions = [ possiblyValidExpansions = [
expansionID for expansionID in self._includedExpansions expansionID for expansionID in self._includeExpansions
if hasattr(expansionsLib, expansionID) and if hasattr(expansionsLib, self._getClassFillMissing(expansionID)) and
not self._getPlayersFillMissing(expansionID).isdisjoint(activePlayers) not self._getPlayersFillMissing(expansionID).isdisjoint(activePlayers)
] ]
return possiblyValidExpansions return possiblyValidExpansions
def _createExpansion(self, expansionID): def _createExpansion(self, expansionID):
terminalErrors = SystemExit, KeyboardInterrupt, GeneratorExit
try: try:
# Create the expansion # Create the expansion
expansion = getattr(expansionsLib, expansionID)(expansionID, self) expansionClass = self._getClassFillMissing(expansionID)
class_to_create = getattr(expansionsLib, expansionClass)
expansion = class_to_create(expansionID, self)
# Update the lookup # Update the lookup
for player in self._getPlayersFillMissing(expansionID): for player in self._getPlayersFillMissing(expansionID):
self._playersLookUp[player].append(expansionID) if player in self._playersLookUp:
self._playersLookUp[player].append(expansionID)
else:
self._playersLookUp[player] = [expansionID]
return expansion return expansion
except terminalErrors as e:
raise e
except Exception as e: except Exception as e:
return e return e
@ -339,7 +353,6 @@ class ExpansionsManager:
# Remove the expansion # Remove the expansion
expansion = self._activeExpansions.pop(expansionID) expansion = self._activeExpansions.pop(expansionID)
expansion.close() expansion.close()
del expansion
def _includeChanged(self): def _includeChanged(self):
""" """
@ -348,7 +361,7 @@ class ExpansionsManager:
# Compute the expansions involved in the modification # Compute the expansions involved in the modification
possiblyValidExpansions = set(self._listPossiblyValidExpansions()) possiblyValidExpansions = set(self._listPossiblyValidExpansions())
previousExpansions = set(self._includedExpansions) previousExpansions = set(self._includeExpansions)
# Remove irrelevant expansions # Remove irrelevant expansions
expansionsToRemove = previousExpansions.difference(possiblyValidExpansions) expansionsToRemove = previousExpansions.difference(possiblyValidExpansions)
for expansionID in expansionsToRemove: for expansionID in expansionsToRemove:
@ -360,14 +373,14 @@ class ExpansionsManager:
if isinstance(creation_out, expansionsLib.Expansion): if isinstance(creation_out, expansionsLib.Expansion):
self._activeExpansions[expansionID] = creation_out self._activeExpansions[expansionID] = creation_out
else: else:
print(creation_out) raise creation_out
def _activePlayerAdded(self, playerName): def _activePlayerAdded(self, playerName):
# Find the expansions that are to be created # Find the expansions that are to be created
possibleAddition = set(self._includedExpansions).difference(self._activeExpansions) possibleAddition = set(self._includeExpansions).difference(self._activeExpansions)
filteredAddition = [ filteredAddition = [
expansionID for expansionID in possibleAddition expansionID for expansionID in possibleAddition
if hasattr(expansionsLib, expansionID) and if hasattr(expansionsLib, self._getClassFillMissing(expansionID)) and
playerName in self._getPlayersFillMissing(expansionID) playerName in self._getPlayersFillMissing(expansionID)
] ]
# Create the expansions # Create the expansions
@ -376,7 +389,7 @@ class ExpansionsManager:
if isinstance(creation_out, expansionsLib.Expansion): if isinstance(creation_out, expansionsLib.Expansion):
self._activeExpansions[expansionID] = creation_out self._activeExpansions[expansionID] = creation_out
else: else:
print(creation_out) raise creation_out
def _activePlayerRemoved(self, playerName): def _activePlayerRemoved(self, playerName):
# Find the expansions that are to be removed # Find the expansions that are to be removed
@ -388,10 +401,10 @@ class ExpansionsManager:
for expansionID in filteredRemoval: for expansionID in filteredRemoval:
self._removeExpansion(expansionID) self._removeExpansion(expansionID)
def _errorOccured(self, expansionID): def _midStepError(self, expansionID):
self._removeExpansion(expansionID) self._removeExpansion(expansionID)
def expansionPlayersChanged(self, expansionID, newPlayers): def expansionPlayersChange(self, expansionID, newPlayers):
""" """
Method that allows changing the assigned players of an expansion. Method that allows changing the assigned players of an expansion.
@ -406,7 +419,7 @@ class ExpansionsManager:
expansionIsActive = expansionID in self._activeExpansions expansionIsActive = expansionID in self._activeExpansions
activePlayers = set(self.playersManager.keys()) activePlayers = set(self.playersManager.keys())
expansionHasNoPlayers = self._getPlayersFillMissing(expansionID).isdisjoint(activePlayers) expansionHasNoPlayers = self._getPlayersFillMissing(expansionID).isdisjoint(activePlayers)
expansionDefined = hasattr(expansionsLib, expansionID) expansionDefined = hasattr(expansionsLib, self._getClassFillMissing(expansionID))
# Update the activation status # Update the activation status
activeChanged = False activeChanged = False
if expansionIsActive and expansionHasNoPlayers: if expansionIsActive and expansionHasNoPlayers:
@ -417,8 +430,8 @@ class ExpansionsManager:
if isinstance(creation_out, expansionsLib.Expansion): if isinstance(creation_out, expansionsLib.Expansion):
self._activeExpansions[expansionID] = creation_out self._activeExpansions[expansionID] = creation_out
activeChanged = True activeChanged = True
elif isinstance(creation_out, Exception): else:
print(creation_out) raise creation_out
# Update the inverse lookup # Update the inverse lookup
if not activeChanged: if not activeChanged:
removedPlayers = set(previousPlayers).difference(newPlayers) removedPlayers = set(previousPlayers).difference(newPlayers)
@ -439,21 +452,34 @@ class ExpansionsManager:
expansionsID (tuple of str): ID of the expansions available to the expansionsID (tuple of str): ID of the expansions available to the
player player
""" """
return tuple(self._playersLookUp[playerName]) return tuple(self._playersLookUp.get(playerName, []))
if __name__ == "__main__": if __name__ == "__main__":
# Test run to make sure nothing is flagrantly flawed # Test run to make sure nothing is flagrantly flawed
configPath = Path(__file__).parent / "players.json" configPath = Path(__file__).parent.parent.parent / "players.json"
pm = PlayersManager(playersPath=configPath, gameID="gameID0") pm = PlayersManager(gameID="gameID0", activePlayers=["Brosef"], playersPath=configPath)
# Iteration # Iteration
for name in pm: for name in pm:
print(name) print(name)
# Modification of a players data # Modification of a players data
brosef = pm["Brosef"] brosef = pm["Brosef"]
brosef.gameSave = [1] brosef.gameSave = [1]
print(pm["Brosef"].gameSave)
brosef.state = ["alive"] brosef.state = ["alive"]
# TODO Verify that changing the gameID works as intended
# TODO Test out the expansion manager # Test run for the expansionsManager
# Implement a way to search within expansions # Create the managers
configPath = Path(__file__).parent.parent.parent / "players.json"
pm = PlayersManager(gameID="gameID0", activePlayers=["Brosef"], playersPath=configPath)
em = ExpansionsManager(pm, includeExpansions=["Simple", "SourCandy"])
# Use the expansion manager to interact with a player using the expansion "Simple"
brosefAvailableExpansionsID = em.lookupPlayer("Brosef")
if "Simple" in brosefAvailableExpansionsID:
# Define some random action
vibrateInstead, duration, intensity = False, 1.0, 100
action = vibrateInstead, duration, intensity
# Perform the action
expansionObject = em["Simple"]
return_infos = expansionObject(action)
assert return_infos["done"], "The step was not successfully completed"