Implement player add/remove

This commit is contained in:
Tim Van Baak 2020-01-19 00:03:23 -08:00
parent e8b209e231
commit 5e8a761675
2 changed files with 109 additions and 3 deletions

View File

@ -122,7 +122,25 @@ def command_player_add(args):
""" """
Add a player to a lexicon Add a player to a lexicon
""" """
raise NotImplementedError() # TODO # Module imports
from config import logger
from lexicon import LexiconModel
from lexicon.manage import add_player
from user import UserModel
# Verify arguments
u = UserModel.by(name=args.username)
if u is None:
logger.error("Could not find user '{}'".format(args.username))
return -1
lex = LexiconModel.by(name=args.lexicon)
if lex is None:
logger.error("Could not find lexicon '{}'".format(args.lexicon))
return -1
# Internal call
add_player(lex, u)
@requires_lexicon @requires_lexicon
@requires_username @requires_username
@ -133,7 +151,28 @@ def command_player_remove(args):
Removing a player dissociates them from any characters Removing a player dissociates them from any characters
they control but does not delete any character data. they control but does not delete any character data.
""" """
raise NotImplementedError() # TODO # Module imports
from config import logger
from lexicon import LexiconModel
from lexicon.manage import remove_player
from user import UserModel
# Verify arguments
u = UserModel.by(name=args.username)
if u is None:
logger.error("Could not find user '{}'".format(args.username))
return -1
lex = LexiconModel.by(name=args.lexicon)
if lex is None:
logger.error("Could not find lexicon '{}'".format(args.lexicon))
return -1
if lex.editor == u.id:
logger.error("Can't remove the editor of a lexicon")
return -1
# Internal call
remove_player(lex, u)
@requires_lexicon @requires_lexicon
def command_player_list(args): def command_player_list(args):

View File

@ -94,4 +94,71 @@ def get_all_lexicons():
# Load all of the lexicons # Load all of the lexicons
lexes = map(lambda id: lexicon.LexiconModel.by(lid=id), lids) lexes = map(lambda id: lexicon.LexiconModel.by(lid=id), lids)
return lexes return lexes
def valid_add(lex, player, password=None):
"""
Checks whether the given player can join a lexicon
"""
# Trivial failures
if lex is None:
return False
if player is None:
return False
# Can't join if already in the game
if player.id in lex.join.joined:
return False
# Can't join if the game is closed
if not lex.join.open:
return False
# Can't join if the player max is reached
if len(lex.join.joined) >= lex.join.max_players:
return False
# Can't join if the password doesn't check out
if lex.join.password is not None and lex.join.password != password:
return False
return True
def add_player(lex, player):
"""
Unconditionally adds a player to a lexicon
"""
# Verify arguments
if lex is None:
raise ValueError("Invalid lexicon: '{}'".format(lex))
if player is None:
raise ValueError("Invalid player: '{}'".format(player))
# Idempotently add player
with config.json_rw(lex.config_path) as cfg:
if player.id not in cfg.join.joined:
cfg.join.joined.append(player.id)
def remove_player(lex, player):
"""
Remove a player from a lexicon
"""
# Verify arguments
if lex is None:
raise ValueError("Invalid lexicon: '{}'".format(lex))
if player is None:
raise ValueError("Invalid player: '{}'".format(player))
if lex.editor == player.id:
raise ValueError("Can't remove the editor '{}' from lexicon '{}'".format(player.username, lex.name))
# Idempotently remove player
with config.json_rw(lex.config_path) as cfg:
if player.id in cfg.join.joined:
cfg.join.joined.remove(player.id)
# TODO Reassign the player's characters to the editor
def list_players(lex):
"""
"""
pass