Looking through our command code after a long hiatus, I realized that it was pretty much awful. So here's part 1 of the command interpreter overhaul.
- The command handler has been drastically simplified. We were doing way too much processing in the handler that should have been done in the individual command functions themselves. - The 'cdat' dict we were previously passing around has been replaced with a Command object that has useful methods for performing some of the parsing command functions will probably want to do from time to time. - All commands were updated to use the new Command object, tested, and cleaned up in general. - A lot of formatting was cleaned up. - A lot of previously un-found bugs and limitations were fixed. - The 'page' command has been broken out into its own file, since it's going to have a number of functions that would otherwise clutter commands/general.py. Expect a commit (probably later today) that will clean up the second half of cmdhandler.py.
This commit is contained in:
parent
37d66093cc
commit
d58f4eb517
16 changed files with 818 additions and 698 deletions
119
src/commands/paging.py
Normal file
119
src/commands/paging.py
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
"""
|
||||
Paging command and support functions.
|
||||
"""
|
||||
from apps.objects.models import Object
|
||||
from src import defines_global
|
||||
|
||||
def get_last_paged_objects(pobject):
|
||||
"""
|
||||
Returns a list of objects of the user's last paged list, or None if invalid
|
||||
or non-existant.
|
||||
"""
|
||||
last_paged_dbrefs = pobject.get_attribute_value("LASTPAGED")
|
||||
if last_paged_dbrefs is not False:
|
||||
last_paged_objects = list()
|
||||
try:
|
||||
last_paged_dbref_list = [
|
||||
x.strip() for x in last_paged_dbrefs.split(',')
|
||||
]
|
||||
for dbref in last_paged_dbref_list:
|
||||
if not Object.objects.is_dbref(dbref):
|
||||
raise ValueError
|
||||
last_paged_object = Object.objects.dbref_search(dbref)
|
||||
if last_paged_object is not None:
|
||||
last_paged_objects.append(last_paged_object)
|
||||
return last_paged_objects
|
||||
except ValueError:
|
||||
# Remove the invalid LASTPAGED attribute
|
||||
pobject.clear_attribute("LASTPAGED")
|
||||
return None
|
||||
|
||||
def cmd_page(command):
|
||||
"""
|
||||
Send a message to target user (if online).
|
||||
"""
|
||||
session = command.session
|
||||
pobject = session.get_pobject()
|
||||
server = command.server
|
||||
args = command.command_argument.split()
|
||||
targets = []
|
||||
|
||||
# Get the last paged person(s)
|
||||
last_paged_objects = get_last_paged_objects(pobject)
|
||||
|
||||
# If they don't give a target, or any data to send to the target
|
||||
# then tell them who they last paged if they paged someone, if not
|
||||
# tell them they haven't paged anyone.
|
||||
if not command.command_argument:
|
||||
if last_paged_objects:
|
||||
session.msg("You last paged: %s." % (
|
||||
', '.join([x.name for x in last_paged_objects])))
|
||||
return
|
||||
session.msg("You have not paged anyone.")
|
||||
return
|
||||
|
||||
# Build a list of targets
|
||||
# If there are no targets, then set the targets to the last person they
|
||||
# paged.
|
||||
cmd_targets = command.get_arg_targets()
|
||||
if cmd_targets is None:
|
||||
targets = last_paged_objects
|
||||
else:
|
||||
# For each of the targets listed, grab their objects and append
|
||||
# it to the targets list
|
||||
for target in cmd_targets:
|
||||
matched_object = Object.objects.local_and_global_search(pobject,
|
||||
target,
|
||||
limit_types=[defines_global.OTYPE_PLAYER])
|
||||
if matched_object:
|
||||
targets.append(matched_object[0])
|
||||
print "MATCH:", matched_object[0]
|
||||
else:
|
||||
# search returned None
|
||||
session.msg("Player '%s' can not be found." % (
|
||||
target))
|
||||
|
||||
# Depending on the argument provided, either send the entire thing as
|
||||
# a message or break off the point after the equal sign.
|
||||
if command.arg_has_target():
|
||||
message = command.get_arg_target_value()
|
||||
else:
|
||||
message = command.command_argument
|
||||
|
||||
sender_name = pobject.get_name(show_dbref=False)
|
||||
# Build our messages
|
||||
target_message = "%s pages: %s"
|
||||
sender_message = "You paged %s with '%s'."
|
||||
# Handle paged emotes
|
||||
if message.startswith(':'):
|
||||
message = message[1:]
|
||||
target_message = "From afar, %s %s"
|
||||
sender_message = "Long distance to %s: %s %s"
|
||||
# Handle paged emotes without spaces
|
||||
if message.startswith(';'):
|
||||
message = message[1:]
|
||||
target_message = "From afar, %s%s"
|
||||
sender_message = "Long distance to %s: %s%s"
|
||||
|
||||
# We build a list of target_names for the sender_message later
|
||||
target_names = []
|
||||
for target in targets:
|
||||
# Check to make sure they're connected, or a player
|
||||
if target.is_connected_plr():
|
||||
target.emit_to(target_message % (sender_name, message))
|
||||
target_names.append(target.get_name(show_dbref=False))
|
||||
else:
|
||||
session.msg("Player %s does not exist or is not online." % (
|
||||
target.get_name(show_dbref=False)))
|
||||
|
||||
# Now send a confirmation to the person doing the paging.
|
||||
if len(target_names) > 0:
|
||||
target_names_string = ', '.join(target_names)
|
||||
try:
|
||||
session.msg(sender_message % (target_names_string, sender_name, message))
|
||||
except TypeError:
|
||||
session.msg(sender_message % (target_names_string, message))
|
||||
|
||||
# Now set the LASTPAGED attribute
|
||||
pobject.set_attribute("LASTPAGED", ','.join(
|
||||
["#%d" % (x.id) for x in targets]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue