Expand char-add, implement char-delete
This commit is contained in:
parent
2608a992fe
commit
ccc027d5ef
@ -226,7 +226,7 @@ def command_char_create(args):
|
||||
# u in lx
|
||||
|
||||
# Internal call
|
||||
add_character(lex, u, args.charname)
|
||||
add_character(lex, u, {"name": args.charname})
|
||||
|
||||
|
||||
@requires_lexicon
|
||||
@ -238,7 +238,19 @@ def command_char_delete(args):
|
||||
Deleting a character dissociates them from any content
|
||||
they have contributed rather than deleting it.
|
||||
"""
|
||||
raise NotImplementedError() # TODO
|
||||
# Module imports
|
||||
from config import logger
|
||||
from lexicon import LexiconModel
|
||||
from 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
|
||||
|
||||
# Internal call
|
||||
delete_character(lex, args.charname)
|
||||
|
||||
@requires_lexicon
|
||||
def command_char_list(args):
|
||||
|
@ -165,28 +165,54 @@ def remove_player(lex, player):
|
||||
# TODO Reassign the player's characters to the editor
|
||||
|
||||
|
||||
def add_character(lex, player, charname):
|
||||
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 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))
|
||||
if not charinfo or not charinfo.get("name"):
|
||||
raise ValueError("Invalid character info: '{}'".format(charinfo))
|
||||
charname = charinfo.get("name")
|
||||
if any([char.name for char in lex.character.values() if char.name == charname]):
|
||||
raise ValueError("Duplicate character name: '{}'".format(charinfo))
|
||||
|
||||
# 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.cid = charinfo.get("cid") or uuid.uuid4().hex
|
||||
character.name = charname
|
||||
character.player = player.id
|
||||
character.signature = "~" + charname
|
||||
character.player = charinfo.get("player") or player.id
|
||||
character.signature = charinfo.get("signature") or ("~" + character.name)
|
||||
|
||||
# Add the character to the lexicon
|
||||
with config.json_rw(lex.config_path) as cfg:
|
||||
cfg.character.new(character.cid, character)
|
||||
|
||||
|
||||
def delete_character(lex, charname):
|
||||
"""
|
||||
Delete a character from a lexicon
|
||||
"""
|
||||
# Verify arguments
|
||||
if lex is None:
|
||||
raise ValueError("Invalid lexicon: '{}'".format(lex))
|
||||
if charname is None:
|
||||
raise ValueError("Invalid character name: '{}'".format(charinfo))
|
||||
|
||||
# Find character in this lexicon
|
||||
matches = [char for cid, char in lex.character.items() if char.name == charname]
|
||||
if len(matches) != 1:
|
||||
raise ValueError(matches)
|
||||
char = matches[0]
|
||||
|
||||
# Remove character from character list
|
||||
with config.json_rw(lex.config_path) as cfg:
|
||||
del cfg.character[char.cid]
|
Loading…
Reference in New Issue
Block a user