示例#1
0
def parse_time(args):
    """parses a time from args like: 13:44:59, or like a duration such as 1h 30m 15s"""
    try:
        duration = parse_duration(args)
        return (datetime.datetime.min + duration).time()
    except ParseError:
        if not args or len(args) > 1:
            raise ParseError("It's not clear what time you mean.")
        try:
            return datetime.datetime.strptime(args[0], "%H:%M:%S").time()
        except ValueError:
            try:
                return datetime.datetime.strptime(args[0], "%H:%M").time()
            except ValueError:
                if args[0] == "noon":
                    return datetime.time(hour=12)
                elif args[0] == "midnight":
                    return datetime.time(hour=0)
                elif args[0] in ("sunrise", "dawn"):
                    return datetime.time(hour=6)
                elif args[0] in ("sunset", "dusk"):
                    return datetime.time(hour=20)
                elif args[0] in ("evening", "morning", "later", "earlier",
                                 "future", "past"):
                    raise ParseError(
                        "You must be more specific about the time you mean.")
                else:
                    raise ParseError("It's not clear what time you mean.")
    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))
示例#3
0
    def match_previously_parsed(self, player, pronoun):

        # Plural that can match any items or creatures
        if pronoun == "them":

            matches = list(self.previously_parsed.obj_order)

            for obj in matches:

                if not player.search_item(
                        obj.name) and obj not in player.location.creatures:
                    player.tell("(By '%s' I assume you mean %s.)" %
                                (pronoun, obj.title))
                    raise ParseError("%s is no longer nearby." %
                                     Lang.capital(obj.subjective))

            if matches:

                player.tell("(By '%s' I assume you mean %s.)" %
                            (pronoun, Lang.join(who.title for who in matches)))
                return [(who, who.name) for who in matches]

            else:

                raise ParseError("It is not clear to whom you are referring.")

        for obj in self.previously_parsed.obj_order:

            # Are we dealing with an exit?
            if pronoun == "it":

                for direction, exit in player.location.exits.items():

                    if exit is obj:
                        player.tell("(By '%s' I assume you mean '%s'.)" %
                                    (pronoun, direction))
                        return [(obj, direction)]

            # If not, are we dealing with an item or creature?
            if pronoun == obj.objective:

                if player.search_item(
                        obj.name) or obj in player.location.creatures:
                    player.tell("(By '%s' I assume you mean %s.)" %
                                (pronoun, obj.title))
                    return [(obj, obj.name)]

                player.tell("(By '%s' I assume you mean %s.)" %
                            (pronoun, obj.title))

                raise ParseError("%s is no longer nearby." %
                                 Lang.capital(obj.subjective))

        raise ParseError("It is not clear who you're referring to.")
    def do_forced(self, actor, parsed, ctx):

        try:

            custom_verbs = set(ctx.engine.current_custom_verbs(self))

            if parsed.verb in custom_verbs:

                if self.location.process_action(parsed, self):
                    ObjectBase.pending_actions.send(
                        lambda actor=self: actor.location.notify_action(
                            parsed, actor))
                    return
                else:
                    raise ParseError(
                        "That action can not be performed in this location's context."
                    )

            if parsed.verb in self.location.exits:
                ctx.engine._go_through_exit(self, parsed.verb)
                return

            action_verbs = set(ctx.engine.current_verbs(self))

            # Sysop actions can be executed by non-Sysops if they are directed to do so by a Sysop
            if parsed.verb in action_verbs:

                func = Actions.get(True)[parsed.verb]

                if getattr(func, "is_generator", False):
                    dialog = func(self, parsed, ctx)
                    # If the action is handled by a generator funciton queue an async dialog
                    ObjectBase.async_dialogs.send((ctx.conn, dialog))
                    return

                func(self, parsed, ctx)
                if func.enable_notify_action:
                    ObjectBase.pending_actions.send(
                        lambda actor=self: actor.location.notify_action(
                            parsed, actor))

                return

            raise ParseError(
                "I do not understand what action you would like the creature to perform."
            )

        except Exception as x:
            actor.tell("The attempt to force action %s failed." % str(x))
示例#5
0
    def func(player, parsed, ctx):

        if len(parsed.obj_order) == 1:
            what = parsed.obj_order[0]
            what.read(player)
        else:
            raise ParseError("What would you like to read?")
示例#6
0
    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))
示例#7
0
    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)
示例#8
0
    def func(player, parsed, ctx):

        if not parsed.unparsed:
            raise ParseError("What feeling are you trying to express?")

        message = Lang.capital(player.title) + " " + parsed.unparsed
        player.tell("%s" % message)
        player.tell_others(message)
示例#9
0
    def func(player, parsed, ctx):

        if not parsed.obj_order:
            raise ParseError("Which object are we removing from the world?")

        if parsed.unrecognized:
            raise ParseError("I could not parse that into an in-world object")

        for target in parsed.obj_info:

            if not (yield "input",
                    ("Are you sure you want to remove %s from the world?" %
                     target.title, Lang.yesno)):
                player.tell("Action cancelled.")
                continue

            target.sys_destroy(player, ctx)
            player.tell("You remove %r from the world." % target)
示例#10
0
    def func(player, parsed, ctx):

        if len(parsed.args) == 0:
            raise ParseError("What would you like to empty?")

        if len(parsed.args) != 1 or not parsed.obj_order:
            raise ParseError("What would you like to empty?")

        # We can't empty more than one container at a time
        if len(parsed.obj_order) > 1:
            raise ParseError("You can only empty one item at a time.")

        # If the object specified isn't a container we can't empty it
        container = parsed.obj_order[0]
        if not isinstance(container, Container):
            raise ActionRefused("That doesn't seem to be something you can empty.")

        # If the sprcified container is in the room we'll empty the contents
        if container in player.location:
            target = player.location
            action = "dropped"
        # Otherwise we'll look for the specified container on the player's person
        elif container in player:
            target = player
            action = "took"
        else:
            raise ParseError("That doesn't seem to be something you can empty.")

        # Try to actually move the items into the player's inventory and let them
        # know if rules for a particular item prevent the action
        items_moved = []
        for item in container.inventory:
            try:
                item.move(target, player)
                items_moved.append(item.title)
            except ActionRefused as x:
                player.tell(str(x))

        if items_moved:
            itemnames = ", ".join(items_moved)
            player.tell("You %s the following items: <item>%s</item>" % (action, itemnames))
            player.tell_others("{Title} %s the following items: <item>%s</item>" % (action, itemnames))
        else:
            player.tell("You %s nothing." % action)
示例#11
0
    def func(player, parsed, ctx):

        if len(parsed.args) < 2 or not parsed.obj_order:
            raise ParseError("Who would you like to force to do what?")

        target = parsed.obj_order[0]
        if not isinstance(target, Creature):
            raise ActionRefused(
                "You cannot force inanimate objects to perform an action.")

        action = parsed.args[1]

        # Make sure we know the action in question
        if action not in ctx.engine.current_verbs(
                target) and action not in target.location.exits:
            raise ParseError("I don't understand the word '%s'." % action)

        action = parsed.unparsed.partition(action)
        action = action[1] + action[2]

        target.tell("An unseen force compels you...")
        player.tell(
            "You force <player>%s</player> into performing an action." %
            target.title)

        # If target is another player then record the action in the player's input buffer
        if isinstance(target, Player):
            target.store_input_line(action)
            return

        # Now perform the action on behalf of the target, from the viewpoint of the current player!

        custom_verbs = set(ctx.engine.current_custom_verbs(target))
        action_verbs = set(ctx.engine.current_verbs(target))
        all_verbs = custom_verbs | action_verbs

        try:
            target_parsed = target.parse(action, all_verbs)
            raise ParseError("Unknown action specified.")
        except NotDefaultVerb as x:
            # if not a default verb try to find the associated action
            target.do_forced(player, x.parsed, ctx)
示例#12
0
    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.")
示例#13
0
    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))
示例#14
0
    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)
示例#15
0
def parse_duration(args):
    """parses a duration from args like: 1 hour 20 minutes 15 seconds (hour/h, minutes/min/m, seconds/sec/s)"""
    hours = minutes = seconds = 0
    if args:
        number = None
        for arg in args:
            if len(arg) >= 2 and arg.endswith(("h", "m", "s")):
                try:
                    if arg[-1] == "h":
                        hours = int(arg[:-1])
                    elif arg[-1] == "m":
                        minutes = int(arg[:-1])
                    elif arg[-1] == "s":
                        seconds = int(arg[:-1])
                    continue
                except ValueError:
                    pass
            if arg in ("hours", "hour", "h"):
                hours = number
                number = None
            elif arg in ("minutes", "minute", "min", "m"):
                minutes = number
                number = None
            elif arg in ("seconds", "second", "sec", "s"):
                seconds = number
                number = None
            else:
                try:
                    number = float(arg)
                except ValueError:
                    raise ParseError("It's not clear what duration you mean.")
    if hours == minutes == seconds == 0:
        raise ParseError("It's not clear what duration you mean.")
    try:
        return datetime.timedelta(hours=hours,
                                  minutes=minutes,
                                  seconds=seconds)
    except TypeError:
        raise ParseError("It's not clear what duration you mean.")
    def func(player, parsed, ctx):

        if not parsed.obj_order:
            raise ParseError("What are you trying to deactivate?")

        for what in parsed.obj_order:
            try:
                what.deactivate(player)
            except ActionRefused as ex:
                msg = str(ex)
                if len(parsed.obj_order) > 1:
                    player.tell("%s: %s" % (what.name, msg))
                else:
                    player.tell(msg)
示例#17
0
    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])
    def func(player, parsed, ctx):

        # Poor man's autocomplete :)
        if parsed.verb == "manip":
            parsed.verb = "manipulate"

        if len(parsed.obj_order) == 1:
            what = parsed.obj_order[0]
            try:
                what.manipulate(parsed.verb, player)
                return
            except ActionRefused:
                raise

        raise ParseError("What would you like to %s?" % Lang.capital(parsed.verb))
示例#19
0
    def func(player, parsed, ctx):

        if parsed.unparsed == "off" or (parsed.args
                                        and parsed.args[0] == "off"):
            player.brief = 0
            player.tell("Full descriptions restored.")
        elif not parsed.args or parsed.unparsed == "on" or (
                parsed.args and parsed.args[0] == "on"):
            player.brief = 2
            player.tell("Short descriptions enabled.")
        elif parsed.args[0] == "known":
            player.brief = 1
            player.tell("Short descriptions enabled for all known locations.")
        else:
            raise ParseError("I do not recognize that parameter.")
示例#20
0
    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.")
示例#21
0
    def func(player, parsed, ctx):

        if len(parsed.obj_info) != 2:
            messages = {
                "combine": "Combine which two items?",
                "attach": "Attach which two items?",
                "apply": "Apply which two items?"
            }
            raise ParseError(messages[parsed.verb])

        item1, item2 = tuple(parsed.obj_info)

        if item1 not in player or item2 not in player:
            raise ActionRefused(
                "You do not have both items in your possession.")

        try:
            item2.combine(item1, player)
        except ActionRefused:
            item1.combine(item2, player)
示例#22
0
    def func(player, parsed, ctx):

        # Remove all objects from the player's current location
        if parsed.args and parsed.args[0] == '.':

            player.tell("Removing all non-player objects from your current location.")

            for item in set(player.location.items):
                player.location.remove(item, player)
                item.destroy(ctx)

            for creature in set(player.location.creatures):
                if not isinstance(creature, Player.Player):
                    player.location.remove(creature, player)
                    creature.destroy(ctx)

            if player.location.items:
                player.tell("Some items were unable to be removed.")

        else:

            if len(parsed.obj_order) != 1:
                raise ParseError("Please specify the container or creature you are trying to remove all objects from.")

            target = parsed.obj_order[0]
            if (yield "input", ("Are you sure you want to remove all items from %s?" % target.title, Lang.yesno)):

                player.tell("Removing all items from %s." % target)

                items = target.inventory
                for item in items:
                    target.remove(item, player)
                    item.destroy(ctx)
                    player.tell(item, "removed from the world.")

                if target.inventory_size:
                    player.tell("Some items were unable to be removed.")

            else:

                player.tell("Action cancelled.")
示例#23
0
    def func(player, parsed, ctx):

        if not parsed.args:
            raise ParseError("Who or what are you trying to find?")

        if len(parsed.args) == 2 and parsed.args[0] == "am":
            if parsed.args[1].rstrip("?") in ("i", "I"):
                player.tell("You're in %s." % player.location.title)
                player.tell(
                    "If you say <userinput>look</userinput> I'll tell you a bit more about where you are."
                )
                return

        if parsed.args[0] in ("is", "are") and len(parsed.args) > 2:
            raise ActionRefused(
                "Please be specific and realize that I can only search for one thing at a time."
            )

        if len(parsed.args) >= 2 and parsed.args[0] in ("is", "are"):
            del parsed.args[0]

        name = parsed.args[0].rstrip("?")
        raise RetryParse("locate " + name)
示例#24
0
    def func(player, parsed, ctx):

        if len(parsed.obj_order) == 1:

            who = parsed.obj_order[0]
            if parsed.obj_info[
                    who].previous_word == "on" or parsed.unparsed.endswith(
                        " on"):
                Activate.func(player, parsed, ctx)
                return
            elif parsed.obj_info[
                    who].previous_word == "off" or parsed.unparsed.endswith(
                        " off"):
                Deactivate.func(player, parsed, ctx)
                return

        elif len(parsed.obj_order) == 0:

            arg = parsed.unparsed.partition(" ")[0]
            if arg in ("on", "off"):
                raise ParseError("What are you trying to switch %s?" % arg)

        raise RetryVerb
示例#25
0
    def func(player, parsed, ctx):

        if len(parsed.obj_order) == 1:

            who = parsed.obj_order[0]
            if parsed.obj_info[
                    who].previous_word == "on" or parsed.unparsed.endswith(
                        " on"):
                Activate.func(player, parsed, ctx)
                return
            elif parsed.obj_info[
                    who].previous_word == "off" or parsed.unparsed.endswith(
                        " off"):
                Deactivate.func(player, parsed, ctx)
                return

        elif len(parsed.obj_order) == 0:
            arg = parsed.unparsed.partition(" ")[0]
            if arg in ("on", "off"):
                raise ParseError("What are you trying to switch %s?" % arg)

        # Rather than switch something on or off they could be using the word turn in a more general sense as to
        # manipulate something in a particular way...
        Manipulate.func(player, parsed, ctx)
示例#26
0
    def func(player, parsed, ctx):

        if len(parsed.obj_order) != 1:
            raise ParseError(
                "Who would you like to show affection or kindness to?")

        if len(parsed.obj_order) == 1:

            target = parsed.obj_order[0]
            if isinstance(target, Creature):
                player.tell("You %s <creature>%s</creature>." %
                            (parsed.verb, target.title))
                room_msg = "%s %ss %s." % (Lang.capital(
                    player.title), parsed.verb, target.title)
                target_msg = "%s %ss you." % (Lang.capital(
                    player.title), parsed.verb)
                player.location.tell(room_msg,
                                     exclude_creature=player,
                                     specific_target_msg=target_msg,
                                     specific_targets=[target])
            else:
                player.tell(
                    "Inanimate objects don't respond to that but it's very sweet of you."
                )
示例#27
0
    def func(player, parsed, ctx):

        if len(parsed.args) < 2:
            raise ParseError(
                "You need to tell me what to put and where you'd like to put it."
            )

        # If player specified all they want to put their entire inventory into the container
        if parsed.args[0] == "all":

            # Does the player have anything in their inventory
            if player.inventory_size == 0:
                raise ActionRefused("You don't seem to be carrying anything.")

            if len(parsed.args) != 2:
                raise ParseError(
                    "You need to tell me what to put and where you'd like to put it."
                )

            what = list(player.inventory)
            where = parsed.obj_order[
                -1]  # The last item represents the "where"

        elif parsed.unrecognized:
            raise ActionRefused("I don't see %s here." %
                                Lang.join(parsed.unrecognized))

        else:
            what = parsed.obj_order[:-1]
            where = parsed.obj_order[-1]

        if isinstance(where, Creature):
            raise ActionRefused(
                "You can't do that but you might be able to give it to them..."
            )

        inventory_items = []
        refused = []

        word_before = parsed.obj_info[where].previous_word or "in"
        if word_before != "in" and word_before != "into":
            raise ActionRefused(
                "You can only put an item 'in' or 'into' a container of some sort."
            )

        for item in what:
            if item is where:
                player.tell("You can't put something inside of itself.")
                continue

            try:
                # Are they using an item they are already carrying?
                if item in player:
                    item.move(where, player)
                    inventory_items.append(item)

                # If the item is in the room then we'll take it first and then put it into the container
                # TODO: We need to handle in a linguistic stylish way the situation where one part of this two-step operation fails
                elif item in player.location:
                    item.move(player, player)
                    item.move(where, player)
                    player.tell("You take %s and put it in the %s." %
                                (item.title, where.name))
                    player.tell_others(
                        "{Title} takes %s and puts it in the %s." %
                        (item.title, where.name))

            except ActionRefused as x:
                refused.append((item, str(x)))

        # The item refused to move at some point so inform the player
        for item, message in refused:
            player.tell(message)

        if inventory_items:
            items_msg = Lang.join(
                Lang.a(item.title) for item in inventory_items)
            player.tell_others("{Title} puts %s in the %s." %
                               (items_msg, where.name))
            player.tell(
                "You put <item>{items}</item> in the <item>{where}</item>.".
                format(items=items_msg, where=where.name))
示例#28
0
    def func(player, parsed, ctx):

        # Did the player specify something to be taken?
        if len(parsed.args) == 0:
            raise ParseError("What would you like to take?")

        # Player is taking a single item
        if len(parsed.args) == 1:
            obj_names = parsed.args
            where = None

        # Player wants to try something more complicated...
        else:

            if parsed.obj_order:

                last_obj = parsed.obj_order[-1]

                # Player is trying to take one or more (comma separated) items from something or someone
                if parsed.obj_info[last_obj].previous_word == "from":
                    obj_names = parsed.args[:-1]
                    where = last_obj

                # Player is trying to take one or more (comma separated) items
                else:
                    # take x[,y and z]
                    obj_names = parsed.args
                    where = None

            else:
                # take x[,y and z] - unrecognised names
                obj_names = parsed.args
                where = None

        # Basic sanity check
        if where is player:
            raise ActionRefused("You can't take items from yourself.")

        # Notify others in the room that something is creature taken
        if isinstance(where, Creature):
            player.tell_others("{Title} takes something from %s." %
                               where.title)

        # Player wants to take all teh things
        if obj_names == ["all"]:

            # Are we taking items from a container?
            if where:

                # Is the container on the player's person or in the room with the player?
                if where in player or where in player.location:

                    # Is there anything in it?
                    if where.inventory_size > 0:
                        Get.take_all(player, where.inventory, where,
                                     where.title)
                        return
                    else:
                        raise ActionRefused("It appears to be empty.")

                raise ActionRefused("What are you trying to take?")

            # We're taking items from the room the player is in
            else:

                # Anything here to take?
                if not player.location.items:
                    raise ActionRefused(
                        "There appears to be nothing here you can carry.")
                # Yes, so dump everything into the player's inventory.
                else:
                    Get.take_all(player, player.location.items,
                                 player.location)
                    return

        # Player is trying to take one or more specific items
        else:

            # Are we taking items from a container?
            if where:

                # Yes, is the container on the player's person or in the room?
                if where in player or where in player.location:

                    # Take each item from the specified container
                    items_by_name = {
                        item.name: item
                        for item in where.inventory
                    }
                    items_to_take = []

                    for name in obj_names:

                        # If it's there let's take it...
                        if name in items_by_name:
                            items_to_take.append(items_by_name[name])
                        # ...otherwise tell the player their action is misguided.
                        else:
                            player.tell("There's no %s in there." % name)

                    Get.take_all(player, items_to_take, where, where.title)
                    return
            else:

                # Looks like the player is trying to take items from the room itself
                if parsed.unrecognized:
                    player.tell("You don't see %s here." %
                                Lang.join(parsed.unrecognized))

                creatures = [
                    item for item in parsed.obj_order
                    if item in player.location.creatures
                ]
                for creature in creatures:
                    player.tell("You can not pick up other living creatures.")

                if not player.location.items:
                    raise ActionRefused(
                        "There appears to be nothing here you can carry.")

                else:

                    items_to_take = []
                    for item in parsed.obj_order:

                        # If item is here let the player take it!
                        if item in player.location.items:
                            items_to_take.append(item)

                        # If the item is an exit let's remind the user it can't be taken
                        elif isinstance(item, Exit):
                            raise ActionRefused(
                                "That is not something you can carry.")

                        elif item not in player.location.creatures:
                            if item in player:
                                player.tell("You already have that item.")
                            else:
                                player.tell(
                                    "There's no <item>%s</item> here." %
                                    item.name)

                    Get.take_all(player, items_to_take, player.location)
                    return
示例#29
0
    def func(player, parsed, ctx):

        # If we're examining a creature let's get some info for it
        creature = None
        if parsed.obj_info and isinstance(parsed.obj_order[0], Creature):
            creature = parsed.obj_order[0]
            name = creature.name

        # If we're not examining a creature we'll get item info
        if not creature:

            # Handle the case where nothing to examine was specified
            if not parsed.args:
                raise ParseError("Who or what would you like to examine?")

            Actions.parse_is_are(parsed.args)
            name = parsed.args[0]
            creature = player.location.search_creature(name)

        # If we're trying to examine a creature we'll figure out
        # which creature we're dealing with
        if creature:

            # Player is trying to examine themselves. Sheesh.
            if creature is player:
                player.tell("You are <creature>%s</creature>." %
                            Lang.capital(creature.title))
                return

            if creature.name.lower() != name.lower() and name.lower(
            ) in creature.aliases:
                player.tell("(By %s you probably meant %s.)" %
                            (name, creature.name))

            # If the creature has a description show the player otherwise we'll just share their title
            if creature.description:
                player.tell(creature.description)
            else:
                player.tell("This is <creature>%s</creature>." %
                            creature.title)

            # If there is extended info about the creature we'll provide that as well
            if name in creature.extra_desc:
                player.tell(creature.extra_desc[name])

            if name in player.location.extra_desc:
                player.tell(player.location.extra_desc[name])

            return

        # Is the player trying to examine an item?
        item, container = player.locate_item(name)
        if item:

            if item.name.lower() != name.lower() and name.lower(
            ) in item.aliases:
                player.tell("Maybe you meant %s?" % item.name)

            # If there is an extended description we'll share that.
            if name in item.extra_desc:
                player.tell(item.extra_desc[name])
            # Otherwise we'll just share what basic info we have
            else:
                if item in player:
                    player.tell("You're carrying <item>%s</item>." %
                                Lang.a(item.title))
                elif container and container in player:
                    Actions.print_object_location(player, item, container)
                else:
                    player.tell("You see <item>%s</item>." %
                                Lang.a(item.title))
                if item.description:
                    player.tell(item.description)

            try:
                inventory = item.inventory
            except ActionRefused:
                pass
            else:
                if inventory:
                    msg = "It contains "
                    for item in inventory:
                        msg += "<item>%s</item>, " % item.title
                    msg = msg[:-2]
                    player.tell(msg)
                else:
                    player.tell("It's empty.")

        # Player is examining an exit?
        elif name in player.location.exits:
            player.tell(
                "<exit>" + player.location.exits[name].description +
                "</exit> represents a way you can travel to a different place."
            )

        # If nothing else we'll do a search for any extended descriptive info associated with
        # locations and items
        else:
            text = player.search_extradesc(name)
            if text:
                player.tell(text)
            else:
                raise ActionRefused("%s doesn't appear to be here." % name)
示例#30
0
    def func(player, parsed, ctx):

        if not parsed.args:
            raise ParseError("What are you asking about?")

        if parsed.args[0] == "are" and len(parsed.args) > 2:
            raise ActionRefused(
                "I can only tell you about one thing at a time.")

        if len(parsed.args) >= 2 and parsed.args[0] in ("is", "are"):
            del parsed.args[0]

        name = parsed.args[0].rstrip("?")

        if not name:
            raise ActionRefused("What are you asking about?")

        found = False

        # Is the player asking about an action?
        all_verbs = ctx.engine.current_verbs(player)
        if name in all_verbs:
            found = True
            doc = all_verbs[name].strip()

            if doc:
                player.tell(doc)
            else:
                player.tell(
                    "That is an action I understand but I can't tell you much more about it."
                )

        # Is the player asking about a particular exit from their current location?
        if name in player.location.exits:
            found = True
            player.tell(
                "That appears to be a way to leave your current location. Perhaps you should examine it?"
            )

        # Is the player asking about a creature in the room with them?
        creature = player.location.search_creature(name)
        if creature and creature.name.lower() != name.lower() and name.lower(
        ) in creature.aliases:
            player.tell("(By %s you probably meant %s.)" %
                        (name, creature.name))

        if creature:
            found = True

            # Is the player referring to themselves here?
            if creature is player:
                player.tell("Well, that's you isn't it?")

            # Or are they referring to someone else in the room?
            else:
                title = Lang.capital(creature.title)
                gender = Lang.GENDERS[creature.gender]
                subj = Lang.capital(creature.subjective)

                if type(creature) is type(player):
                    player.tell(
                        "Is another visitor like yourself. %s's here in the room with you."
                        % subj)
                else:
                    player.tell(
                        "<creature>%s</creature> is a %s character in our story."
                        % (title, gender))

        # Is the player referring to an item?
        item, container = player.locate_item(name,
                                             include_inventory=True,
                                             include_location=True,
                                             include_containers=True)
        if item:
            found = True
            if item.name.lower() != name.lower() and name.lower(
            ) in item.aliases:
                player.tell("Maybe you meant %s.)" % item.name)
            player.tell(
                "That's a nearby item you could try to examine for more information."
            )

        if name in ("that", "this", "they", "them", "it"):
            raise ActionRefused("Sorry, you need to be more specific.")

        # Shrug our virtual shoulders...
        if not found:
            player.tell(
                "Sorry, I can't figure out what you mean or there is no additional information I can provide."
            )