From e125763ea553b0e48ef0112bce9346ccab3089ab Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 5 Sep 2010 18:20:39 +0000 Subject: [PATCH] Made so the default add_default_cmdset script also removes the added cmdset when stopped. Fixed the function of @delplayer command. --- game/gamesrc/commands/default/privileged.py | 95 ++++++++++++++------- game/gamesrc/commands/default/unloggedin.py | 8 +- src/commands/cmdsethandler.py | 7 +- src/scripts/scripts.py | 10 +++ src/utils/create.py | 2 +- 5 files changed, 86 insertions(+), 36 deletions(-) diff --git a/game/gamesrc/commands/default/privileged.py b/game/gamesrc/commands/default/privileged.py index 709e4520e1..1cd64b38b5 100644 --- a/game/gamesrc/commands/default/privileged.py +++ b/game/gamesrc/commands/default/privileged.py @@ -12,7 +12,7 @@ from src.scripts.models import ScriptDB from src.objects.models import ObjectDB from src.permissions.models import PermissionGroup from src.utils import reloads, create, logger, utils -from src.permissions.permissions import has_perm +from src.permissions.permissions import has_perm, has_perm_string from game.gamesrc.commands.default.muxcommand import MuxCommand @@ -437,43 +437,78 @@ class CmdDelPlayer(MuxCommand): if ':' in args: args, reason = [arg.strip() for arg in args.split(':', 1)] - # Search for the object connected to this user (this is done by - # adding a * to the beginning of the search criterion) - pobj = caller.search("*%s" % args, global_search=True) - if not pobj: - # if we cannot find an object connected to this user, - # try a more direct approach + # We use player_search since we want to be sure to find also players + # that lack characters. + players = PlayerDB.objects.filter(db_key=args) + if not players: + try: + players = PlayerDB.objects.filter(id=args) + except ValueError: + pass + + if not players: + # try to find a user instead of a Player try: user = User.objects.get(id=args) except Exception: try: - user = User.objects.get(name__iexact=args) + user = User.objects.get(username__iexact=args) except Exception: - caller.msg("Could not find user/id '%s'." % args) - return - uprofile = user.get_profile - else: - user = pobj.user - uprofile = pobj.user_profile - - if not has_perm(caller, uprofile, 'manage_players'): - string = "You don't have the permissions to delete that player." + string = "No Player nor User found matching '%s'." % args + caller.msg(string) + return + try: + player = user.get_profile() + except Exception: + player = None + + if not has_perm_string(caller, 'manage_players'): + string = "You don't have the permissions to delete this player." + caller.msg(string) + return + string = "" + name = user.username + user.delete() + if player: + name = player.name + player.delete() + string = "Player %s was deleted." % name + else: + string += "The User %s was deleted, but had no Player associated with it." % name caller.msg(string) return - - uname = user.username - # boot the player then delete - if pobj and pobj.has_user: - caller.msg("Booting and informing player ...") - msg = "\nYour account '%s' is being *permanently* deleted.\n" % uname - if reason: - msg += " Reason given:\n '%s'" % reason - pobj.msg(msg) - caller.execute_cmd("@boot %s" % uname) + + elif len(players) > 1: + string = "There where multiple matches:" + for player in players: + string += "\n %s %s" % (player.id, player.key) + return - uprofile.delete() - user.delete() - caller.msg("Player %s was successfully deleted." % uname) + else: + # one single match + + player = players[0] + user = player.user + character = player.character + + if not has_perm(caller, player, 'manage_players'): + string = "You don't have the permissions to delete that player." + caller.msg(string) + return + + uname = user.username + # boot the player then delete + if character and character.has_player: + caller.msg("Booting and informing player ...") + string = "\nYour account '%s' is being *permanently* deleted.\n" % uname + if reason: + string += " Reason given:\n '%s'" % reason + character.msg(string) + caller.execute_cmd("@boot %s" % uname) + + player.delete() + user.delete() + caller.msg("Player %s was successfully deleted." % uname) class CmdNewPassword(MuxCommand): diff --git a/game/gamesrc/commands/default/unloggedin.py b/game/gamesrc/commands/default/unloggedin.py index da2197ff16..f760b66a5a 100644 --- a/game/gamesrc/commands/default/unloggedin.py +++ b/game/gamesrc/commands/default/unloggedin.py @@ -187,10 +187,10 @@ class CmdCreate(MuxCommand): permissions = settings.PERMISSION_PLAYER_DEFAULT new_character = create.create_player(playername, email, password, - permissions=permissions, - location=default_home, - typeclass=typeclass, - home=default_home) + permissions=permissions, + location=default_home, + typeclass=typeclass, + home=default_home) # set a default description new_character.db.desc = "This is a Player." diff --git a/src/commands/cmdsethandler.py b/src/commands/cmdsethandler.py index 36f2b5d85c..844a3b2c20 100644 --- a/src/commands/cmdsethandler.py +++ b/src/commands/cmdsethandler.py @@ -307,7 +307,7 @@ class CmdSetHandler(object): the last cmdset in the stack is removed. Whenever the cmdset_stack changes, the cmdset is updated. The default cmdset (first entry in stack) is never - removed - set it to an empty set with add_default instead. + removed - remove it explicitly with delete_default. key_or_class - a specific cmdset key or a cmdset class (in the latter case, *all* cmdsets of this class @@ -339,6 +339,11 @@ class CmdSetHandler(object): # re-sync the cmdsethandler. self.update() + def delete_default(self): + "This explicitly deletes the default cmdset. It's the only command that can." + self.cmdset_stack[0] = CmdSet(cmdsetobj=self.obj, key="Empty") + self.update() + def all(self): """ Returns the list of cmdsets. Mostly useful to check if stack if empty or not. diff --git a/src/scripts/scripts.py b/src/scripts/scripts.py index fd72f0b62a..b12764d1cc 100644 --- a/src/scripts/scripts.py +++ b/src/scripts/scripts.py @@ -301,3 +301,13 @@ class AddCmdSet(Script): else: self.obj.cmdset.add(cmdset) + def at_stop(self): + """ + This removes the cmdset when the script stops + """ + cmdset = self.db.cmdset + if cmdset: + if self.db.add_default: + self.obj.cmdset.delete_default() + else: + self.obj.cmdset.delete(cmdset) diff --git a/src/utils/create.py b/src/utils/create.py index 59b1265d8d..6975cc2e5a 100644 --- a/src/utils/create.py +++ b/src/utils/create.py @@ -417,7 +417,7 @@ def create_player(name, email, password, new_user = User.objects.create_user(name, email, password) # create the associated Player for this User, and tie them together - new_player = PlayerDB(user=new_user) + new_player = PlayerDB(db_key=name, user=new_user) new_player.save() # assign mud permissions