Пример #1
0
    def tell_surroundings(self):
        room = self.container

        messages = []

        if room.is_("Named"):
            messages.extend(
                [string.capwords(room.Named.name), room.Named.desc])

        if room.is_(Exitable):
            exits = room.Exitable.exits
            if exits:
                messages.append("Exits:")
                for exit in exits:
                    name = "elsewhere"
                    if exit.Exit.destination.is_("Named"):
                        name = exit.Exit.destination.Named.name
                    messages.append(
                        indent("{0}: {1}".format(exit.Exit.direction, name)))

        entities_nearby = [e for e in self.nearby() if not e.is_(Invisible)]
        if entities_nearby:
            messages.append("Things nearby:")
            for entity in entities_nearby:
                message = "something unnamed"
                if entity.is_("Named"):
                    message = entity.Named.name
                messages.append(indent(message))

        self.entity.tell("\n".join(messages))
Пример #2
0
def look_in(actor, selector):
    if not actor.is_(Spatial):
        actor.tell("You're unable to do that.")
        return

    targets = actor.Spatial.pick_nearby_inventory(selector)

    if not unique_selection(actor, 'look_in', 'selector', selector, {'selector': selector}, targets, 'nearby'):
        return

    target = targets.pop()

    if not target.is_(Container):
        actor.tell("You can't look inside of that.")
        return

    if target.is_('Dark'):
        actor.tell("It's too dark in there to see anything.")
        return

    actor.tell('Contents:')

    contents = [e for e in target.Container.contents if not e.is_(Invisible)]
    if contents:
        for item in contents:
            # TODO FIXME: Assumes Named (see 'something unnamed' below)
            actor.tell(indent(item.Named.name))
    else:
        actor.tell(indent('nothing'))
Пример #3
0
def inspect(actor, entity_id):
    if not actor.is_(Admin):
        actor.tell("You're unable to do that.")
        return

    entity_id = actor.Admin.aliases.get(entity_id, entity_id)

    try:
        entity_id = int(entity_id)
    except ValueError:
        actor.tell('Entity ID must be numeric.')
        return

    entity = actor.zone.get(entity_id)

    if entity is None:
        actor.tell('No such entity.')
        return

    actor.tell('[{0.id}]'.format(entity))

    actor.tell(indent('hearing: {0}', depth=1).format(json.dumps(entity.hearing)))

    for component in entity.components:
        actor.tell(indent('{0}').format(component.__class__.__name__))
        for key, value in component.to_dict().items():
            actor.tell(indent('{0}: {1}', depth=2).format(key, json.dumps(value)))
Пример #4
0
    def tell_surroundings(self):
        room = self.container

        messages = []

        if room.is_('Named'):
            messages.extend([
                string.capwords(room.Named.name),
                room.Named.desc
            ])

        if room.is_(Exitable):
            exits = room.Exitable.exits
            if exits:
                messages.append('Exits:')
                for exit in exits:
                    name = 'elsewhere'
                    if exit.Exit.destination.is_('Named'):
                        name = exit.Exit.destination.Named.name
                    messages.append(indent('{0}: {1}'.format(exit.Exit.direction, name)))

        entities_nearby = [e for e in self.nearby() if not e.is_(Invisible)]
        if entities_nearby:
            messages.append('Things nearby:')
            for entity in entities_nearby:
                message = 'something unnamed'
                if entity.is_('Named'):
                    message = entity.Named.name
                messages.append(indent(message))

        self.entity.tell('\n'.join(messages))
Пример #5
0
def inspect(actor, entity_id):
    if not actor.is_(Admin):
        actor.tell("You're unable to do that.")
        return

    entity_id = actor.Admin.aliases.get(entity_id, entity_id)

    try:
        entity_id = int(entity_id)
    except ValueError:
        actor.tell("Entity ID must be numeric.")
        return

    entity = actor.zone.get(entity_id)

    if entity is None:
        actor.tell("No such entity.")
        return

    actor.tell("[{0.id}]".format(entity))

    actor.tell(
        indent("hearing: {0}", depth=1).format(json.dumps(entity.hearing)))

    for component in entity.components:
        actor.tell(indent("{0}").format(component.__class__.__name__))
        for key, value in component.to_dict().items():
            actor.tell(
                indent("{0}: {1}", depth=2).format(key, json.dumps(value)))
Пример #6
0
def unique_selection(
    actor,
    action_name,
    argument_name,
    selector,
    kwargs,
    choices,
    area="in the area",
    quantity=None,
):
    if quantity is None:
        quantity = "any"

    if not choices:
        actor.tell("You don't see {0} '{1}' {2}.".format(
            quantity, selector, area))
        return False

    if len(choices) == 1:
        return True

    actor.mode = DisambiguationMode(action_name, argument_name, kwargs,
                                    [entity.id for entity in choices])

    actor.tell("Which '{0}' do you mean?".format(selector))

    for index, entity in enumerate(choices):
        location = "in inventory" if entity.Spatial.container == actor else "nearby"
        actor.tell(
            indent("{0}. {1.Named.name} ({2})".format(index + 1, entity,
                                                      location)))

    return False
Пример #7
0
def set_attribute(actor, entity_id, attribute, value):
    if not actor.is_(Admin):
        actor.tell("You're unable to do that.")
        return

    entity_id = actor.Admin.aliases.get(entity_id, entity_id)

    try:
        entity_id = int(entity_id)
    except ValueError:
        actor.tell('Entity ID must be numeric.')
        return

    entity = actor.zone.get(entity_id)

    if entity is None:
        actor.tell('No such entity "{0}".'.format(entity_id))
        return

    try:
        value = json.loads(value)
    except Exception as e:
        actor.tell('[{0.id}] failed to set "{1}":'.format(entity, attribute))
        actor.tell(indent(str(e)))
        return

    if attribute == 'hearing':
        entity.hearing = value
    else:
        # TODO: Components
        # Split attribute on dot
        pass

    actor.tell('OK.')
Пример #8
0
def unique_selection(actor, action_name, argument_name, selector, kwargs, choices, area='in the area', quantity=None):
    if quantity is None:
        quantity = 'any'

    if not choices:
        actor.tell("You don't see {0} '{1}' {2}.".format(quantity, selector, area))
        return False

    if len(choices) == 1:
        return True

    actor.mode = DisambiguationMode(
        action_name,
        argument_name,
        kwargs,
        [entity.id for entity in choices],
    )

    actor.tell("Which '{0}' do you mean?".format(selector))

    for index, entity in enumerate(choices):
        location = 'in inventory' if entity.Spatial.container == actor else 'nearby'
        actor.tell(indent('{0}. {1.Named.name} ({2})'.format(index + 1, entity, location)))

    return False
Пример #9
0
def set_attribute(actor, entity_id, attribute, value):
    if not actor.is_(Admin):
        actor.tell("You're unable to do that.")
        return

    entity_id = actor.Admin.aliases.get(entity_id, entity_id)

    try:
        entity_id = int(entity_id)
    except ValueError:
        actor.tell("Entity ID must be numeric.")
        return

    entity = actor.zone.get(entity_id)

    if entity is None:
        actor.tell('No such entity "{0}".'.format(entity_id))
        return

    try:
        value = json.loads(value)
    except Exception as e:
        actor.tell('[{0.id}] failed to set "{1}":'.format(entity, attribute))
        actor.tell(indent(str(e)))
        return

    if attribute == "hearing":
        entity.hearing = value
    else:
        # TODO: Components
        # Split attribute on dot
        pass

    actor.tell("OK.")
Пример #10
0
def look_in(actor, selector):
    if not actor.is_(Spatial):
        actor.tell("You're unable to do that.")
        return

    targets = actor.Spatial.pick_nearby_inventory(selector)

    if not unique_selection(
            actor,
            "look_in",
            "selector",
            selector,
        {"selector": selector},
            targets,
            "nearby",
    ):
        return

    target = targets.pop()

    if not target.is_(Container):
        actor.tell("You can't look inside of that.")
        return

    if target.is_("Dark"):
        actor.tell("It's too dark in there to see anything.")
        return

    actor.tell("Contents:")

    contents = [e for e in target.Container.contents if not e.is_(Invisible)]
    if contents:
        for item in contents:
            # TODO FIXME: Assumes Named (see 'something unnamed' below)
            actor.tell(indent(item.Named.name))
    else:
        actor.tell(indent("nothing"))
Пример #11
0
def attach(actor, entity_id, component_class_name, arguments=None):
    if not actor.is_(Admin):
        actor.tell("You're unable to do that.")
        return

    entity_id = actor.Admin.aliases.get(entity_id, entity_id)

    try:
        entity_id = int(entity_id)
    except ValueError:
        actor.tell("Entity ID must be numeric.")
        return

    entity = actor.zone.get(entity_id)

    if entity is None:
        actor.tell('No such entity "{0}".'.format(entity_id))
        return

    component_class = entity.zone.components.get(component_class_name)

    if component_class is None:
        actor.tell('No such component "{0}".'.format(component_class_name))
        return

    if entity.is_(component_class):
        actor.tell('[{0.id}] already has component "{1}".'.format(
            entity, component_class_name))
        return

    if arguments is None:
        arguments = "{}"

    try:
        component = component_class.from_dict(json.loads(arguments))
    except Exception as e:
        actor.tell('[{0.id}] failed to attach "{1}":'.format(
            entity, component_class_name))
        actor.tell(indent(str(e)))
        return

    entity.components.add(component)
    actor.tell('[{0.id}] is now "{1}".'.format(entity, component_class_name))
Пример #12
0
def attach(actor, entity_id, component_class_name, arguments=None):
    if not actor.is_(Admin):
        actor.tell("You're unable to do that.")
        return

    entity_id = actor.Admin.aliases.get(entity_id, entity_id)

    try:
        entity_id = int(entity_id)
    except ValueError:
        actor.tell('Entity ID must be numeric.')
        return

    entity = actor.zone.get(entity_id)

    if entity is None:
        actor.tell('No such entity "{0}".'.format(entity_id))
        return

    component_class = entity.zone.components.get(component_class_name)

    if component_class is None:
        actor.tell('No such component "{0}".'.format(component_class_name))
        return

    if entity.is_(component_class):
        actor.tell('[{0.id}] already has component "{1}".'.format(entity, component_class_name))
        return

    if arguments is None:
        arguments = '{}'

    try:
        component = component_class.from_dict(json.loads(arguments))
    except Exception as e:
        actor.tell('[{0.id}] failed to attach "{1}":'.format(entity, component_class_name))
        actor.tell(indent(str(e)))
        return

    entity.components.add(component)
    actor.tell('[{0.id}] is now "{1}".'.format(entity, component_class_name))