Removing direct access to Account.db._playable_characters in favor of Account.add_character(char) and Account.remove_character(char). Account.characters already handles all filtering so am cleaning up lots of repeated list comprehensions which remove/filter deleted characters.

This commit is contained in:
Andrew Bastien 2023-05-06 02:31:07 -04:00
parent a8cf8e166a
commit f782cd8fc8
10 changed files with 124 additions and 60 deletions

View file

@ -60,12 +60,7 @@ class MuxAccountLookCommand(COMMAND_DEFAULT_CLASS):
super().parse()
playable = self.account.db._playable_characters
if playable is not None:
# clean up list if character object was deleted in between
if None in playable:
playable = [character for character in playable if character]
self.account.db._playable_characters = playable
playable = self.account.characters
# store playable property
if self.args:
self.playable = dict((utils.to_str(char.key.lower()), char) for char in playable).get(
@ -155,8 +150,8 @@ class CmdCharCreate(COMMAND_DEFAULT_CLASS):
if (
not account.is_superuser
and not account.check_permstring("Developer")
and account.db._playable_characters
and len(account.db._playable_characters) >= _MAX_NR_CHARACTERS
and account.characters
and len(account.characters) >= _MAX_NR_CHARACTERS
):
plural = "" if _MAX_NR_CHARACTERS == 1 else "s"
self.msg(f"You may only have a maximum of {_MAX_NR_CHARACTERS} character{plural}.")
@ -184,7 +179,7 @@ class CmdCharCreate(COMMAND_DEFAULT_CLASS):
"puppet:id(%i) or pid(%i) or perm(Developer) or pperm(Developer);delete:id(%i) or"
" perm(Admin)" % (new_character.id, account.id, account.id)
)
account.db._playable_characters.append(new_character)
account.add_character(new_character)
if desc:
new_character.db.desc = desc
elif not new_character.db.desc:
@ -223,7 +218,7 @@ class CmdCharDelete(COMMAND_DEFAULT_CLASS):
# use the playable_characters list to search
match = [
char
for char in utils.make_iter(account.db._playable_characters)
for char in utils.make_iter(account.characters)
if char.key.lower() == self.args.lower()
]
if not match:
@ -243,9 +238,7 @@ class CmdCharDelete(COMMAND_DEFAULT_CLASS):
# only take action
delobj = caller.ndb._char_to_delete
key = delobj.key
caller.db._playable_characters = [
pc for pc in caller.db._playable_characters if pc != delobj
]
caller.remove_character(delobj)
delobj.delete()
self.msg(f"Character '{key}' was permanently deleted.")
logger.log_sec(
@ -314,13 +307,13 @@ class CmdIC(COMMAND_DEFAULT_CLASS):
else:
# argument given
if account.db._playable_characters:
if (playables := account.characters):
# look at the playable_characters list first
character_candidates.extend(
utils.make_iter(
account.search(
self.args,
candidates=account.db._playable_characters,
candidates=playables,
search_object=True,
quiet=True,
)

View file

@ -465,3 +465,64 @@ class CmdUnconnectedInfo(COMMAND_DEFAULT_CLASS):
utils.get_evennia_version(),
)
)
def _create_account(session, accountname, password, permissions, typeclass=None, email=None):
"""
Helper function, creates an account of the specified typeclass.
"""
try:
new_account = create.create_account(
accountname, email, password, permissions=permissions, typeclass=typeclass
)
except Exception as e:
session.msg(
"There was an error creating the Account:\n%s\n If this problem persists, contact an"
" admin." % e
)
logger.log_trace()
return False
# This needs to be set so the engine knows this account is
# logging in for the first time. (so it knows to call the right
# hooks during login later)
new_account.db.FIRST_LOGIN = True
# join the new account to the public channel
pchannel = ChannelDB.objects.get_channel(settings.DEFAULT_CHANNELS[0]["key"])
if not pchannel or not pchannel.connect(new_account):
string = "New account '%s' could not connect to public channel!" % new_account.key
logger.log_err(string)
return new_account
def _create_character(session, new_account, typeclass, home, permissions):
"""
Helper function, creates a character based on an account's name.
This is meant for Guest and AUTO_CREATRE_CHARACTER_WITH_ACCOUNT=True situations.
"""
try:
new_character = create.create_object(
typeclass, key=new_account.key, home=home, permissions=permissions
)
# set playable character list
new_account.add_character(new_character)
# allow only the character itself and the account to puppet this character (and Developers).
new_character.locks.add(
"puppet:id(%i) or pid(%i) or perm(Developer) or pperm(Developer)"
% (new_character.id, new_account.id)
)
# If no description is set, set a default description
if not new_character.db.desc:
new_character.db.desc = "This is a character."
# We need to set this to have ic auto-connect to this character
new_account.db._last_puppet = new_character
except Exception as e:
session.msg(
"There was an error creating the Character:\n%s\n If this problem persists, contact an"
" admin." % e
)
logger.log_trace()