Compare commits
3 Commits
f2e247caac
...
dd72d14ef0
| Author | SHA1 | Date | |
|---|---|---|---|
| dd72d14ef0 | |||
| 9bb383e03c | |||
| 6b3f94bd68 |
@ -5,6 +5,7 @@ Needs to handle modifying number of players
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import abc
|
||||||
import copy
|
import copy
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@ -16,7 +17,8 @@ _log = logging.getLogger('NoPE-Lib')
|
|||||||
class PlayersManager:
|
class PlayersManager:
|
||||||
"""
|
"""
|
||||||
Manager of players for a given game.
|
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:
|
Attributes:
|
||||||
gameID (str): The gameID of the active game
|
gameID (str): The gameID of the active game
|
||||||
@ -26,7 +28,7 @@ class PlayersManager:
|
|||||||
"""
|
"""
|
||||||
defaultPlayerConfig = {"flags": [], "expansions": {}, "games": {}}
|
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.
|
Initialises a list of players.
|
||||||
|
|
||||||
@ -114,7 +116,7 @@ class PlayersManager:
|
|||||||
def gameID(self):
|
def gameID(self):
|
||||||
self._currentGameID = None
|
self._currentGameID = None
|
||||||
self._player_data = {name: Player(name, self, **cfg) for name, cfg in self._cfg["players"].items()}
|
self._player_data = {name: Player(name, self, **cfg) for name, cfg in self._cfg["players"].items()}
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
""" Iterator of the active players' names """
|
""" Iterator of the active players' names """
|
||||||
return self._player_data.keys()
|
return self._player_data.keys()
|
||||||
@ -131,28 +133,16 @@ class PlayersManager:
|
|||||||
with open(self._playersPath, 'w') as f:
|
with open(self._playersPath, 'w') as f:
|
||||||
self._cfg = json.dump(f, self._cfg)
|
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:
|
class Player:
|
||||||
"""
|
"""
|
||||||
The game settings are not guaranteed to have data in it.
|
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
flags:
|
flags:
|
||||||
expansions:
|
availableExpansions:
|
||||||
gameSave:
|
expansionsConfig:
|
||||||
|
gameSave: The game settings are not guaranteed to have data in it.
|
||||||
|
gameState:
|
||||||
|
|
||||||
Methods:
|
Methods:
|
||||||
generateConfig:
|
generateConfig:
|
||||||
@ -181,12 +171,12 @@ class Player:
|
|||||||
self._manager[self._name] = self.generateConfig()
|
self._manager[self._name] = self.generateConfig()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def expansions(self):
|
def expansionsConfig(self):
|
||||||
return self._expansions
|
return self._expansionsConfig
|
||||||
|
|
||||||
@expansions.setter
|
@expansionsConfig.setter
|
||||||
def expansions(self, newExpansions):
|
def expansions(self, newExpansions):
|
||||||
self._expansions = newExpansions
|
self._expansionsConfig = newExpansions
|
||||||
# The container was changed and must be transmited to the manager
|
# The container was changed and must be transmited to the manager
|
||||||
self._manager[self._name] = self.generateConfig()
|
self._manager[self._name] = self.generateConfig()
|
||||||
|
|
||||||
@ -201,6 +191,110 @@ class Player:
|
|||||||
|
|
||||||
def generateConfig(self):
|
def generateConfig(self):
|
||||||
return {"flags": self._flags, "expansions": self._expansions, "games": self._games}
|
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__":
|
if __name__ == "__main__":
|
||||||
@ -208,9 +302,10 @@ if __name__ == "__main__":
|
|||||||
configPath = Path(__file__).parent / "players.json"
|
configPath = Path(__file__).parent / "players.json"
|
||||||
manager = PlayersManager(playersPath=configPath, gameID="gameID0")
|
manager = PlayersManager(playersPath=configPath, gameID="gameID0")
|
||||||
# Iteration
|
# Iteration
|
||||||
for name in manager:
|
for playerName in manager:
|
||||||
print(name)
|
print(playerName)
|
||||||
# Modification of a players data
|
# Modification of a players data
|
||||||
brosef = manager["Brosef"]
|
brosef = manager["Brosef"]
|
||||||
brosef.gameSave = [1]
|
brosef.gameSave = [1]
|
||||||
print(manager["Brosef"].gameSave)
|
print(manager["Brosef"].gameSave)
|
||||||
|
brosef.state = ["alive"]
|
||||||
@ -1,16 +1,11 @@
|
|||||||
{
|
{
|
||||||
"exampleExpansion": {
|
"expansions":
|
||||||
"optionA": 1,
|
|
||||||
"optionB": 2
|
|
||||||
},
|
|
||||||
|
|
||||||
"availableExpansion":
|
|
||||||
{
|
{
|
||||||
"bracelet1": "Brosef",
|
"shockColar1": {"players": ["Brosef"], "class": "pishock", "type": ["shock"]},
|
||||||
"robotic_barman": "Tango",
|
"robotBarman": {"players": ["Tango", "TRS_MML"], "class": "roboticBarman", "type": ["drink"]},
|
||||||
"challenge": "TRS"
|
"challengeDB": {"players": ["TRS_MML"], "class": "challengeDataBase", "type": ["challenge"]},
|
||||||
|
"sourCandy": {"players": [], "class": "sourCandy", "type": ["food"]}
|
||||||
},
|
},
|
||||||
|
|
||||||
"players": {
|
"players": {
|
||||||
"Brosef": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": 2}},
|
"Brosef": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": 2}},
|
||||||
"TRS_MML": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": 1}},
|
"TRS_MML": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": 1}},
|
||||||
|
|||||||
Reference in New Issue
Block a user