From 13138cabcedc52b00eb49b1a6b4e0818469bee74 Mon Sep 17 00:00:00 2001 From: Jeremy Stanley Date: Wed, 25 Dec 2019 21:24:06 +0000 Subject: [PATCH] Move selected menu item into the User object In order to simplify menu handlers, move the active choice into an attribute of the User class so that only one object needs to be handed off, and update menus accordingly. --- mudpy/misc.py | 35 ++++++++++++++++++----------------- share/menu.yaml | 12 +++++++----- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/mudpy/misc.py b/mudpy/misc.py index 62f531e..dc37b6e 100644 --- a/mudpy/misc.py +++ b/mudpy/misc.py @@ -488,6 +488,7 @@ class User: self.address = "" self.authenticated = False self.avatar = None + self.choice = "" self.columns = 79 self.connection = None self.error = "" @@ -1638,12 +1639,12 @@ def get_default_branch(state): return universe.groups["menu"][state].get("branch") -def get_choice_branch(user, choice): +def get_choice_branch(user): """Returns the new state matching the given choice.""" branches = get_menu_branches(user.state) - if choice in branches.keys(): - return branches[choice] - elif choice in user.menu_choices.keys(): + if user.choice in branches.keys(): + return branches[user.choice] + elif user.choice in user.menu_choices.keys(): return get_default_branch(user.state) else: return "" @@ -1665,12 +1666,12 @@ def get_default_action(state): return universe.groups["menu"][state].get("action") -def get_choice_action(user, choice): +def get_choice_action(user): """Run any indicated script for the given choice.""" actions = get_menu_actions(user.state) - if choice in actions.keys(): - return actions[choice] - elif choice in user.menu_choices.keys(): + if user.choice in actions.keys(): + return actions[user.choice] + elif user.choice in user.menu_choices.keys(): return get_default_action(user.state) else: return "" @@ -1702,16 +1703,16 @@ def generic_menu_handler(user): # get a lower-case representation of the next line of input if user.input_queue: - choice = user.input_queue.pop(0) - if choice: - choice = choice.lower() + user.choice = user.input_queue.pop(0) + if user.choice: + user.choice = user.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) + user.choice = "" + if not user.choice: + user.choice = get_default_menu_choice(user.state) + if user.choice in user.menu_choices: + exec(get_choice_action(user)) + new_state = get_choice_branch(user) if new_state: user.state = new_state else: diff --git a/share/menu.yaml b/share/menu.yaml index f0fba9c..3350e8f 100644 --- a/share/menu.yaml +++ b/share/menu.yaml @@ -7,7 +7,7 @@ _desc: This is the standard library of menu definitions. _lock: true -menu.activate_avatar.action: user.activate_avatar_by_index(int(choice)-1) +menu.activate_avatar.action: user.activate_avatar_by_index(int(user.choice)-1) menu.activate_avatar.action_a: pass menu.activate_avatar.branch_a: main_utility menu.activate_avatar.choice_a: abort selection @@ -15,7 +15,7 @@ menu.activate_avatar.create: dict([(str(x+1),y) for x,y in enumerate(user.list_avatar_names())]) menu.activate_avatar.default: "1" menu.activate_avatar.description: This is the list of avatars available for you - to awaken. + to awaken. menu.activate_avatar.prompt: Whom would you like to awaken? menu.active.prompt: ">" @@ -39,7 +39,8 @@ menu.checking_password.echo: false menu.checking_password.error_incorrect: Incorrect password, please try again... menu.checking_password.prompt: "Password:" -menu.choose_gender.action: user.avatar.set("gender", user.menu_choices[choice]) +menu.choose_gender.action: user.avatar.set("gender", + user.menu_choices[user.choice]) menu.choose_gender.branch: choose_name menu.choose_gender.choice_f: female menu.choose_gender.choice_m: male @@ -47,7 +48,8 @@ menu.choose_gender.description: First, your new avatar needs a birth gender. Avatars are one of male or female at birth. menu.choose_gender.prompt: "Pick a birth gender for your new avatar:" -menu.choose_name.action: user.avatar.set("name", user.menu_choices[choice]) +menu.choose_name.action: user.avatar.set("name", + user.menu_choices[user.choice]) menu.choose_name.branch: main_utility menu.choose_name.branch_g: choose_name menu.choose_name.choice_g: generate more names @@ -78,7 +80,7 @@ menu.delete_account.prompt: Are you certain you wish to permanently delete your account? menu.delete_avatar.action: user.delete_avatar( - user.account.get("avatars")[int(choice)-1]) + user.account.get("avatars")[int(user.choice)-1]) menu.delete_avatar.action_a: pass menu.delete_avatar.branch: main_utility menu.delete_avatar.branch_a: main_utility -- 2.11.0