Move player join code to lexicon.setup
This commit is contained in:
parent
97ac9bf7e3
commit
afb49a86db
|
@ -1,6 +1,11 @@
|
||||||
from amanuensis.lexicon.admin import valid_name, create_lexicon
|
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 [
|
__all__ = [member.__name__ for member in [
|
||||||
valid_name,
|
valid_name,
|
||||||
create_lexicon,
|
create_lexicon,
|
||||||
|
player_can_join_lexicon,
|
||||||
|
add_player_to_lexicon,
|
||||||
]]
|
]]
|
||||||
|
|
|
@ -68,53 +68,6 @@ def get_user_lexicons(user):
|
||||||
if user.in_lexicon(lexicon)]
|
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):
|
def remove_player(lex, player):
|
||||||
"""
|
"""
|
||||||
Remove a player from a lexicon
|
Remove a player from a lexicon
|
||||||
|
|
|
@ -2,3 +2,57 @@
|
||||||
Submodule of functions for managing lexicon games during the setup and
|
Submodule of functions for managing lexicon games during the setup and
|
||||||
joining part of the game lifecycle.
|
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))
|
||||||
|
|
Loading…
Reference in New Issue