Abstraction of generic player object searches, as should've been case from the beginning. Bad me for letting this get out of hand! In any case, this eliminated a lot of code and makes pretty much all of the commands that interact with objects a lot neater, readable, and consistent.

This commit is contained in:
Greg Taylor 2007-05-24 14:22:11 +00:00
parent b47d0d2b07
commit b6c869b076
4 changed files with 154 additions and 226 deletions

View file

@ -48,18 +48,10 @@ def cmd_look(cdat):
if len(args) == 0: if len(args) == 0:
target_obj = pobject.get_location() target_obj = pobject.get_location()
else: else:
results = functions_db.local_and_global_search(pobject, ' '.join(args)) target_obj = functions_db.standard_plr_objsearch(session, ' '.join(args))
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(results) > 1: if not target_obj:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return return
elif len(results) == 0:
session.msg("I don't see that here.")
return
else:
target_obj = results[0]
retval = "%s\r\n%s" % ( retval = "%s\r\n%s" % (
target_obj.get_name(), target_obj.get_name(),
@ -106,19 +98,10 @@ def cmd_get(cdat):
session.msg("Get what?") session.msg("Get what?")
return return
else: else:
results = functions_db.local_and_global_search(pobject, ' '.join(args), search_contents=False) target_obj = functions_db.standard_plr_objsearch(session, ' '.join(args), search_contents=False)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(results) > 1: if not target_obj:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return return
elif len(results) == 0:
session.msg("I don't see that here.")
return
else:
# We've got a victim to get now.
target_obj = results[0]
if pobject == target_obj: if pobject == target_obj:
session.msg("You can't get yourself.") session.msg("You can't get yourself.")
@ -149,19 +132,10 @@ def cmd_drop(cdat):
session.msg("Drop what?") session.msg("Drop what?")
return return
else: else:
results = functions_db.local_and_global_search(pobject, ' '.join(args), search_location=False) target_obj = functions_db.standard_plr_objsearch(session, ' '.join(args), search_location=False)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(results) > 1: if not target_obj:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return return
elif len(results) == 0:
session.msg("You don't appear to be carrying that.")
return
else:
# We've got a victim to get now.
target_obj = results[0]
if not pobject == target_obj.get_location(): if not pobject == target_obj.get_location():
session.msg("You don't appear to be carrying that.") session.msg("You don't appear to be carrying that.")
@ -199,18 +173,10 @@ def cmd_examine(cdat):
else: else:
searchstr = ' '.join(args) searchstr = ' '.join(args)
results = functions_db.local_and_global_search(pobject, searchstr) target_obj = functions_db.standard_plr_objsearch(session, searchstr)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(results) > 1: if not target_obj:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return return
elif len(results) == 0:
session.msg("I don't see that here.")
return
else:
target_obj = results[0]
if attr_search: if attr_search:
attr_matches = target_obj.attribute_namesearch(attr_searchstr) attr_matches = target_obj.attribute_namesearch(attr_searchstr)

View file

@ -53,34 +53,26 @@ def cmd_destroy(cdat):
session.msg("Destroy what?") session.msg("Destroy what?")
return return
else: else:
results = functions_db.local_and_global_search(pobject, ' '.join(args)) target_obj = functions_db.standard_plr_objsearch(session, ' '.join(args))
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if not target_obj:
return
if len(results) > 1: if target_obj.is_player():
session.msg("More than one match found (please narrow target):") if pobject.id == target_obj.id:
for result in results:
session.msg(" %s" % (result.get_name(),))
return
elif len(results) == 0:
session.msg("I don't see that here.")
return
elif results[0].is_player():
if pobject.id == results[0].id:
session.msg("You can't destroy yourself.") session.msg("You can't destroy yourself.")
return return
if not switch_override: if not switch_override:
session.msg("You must use @destroy/override on players.") session.msg("You must use @destroy/override on players.")
return return
if results[0].is_superuser(): if target_obj.is_superuser():
session.msg("You can't destroy a superuser.") session.msg("You can't destroy a superuser.")
return return
target_obj = results[0] elif target_obj.is_going() or target_obj.is_garbage():
elif results[0].is_going() or results[0].is_garbage():
session.msg("That object is already destroyed.") session.msg("That object is already destroyed.")
return return
else:
target_obj = results[0]
session.msg("You destroy %s." % (target_obj,)) session.msg("You destroy %s." % (target_obj.get_name(),))
target_obj.destroy() target_obj.destroy()
def cmd_list(cdat): def cmd_list(cdat):
@ -130,18 +122,11 @@ def cmd_description(cdat):
elif len(eq_args) < 2: elif len(eq_args) < 2:
session.msg("How would you like to describe that object?") session.msg("How would you like to describe that object?")
else: else:
results = functions_db.local_and_global_search(pobject, searchstring) target_obj = functions_db.standard_plr_objsearch(session, searchstring)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if not target_obj:
return
if len(results) > 1:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return
elif len(results) == 0:
session.msg("I don't see that here.")
return
else:
target_obj = results[0]
if not pobject.controls_other(target): if not pobject.controls_other(target):
session.msg(defines_global.NOCONTROL_MSG) session.msg(defines_global.NOCONTROL_MSG)
return return
@ -168,26 +153,24 @@ def cmd_newpassword(cdat):
session.msg("You must supply a new password.") session.msg("You must supply a new password.")
return return
results = functions_db.local_and_global_search(pobject, searchstring) target_obj = functions_db.standard_plr_objsearch(session, searchstring)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if not target_obj:
return
if not target_obj.is_player():
if len(results) > 1: session.msg("You can only change passwords on players.")
session.msg("More than one match found (please narrow target):") elif not pobject.controls_other(target_obj):
for result in results: session.msg("You do not control %s." % (target_obj.get_name(),))
session.msg(" %s" % (result.get_name(),))
elif len(results) == 0:
session.msg("I don't see that here.")
elif not pobject.controls_other(results[0]):
session.msg("You do not control %s." % (results[0],))
else: else:
uaccount = results[0].get_user_account() uaccount = target_obj.get_user_account()
if len(newpass) == 0: if len(newpass) == 0:
uaccount.set_password() uaccount.set_password()
else: else:
uaccount.set_password(newpass) uaccount.set_password(newpass)
uaccount.save() uaccount.save()
session.msg("%s - PASSWORD set." % (results[0],)) session.msg("%s - PASSWORD set." % (target_obj.get_name(),))
results[0].emit_to("%s has changed your password." % (pobject,)) target_obj.emit_to("%s has changed your password." % (pobject.get_name(show_dbref=False),))
def cmd_password(cdat): def cmd_password(cdat):
""" """
@ -234,21 +217,15 @@ def cmd_name(cdat):
elif len(eq_args) < 2: elif len(eq_args) < 2:
session.msg("What would you like to name that object?") session.msg("What would you like to name that object?")
else: else:
results = functions_db.local_and_global_search(pobject, searchstring) target_obj = functions_db.standard_plr_objsearch(session, searchstring)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if not target_obj:
return
if len(results) > 1: if len(eq_args[1]) == 0:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return
elif len(results) == 0:
session.msg("I don't see that here.")
return
elif len(eq_args[1]) == 0:
session.msg("What would you like to name that object?") session.msg("What would you like to name that object?")
else: else:
newname = '='.join(eq_args[1:]) newname = '='.join(eq_args[1:])
target_obj = results[0]
session.msg("You have renamed %s to %s." % (target_obj, ansi.parse_ansi(newname, strip_formatting=True))) session.msg("You have renamed %s to %s." % (target_obj, ansi.parse_ansi(newname, strip_formatting=True)))
target_obj.set_name(newname) target_obj.set_name(newname)
@ -348,15 +325,11 @@ def cmd_open(cdat):
if len(eq_args) > 1: if len(eq_args) > 1:
# Opening an exit to another location via @open <Name>=<Dbref>[,<Name>]. # Opening an exit to another location via @open <Name>=<Dbref>[,<Name>].
comma_split = eq_args[1].split(',') comma_split = eq_args[1].split(',')
destination = functions_db.local_and_global_search(pobject, comma_split[0]) destination = functions_db.standard_plr_objsearch(session, comma_split[0])
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(destination) == 0: if not destination:
session.msg("I can't find the location to link to.")
return return
elif len(destination) > 1:
session.msg("Multiple results returned for exit destination!")
else:
destination = destination[0]
if destination.is_exit(): if destination.is_exit():
session.msg("You can't open an exit to an exit!") session.msg("You can't open an exit to an exit!")
return return
@ -404,40 +377,28 @@ def cmd_link(cdat):
return return
if len(eq_args) > 1: if len(eq_args) > 1:
target = functions_db.local_and_global_search(pobject, target_name) target_obj = functions_db.standard_plr_objsearch(session, target_name)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(target) == 0: if not target_obj:
session.msg("I can't find the object you want to link.")
return
elif len(target) > 1:
session.msg("Multiple results returned for link target.")
return return
# We know we can get the first entry now. if not pobject.controls_other(target_obj):
target = target[0]
if not pobject.controls_other(target):
session.msg(defines_global.NOCONTROL_MSG) session.msg(defines_global.NOCONTROL_MSG)
return return
# If we do something like "@link blah=", we unlink the object. # If we do something like "@link blah=", we unlink the object.
if len(dest_name) == 0: if len(dest_name) == 0:
target.set_home(None) target_obj.set_home(None)
session.msg("You have unlinked %s." % (target,)) session.msg("You have unlinked %s." % (target_obj,))
return return
destination = functions_db.local_and_global_search(pobject, dest_name) destination = functions_db.standard_plr_objsearch(session, dest_name)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(destination) == 0: if not destination:
session.msg("I can't find the location to link to.")
return
elif len(destination) > 1:
session.msg("Multiple results returned for destination.")
return return
destination = destination[0] target_obj.set_home(destination)
target.set_home(destination) session.msg("You link %s to %s." % (target_obj,destination))
session.msg("You link %s to %s." % (target,destination))
else: else:
# We haven't provided a target. # We haven't provided a target.
@ -456,23 +417,17 @@ def cmd_unlink(cdat):
session.msg("Unlink what?") session.msg("Unlink what?")
return return
else: else:
results = functions_db.local_and_global_search(pobject, ' '.join(args)) target_obj = functions_db.standard_plr_objsearch(session, ' '.join(args))
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if not target_obj:
return
if len(results) > 1: if not pobject.controls_other(target_obj):
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return
elif len(results) == 0:
session.msg("I don't see that here.")
return
else:
if not pobject.controls_other(results[0]):
session.msg(defines_global.NOCONTROL_MSG) session.msg(defines_global.NOCONTROL_MSG)
return return
results[0].set_home(None) target_obj.set_home(None)
session.msg("You have unlinked %s." % (results[0],)) session.msg("You have unlinked %s." % (target_obj.get_name(),))
def cmd_teleport(cdat): def cmd_teleport(cdat):
""" """
@ -495,32 +450,27 @@ def cmd_teleport(cdat):
# a direct teleport, @tel <destination>. # a direct teleport, @tel <destination>.
if len(eq_args) > 1: if len(eq_args) > 1:
# Equal sign teleport. # Equal sign teleport.
victim = functions_db.local_and_global_search(pobject, eq_args[0]) victim = functions_db.standard_plr_objsearch(session, eq_args[0])
destination = functions_db.local_and_global_search(pobject, eq_args[1]) # Use standard_plr_objsearch to handle duplicate/nonexistant results.
if not victim:
return
if len(victim) == 0: destination = functions_db.standard_plr_objsearch(session, eq_args[1])
session.msg("I can't find the victim to teleport.") # Use standard_plr_objsearch to handle duplicate/nonexistant results.
if not destination:
return return
elif len(destination) == 0:
session.msg("I can't find the destination for the victim.")
return
elif len(victim) > 1:
session.msg("Multiple results returned for victim!")
return
elif len(destination) > 1:
session.msg("Multiple results returned for destination!")
else:
if victim == destination: if victim == destination:
session.msg("You can't teleport an object inside of itself!") session.msg("You can't teleport an object inside of itself!")
return return
session.msg("Teleported.") session.msg("Teleported.")
victim[0].move_to(destination[0]) victim.move_to(destination)
# This is somewhat kludgy right now, we'll have to find a better way # This is somewhat kludgy right now, we'll have to find a better way
# to do it sometime else. If we can find a session in the server's # to do it sometime else. If we can find a session in the server's
# session list matching the object we're teleporting, force it to # session list matching the object we're teleporting, force it to
# look. This is going to typically be a player. # look. This is going to typically be a player.
victim_session = session_mgr.session_from_object(victim[0]) victim_session = session_mgr.session_from_object(victim)
if victim_session: if victim_session:
# We need to form up a new cdat dictionary to pass with the command. # We need to form up a new cdat dictionary to pass with the command.
# Kinda yucky I guess. # Kinda yucky I guess.
@ -529,21 +479,16 @@ def cmd_teleport(cdat):
else: else:
# Direct teleport (no equal sign) # Direct teleport (no equal sign)
results = functions_db.local_and_global_search(pobject, search_str) target_obj = functions_db.standard_plr_objsearch(session, search_str)
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(results) > 1: if not target_obj:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
elif len(results) == 0:
session.msg("I don't see that here.")
return return
else:
if results[0] == pobject: if target_obj == pobject:
session.msg("You can't teleport inside yourself!") session.msg("You can't teleport inside yourself!")
return return
session.msg("Teleported.") session.msg("Teleported.")
pobject.move_to(results[0]) pobject.move_to(target_obj)
commands_general.cmd_look(cdat) commands_general.cmd_look(cdat)
def cmd_set(cdat): def cmd_set(cdat):
@ -568,16 +513,11 @@ def cmd_set(cdat):
session.msg("Set what?") session.msg("Set what?")
return return
victim = functions_db.local_and_global_search(pobject, eq_args[0]) victim = functions_db.standard_plr_objsearch(session, eq_args[0])
# Use standard_plr_objsearch to handle duplicate/nonexistant results.
if len(victim) == 0: if not victim:
session.msg("I don't see that here.")
return
elif len(victim) > 1:
session.msg("I don't know which one you mean!")
return return
victim = victim[0]
if not pobject.controls_other(victim): if not pobject.controls_other(victim):
session.msg(defines_global.NOCONTROL_MSG) session.msg(defines_global.NOCONTROL_MSG)
return return

View file

@ -100,6 +100,28 @@ def player_search(searcher, ostring):
else: else:
return local_and_global_search(searcher, ostring, limit_types=[global_defines.OTYPE_PLAYER]) return local_and_global_search(searcher, ostring, limit_types=[global_defines.OTYPE_PLAYER])
def standard_plr_objsearch(session, ostring, search_contents=True, search_location=True, dbref_only=False, limit_types=False):
"""
Perform a standard object search via a player session, handling multiple
results and lack thereof gracefully.
session: (SessionProtocol) Reference to the player's session.
ostring: (str) The string to match object names against.
"""
pobject = session.get_pobject()
results = local_and_global_search(pobject, ostring, search_contents=search_contents, search_location=search_location, dbref_only=dbref_only, limit_types=limit_types)
if len(results) > 1:
session.msg("More than one match found (please narrow target):")
for result in results:
session.msg(" %s" % (result.get_name(),))
return False
elif len(results) == 0:
session.msg("I don't see that here.")
return False
else:
return results[0]
def alias_search(searcher, ostring): def alias_search(searcher, ostring):
""" """
Search players by alias. Returns a list of objects whose "ALIAS" attribute Search players by alias. Returns a list of objects whose "ALIAS" attribute

View file

@ -49,7 +49,7 @@ def log_errmsg(errormsg):
errormsg: (string) The message to be logged. errormsg: (string) The message to be logged.
""" """
log.error('ERROR: %s' % (errormsg,)) log.err('ERROR: %s' % (errormsg,))
def log_infomsg(infomsg): def log_infomsg(infomsg):
""" """