Beispiel #1
0
async def emit_room_sys_msg(entity: Entity, event_type: str, details: typing.Dict, room=None, include_origin=True):
    from core.src.world.builder import transport
    assert isinstance(details, dict)
    room = room or (entity.get_room() or await get_current_room(entity))
    listeners = await get_eligible_listeners_for_room(room)
    if listeners:
        listeners = listeners if not include_origin else [e for e in listeners if e.entity_id != entity.entity_id]
        listeners and (
            await batch_load_components((SystemComponent, 'connection'), PositionComponent, entities=listeners)
        )
    futures = []
    for entity in listeners:
        position = entity.get_component(PositionComponent)
        if position.coord == room.position.coord and entity.get_component(SystemComponent).connection:
            payload = {
                "event": event_type,
                "target": "entity",
                "details": details,
                "position": room.position.value
            }
            futures.append(
                transport.send_system_event(
                    entity.get_component(SystemComponent).connection.value,
                    payload
                )
            )
    await asyncio.gather(*futures)
Beispiel #2
0
async def emit_room_msg(origin: Entity, message_template, target: Entity = None, room=None):
    """
    Emit a room message in the same room of "origin".
    The room can be overridden with the room= keyword argument, accepting a Room type as input.
    The message template must have a mandatory {origin} and an optional {target} placeholders.
    origin and target parameters must be type Entity, with the AttributesComponent loaded.
    origin is mandatory, target is optional.

    The emitted message type is a string, the target is the client text field.
    """
    from core.src.world.builder import transport
    room = room or (origin.get_room() or await get_current_room(origin))
    listeners = await get_eligible_listeners_for_room(room)
    listeners = [
        listener for listener in listeners if listener.entity_id not in (
            origin and origin.entity_id, target and target.entity_id
        )
    ]
    if not listeners:
        return
    await batch_load_components((SystemComponent, 'connection'), PositionComponent, entities=listeners)
    futures = []
    for entity in listeners:
        position = entity.get_component(PositionComponent)
        if position.coord == room.position.coord and entity.get_component(SystemComponent).connection:
            # TODO - Evaluate VS entity memory
            futures.append(
                transport.send_message(
                    entity.get_component(SystemComponent).connection.value,
                    message_template.format(
                        origin=origin.get_component(AttributesComponent).keyword,
                        target=target and target.get_component(AttributesComponent).keyword
                    )
                )
            )
    await asyncio.gather(*futures)