示例#1
0
文件: server.py 项目: flberger/fabula
    def process_DropsEvent(self, event, **kwargs):
        """Respawn the Entity to be dropped in the respective room, delete it from Engine.rack and pass the PicksUpEvent on.
        """

        fabula.LOGGER.debug("called")

        room = None

        for current_room in self.room_by_id.values():

            if event.identifier in current_room.entity_dict.keys():

                room = current_room

        # Respawn the Entity to be dropped in room
        # Delete it from Engine.rack
        #
        # TODO: Fails when Entity not in rack. Contracts.
        #
        fabula.LOGGER.info(
            "removing '{}' from Rack and respawning in Room".format(
                event.entity.identifier))

        dropped_entity = self.rack.retrieve(event.entity.identifier)

        spawn_event = fabula.SpawnEvent(dropped_entity, event.location)

        room.process_SpawnEvent(spawn_event)

        # and pass the DropsEvent on
        #
        kwargs["message"].event_list.append(event)

        return
示例#2
0
    def process_DropsEvent(self, event, **kwargs):
        """Remove affected Entity from rack,
           queue SpawnEvent for room and Plugin,
           and pass on the DropsEvent.
        """

        # TODO: the DropsEvent and PicksUpEvent handling in Client and Plugin are asymmetrical. This is ugly. Unify.

        # Former default
        #
        fabula.LOGGER.debug("called")

        # Maybe someone drops something which is not in our rack, for example
        # if the dropping Entity brings the item from a different room.

        dropped_entity = None

        if event.entity.identifier in self.rack.entity_dict.keys():

            # Respawn the Entity to be dropped in Engine.room
            # Delete it from Engine.rack
            #
            fabula.LOGGER.info("removing '{}' from Rack".format(
                event.entity.identifier))

            dropped_entity = self.rack.retrieve(event.entity.identifier)

        else:

            dropped_entity = event.entity

        fabula.LOGGER.info("Spawning '{}' in Room".format(
            dropped_entity.identifier))

        spawn_event = fabula.SpawnEvent(dropped_entity, event.location)

        self.room.process_SpawnEvent(spawn_event)

        # and pass the DropsEvent on
        #
        kwargs["message"].event_list.append(event)

        # Drop confirmed
        #
        if event.identifier == self.client_id:

            self.await_confirmation = False

        return
示例#3
0
文件: ui.py 项目: flberger/fabula
    def process_DropsEvent(self, event):
        """Pass the Event to the dropping Entity and spawn the dropped Entity.
        """

        fabula.LOGGER.debug("passing Event to '{}'".format(event.identifier))

        self.host.room.entity_dict[event.identifier].process_DropsEvent(event)

        # Spawn the dropped Entity
        #
        fabula.LOGGER.info("spawning '{}' in room".format(
            event.entity.identifier))

        entity = self.host.room.entity_dict[event.entity.identifier]

        self.process_SpawnEvent(fabula.SpawnEvent(entity, event.location))

        return
示例#4
0
文件: server.py 项目: flberger/fabula
    def _generate_room_rack_events(self, room_identifier):
        """Generate and return a series of Events that establish an existing Room and the Rack.

           EnterRoomEvent and RoomCompleteEvent will not be included.
        """

        event_list = []

        room = self.room_by_id[room_identifier]

        for tuple in room.floor_plan:

            tile = room.floor_plan[tuple].tile

            change_map_element_event = fabula.ChangeMapElementEvent(
                tile, tuple + (room.identifier, ))

            event_list.append(change_map_element_event)

        for identifier in room.entity_locations:

            entity = room.entity_dict[identifier]

            spawn_event = fabula.SpawnEvent(
                entity,
                room.entity_locations[identifier] + (room.identifier, ))

            event_list.append(spawn_event)

            # Set properties of the Entity if there are any
            #
            for property in entity.property_dict.keys():

                change_property_event = fabula.ChangePropertyEvent(
                    entity.identifier, property,
                    entity.property_dict[property])

                event_list.append(change_property_event)

        if len(self.rack.entity_dict):

            for identifier in self.rack.entity_dict:

                # Only send items that are owned by Entities currently in this
                # room.
                #
                if self.rack.owner_dict[identifier] in self.room_by_id[
                        room.identifier].entity_dict.keys():

                    # We must spawn at a valid location. Spawning at the first
                    # coordinate tuple in the room. Doesn't matter, it will be
                    # picked up in an instant anyway.
                    #
                    spawn_event = fabula.SpawnEvent(
                        self.rack.entity_dict[identifier],
                        list(room.floor_plan.keys())[0] + (room.identifier, ))

                    event_list.append(spawn_event)

                    picks_up_event = fabula.PicksUpEvent(
                        self.rack.owner_dict[identifier], identifier)

                    event_list.append(picks_up_event)

        return event_list
示例#5
0
    def handle_messages(self, remote_message_buffer):
        """This background thread method transfers messages between local and remote MessageBuffer.
           It uses representations of the Events being sent to create new copies
           of the original ones.
        """

        fabula.LOGGER.info("starting up")

        # Run thread as long as no shutdown is requested
        #
        while not self.shutdown_flag:

            # Get messages from remote
            #
            if remote_message_buffer.messages_for_remote:

                original_message = remote_message_buffer.messages_for_remote.popleft(
                )

                new_message = fabula.Message([])

                for event in original_message.event_list:

                    if isinstance(
                            event, fabula.SpawnEvent
                    ) and event.entity.__class__ is not fabula.Entity:

                        # These need special care. We need to have a canonic
                        # fabula.Entity. Entity.clone() will produce one.
                        #
                        fabula.LOGGER.debug(
                            "cloning canonical Entity from {}".format(
                                event.entity))

                        event = fabula.SpawnEvent(event.entity.clone(),
                                                  event.location)

                    # Create new instances from string representations to avoid
                    # concurrent access of client and server to the object
                    #
                    fabula.LOGGER.debug("evaluating: '{}'".format(repr(event)))

                    try:
                        new_message.event_list.append(eval(repr(event)))

                    except SyntaxError:

                        # Well, well, well. Some __repr__ does not return a
                        # string that can be evaluated here!
                        #
                        fabula.LOGGER.error(
                            "error: can not evaluate '{}', skipping".format(
                                repr(event)))

                # Blindly use the first connection
                #
                list(self.connections.values())[0].messages_for_local.append(
                    new_message)

            # No need to deliver messages to remote since it will grab them -
            # see above.

            # No need to run as fast as possible
            #
            sleep(1 / self.framerate)

        # Caught shutdown notification, stopping thread
        #
        fabula.LOGGER.info("shutting down")

        self.shutdown_confirmed = True

        raise SystemExit