Update lexicon-char cli commands
This commit is contained in:
parent
30d7c53919
commit
cae12b960d
@ -222,23 +222,24 @@ def command_char_create(args):
|
||||
|
||||
The specified player will be set as the character's player.
|
||||
"""
|
||||
lexicon: LexiconModel = args.lexicon
|
||||
user: UserModel = args.user
|
||||
|
||||
# Module imports
|
||||
from amanuensis.lexicon import LexiconModel
|
||||
from amanuensis.lexicon.manage import add_character
|
||||
from amanuensis.user import UserModel
|
||||
from amanuensis.lexicon import create_character_in_lexicon
|
||||
|
||||
# Verify arguments
|
||||
if not args.user.in_lexicon(args.lexicon):
|
||||
if user.uid not in lexicon.cfg.join.joined:
|
||||
logger.error('"{0.username}" is not a player in lexicon "{1.name}"'
|
||||
''.format(args.user, args.lexicon))
|
||||
''.format(user.cfg, lexicon.cfg))
|
||||
return -1
|
||||
|
||||
# Perform command
|
||||
add_character(args.lexicon, args.user, {"name": args.charname})
|
||||
create_character_in_lexicon(user, lexicon, args.charname)
|
||||
|
||||
# Output
|
||||
logger.info('Created character "{0.charname}" for "{0.user.username}" in '
|
||||
'"{0.lexicon.name}"'.format(args))
|
||||
logger.info(f'Created character "{args.charname}" for "{user.cfg.username}"'
|
||||
f' in "{lexicon.cfg.name}"')
|
||||
return 0
|
||||
|
||||
|
||||
@ -252,19 +253,20 @@ def command_char_delete(args):
|
||||
Deleting a character dissociates them from any content
|
||||
they have contributed rather than deleting it.
|
||||
"""
|
||||
# Module imports
|
||||
from amanuensis.lexicon import LexiconModel
|
||||
from amanuensis.lexicon.manage import delete_character
|
||||
raise NotImplementedError()
|
||||
# # Module imports
|
||||
# from amanuensis.lexicon import LexiconModel
|
||||
# from amanuensis.lexicon.manage import delete_character
|
||||
|
||||
# Verify arguments
|
||||
lex = LexiconModel.by(name=args.lexicon)
|
||||
if lex is None:
|
||||
logger.error("Could not find lexicon '{}'".format(args.lexicon))
|
||||
return -1
|
||||
# # Verify arguments
|
||||
# lex = LexiconModel.by(name=args.lexicon)
|
||||
# if lex is None:
|
||||
# logger.error("Could not find lexicon '{}'".format(args.lexicon))
|
||||
# return -1
|
||||
|
||||
# Internal call
|
||||
delete_character(lex, args.charname)
|
||||
return 0
|
||||
# # Internal call
|
||||
# delete_character(lex, args.charname)
|
||||
# return 0
|
||||
|
||||
|
||||
@alias('lcl')
|
||||
@ -273,19 +275,20 @@ def command_char_list(args):
|
||||
"""
|
||||
List all characters in a lexicon
|
||||
"""
|
||||
import json
|
||||
# Module imports
|
||||
from amanuensis.lexicon import LexiconModel
|
||||
raise NotImplementedError()
|
||||
# import json
|
||||
# # Module imports
|
||||
# from amanuensis.lexicon import LexiconModel
|
||||
|
||||
# Verify arguments
|
||||
lex = LexiconModel.by(name=args.lexicon)
|
||||
if lex is None:
|
||||
logger.error("Could not find lexicon '{}'".format(args.lexicon))
|
||||
return -1
|
||||
# # Verify arguments
|
||||
# lex = LexiconModel.by(name=args.lexicon)
|
||||
# if lex is None:
|
||||
# logger.error("Could not find lexicon '{}'".format(args.lexicon))
|
||||
# return -1
|
||||
|
||||
# Internal call
|
||||
print(json.dumps(lex.character, indent=2))
|
||||
return 0
|
||||
# # Internal call
|
||||
# print(json.dumps(lex.character, indent=2))
|
||||
# return 0
|
||||
|
||||
#
|
||||
# Procedural commands
|
||||
|
@ -1,11 +1,13 @@
|
||||
from amanuensis.lexicon.admin import valid_name, create_lexicon
|
||||
from amanuensis.lexicon.setup import (
|
||||
player_can_join_lexicon,
|
||||
add_player_to_lexicon)
|
||||
add_player_to_lexicon,
|
||||
create_character_in_lexicon)
|
||||
|
||||
__all__ = [member.__name__ for member in [
|
||||
valid_name,
|
||||
create_lexicon,
|
||||
player_can_join_lexicon,
|
||||
add_player_to_lexicon,
|
||||
create_character_in_lexicon,
|
||||
]]
|
||||
|
@ -90,45 +90,6 @@ def remove_player(lex, player):
|
||||
# TODO Reassign the player's characters to the editor
|
||||
|
||||
|
||||
def add_character(lex, player, charinfo={}):
|
||||
"""
|
||||
Unconditionally adds a character to a lexicon
|
||||
|
||||
charinfo is a dictionary of character settings
|
||||
"""
|
||||
# Verify arguments
|
||||
if lex is None:
|
||||
raise ArgumentError("Invalid lexicon: '{}'".format(lex))
|
||||
if player is None:
|
||||
raise ArgumentError("Invalid player: '{}'".format(player))
|
||||
if not charinfo or not charinfo.get("name"):
|
||||
raise ArgumentError("Invalid character info: '{}'".format(charinfo))
|
||||
charname = charinfo.get("name")
|
||||
if any([
|
||||
char.name for char in lex.character.values()
|
||||
if char.name == charname]):
|
||||
raise ArgumentError("Duplicate character name: '{}'".format(charinfo))
|
||||
|
||||
# Load the character template
|
||||
with get_stream('character.json') as template:
|
||||
character = json.load(template, object_pairs_hook=AttrOrderedDict)
|
||||
|
||||
# Fill out the character's information
|
||||
character.cid = charinfo.get("cid") or uuid.uuid4().hex
|
||||
character.name = charname
|
||||
character.player = charinfo.get("player") or player.id
|
||||
character.signature = charinfo.get("signature") or ("~" + character.name)
|
||||
|
||||
# Add the character to the lexicon
|
||||
added = False
|
||||
with json_rw(lex.config_path) as cfg:
|
||||
cfg.character.new(character.cid, character)
|
||||
added = True
|
||||
|
||||
# Log addition
|
||||
if added:
|
||||
lex.add_log("Character '{0.name}' created ({0.cid})".format(character))
|
||||
|
||||
def delete_character(lex, charname):
|
||||
"""
|
||||
Delete a character from a lexicon
|
||||
|
@ -2,8 +2,13 @@
|
||||
Submodule of functions for managing lexicon games during the setup and
|
||||
joining part of the game lifecycle.
|
||||
"""
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from amanuensis.config import AttrOrderedDict
|
||||
from amanuensis.errors import ArgumentError
|
||||
from amanuensis.models import LexiconModel, UserModel
|
||||
from amanuensis.resources import get_stream
|
||||
|
||||
|
||||
def player_can_join_lexicon(
|
||||
@ -56,3 +61,42 @@ def add_player_to_lexicon(
|
||||
# Log to the lexicon's log
|
||||
if added:
|
||||
lexicon.log('Player "{0.cfg.username}" joined ({0.uid})'.format(player))
|
||||
|
||||
|
||||
def create_character_in_lexicon(
|
||||
player: UserModel,
|
||||
lexicon: LexiconModel,
|
||||
name: str) -> None:
|
||||
"""
|
||||
Unconditionally creates a character for a player
|
||||
"""
|
||||
# Verify arguments
|
||||
if lexicon is None:
|
||||
raise ArgumentError(f'Invalid lexicon: {lexicon}')
|
||||
if player is None:
|
||||
raise ArgumentError(f'Invalid player: {player}')
|
||||
if player.uid not in lexicon.cfg.join.joined:
|
||||
raise ArgumentError(f'Player {player} not in lexicon {lexicon}')
|
||||
if name is None:
|
||||
raise ArgumentError(f'Invalid character name: "{name}"')
|
||||
if any([
|
||||
char.name for char in lexicon.cfg.character.values()
|
||||
if char.name == name]):
|
||||
raise ArgumentError(f'Duplicate character name: "{name}"')
|
||||
|
||||
# Load the character template
|
||||
with get_stream('character.json') as template:
|
||||
character = json.load(template, object_pairs_hook=AttrOrderedDict)
|
||||
|
||||
# Fill out the character's information
|
||||
character.cid = uuid.uuid4().hex
|
||||
character.name = name
|
||||
character.player = player.uid
|
||||
character.signature = "~" + character.name
|
||||
|
||||
# Add the character to the lexicon
|
||||
with lexicon.ctx.edit_config() as cfg:
|
||||
cfg.character.new(character.cid, character)
|
||||
|
||||
# Log addition
|
||||
lexicon.log(f'Character "{name}" created ({character.cid})')
|
||||
|
Loading…
Reference in New Issue
Block a user