Added a give command to the default command set. As part of this also fixed some bugs in how object typeclasses were compared.

This commit is contained in:
Griatch 2012-12-08 17:11:22 +01:00
parent 949da47917
commit c615693a2a
8 changed files with 53 additions and 21 deletions

View file

@ -1678,7 +1678,7 @@ class CmdExamine(ObjManipCommand):
string += headers["cmdset"] % ("\n ".join("%s (prio %s)" % (cmdset.path, cmdset.priority) for cmdset in all_cmdsets))
#cmdsetstr = "\n".join([utils.fill(cmdset, indent=2) for cmdset in str(obj.cmdset).split("\n")])
# list the actually available commands
# list the commands available to this object
avail_cmdset = sorted([cmd.key for cmd in avail_cmdset if cmd.access(obj, "cmd")])
cmdsetstr = utils.fill(", ".join(avail_cmdset), indent=2)

View file

@ -27,6 +27,7 @@ class DefaultCmdSet(CmdSet):
self.add(general.CmdNick())
self.add(general.CmdGet())
self.add(general.CmdDrop())
self.add(general.CmdGive())
self.add(general.CmdSay())
self.add(general.CmdAccess())
self.add(general.CmdColorTest())

View file

@ -11,7 +11,7 @@ from src.commands.default.muxcommand import MuxCommand, MuxCommandOOC
# limit symbol import for API
__all__ = ("CmdHome", "CmdLook", "CmdPassword", "CmdNick",
"CmdInventory", "CmdGet", "CmdDrop", "CmdQuit", "CmdWho",
"CmdInventory", "CmdGet", "CmdDrop", "CmdGive", "CmdQuit", "CmdWho",
"CmdSay", "CmdPose", "CmdEncoding", "CmdAccess",
"CmdOOCLook", "CmdIC", "CmdOOC", "CmdColorTest")
@ -345,6 +345,42 @@ class CmdDrop(MuxCommand):
obj.at_drop(caller)
class CmdGive(MuxCommand):
"""
give away things
Usage:
give <inventory obj> = <target>
Gives an items from your inventory to another character,
placing it in their inventory.
"""
key = "give"
locks = "cmd:all()"
def func(self):
"Implement give"
caller = self.caller
if not self.args or not self.rhs:
caller.msg("Usage: give <inventory object> = <target>")
return
to_give = caller.search(self.lhs)
target = caller.search(self.rhs)
if not (to_give and target):
return
if target == caller:
caller.msg("You keep %s to yourself." % to_give.key)
return
if not to_give.location == caller:
caller.msg("You are not holding %s." % to_give.key)
return
# give object
to_give.location = target
caller.msg("You give %s to %s." % (to_give.key, target.key))
target.msg("%s gives you %s." % (caller.key, to_give.key))
class CmdQuit(MuxCommand):
"""
quit

View file

@ -387,13 +387,11 @@ class Object(TypeClass):
parent doesn't work.
"""
try:
return _GA(_GA(self, "dbobj"),"dbid") == other \
or _GA(_GA(self, "dbobj"),"dbid") == _GA(_GA(other,"dbobj"),"dbid")
return _GA(_GA(self, "dbobj"),"dbid") == _GA(_GA(other,"dbobj"),"dbid")
except AttributeError:
# compare players instead
try:
return _GA(_GA(_GA(self, "dbobj"),"player"),"uid") == other \
or _GA(_GA(_GA(self, "dbobj"),"player"),"uid") == _GA(_GA(other, "player"),"uid")
return _GA(_GA(_GA(self, "dbobj"),"player"),"uid") == _GA(_GA(other, "player"),"uid")
except AttributeError:
return False

View file

@ -157,10 +157,9 @@ class TypeClass(object):
dbobj-recognized comparison
"""
try:
return other == self or other == _GA(self, dbobj) or other == _GA(self, dbobj).user
return _GA(_GA(self, "dbobj"), "dbid") == _GA(_GA(other, "dbobj"), "dbid")
except AttributeError:
# if self.dbobj.user fails it means the two previous comparisons failed already
return False
return id(self) == id(other)
def __delattr__(self, propname):

View file

@ -350,13 +350,10 @@ def to_str(obj, encoding='utf-8', force_string=False):
if force_string and not isinstance(obj, basestring):
# some sort of other object. Try to
# convert it to a string representation.
if hasattr(obj, '__str__'):
obj = obj.__str__()
elif hasattr(obj, '__unicode__'):
obj = obj.__unicode__()
else:
# last resort
try:
obj = str(obj)
except Exception:
obj = unicode(obj)
if isinstance(obj, basestring) and isinstance(obj, unicode):
try: