Relocate choose_gender menu function
[mudpy.git] / mudpy / misc.py
index b83215c..dc37b6e 100644 (file)
@@ -406,7 +406,7 @@ class Universe:
         """Create a new, empty Universe (the Big Bang)."""
         new_universe = Universe()
         for attribute in vars(self).keys():
-            exec("new_universe." + attribute + " = self." + attribute)
+            setattr(new_universe, attribute, getattr(self, attribute))
         new_universe.reload_flag = False
         del self
         return new_universe
@@ -488,6 +488,7 @@ class User:
         self.address = ""
         self.authenticated = False
         self.avatar = None
+        self.choice = ""
         self.columns = 79
         self.connection = None
         self.error = ""
@@ -637,14 +638,13 @@ class User:
         """Flag the user as authenticated and disconnect duplicates."""
         if self.state != "authenticated":
             self.authenticated = True
+            log("User %s authenticated for account %s." % (
+                    self, self.account.subkey), 2)
             if ("mudpy.limit" in universe.contents and self.account.subkey in
                     universe.contents["mudpy.limit"].get("admins")):
                 self.account.set("administrator", True)
-                log("Administrator %s authenticated." %
-                    self.account.get("name"), 2)
-            else:
-                log("User %s authenticated for account %s." % (
-                        self, self.account.subkey), 2)
+                log("Account %s is an administrator." % (
+                        self.account.subkey), 2)
 
     def show_menu(self):
         """Send the user their current menu."""
@@ -1582,8 +1582,8 @@ def get_menu_prompt(state):
 
 def get_menu_choices(user):
     """Return a dict of choice:meaning."""
-    menu = universe.groups["menu"][user.state]
-    create_choices = menu.get("create")
+    state = universe.groups["menu"][user.state]
+    create_choices = state.get("create")
     if create_choices:
         choices = eval(create_choices)
     else:
@@ -1591,7 +1591,7 @@ def get_menu_choices(user):
     ignores = []
     options = {}
     creates = {}
-    for facet in menu.facets():
+    for facet in state.facets():
         if facet.startswith("demand_") and not eval(
            universe.groups["menu"][user.state].get(facet)
            ):
@@ -1602,10 +1602,10 @@ def get_menu_choices(user):
             options[facet] = facet.split("_", 2)[1]
     for facet in creates.keys():
         if not creates[facet] in ignores:
-            choices[creates[facet]] = eval(menu.get(facet))
+            choices[creates[facet]] = eval(state.get(facet))
     for facet in options.keys():
         if not options[facet] in ignores:
-            choices[options[facet]] = menu.get(facet)
+            choices[options[facet]] = state.get(facet)
     return choices
 
 
@@ -1639,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 ""
@@ -1666,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 ""
@@ -1686,9 +1686,9 @@ def handle_user_input(user):
         user.send("", add_prompt=False, prepend_padding=False)
 
     # check to make sure the state is expected, then call that handler
-    if "handler_" + user.state in globals():
-        exec("handler_" + user.state + "(user)")
-    else:
+    try:
+        globals()["handler_" + user.state](user)
+    except KeyError:
         generic_menu_handler(user)
 
     # since we got input, flag that the menu/prompt needs to be redisplayed
@@ -1703,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: