projects
/
mudpy.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add a mudpy.menu module to hold menu functions
[mudpy.git]
/
mudpy
/
misc.py
diff --git
a/mudpy/misc.py
b/mudpy/misc.py
index
045e6a5
..
dc37b6e
100644
(file)
--- a/
mudpy/misc.py
+++ b/
mudpy/misc.py
@@
-406,7
+406,7
@@
class Universe:
"""Create a new, empty Universe (the Big Bang)."""
new_universe = Universe()
for attribute in vars(self).keys():
"""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
new_universe.reload_flag = False
del self
return new_universe
@@
-457,10
+457,8
@@
class Universe:
self.listening_socket.listen(1)
# note that we're now ready for user connections
self.listening_socket.listen(1)
# note that we're now ready for user connections
- log(
- "Listening for Telnet connections on: " +
- host + ":" + str(port)
- )
+ log("Listening for Telnet connections on %s port %s" % (
+ host, str(port)))
def get_time(self):
"""Convenience method to get the elapsed time counter."""
def get_time(self):
"""Convenience method to get the elapsed time counter."""
@@
-490,6
+488,7
@@
class User:
self.address = ""
self.authenticated = False
self.avatar = None
self.address = ""
self.authenticated = False
self.avatar = None
+ self.choice = ""
self.columns = 79
self.connection = None
self.error = ""
self.columns = 79
self.connection = None
self.error = ""
@@
-639,14
+638,13
@@
class User:
"""Flag the user as authenticated and disconnect duplicates."""
if self.state != "authenticated":
self.authenticated = True
"""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)
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."""
def show_menu(self):
"""Send the user their current menu."""
@@
-1584,8
+1582,8
@@
def get_menu_prompt(state):
def get_menu_choices(user):
"""Return a dict of choice:meaning."""
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:
if create_choices:
choices = eval(create_choices)
else:
@@
-1593,7
+1591,7
@@
def get_menu_choices(user):
ignores = []
options = {}
creates = {}
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)
):
if facet.startswith("demand_") and not eval(
universe.groups["menu"][user.state].get(facet)
):
@@
-1604,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:
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:
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
return choices
@@
-1641,12
+1639,12
@@
def get_default_branch(state):
return universe.groups["menu"][state].get("branch")
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)
"""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 ""
return get_default_branch(user.state)
else:
return ""
@@
-1668,12
+1666,12
@@
def get_default_action(state):
return universe.groups["menu"][state].get("action")
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)
"""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 ""
return get_default_action(user.state)
else:
return ""
@@
-1688,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
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)"
)
- e
lse
:
+
try
:
+
globals()["handler_" + user.state](user
)
+ e
xcept KeyError
:
generic_menu_handler(user)
# since we got input, flag that the menu/prompt needs to be redisplayed
generic_menu_handler(user)
# since we got input, flag that the menu/prompt needs to be redisplayed
@@
-1705,16
+1703,16
@@
def generic_menu_handler(user):
# get a lower-case representation of the next line of input
if user.input_queue:
# 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:
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:
if new_state:
user.state = new_state
else:
@@
-1893,12
+1891,14
@@
def handler_active(user):
if actor.can_run(command):
# dereference the relative object path for the requested function
action = mudpy
if actor.can_run(command):
# dereference the relative object path for the requested function
action = mudpy
- for component in command.get("action").split("."):
+ action_fname = command.get("action", command.key)
+ for component in action_fname.split("."):
try:
action = getattr(action, component)
ran = True
except AttributeError:
try:
action = getattr(action, component)
ran = True
except AttributeError:
- log('Could not find action function for command "%s"')
+ log('Could not find action function "%s" for command "%s"'
+ % (action_fname, command_name))
action = None
break
if action:
action = None
break
if action:
@@
-1909,6
+1909,7
@@
def handler_active(user):
'exception...\n%s' % (
input_data, actor.owner.account.get("name"),
traceback.format_exc()))
'exception...\n%s' % (
input_data, actor.owner.account.get("name"),
traceback.format_exc()))
+ mudpy.command.error(actor, input_data)
# if the command was not run, give an error
if not ran:
# if the command was not run, give an error
if not ran:
@@
-2077,8
+2078,9
@@
def setup():
log("Import path: %s" % ", ".join(sys.path), 1)
log("Installed dependencies: %s" % universe.versions.dependencies_text, 1)
log("Other python packages: %s" % universe.versions.environment_text, 1)
log("Import path: %s" % ", ".join(sys.path), 1)
log("Installed dependencies: %s" % universe.versions.dependencies_text, 1)
log("Other python packages: %s" % universe.versions.environment_text, 1)
- log("Started %s with command line: %s" % (
- universe.versions.version, " ".join(sys.argv)), 1)
+ log("Running version: %s" % universe.versions.version, 1)
+ log("Initial directory: %s" % universe.startdir, 1)
+ log("Command line: %s" % " ".join(sys.argv), 1)
# pass the initialized universe back
return universe
# pass the initialized universe back
return universe