Added an initial (untested) implementation of the PlayersManager
This commit is contained in:
@ -1,3 +1 @@
|
||||
import logging
|
||||
|
||||
_log = logging.getLogger('NoPE-Lib')
|
||||
pass
|
||||
@ -1,46 +1,3 @@
|
||||
import json
|
||||
|
||||
class Players:
|
||||
def __init__(self, playersPath: str='./players.json', loggerID: str='Players'):
|
||||
"""
|
||||
Initialises a list of players.
|
||||
|
||||
Args:
|
||||
playersPath (str, optional): The path of the players.json file. Defaults to './players.json'.
|
||||
loggerID (str, optional): The ID used for logging. Defaults to 'Players'.
|
||||
"""
|
||||
self._log = _log.getChild(loggerID)
|
||||
|
||||
with open(playersPath, 'r') as f:
|
||||
self._cfg = json.loads(f.read())
|
||||
|
||||
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))
|
||||
|
||||
def loadPlayers(self):
|
||||
"""
|
||||
Actually loads all the players as objects.
|
||||
|
||||
The reason this isn't done in __init__ is
|
||||
simply because no expansions have been loaded
|
||||
yet.
|
||||
"""
|
||||
pass
|
||||
|
||||
class Player:
|
||||
def __init__(self, name, flags: list[str] = None):
|
||||
self.name = name
|
||||
self.flags = flags if flags != None else []
|
||||
|
||||
class Expansion:
|
||||
def __init__(self, parent, globalConfig):
|
||||
setattr(parent, self.__class__.ID, self)
|
||||
@ -48,3 +5,14 @@ class Expansion:
|
||||
class PlayerExpansion:
|
||||
def __init__(self, player, localConfig):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
import PDOLib
|
||||
|
||||
def receive_punisment(value):
|
||||
expansion_available = self.expansion
|
||||
getattr(PDFLib, expansion_available).punish(value)
|
||||
|
||||
player.receive_punishment(0.5)
|
||||
200
NoPELib/player_settings.py
Normal file
200
NoPELib/player_settings.py
Normal file
@ -0,0 +1,200 @@
|
||||
"""
|
||||
Needs to manage any number of players
|
||||
Needs to work with mulitple rounds/turns
|
||||
Needs to handle modifying number of players
|
||||
|
||||
"""
|
||||
|
||||
import copy
|
||||
import json
|
||||
import logging
|
||||
|
||||
_log = logging.getLogger('NoPE-Lib')
|
||||
|
||||
|
||||
class PlayersManager:
|
||||
"""
|
||||
Attribute:
|
||||
gameID (str): The gameID of the active game
|
||||
|
||||
|
||||
Methods:
|
||||
save:
|
||||
"""
|
||||
defaultPlayerConfig = {"flags": [], "expansions": {}, "games": {}}
|
||||
|
||||
def __init__(self, gameID: str, activePlayers: list[str]=None, playersPath: str='./players.json', loggerID: str='Players'):
|
||||
"""
|
||||
Initialises a list of players.
|
||||
|
||||
Args:
|
||||
gameID (str): The ID of the game used in the configuration file.
|
||||
activePlayers (list of str): The names of the players that are playing
|
||||
playersPath (str, optional): The path of the players.json file. Defaults to './players.json'.
|
||||
loggerID (str, optional): The ID used for logging. Defaults to 'Players'.
|
||||
"""
|
||||
|
||||
"""
|
||||
Need to allow:
|
||||
getting a list of players
|
||||
getting the number of players
|
||||
modify the list of players
|
||||
modify the current game
|
||||
"""
|
||||
|
||||
# Store the arguments
|
||||
self._log = _log.getChild(loggerID)
|
||||
self._currentGameID = gameID
|
||||
self._playersPath = playersPath
|
||||
activePlayers = activePlayers if activePlayers is not None else []
|
||||
# Get the config file
|
||||
with open(playersPath, 'r') as f:
|
||||
self._cfg = json.load(f)
|
||||
# Create the players
|
||||
self._player_data = {
|
||||
Player(name, game=None, **cfg)
|
||||
for name, cfg in self._cfg["players"].items()
|
||||
if name in activePlayers
|
||||
}
|
||||
|
||||
def __getitem__(self, playerName: str):
|
||||
"""
|
||||
Get a player object.
|
||||
If the player wasn't active, make them active.
|
||||
If the player didn't exist previously, create a default config and make them active.
|
||||
"""
|
||||
if playerName not in self._cfg:
|
||||
# Create the brand new player
|
||||
self._cfg[playerName] = copy.deepcopy(self.defaultPlayerConfig)
|
||||
newPlayer = Player(playerName, self, **self._cfg["players"][playerName])
|
||||
# Make the player active
|
||||
self._player_data[playerName] = newPlayer
|
||||
self._log.debug(f"Created a new player called {playerName}")
|
||||
return newPlayer
|
||||
elif playerName not in self._player_data:
|
||||
# Fetch the player's data and make them active
|
||||
playerAdded = Player(playerName, self, **self._cfg["players"][playerName])
|
||||
self._player_data[playerName] = playerAdded
|
||||
self._log.debug(f"Fetched {playerName}'s data and made them active")
|
||||
return playerAdded
|
||||
else:
|
||||
return self._player_data[playerName]
|
||||
|
||||
def __setitem__(self, playerName: str, config: dict, makeNewPlayerObject=False):
|
||||
"""
|
||||
Replace a player's config with another one. It possible to also automatically replace the
|
||||
player object by specifying the corresponding argument.
|
||||
"""
|
||||
self._log.debug(f"Changed {playerName}'s config")
|
||||
if makeNewPlayerObject:
|
||||
self._player_data[playerName] = Player(playerName, self, **config)
|
||||
self._cfg["players"][playerName] = config
|
||||
|
||||
def __delitem__(self, playerName):
|
||||
self._log.debug(f"Removing {playerName} from active players")
|
||||
del self._player_data[playerName]
|
||||
|
||||
@property
|
||||
def keys(self):
|
||||
"""
|
||||
Iterator of the active players' names
|
||||
"""
|
||||
return self._player_data.keys()
|
||||
|
||||
@property
|
||||
def items(self):
|
||||
"""
|
||||
Two iterators of the activate players' names and data
|
||||
"""
|
||||
return self._player_data.items()
|
||||
|
||||
@property
|
||||
def gameID(self):
|
||||
return self._currentGameID
|
||||
|
||||
@gameID.setter
|
||||
def gameID(self, gameID: str):
|
||||
self._currentGameID = gameID
|
||||
self._player_data = {name: Player(name, self, **cfg) for name, cfg in self._cfg["players"].items()}
|
||||
|
||||
@gameID.deleter
|
||||
def gameID(self):
|
||||
self._currentGameID = None
|
||||
self._player_data = {name: Player(name, self, **cfg) for name, cfg in self._cfg["players"].items()}
|
||||
|
||||
def save(self):
|
||||
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:
|
||||
|
||||
Methods:
|
||||
generateConfig:
|
||||
"""
|
||||
def __init__(self, name: str, manager: PlayersManager, flags: list[str], expansions: dict, games: dict):
|
||||
self._name = name
|
||||
self._manager = manager
|
||||
self._flags = flags
|
||||
self._expansions = expansions
|
||||
self._gameSave = games[manager._currentGameID] if manager._currentGameID is not None else games
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def flags(self):
|
||||
return self._flags
|
||||
|
||||
@flags.setter
|
||||
def flags(self, newFlags):
|
||||
self._flags = newFlags
|
||||
self._manager[self._name] = self.generateConfig()
|
||||
|
||||
@property
|
||||
def expansions(self):
|
||||
return self._expansions
|
||||
|
||||
@expansions.setter
|
||||
def expansions(self, newExpansions):
|
||||
self._expansions = newExpansions
|
||||
self._manager[self._name] = self.generateConfig()
|
||||
|
||||
@property
|
||||
def gameSave(self):
|
||||
return self._gameSave
|
||||
|
||||
@gameSave.setter
|
||||
def gameSave(self, newGameSave):
|
||||
self._gameSave = newGameSave
|
||||
self._manager[self._name] = self.generateConfig()
|
||||
|
||||
def generateConfig(self):
|
||||
# Get the config of all games
|
||||
games = self._manager[self._name]
|
||||
if self._game is not None:
|
||||
games[self.manager._currentGameID] = self._gameSave
|
||||
# Create the config
|
||||
config = {"flags": self._flags, "expansions": self._expansions, "games": games}
|
||||
return config
|
||||
@ -4,9 +4,16 @@
|
||||
"optionB": 2
|
||||
},
|
||||
|
||||
"players": [
|
||||
{"name": "Brosef", "flags": [], "exampleExpansion": {"playerOption": 5}},
|
||||
{"name": "TRS_MML", "flags": [], "exampleExpansion": {"playerOption": 2}},
|
||||
{"name": "Tango", "flags": [], "exampleExpansion": {"playerOption": 3}}
|
||||
]
|
||||
"availableExpansion":
|
||||
{
|
||||
"bracelet1": "Brosef",
|
||||
"robotic_barman": "Tango",
|
||||
"challenge": "TRS"
|
||||
},
|
||||
|
||||
"players": {
|
||||
"Brosef": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": []}},
|
||||
"TRS_MML": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": []}},
|
||||
"Tango": {"flags": [], "expansions": {"exampleExpansion": {"playerOption": 5}}, "games": {"gameID0": []}}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user