Beispiel #1
0
async def cast_entity(
        entity: Entity,
        position: PositionComponent,
        update=True,
        on_connect=False,
        reason=None
):
    assert isinstance(position, PositionComponent)
    from core.src.world.builder import events_subscriber_service, events_publisher_service
    loop = asyncio.get_event_loop()
    if update:
        current_position = await get_components_for_entity(entity, (PositionComponent, 'coord'))
        position.add_previous_position(current_position)
        entity = entity.set_for_update(position).set_room(Room(position))
        update_response = await update_entities(entity.set_for_update(position))
        if not update_response:
            LOGGER.core.error(
                'Impossible to cast entity {}'.format(entity.entity_id))
            return
    area = Area(position).make_coordinates()
    listeners = await get_eligible_listeners_for_area(area)
    entity.entity_id in listeners and listeners.remove(entity.entity_id)
    if on_connect:
        await events_publisher_service.on_entity_appear_position(entity, position, reason, targets=listeners)
        loop.create_task(events_subscriber_service.subscribe_events(entity))
    else:
        pass
        await events_publisher_service.on_entity_change_position(entity, position, reason, targets=listeners)
    entity.set_component(position)
    return True
    async def _test_selective_repo_queries(self):

        class TestComponent(StructComponent):
            enum = 1
            key = enum
            meta = (
                ('weirdstuff', str),
                ('manystuffhere', list),
                ('integerrr', int),
                ('boolean', bool),
                ('a', dict)
            )

        c = TestComponent().weirdstuff.set('weirdstuff').a.set('key', 'value').manystuffhere.append(3)
        entity = Entity(123)
        entity.set_for_update(c)
        await self.sut.update_entities(entity)
        res = await self.sut.read_struct_components_for_entities([123], (TestComponent, 'weirdstuff'))
        r = res[123][c.enum]
        self.assertEqual(r.weirdstuff, 'weirdstuff')
        self.assertEqual(r.manystuffhere, [])
        self.assertEqual(r.integerrr, 0)
        self.assertEqual(r.boolean, False)
        self.assertEqual(r.a, {})
        self.test_success = True
    async def _test_stuff_struct_component(self):
        class TestComponent2(StructComponent):
            enum = ComponentTypeEnum.SYSTEM
            meta = (
                ('weirdstuff', str),
                ('manystuffhere', list),
                ('integerrr', int),
                ('boolean', bool),
                ('a', dict)
            )

        class TestComponent(StructComponent):
            enum = ComponentTypeEnum.INVENTORY
            meta = (
                ('weirdstuff', str),
                ('manystuffhere', list),
                ('integerrr', int),
                ('boolean', bool),
                ('a', dict)
            )
            indexes = ('weirdstuff', )

        ent = Entity(444)
        c3 = TestComponent().weirdstuff.set('weirdstuff').a.set('key', 'value').manystuffhere.append(3)
        c4 = TestComponent2().weirdstuff.set('weirdstuff2').a.set('key', 'value2').manystuffhere.append(6)
        ent.set_for_update(c3).set_for_update(c4)
        await self.sut.update_entities(ent)
        res = await self.sut.read_struct_components_for_entity(444, TestComponent, TestComponent2)
        self.assertEqual(res[c3.enum].weirdstuff, 'weirdstuff')
        self.assertEqual(res[c3.enum].a, {'key': 'value'})
        self.assertEqual(res[c4.enum].weirdstuff, 'weirdstuff2')
        self.assertEqual(res[c4.enum].a, {'key': 'value2'})

        self.test_success = True
Beispiel #4
0
async def disconnect_entity(entity: Entity, msg=True):
    from core.src.world.builder import map_repository
    from core.src.world.builder import websocket_channels_service
    from core.src.world.builder import cmds_observer
    events_publisher = get_events_publisher()
    await load_components(entity, PositionComponent)
    listeners = await get_eligible_listeners_for_area(
        entity.get_component(PositionComponent))
    await events_publisher.on_entity_disappear_position(
        entity, entity.get_component(PositionComponent), "disconnect",
        listeners)
    msg and await emit_sys_msg(entity, "event", {"event": "quit"})
    current_channel_id = entity.get_component(SystemComponent).connection.value
    entity.set_for_update(SystemComponent().connection.set(""))
    if await update_entities(entity):
        await map_repository.remove_entity_from_map(
            entity.entity_id, entity.get_component(PositionComponent))
        await websocket_channels_service.close_namespace_for_entity_id(
            entity.entity_id)
        cmds_observer.close_channel(current_channel_id)
Beispiel #5
0
def move_entity_from_container(
        entity: Entity,
        target: (PositionComponent, InventoryComponent),
        current_owner: Entity = None
):
    current_position = entity.get_component(PositionComponent)
    if current_position.parent_of:
        assert current_owner and current_owner.entity_id == current_position.parent_of, (
                current_owner and current_owner.entity_id, current_position.parent_of
        )
        current_owner.get_component(InventoryComponent).content.remove(entity.entity_id)
        current_owner.set_for_update(current_owner.get_component(InventoryComponent))

    if isinstance(target, InventoryComponent):
        target_owner = target.owned_by()
        new_position = PositionComponent().parent_of.set(target_owner.entity_id).coord.null()
        new_position.add_previous_position(current_position)
        target.content.append(entity.entity_id)
        target_owner.set_for_update(target)
        entity.set_for_update(new_position)

    elif isinstance(target, PositionComponent):
        assert target.coord.value
        new_position = PositionComponent().parent_of.null().coord.set(target.coord.value)
        entity.set_for_update(new_position)

    else:
        raise ValueError('Target must be type PosComponent or ContainerComponent, is: %s' % target)
    return entity
Beispiel #6
0
async def drop(entity: Entity, keyword: str):
    await load_components(entity, PositionComponent, InventoryComponent)
    inventory = entity.get_component(InventoryComponent)
    items = await search_entities_in_container_by_keyword(inventory, keyword)
    msgs_stack = get_stacker()
    items_to_drop = []
    for item in items:
        items_to_drop.append(
            move_entity_from_container(
                item,
                target=entity.get_component(PositionComponent),
                current_owner=entity))
    if not items_to_drop:
        await emit_msg(entity, messages.target_not_found())
        return
    entity.set_for_update(inventory)
    msgs_stack.add(
        emit_sys_msg(entity, 'remove_items',
                     messages.items_to_message(items_to_drop)),
        emit_room_sys_msg(entity, 'add_items',
                          messages.items_to_message(items_to_drop)))
    if len(items_to_drop) == 1:
        msgs_stack.add(
            emit_msg(entity, messages.on_drop_item(items[0])),
            emit_room_msg(origin=entity,
                          message_template=messages.on_entity_drop_item(
                              items[0])))
    else:
        msgs_stack.add(
            emit_msg(entity, messages.on_drop_multiple_items()),
            emit_room_msg(
                origin=entity,
                message_template=messages.on_entity_drops_multiple_items()))
    if not await update_entities(entity, *items_to_drop):
        await emit_msg(entity, messages.target_not_found())
        msgs_stack.cancel()
    else:
        await msgs_stack.execute()
Beispiel #7
0
 async def on_connect(self, entity: Entity):
     connection_id = entity.get_component(SystemComponent).connection.value
     self.events_subscriber_service.add_observer_for_entity_id(
         entity.entity_id, self.pubsub_observer)
     await update_entities(
         entity.set_for_update(
             SystemComponent().connection.set(connection_id)))
     await load_components(entity, PositionComponent)
     if not entity.get_component(PositionComponent).coord:
         await cast_entity(entity,
                           get_base_room_for_entity(entity),
                           on_connect=True,
                           reason="connect")
         self.loop.create_task(self.greet(entity))
     else:
         await cast_entity(entity,
                           entity.get_component(PositionComponent),
                           update=False,
                           on_connect=True,
                           reason="connect")
     self.manager.set_transport(entity.entity_id, connection_id)
     self.commands_observer.enable_channel(connection_id)
     self.loop.create_task(look(entity))
     self.loop.create_task(getmap(entity))
    def get_instance_of(self, name: str,
                        entity: Entity) -> typing.Optional[Entity]:
        e = Entity()
        data = self._local_copy.get(name)
        if not data:
            return

        system_component = SystemComponent()\
            .instance_of.set(data['libname'])\
            .created_at.set(int(time.time()))\
            .instance_by.set(entity.entity_id)

        e.set_for_update(system_component)
        for component in data['components']:
            comp_type = get_component_by_type(component)
            if comp_type.has_default:
                e.set_for_update(comp_type().activate())
            else:
                e.set_for_update(
                    comp_type(value=data['components'][component]))
        return e
    async def _test_save_struct_component(self):
        entity = Entity(555)
        entity2 = Entity(556)

        class TestComponent(StructComponent):
            component_enum = 0
            meta = (
                ('weirdstuff', str),
                ('manystuffhere', list),
                ('integerrr', int),
                ('boolean', bool),
                ('a', dict)
            )

        c = TestComponent()\
            .weirdstuff.set('a lot of')\
            .manystuffhere.append(3, 6, 9)\
            .integerrr.incr(42)\
            .boolean.set(True)\
            .a.set('key1', 'value1')\
            .a.set('key2', 'value2')\
            .a.set('key3', 'value3')
        entity.set_for_update(c)
        await self.sut.update_entities(entity)
        res = await self.sut.read_struct_components_for_entities([555], TestComponent)
        component = res[555][0]
        self.assertEqual(component.weirdstuff, 'a lot of')
        self.assertEqual(component.manystuffhere, [3, 6, 9])
        self.assertEqual(component.integerrr, 42)
        self.assertEqual(component.boolean, True)
        self.assertEqual(component.a['key1'], 'value1')
        self.assertEqual(component.a['key2'], 'value2')
        self.assertEqual(component.a['key3'], 'value3')
        c.a.remove('key1')
        entity.set_for_update(c)
        await self.sut.update_entities(entity)
        res = await self.sut.read_struct_components_for_entities([555], TestComponent)
        component = res[555][0]
        self.assertEqual(component.a.get('key1'), None)
        self.assertFalse(component.a.has_key('key1'))
        self.assertEqual(component.a, {'key2': 'value2', 'key3': 'value3'})
        self.assertEqual(component.manystuffhere, [3, 6, 9])
        self.assertEqual(component.integerrr, 42)
        self.assertEqual(component.boolean, True)
        c.weirdstuff.null()
        entity.set_for_update(c)
        await self.sut.update_entities(entity)
        res = await self.sut.read_struct_components_for_entities([555], TestComponent)
        component = res[555][0]
        self.assertEqual(component.weirdstuff, None)
        self.assertEqual(component.manystuffhere, [3, 6, 9])
        self.assertEqual(component.integerrr, 42)
        self.assertEqual(component.boolean, True)
        c.manystuffhere.remove(3)
        c.weirdstuff.set('yahi!')
        entity.set_for_update(c)
        await self.sut.update_entities(entity)
        res = await self.sut.read_struct_components_for_entities([555], TestComponent)
        component = res[555][0]
        self.assertEqual(component.weirdstuff, 'yahi!')
        self.assertEqual(component.manystuffhere, [6, 9])
        c.manystuffhere.remove(6, 9)
        entity.set_for_update(c)
        c2 = TestComponent().manystuffhere.append(666)
        entity2.set_for_update(c2)
        await self.sut.update_entities(entity, entity2)
        res = await self.sut.read_struct_components_for_entities([555, 556], TestComponent)
        component = res[555][0]
        component2 = res[556][0]
        self.assertEqual(component.manystuffhere, [])
        self.assertEqual(component2.manystuffhere, [666])
        self.assertEqual(component2.manystuffhere + [2], [666, 2])
        self.test_success = True