From 5e8a761675d4dca4036d84573be873183bebf7c9 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 19 Jan 2020 00:03:23 -0800 Subject: [PATCH] Implement player add/remove --- amanuensis/cli/lexicon.py | 43 ++++++++++++++++++++-- amanuensis/lexicon/manage.py | 69 +++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/amanuensis/cli/lexicon.py b/amanuensis/cli/lexicon.py index d4bdcde..41a8018 100644 --- a/amanuensis/cli/lexicon.py +++ b/amanuensis/cli/lexicon.py @@ -122,7 +122,25 @@ def command_player_add(args): """ 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_username @@ -133,7 +151,28 @@ def command_player_remove(args): Removing a player dissociates them from any characters 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 def command_player_list(args): diff --git a/amanuensis/lexicon/manage.py b/amanuensis/lexicon/manage.py index 02c5c5b..7fe0763 100644 --- a/amanuensis/lexicon/manage.py +++ b/amanuensis/lexicon/manage.py @@ -94,4 +94,71 @@ def get_all_lexicons(): # Load all of the lexicons lexes = map(lambda id: lexicon.LexiconModel.by(lid=id), lids) - return lexes \ No newline at end of file + 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 \ No newline at end of file