From afb49a86dbf9d066b155eda2eedeaca5cb4dbdc5 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 23 Apr 2020 23:24:05 -0700 Subject: [PATCH] Move player join code to lexicon.setup --- amanuensis/lexicon/__init__.py | 5 ++++ amanuensis/lexicon/manage.py | 47 ----------------------------- amanuensis/lexicon/setup.py | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 47 deletions(-) diff --git a/amanuensis/lexicon/__init__.py b/amanuensis/lexicon/__init__.py index 3eeefb2..909e71b 100644 --- a/amanuensis/lexicon/__init__.py +++ b/amanuensis/lexicon/__init__.py @@ -1,6 +1,11 @@ from amanuensis.lexicon.admin import valid_name, create_lexicon +from amanuensis.lexicon.setup import ( + player_can_join_lexicon, + add_player_to_lexicon) __all__ = [member.__name__ for member in [ valid_name, create_lexicon, + player_can_join_lexicon, + add_player_to_lexicon, ]] diff --git a/amanuensis/lexicon/manage.py b/amanuensis/lexicon/manage.py index b6144c9..65bbe46 100644 --- a/amanuensis/lexicon/manage.py +++ b/amanuensis/lexicon/manage.py @@ -68,53 +68,6 @@ def get_user_lexicons(user): if user.in_lexicon(lexicon)] -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 ArgumentError("Invalid lexicon: '{}'".format(lex)) - if player is None: - raise ArgumentError("Invalid player: '{}'".format(player)) - - # Idempotently add player - added = False - with json_rw(lex.config_path) as cfg: - if player.id not in cfg.join.joined: - cfg.join.joined.append(player.id) - added = True - - # Log to the lexicon's log - if added: - lex.add_log("Player '{0.username}' joined ({0.id})".format(player)) - - def remove_player(lex, player): """ Remove a player from a lexicon diff --git a/amanuensis/lexicon/setup.py b/amanuensis/lexicon/setup.py index 25bb13e..f7e4174 100644 --- a/amanuensis/lexicon/setup.py +++ b/amanuensis/lexicon/setup.py @@ -2,3 +2,57 @@ Submodule of functions for managing lexicon games during the setup and joining part of the game lifecycle. """ +from amanuensis.errors import ArgumentError +from amanuensis.models import LexiconModel, UserModel + + +def player_can_join_lexicon( + player: UserModel, + lexicon: LexiconModel, + password: str = None): + """ + Checks whether the given player can join a lexicon + """ + # Trivial failures + if lexicon is None: + return False + if player is None: + return False + # Can't join if already in the game + if player.uid in lexicon.cfg.join.joined: + return False + # Can't join if the game is closed + if not lexicon.cfg.join.open: + return False + # Can't join if there's no room left + if len(lexicon.cfg.join.joined) >= lexicon.cfg.join.max_players: + return False + # Can't join if the password doesn't check out + if (lexicon.cfg.join.password is not None + and lexicon.cfg.join.password != password): + return False + return True + + +def add_player_to_lexicon( + player: UserModel, + lexicon: LexiconModel): + """ + Unconditionally adds a player to a lexicon + """ + # Verify arguments + if lexicon is None: + raise ArgumentError(f'Invalid lexicon: {lexicon}') + if player is None: + raise ArgumentError(f'Invalid player: {player}') + + # Idempotently add player + added = False + with lexicon.ctx.edit_config() as cfg: + if player.uid not in cfg.join.joined: + cfg.join.joined.append(player.uid) + added = True + + # Log to the lexicon's log + if added: + lexicon.log('Player "{0.cfg.username}" joined ({0.uid})'.format(player))