Basic character add command
This commit is contained in:
parent
8c70067076
commit
2608a992fe
@ -208,7 +208,26 @@ def command_char_create(args):
|
||||
|
||||
The specified player will be set as the character's player.
|
||||
"""
|
||||
raise NotImplementedError() # TODO
|
||||
# Module imports
|
||||
from config import logger
|
||||
from lexicon import LexiconModel
|
||||
from lexicon.manage import add_character
|
||||
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
|
||||
# u in lx
|
||||
|
||||
# Internal call
|
||||
add_character(lex, u, args.charname)
|
||||
|
||||
|
||||
@requires_lexicon
|
||||
@add_argument("--charname", required=True, help="The character's name")
|
||||
|
@ -2,6 +2,7 @@
|
||||
Functions for managing lexicons, primarily within the context of the
|
||||
Amanuensis config directory.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
@ -9,6 +10,7 @@ import time
|
||||
import uuid
|
||||
|
||||
import config
|
||||
from config.loader import AttrOrderedDict
|
||||
import lexicon
|
||||
import resources
|
||||
|
||||
@ -160,4 +162,31 @@ def remove_player(lex, player):
|
||||
if player.id in cfg.join.joined:
|
||||
cfg.join.joined.remove(player.id)
|
||||
|
||||
# TODO Reassign the player's characters to the editor
|
||||
# TODO Reassign the player's characters to the editor
|
||||
|
||||
|
||||
def add_character(lex, player, charname):
|
||||
"""
|
||||
Unconditionally adds a character 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))
|
||||
if not charname:
|
||||
raise ValueError("Invalid character name: '{}'".format(charname))
|
||||
|
||||
# Load the character template
|
||||
with resources.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 = charname
|
||||
character.player = player.id
|
||||
character.signature = "~" + charname
|
||||
|
||||
# Add the character to the lexicon
|
||||
with config.json_rw(lex.config_path) as cfg:
|
||||
cfg.character.new(character.cid, character)
|
||||
|
Loading…
Reference in New Issue
Block a user