Compare commits

...

3 Commits

Author SHA1 Message Date
dd72d14ef0 Merge branch 'main' of https://git.personal.imadumbass.dog/Brosef/NoPELib 2025-06-20 16:49:10 -04:00
9bb383e03c Sync 2025-06-20 16:44:18 -04:00
6b3f94bd68 Made an outline of ExpansionManager and Expansion 2025-06-20 15:52:05 -04:00
2 changed files with 125 additions and 35 deletions

View File

@ -5,6 +5,7 @@ Needs to handle modifying number of players
"""
import abc
import copy
import json
import logging
@ -16,7 +17,8 @@ _log = logging.getLogger('NoPE-Lib')
class PlayersManager:
"""
Manager of players for a given game.
This class implements most methods available to classic dict.
Since this class implements most methods available to classic dict,
you can think of this class as a python dict.
Attributes:
gameID (str): The gameID of the active game
@ -26,7 +28,7 @@ class PlayersManager:
"""
defaultPlayerConfig = {"flags": [], "expansions": {}, "games": {}}
def __init__(self, gameID: str=None, activePlayers: list[str]=None, playersPath: str='./players.json', loggerID: str='PlayersManager'):
def __init__(self, gameID: str=None, activePlayers: list[str]=None, playersPath: str='./players.json', loggerID: str='PlayersManager', includedExpansions: tuple[str]=None):
"""
Initialises a list of players.
@ -114,7 +116,7 @@ class PlayersManager:
def gameID(self):
self._currentGameID = None
self._player_data = {name: Player(name, self, **cfg) for name, cfg in self._cfg["players"].items()}
def keys(self):
""" Iterator of the active players' names """
return self._player_data.keys()
@ -131,28 +133,16 @@ class PlayersManager:
with open(self._playersPath, 'w') as f:
self._cfg = json.dump(f, self._cfg)
def addExpansion(self, expansion):
"""
Adds an expansion, used by things like PDO-Lib.
Args:
expansion (Expansion): The expansion to add.
"""
expID = expansion.__class__.ID
_log.debug(f'Adding expansion {expID}...')
expansion = expansion(self._cfg.get(expID))
class Player:
"""
The game settings are not guaranteed to have data in it.
Attributes:
flags:
expansions:
gameSave:
availableExpansions:
expansionsConfig:
gameSave: The game settings are not guaranteed to have data in it.
gameState:
Methods:
generateConfig:
@ -181,12 +171,12 @@ class Player:
self._manager[self._name] = self.generateConfig()
@property
def expansions(self):
return self._expansions
def expansionsConfig(self):
return self._expansionsConfig
@expansions.setter
@expansionsConfig.setter
def expansions(self, newExpansions):
self._expansions = newExpansions
self._expansionsConfig = newExpansions
# The container was changed and must be transmited to the manager
self._manager[self._name] = self.generateConfig()
@ -201,6 +191,110 @@ class Player:
def generateConfig(self):
return {"flags": self._flags, "expansions": self._expansions, "games": self._games}
def punish(self, value, preferedExpansion: str=None):
do_something = lambda value: value
additionalInfos = {"expansionID": "challengeDB", "balancedValue": 0.2, "showOnScreen": "Do 100 push-up", "error": None, "done": False}
# NOTE Make a result class instead and an error class
additionalInfos = do_something(value)
return additionalInfos
class Expansion:
"""
Attributes:
Methods:
step:
reset:
close:
"""
@abc.abstractmethod
def __init__(self):
"""
Raise an error if not available
"""
pass
@abc.abstractmethod
def step(self, action):
"""
Call close if an error is thrown
"""
pass
@abc.abstractmethod
def reset(self):
pass
@abc.abstractmethod
def close(self):
pass
class ExpansionManager:
"""
Manager of the availability of the expansions.
Attributes:
includedExpansions: tuple of str
Container of the expansions to try making available
releventExpansions: dict of Expansion
Container of the relevent expansions
"""
tags = ["shock", "spice", "sour", "drink", "challenge"]
def __init__(self, playersManager: PlayersManager, includedExpansions: tuple[str]=None):
self.playersManager = playersManager
self._includedExpansions = tuple(includedExpansions) if includedExpansions is not None else ()
self._releventExpansions = {} # TODO Populate the dictionnary
@property
def includedExpansions(self):
return self._includedExpansions
@includedExpansions.setter
def includedExpansions(self, newIncluded: tuple[str]):
self._includedExpansions = tuple(newIncluded)
self._includedChanged()
@includedExpansions.deleter
def includedExpansions(self):
self._includedExpansions = ()
@property
def releventExpansions(self):
return self._releventExpansions
def _includedChanged(self):
"""
Compute the list of only the relevant expansions.
The expansions that do not exists are excluded.
The expansions that have no players assigned to them are excluded.
The expansions that are not responding are excluded.
"""
pass
def _activePlayersChanged(self, activatePlayers):
pass
def _errorOccured(self, expansionID):
pass
def _expansionPlayersChanged(self, expansionID):
pass
def resetExpansion(self, expansion: Expansion, suppressError: bool=False):
"""
If the expansion was relevent, close it.
In any case, try creating a new
"""
# If the expansion was relevent, close it
# Try creating a new instance of the expansion
# Update the list of relevent expansions
pass
if __name__ == "__main__":
@ -208,9 +302,10 @@ if __name__ == "__main__":
configPath = Path(__file__).parent / "players.json"
manager = PlayersManager(playersPath=configPath, gameID="gameID0")
# Iteration
for name in manager:
print(name)
for playerName in manager:
print(playerName)
# Modification of a players data
brosef = manager["Brosef"]
brosef.gameSave = [1]
print(manager["Brosef"].gameSave)
brosef.state = ["alive"]

View File

@ -1,16 +1,11 @@
{
"exampleExpansion": {
"optionA": 1,
"optionB": 2
},
"availableExpansion":
"expansions":
{
"bracelet1": "Brosef",
"robotic_barman": "Tango",
"challenge": "TRS"
"shockColar1": {"players": ["Brosef"], "class": "pishock", "type": ["shock"]},
"robotBarman": {"players": ["Tango", "TRS_MML"], "class": "roboticBarman", "type": ["drink"]},
"challengeDB": {"players": ["TRS_MML"], "class": "challengeDataBase", "type": ["challenge"]},
"sourCandy": {"players": [], "class": "sourCandy", "type": ["food"]}
},
"players": {
"Brosef": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": 2}},
"TRS_MML": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": 1}},