From: Jeremy Stanley Date: Mon, 5 Sep 2005 03:43:11 +0000 (+0000) Subject: Imported from archive. X-Git-Tag: 0.0.1~335 X-Git-Url: https://mudpy.org/gitweb?a=commitdiff_plain;h=7a6aa3bc9d4a51bf19f3a967c736e532952d887f;p=mudpy.git Imported from archive. * menu (menu:activate_avatar): Add a default of the first avatar in the list. (menu:choose_name): Change the generate more names choice from m to g and make it the default. (menu:delete_avatar): Make abort the default choice and correct a misleading copy/paste error in the prompt. * menu (menu:delete_account), mudpy.py (User.delete): New menu and supporting method to allow deletion of an existing account. * mudpy.py (Element.get, Element.getboolean, Element.getdict) (Element.getfloat, Element.getint, Element.getlist): Reworked default parameter handling to fall back to the None type and then replace it with the appropriate type of empty variable if not overridden, solving obscure bugs arising from mutable sequence reuse. (get_choice_action, get_choice_branch, generic_menu_handler): Consolidated default choice fallback into generic_menu_handler. * testdata: Removed pending something better. --- diff --git a/menu b/menu index b1ad12b..951df54 100644 --- a/menu +++ b/menu @@ -8,6 +8,7 @@ branch = active branch_a = main_utility choice_a = abort selection create = dict([(str(x+1),y) for x,y in enumerate(user.list_avatar_names())]) +default = 1 description = This is the list of avatars available for you to awaken. prompt = Whom would you like to awaken? @@ -43,27 +44,39 @@ prompt = Pick a gender for your new avatar: [menu:choose_name] action = user.avatar.set("name", user.menu_choices[choice]) branch = main_utility -branch_m = choose_name -choice_m = generate more names +branch_g = choose_name +choice_g = generate more names create_1 = random_name() -create_3 = random_name() create_2 = random_name() -create_5 = random_name() +create_3 = random_name() create_4 = random_name() -create_7 = random_name() +create_5 = random_name() create_6 = random_name() +create_7 = random_name() +default = g description = Your new avatar needs a name. This will be the name with which $(tpsp) grew up, and will initially be the name by which $(tpsp) is known in the world of Example. There are ways for your new avatar to make a name for $(tpop)self over time, so $(tpsp) won't be stuck going by such an unremarkable name forever. prompt = Choose a name for $(tpop): +[menu:delete_account] +action_y = user.delete() +branch_n = main_utility +branch_y = disconnecting +choice_n = no, don't delete my account +choice_y = yes, permanently delete my account +default = n +description = By deleting your account, all your avatars will also be permanently deleted. +prompt = Are you certain you wish to permanently delete your account? + [menu:delete_avatar] -action = user.delete_avatar(user.account.getlist("avatars")[int(choice)-1], universe) +action = user.delete_avatar(user.account.getlist("avatars")[int(choice)-1]) action_a = pass branch = main_utility branch_a = main_utility choice_a = abort selection create = dict([(str(x+1),y) for x,y in enumerate(user.list_avatar_names())]) +default = a description = This is the list of avatars available for you to awaken. -prompt = Whom would you like to awaken? +prompt = Whom would you like to delete? [menu:disconnecting] description = $(red)Disconnecting...$(nrm) @@ -94,9 +107,9 @@ choice_c = create a new avatar choice_d = delete an unwanted avatar choice_l = leave example for now choice_p = permanently remove your account -demand_a = user.account.get("avatars") +demand_a = user.account.getlist("avatars") demand_c = len(user.account.getlist("avatars")) < universe.categories["internal"]["limits"].getint("max_avatars") -demand_d = user.account.get("avatars") +demand_d = user.account.getlist("avatars") description = From here you can awaken, create and delete avatars. An avatar is your persona in the world of Example. You can also leave or permanently delete your account. prompt = What would you like to do? diff --git a/mudpy.conf b/mudpy.conf index 6ad101e..c349956 100644 --- a/mudpy.conf +++ b/mudpy.conf @@ -1,6 +1,5 @@ [control] default_files = {"account": "account", "actor": "actor", "command": "command", "internal": "internal", "location": "location", "menu": "menu", "other": "other" } -include_files = testdata private_files = account read_only = yes diff --git a/mudpy.py b/mudpy.py index 444c2e1..006b6a8 100644 --- a/mudpy.py +++ b/mudpy.py @@ -43,36 +43,42 @@ class Element: def facets(self): """Return a list of facets for this element.""" return universe.files[self.origin].data.options(self.key) - def get(self, facet, default=""): + def get(self, facet, default=None): """Retrieve values.""" + if default is None: default = "" if universe.files[self.origin].data.has_option(self.key, facet): return universe.files[self.origin].data.get(self.key, facet) else: return default - def getboolean(self, facet, default=False): + def getboolean(self, facet, default=None): """Retrieve values as boolean type.""" + if default is None: default=False if universe.files[self.origin].data.has_option(self.key, facet): return universe.files[self.origin].data.getboolean(self.key, facet) else: return default - def getint(self, facet, default=0): + def getint(self, facet, default=None): """Return values as int/long type.""" + if default is None: default = 0 if universe.files[self.origin].data.has_option(self.key, facet): return universe.files[self.origin].data.getint(self.key, facet) else: return default - def getfloat(self, facet, default=0.0): + def getfloat(self, facet, default=None): """Return values as float type.""" + if default is None: default = 0.0 if universe.files[self.origin].data.has_option(self.key, facet): return universe.files[self.origin].data.getfloat(self.key, facet) else: return default - def getlist(self, facet, default=[]): + def getlist(self, facet, default=None): """Return values as list type.""" + if default is None: default = [] value = self.get(facet) - if not value: return default - else: return makelist(value) - def getdict(self, facet, default={}): + if value: return makelist(value) + else: return default + def getdict(self, facet, default=None): """Return values as dict type.""" + if default is None: default = {} value = self.get(facet) - if not value: return default - else: return makedict(value) + if value: return makedict(value) + else: return default def set(self, facet, value): """Set values.""" if type(value) is long: value = str(value) @@ -111,7 +117,7 @@ class DataFile: include_file = path_join(dirname(filename), include_file) DataFile(include_file, universe) def save(self): - if self.data.sections() and ( not self.data.has_option("control", "read_only") or not self.data.getboolean("control", "read_only") ): + if ( self.data.sections() or exists(self.filename) ) and not ( self.data.has_option("control", "read_only") and self.data.getboolean("control", "read_only") ): if not exists(dirname(self.filename)): makedirs(dirname) file_descriptor = file(self.filename, "w") if self.filename in universe.private_files and oct(S_IMODE(stat(self.filename)[ST_MODE])) != 0600: @@ -400,15 +406,14 @@ class User: def new_avatar(self): """Instantiate a new, unconfigured avatar for this user.""" - counter = universe.categories["internal"]["counters"].getint("next_avatar") - while "avatar:" + str(counter + 1) in universe.categories["actor"].keys(): counter += 1 - universe.categories["internal"]["counters"].set("next_avatar", counter + 1) - self.avatar = Element("actor:avatar:" + str(counter), universe) + counter = 0 + while "avatar:" + self.account.get("name") + ":" + str(counter) in universe.categories["actor"].keys(): counter += 1 + self.avatar = Element("actor:avatar:" + self.account.get("name") + ":" + str(counter), universe) avatars = self.account.getlist("avatars") avatars.append(self.avatar.key) self.account.set("avatars", avatars) - def delete_avatar(self, avatar, universe): + def delete_avatar(self, avatar): """Remove an avatar from the world and from the user's list.""" if self.avatar is universe.contents[avatar]: self.avatar = None universe.contents[avatar].delete() @@ -416,6 +421,11 @@ class User: avatars.remove(avatar) self.account.set("avatars", avatars) + def delete(self): + """Delete the user and associated avatars.""" + for avatar in self.account.getlist("avatars"): self.delete_avatar(avatar) + self.account.delete() + def list_avatar_names(self): """List names of assigned avatars.""" return [ universe.contents[avatar].get("name") for avatar in self.account.getlist("avatars") ] @@ -726,8 +736,8 @@ def get_default_menu_choice(state): def get_formatted_default_menu_choice(state): """Default menu choice foratted for inclusion in a prompt string.""" - default = get_default_menu_choice(state) - if default: return "[$(red)" + default + "$(nrm)] " + default_choice = get_default_menu_choice(state) + if default_choice: return "[$(red)" + default_choice + "$(nrm)] " else: return "" def get_menu_description(state, error): @@ -807,7 +817,6 @@ def get_default_branch(state): def get_choice_branch(user, choice): """Returns the new state matching the given choice.""" branches = get_menu_branches(user.state) - if not choice: choice = get_default_menu_choice(user.state) if choice in branches.keys(): return branches[choice] elif choice in user.menu_choices.keys(): return get_default_branch(user.state) else: return "" @@ -827,7 +836,6 @@ def get_default_action(state): def get_choice_action(user, choice): """Run any indicated script for the given choice.""" actions = get_menu_actions(user.state) - if not choice: choice = get_default_menu_choice(user.state) if choice in actions.keys(): return actions[choice] elif choice in user.menu_choices.keys(): return get_default_action(user.state) else: return "" @@ -855,7 +863,7 @@ def generic_menu_handler(user): choice = user.input_queue.pop(0) if choice: choice = choice.lower() else: choice = "" - + if not choice: choice = get_default_menu_choice(user.state) if choice in user.menu_choices: exec(get_choice_action(user, choice)) new_state = get_choice_branch(user, choice) diff --git a/testdata b/testdata deleted file mode 100644 index 8b84c39..0000000 --- a/testdata +++ /dev/null @@ -1,9 +0,0 @@ -[prop:example_prop] -name = The Example Prop - -[location:0:0:0:0] -name = The Origin of the Universe - -[actor:example_actor] -name = The Example Actor -