Fixed some bugs

This commit is contained in:
2025-07-08 16:09:33 -04:00
parent bb2b953b04
commit 63f44aacec
3 changed files with 55 additions and 41 deletions

View File

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

View File

@ -55,12 +55,12 @@ class Expansion:
self._closed = False
self._manager = expansionsManager
self.midStepError = Hook()
self.midStepError.connect(expansionsManager._midStepError(ID))
self.midStepError.connect(expansionsManager._midStepError)
def __call__(self, action):
try:
assert not self._closed, "Can't use an expansion that has been closed."
step_info = self.step(action)
return self.step(action)
except Exception as e:
step_info = {
"expansionID": self._ID,
@ -68,7 +68,7 @@ class Expansion:
"error": e,
"done": False
}
self.midStepError()
self.midStepError(self._ID)
return step_info
@property
@ -181,24 +181,18 @@ class simplest(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
values = f"duration={duration}, intensity={intensity}"
interact_type = "Vibrate" if vibrateInstead else "Shock"
# Try to execute the step
try:
message = f"{interact_type} with {values}"
error = None
done = True
except Exception as e:
message = None
error = e
done = False
message = f"{interact_type} with {values}"
print(message)
# Return additionnal info
step_info = {
"expansionID": self._ID,
"showOnScreen": message,
"error": error,
"done": done
"error": None,
"done": True
}
return step_info

View File

@ -6,7 +6,7 @@ import copy
import json
import logging
from pathlib import Path
import NoPELib.expansionsLib as expansionsLib
import expansionsLib
_log = logging.getLogger('NoPE-Lib')
@ -243,10 +243,8 @@ class ExpansionsManager:
lookupPlayer: Provide a list of expansion available to a player
"""
defaultExpansionConfig = {"players": (), "types": ()}
keysConvert2Tuple = ("players", "types")
keysConvert2Tuple = ("players", "tags")
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, includeExpansions: tuple[str]=None):
"""
@ -262,14 +260,18 @@ class ExpansionsManager:
else:
self._includeExpansions = ()
# Convert the required lists into tuples
for expansionID in self._cfg["expansions"]:
for expansionID in self._cfg:
for key in self.keysConvert2Tuple:
converted = tuple(self._cfg["expansions"][expansionID][key])
self._cfg["expansions"][expansionID][key] = converted
converted = tuple(self._cfg[expansionID][key])
self._cfg[expansionID][key] = converted
# Compute the active expansions
self._activeExpansions = {}
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
playersManager.onMadePlayerActive.connect(self._activePlayerAdded)
@ -307,10 +309,10 @@ class ExpansionsManager:
return self._cfg
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["expansions"].get(expansionID, {}).get("class", "")
return self._cfg.get(expansionID, {}).get("class", "")
def _listPossiblyValidExpansions(self):
"""
@ -329,10 +331,14 @@ class ExpansionsManager:
try:
# Create the expansion
expansionClass = self._getClassFillMissing(expansionID)
expansion = getattr(expansionsLib, expansionClass)(expansionID, self)
class_to_create = getattr(expansionsLib, expansionClass)
expansion = class_to_create(expansionID, self)
# Update the lookup
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
except Exception as e:
return e
@ -364,7 +370,7 @@ class ExpansionsManager:
if isinstance(creation_out, expansionsLib.Expansion):
self._activeExpansions[expansionID] = creation_out
else:
print(creation_out)
raise creation_out
def _activePlayerAdded(self, playerName):
# Find the expansions that are to be created
@ -380,7 +386,7 @@ class ExpansionsManager:
if isinstance(creation_out, expansionsLib.Expansion):
self._activeExpansions[expansionID] = creation_out
else:
print(creation_out)
raise creation_out
def _activePlayerRemoved(self, playerName):
# Find the expansions that are to be removed
@ -421,8 +427,8 @@ class ExpansionsManager:
if isinstance(creation_out, expansionsLib.Expansion):
self._activeExpansions[expansionID] = creation_out
activeChanged = True
elif isinstance(creation_out, Exception):
print(creation_out)
else:
raise creation_out
# Update the inverse lookup
if not activeChanged:
removedPlayers = set(previousPlayers).difference(newPlayers)
@ -443,21 +449,34 @@ class ExpansionsManager:
expansionsID (tuple of str): ID of the expansions available to the
player
"""
return tuple(self._playersLookUp[playerName])
return tuple(self._playersLookUp.get(playerName, []))
if __name__ == "__main__":
# Test run to make sure nothing is flagrantly flawed
configPath = Path(__file__).parent / "players.json"
pm = PlayersManager(playersPath=configPath, gameID="gameID0")
configPath = Path(__file__).parent.parent.parent / "players.json"
pm = PlayersManager(gameID="gameID0", activePlayers=["Brosef"], playersPath=configPath)
# Iteration
for name in pm:
print(name)
# Modification of a players data
brosef = pm["Brosef"]
brosef.gameSave = [1]
print(pm["Brosef"].gameSave)
brosef.state = ["alive"]
# TODO Verify that changing the gameID works as intended
# TODO Test out the expansion manager
# Implement a way to search within expansions
# Test run for the expansionsManager
# 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"