Imported from archive.
[mudpy.git] / lib / muff / muffcmds.py
index 6ade3b1..c640c96 100644 (file)
@@ -53,11 +53,32 @@ def handle_user_input(user):
        """The main handler, branches to a state-specific handler."""
 
        # check to make sure the state is expected, then call that handler
-       exec("handler_" + user.state.replace(" ", "_") + "(user)")
+       try:
+               exec("handler_" + user.state + "(user)")
+       except NameError:
+               generic_menu_handler(user)
 
        # since we got input, flag that the menu/prompt needs to be redisplayed
        user.menu_seen = False
 
+       # if the user's client echo is off, send a blank line for aesthetics
+       if not user.echoing: user.send("", "")
+
+def generic_menu_handler(user):
+       """A generic menu choice handler."""
+
+       # get a lower-case representation of the next line of input
+       choice = user.input_queue.pop(0)
+       if choice: choice = choice.lower()
+
+       # run any script related to this choice
+       exec(muffmenu.get_choice_action(user, choice))
+
+       # move on to the next state or return an error
+       new_state = muffmenu.get_choice_branch(user, choice)
+       if new_state: user.state = new_state
+       else: user.error = "default"
+
 def handler_entering_account_name(user):
        """Handle the login account name."""
 
@@ -72,7 +93,7 @@ def handler_entering_account_name(user):
 
                # if we have a password hash, time to request a password
                if user.get_passhash():
-                       user.state = "checking password"
+                       user.state = "checking_password"
 
                # otherwise, this could be a brand new user
                else:
@@ -80,7 +101,7 @@ def handler_entering_account_name(user):
                        user.proposed_name = None
                        user.load()
                        muffmisc.log("New user: " + user.name)
-                       user.state = "checking new account name"
+                       user.state = "checking_new_account_name"
 
        # if the user entered nothing for a name, then buhbye
        else:
@@ -96,13 +117,12 @@ def handler_checking_password(user):
        if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash:
 
                # if so, set the username and load from cold storage
-               # TODO: branch to character creation and selection menus
                user.name = user.proposed_name
                del(user.proposed_name)
                if not user.replace_old_connections():
                        user.load()
                        user.authenticate()
-                       user.state = "active"
+                       user.state = "main_utility"
 
        # if at first your hashes don't match, try, try again
        elif user.password_tries < muffconf.getint("general", "password_tries"):
@@ -114,14 +134,6 @@ def handler_checking_password(user):
                user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
                user.state = "disconnecting"
 
-def handler_disconnecting(user):
-       """Waiting for the user's connection to close."""
-       pass
-
-def handler_disconnecting_duplicates(user):
-       """Waiting for duplicate connections to close."""
-       pass
-
 def handler_checking_new_account_name(user):
        """Handle input for the new user menu."""
 
@@ -134,7 +146,7 @@ def handler_checking_new_account_name(user):
 
        # if there's no input, use the default
        else:
-               choice = muffmenu.get_default(user)
+               choice = muffmenu.get_default_menu_choice(user.state)
 
        # user selected to disconnect
        if choice == "d":
@@ -142,11 +154,11 @@ def handler_checking_new_account_name(user):
 
        # go back to the login screen
        elif choice == "g":
-               user.state = "entering account name"
+               user.state = "entering_account_name"
 
        # new user, so ask for a password
        elif choice == "n":
-               user.state = "entering new password"
+               user.state = "entering_new_password"
 
        # user entered a non-existent option
        else:
@@ -164,7 +176,7 @@ def handler_entering_new_password(user):
 
                # hash and store it, then move on to verification
                user.passhash = md5.new(user.name + input_data).hexdigest()
-               user.state = "verifying new password"
+               user.state = "verifying_new_password"
 
        # the password was weak, try again if you haven't tried too many times
        elif user.password_tries < muffconf.getint("general", "password_tries"):
@@ -188,15 +200,14 @@ def handler_verifying_new_password(user):
                user.save()
 
                # the hashes matched, so go active
-               # TODO: branch to character creation and selection menus
-               if not user.replace_old_connections(): user.state = "active"
+               if not user.replace_old_connections(): user.state = "main_utility"
 
        # go back to entering the new password as long as you haven't tried
        # too many times
        elif user.password_tries < muffconf.getint("general", "password_tries"):
                user.password_tries += 1
                user.error = "differs"
-               user.state = "entering new password"
+               user.state = "entering_new_password"
 
        # otherwise, sayonara
        else:
@@ -357,6 +368,22 @@ def command_say(user, command="", parameters=""):
        else:
                user.send("What do you want to say?")
 
+def command_show(user, command="", parameters=""):
+       """Show program data."""
+       if parameters == "universe":
+               message = "These are the current elements in the universe:$(eol)"
+               keys = muffuniv.universe.contents.keys()
+               keys.sort()
+               for key in keys: message += "$(eol)   $(grn)" + key + "$(nrm)"
+       elif parameters == "avatars":
+               message = "These are the avatars managed by your account:$(eol)"
+               avatars = user.list_avatar_names()
+               avatars.sort()
+               for avatar in avatars: message += "$(eol)   $(grn)" + avatar + "$(nrm)"
+       elif parameters: message = "I don't know what \"" + parameters + "\" is."
+       else: message = "What do you want to show?"
+       user.send(message)
+
 def command_error(user, command="", parameters=""):
        """Generic error for an unrecognized command word."""