def give_all(player, items, target_name, target=None): if not target: target = player.location.search_creature(target_name) if not target: raise ActionRefused("%s isn't here with you." % target_name) if target is player: raise ActionRefused("Giving something to yourself doesn't make much sense.") # Actually try to give the items... items = list(items) refused = [] for item in items: try: item.move(target, player) except ActionRefused as x: refused.append((item, str(x))) # Let the player know why giving a particular item failed for item, message in refused: player.tell(message) items.remove(item) if items: items_str = Lang.join(Lang.a(item.title) for item in items) player_str = Lang.capital(player.title) room_msg = "<player>%s</player> gives <item>%s</item> to <creature>%s</creature>." % (player_str, items_str, target.title) target_msg = "<player>%s</player> gives you <item>%s</item>." % (player_str, items_str) player.location.tell(room_msg, exclude_creature=player, specific_targets=[target], specific_target_msg=target_msg) player.tell("You give <creature>%s</creature> <item>%s</item>." % (target.title, items_str)) else: player.tell("You weren't able to give <creature>%s</creature> anything." % target.title)
def func(player, parsed, ctx): if len(parsed.args) < 1: raise ActionRefused("Who are you trying to communicate with? You should use the form <userinput>tell [to name] 'your message'</userinput>") name = parsed.args[0] creature = player.location.search_creature(name) if not creature: creature = ctx.engine.search_player(name) # is there a player around with this name? if not creature: raise ActionRefused("%s is not online." % name) # We found the person or creature. Make sure it isn't the player his/herself if creature is player: player.tell("A voice booms from deep within your mind 'Talk to yourself on your own time!'") # Seems to be a legit target else: # Some text formatting stuff # TODO: See Say.py for some limitations we should correct here too if parsed.unparsed[0:3] == "to " and len(parsed.unparsed.strip()) > 5: unparsed = parsed.unparsed[len(name) + 3:].lstrip() else: unparsed = parsed.unparsed[len(name):].lstrip() if unparsed: creature.tell("<player>%s</player> tells you: %s" % (player.name, unparsed)) player.tell("You sent <creature>%s</creature> the message: %s" % (name, unparsed)) # Looks like they forgot to provide a message. else: player.tell("What would you like to tell %s?" % creature.objective)
def func(player, parsed, ctx): if len(parsed.args) < 1: raise ActionRefused("Who are you trying to whisper to? You should use the form <userinput>whisper [to name] 'your message'</userinput>") # we can't use parsed.obj_order directly, because the message could be directed to a player # that is not in the same location (and hence will not appear in parsed.obj_order) name = parsed.args[0] creature = player.location.search_creature(name) if not creature: creature = ctx.engine.search_player(name) # is there a player around with this name? if not creature: raise ActionRefused("%s is not online." % name) # We found the person or creature. Make sure it isn't the player his/herself if creature is player: player.tell("A voice booms from deep within your mind 'Mutter to yourself on your own time!'") # Seems to be a legit target else: # Some text formatting stuff # TODO: See Say.py for some limitations we should correct here too if parsed.unparsed[0:3] == "to " and len(parsed.unparsed.strip()) > 5: unparsed = parsed.unparsed[len(name) + 3:].lstrip() else: unparsed = parsed.unparsed[len(name):].lstrip() if unparsed: creature.tell("<player>%s</player> whispers '%s' to you." % (player.name, unparsed)) player.tell("You whisper the message %s to %s" % (unparsed, name)) # Looks like they forgot to provide a message. else: player.tell("What would you like to whisper to %s?" % creature.objective)
def func(player, parsed, ctx): if len(parsed.args) != 2 or len(parsed.obj_order) < 1: raise ActionRefused("You must specify what to move and where to move it." "Only functional for items and creatures in your current location.") thing = parsed.obj_order[0] if isinstance(thing, Creature): raise ActionRefused("Use the 'transport' action to move creatures to another location.") # Is the player's current location the target? if parsed.args[1] == "here" and len(parsed.obj_order) == 1: target = player.location # If not the target must be another creature's inventory elif len(parsed.obj_order) == 2: target = parsed.obj_order[1] else: raise ParseError("I'm not sure what you want to move or where you want to move it to.") if thing is target: raise ActionRefused("You can not move something inside itself.") # What sort of container are we moving the item into? if thing in player: thing_container = player elif thing in player.location: thing_container = player.location else: raise ParseError("I don't understand what you are trying to move the item into.") thing.move(target, player) player.tell("Successfully moved <item>%s</item> from %s to %s." % (thing.name, thing_container.name, target.name))
def lock(self, actor, item=None): if self.locked: raise ActionRefused("It is already locked.") if item: if self.check_key(item): key = item else: raise ActionRefused("That can not be used to lock it.") else: key = self.search_key(actor) if not key: raise ActionRefused( "You don't seem to have the key necessary to lock it.") self.locked = True actor.tell("You lock the %s with %s." % (self.name, Lang.a(key.title))) actor.tell_others("{Title} locked the %s with %s." % (self.name, Lang.a(key.title))) if self.linked_door: self.linked_door.door.locked = True
def unlock(self, actor, item=None): if not self.locked: raise ActionRefused("It is not locked.") if item: if self.check_key(item): key = item else: raise ActionRefused("You can't use that to unlock it.") else: key = self.search_key(actor) if not key: raise ActionRefused( "You don't seem to have the key necessary to unlock it.") self.locked = False actor.tell("You unlock the %s with %s." % (self.name, Lang.a(key.title))) actor.tell_others("{Title} unlocked the %s with %s." % (self.name, Lang.a(key.title))) if self.linked_door: self.linked_door.door.locked = False
def func(player, parsed, ctx): if not parsed.obj_order: raise ActionRefused("What do you want to use?") subj = "" if len(parsed.obj_order) > 1: item1, item2 = tuple(parsed.obj_info) if item1 in player and item2 in player: ActionRefused( "To use multiple items together you'll need to combine them first." ) else: who = parsed.obj_order[0] if isinstance(who, Creature): if who is player: raise ActionRefused("You can't use another player.") subj = who.objective else: subj = "it" raise ActionRefused( "You'll need to be more specific and tell me exactly what you want to do with %s." % subj)
def parse_is_are(cls, args): if args: if args[0] == "are": raise ActionRefused("Be more specific.") elif args[0] == "is": if len(args) >= 2: del args[0] # skip 'is', but only if more args follow else: raise ActionRefused("Who do you mean?")
def insert(self, item, actor): if self.check_key(item): if self.locked: raise ActionRefused( "You should try to unlock the door with it instead.") else: raise ActionRefused( "You should try to lock the door with it instead.") raise ActionRefused("The %s doesn't seem to fit." % item.title)
def func(player, parsed, ctx): if not parsed.args: raise ParseError( "What would you like to drop? You can also 'drop all' or 'drop everything'." ) def drop(items, container): items = list(items) refused = [] for item in items: try: item.move(player.location, player, verb="drop") if container is not player and container in player: Drop.notify_item_removal(player, item, container) except ActionRefused as x: refused.append((item, str(x))) for item, message in refused: items.remove(item) player.tell(message) if items: strItems = Lang.join(Lang.a(item.title) for item in items) player.tell("You discard <item>%s</item>." % strItems) player.tell_others("{Title} drops %s." % strItems) else: player.tell("Nothing was dropped.") arg = parsed.args[0] # drop all items? if arg == "all" or arg == "everything": drop(player.inventory, player) else: # drop a single item from inventory if parsed.obj_order: item = parsed.obj_order[0] if item in player: drop([item], player) else: raise ActionRefused("You can't seem to drop that!") # drop a container from inventory else: item, container = player.locate_item(arg, include_location=False) if item: if container is not player: Actions.print_object_location(player, item, container) drop([item], container) else: raise ActionRefused("You don't have <item>%s</item>." % Lang.a(arg))
def func(player, parsed, ctx): if not parsed.unparsed: raise ActionRefused( "What would you like to say? You should use the form <userinput>say [to name] 'your message'</userinput>" ) message = parsed.unparsed # Figure out who the player is trying to talk to. Requires the form: # 'Say to <creature> well, hello there # TODO: We should handle the case of form 'Say message to <creature>' target = "" if parsed.obj_order: possible_target = parsed.obj_order[0] if parsed.obj_info[possible_target].previous_word == "to": if parsed.args[0] in ( possible_target.name, possible_target.title ) or parsed.args[0] in possible_target.aliases: target = " to " + possible_target.title _, _, message = message.partition(parsed.args[0]) message = message.lstrip() # TODO: Remind user that the other creature must be in the room with them # e.g., raise ActionRefused("You can't say that%s as they don't appear to be here with you.", target) player.tell("You say%s: %s" % (target, message)) player.tell_others("{Title} says%s: %s" % (target, message))
def insert(self, item, actor): if actor is self or actor is not None and actor.isSysOp: super(NPC, self).insert(item, self) else: raise ActionRefused("%s does not accept %s." % (Lang.capital(self.title), item.title))
def move(self, target, actor=None, silent=False, is_player=False, verb="move"): raise ActionRefused("You can't %s that." % verb)
def func(player, parsed, ctx): if not parsed.args: raise ParseError( "Display the internal attributes associated with what location, item, or creature?" ) name = parsed.args[0] if name == "location": obj = player.location elif parsed.obj_order: obj = parsed.obj_order[0] else: raise ActionRefused("I can't locate the object %s." % name) player.tell([ "%r" % obj, "Python class defined in module : " + inspect.getfile(obj.__class__) ]) for varname, value in sorted(vars(obj).items()): player.tell(".%s: %r" % (varname, value)) if obj in ctx.engine.heartbeats: player.tell("%s is subscribed to heartbeat topic." % obj.name)
def inventory_size(self): if self.opened: return super(Box, self).inventory_size else: raise ActionRefused( "You'll need to open it to see what's inside...")
def func(player, parsed, ctx): if len(parsed.obj_order) != 2: raise ParseError( "You need to tell me what to throw and where to throw it.") item, where = parsed.obj_order[0], parsed.obj_order[1] if isinstance(item, Creature): raise ActionRefused("You can't throw another living creature.") # If the item is in the room with the player we'll need to pick it up first if item in player.location: item.move(player, player, verb="take") player.tell("You take <item>%s</item>." % item.title) player.tell_others("{Title} takes %s." % item.title) # Toss the item at the target. item.move(player.location, player, verb="throw") player.tell("You throw the <item>%s</item> at %s" % (item.title, where.title)) player.tell_others("{Title} throws the %s at %s." % (item.title, where.title))
def allow_passage(self, actor): if not self.bound: raise LocationIntegrityError("This door appears to be stuck shut.", None, self, None) if not self.opened: raise ActionRefused("The door is closed.")
def insert(self, item, actor): if isinstance(item, Item) and (actor is self or actor is not None and actor.isSysOp): self.__inventory.add(item) item.contained_in = self else: raise ActionRefused("That item can not be given to this creature.")
def remove(self, item, actor): if actor is self or actor is not None and actor.isSysOp: self.__inventory.remove(item) item.contained_in = None else: raise ActionRefused("You can not take %s from %s." % (item.title, self.title))
def open(self, actor, item=None): if self.opened: raise ActionRefused("It is already open.") elif self.locked: raise ActionRefused("It is locked.") else: self.opened = True actor.tell("You open it.") actor.tell_others("{Title} opens the %s." % self.name) if self.linked_door: self.linked_door.door.opened = True if self.linked_door.open_msg: self.target.tell(self.linked_door.open_msg)
def func(player, parsed, ctx): if len(parsed.args) not in (1, 2) or parsed.unrecognized: raise ParseError("What are you trying to %s?" % Lang.capital(parsed.verb)) if parsed.obj_order: if isinstance(parsed.obj_order[0], Creature): raise ActionRefused( "You can't do that to other living creatures.") obj_name = parsed.args[0] # Is the player using something to try to manipulate the object? with_item_name = None with_item = None if len(parsed.args) == 2: with_item_name = parsed.args[1] what = player.search_item(obj_name, include_inventory=True, include_location=True, include_containers=False) # Are we dealing with an exit? if not what: if obj_name in player.location.exits: what = player.location.exits[obj_name] # Are we dealing with an item? if what: # If so, are they using an item to accomplish the manipulation? if with_item_name: with_item = player.search_item(with_item_name, include_inventory=True, include_location=False, include_containers=False) if not with_item: raise ActionRefused( "You don't seem to have <item>%s</item>." % Lang.a(with_item_name)) getattr(what, parsed.verb)(player, with_item) else: raise ActionRefused("You don't see that here.")
def open(self, actor, item=None): if self.opened: raise ActionRefused("It is already open.") self.opened = True actor.tell("You open the %s." % self.name) actor.tell_others("{Title} opens the %s." % self.name)
def close(self, actor, item=None): if not self.opened: raise ActionRefused("It is already closed.") self.opened = False actor.tell("You close the %s." % self.name) actor.tell_others("{Title} closes the %s." % self.name)
def wrapped(*args, **kwargs): # check if the supplied actor actor = inspect.getcallargs(f, *args, **kwargs)["actor"] try: if actor and actor.isSysOp: return f(*args, **kwargs) except AttributeError: # actors without isSysOp are also not allowed to do anything pass raise ActionRefused("You're not allowed to do that.")
def func(player, parsed, ctx): if len(parsed.args) < 2: raise ParseError("You must specify what to give and whom to give it to.") if parsed.unrecognized or player.inventory_size == 0: raise ParseError("You don't have %s to give." % Lang.join(parsed.unrecognized)) # Does the player want to give everything they have? if "all" in parsed.args: if len(parsed.args) != 2: raise ParseError("You must specify who you want to give the items to.") what = player.inventory if parsed.args[0] == "all": Give.give_all(player, what, parsed.args[1]) return else: Give.give_all(player, what, parsed.args[0]) return # Player wants to give just a single item if len([who for who in parsed.obj_order if isinstance(who, Creature)]) > 1: # if there's more than one creature, it's not clear who to give stuff to raise ActionRefused("It's not clear who you want to give things to.") # if the first parsed word is a creature assume the syntax "give creature [the] thing(s)" if isinstance(parsed.obj_order[0], Creature): what = parsed.obj_order[1:] Give.give_all(player, what, None, target=parsed.obj_order[0]) return # if the last parsed word is a creature assume the syntax "give thing(s) [to] creature" elif isinstance(parsed.obj_order[-1], Creature): what = parsed.obj_order[:-1] Give.give_all(player, what, None, target=parsed.obj_order[-1]) return else: raise ActionRefused("It's not clear to who you want to give the item.")
def func(player, parsed, ctx): if not parsed.args: raise ParseError("usage: set object.attribute=value") args = parsed.args[0].split("=") if len(args) != 2: raise ParseError("usage: set object.attribute=value") name, field = args[0].split(".") # Player location? if name == "": obj = player.location # An item in the player's inventory or current location? else: obj = player.search_item(name, include_inventory=True, include_location=True) # A creature in the player's location? if not obj: obj = player.location.search_creature(name) if not obj: raise ActionRefused("I can not find the object you specified.") player.tell(repr(obj)) import ast value = ast.literal_eval(args[1]) expected_type = type(getattr(obj, field)) if expected_type is type(value): setattr(obj, field, value) player.tell("Object %s attribute %s set to %r" % (name, field, value)) else: raise ActionRefused("I expected a %s value type." % expected_type)
def close(self, actor, item=None): if not self.opened: raise ActionRefused("It is already closed.") self.opened = False actor.tell("You close it.") actor.tell_others("{Title} closes the %s." % self.name) if self.linked_door: self.linked_door.door.opened = False if self.linked_door.close_msg: self.target.tell(self.linked_door.close_msg)
def func(player, parsed, ctx): current_pw = yield "input-noecho", "What is the secret that we share?<password>" new_pw = yield "input-noecho", ( "What is the new secret we should both know?<password>", Accounts.accept_password) try: ctx.engine.accounts.change_password(player.name, current_pw, new_password=new_pw) player.tell("Keep it secret. Keep it safe.") except ValueError as x: raise ActionRefused("%s" % x)
def func(player, parsed, ctx): if not parsed.unparsed: raise ActionRefused( "What do you wish to shout at the top of your lungs?") message = parsed.unparsed if not parsed.unparsed.endswith((".", "!", "?")): message += "!" player.tell("You scream '%s'" % message) player.tell_others("{Title} shouts '%s'" % message) player.location.tell_adjacent_locations("Someone nearby shouts '%s'" % message)
def func(player, parsed, ctx): if len(parsed.obj_order) != 2: raise ParseError("What would you like to show and to whom?") shown = parsed.obj_order[0] if shown not in player: raise ActionRefused("You do not have <item>%s</item> to show." % Lang.a(shown.title)) target = parsed.obj_order[1] player.tell("You reveal the <item>%s</item> to <creature>%s</creature>." % (shown.title, target.title)) room_msg = "%s shows something to %s." % (Lang.capital(player.title), target.title) target_msg = "%s reveals the %s to you." % (Lang.capital(player.title), Lang.a(shown.title)) player.location.tell(room_msg, exclude_creature=player, specific_target_msg=target_msg, specific_targets=[target])