Exemple #1
0
    def process_packet(self, client, packet):
        ptype = type(packet)
        print "Processing packet=%s: %s from client=%s." % (
            packet.id, ptype.__name__, client.client_id)

        # JoinRequest
        if ptype is packets.JoinRequest:
            # @todo: deny conditions
            # Create the player in the world.
            player = gamestate.objects.Player(self.world)
            player.position = (-20, -20)
            player.rotation = 1.5707963267948966
            self.world.add_object(player)
            client.player = player
            print "Creating player in world with id=%s for client id=%s." % \
                (player.object_id, client.client_id)

            # Send the JoinResponse.
            response = packets.JoinResponse()
            response.player_id = player.object_id
            self.server.output.put_nowait((client, response))

            # Sent a ObjectInit for the new player obeject to everyone but the player.
            init = packets.ObjectInit()
            init.object_id = player.object_id
            init.object_type = "player"
            self.server.output_broadcast.put_nowait((init, player))

            # Send a ObjectUpdate for the new player object to everyone.
            update = self._update_from_object(player)
            self.server.output_broadcast.put_nowait((update, None))

            # Send ObjectUpdate and ObjectInit to the player for each object.
            for object in self.world.objects:
                if not isinstance(object, gamestate.objects.MobileObject):
                    continue
                if object == player:
                    continue
                init = packets.ObjectInit()
                init.object_id = object.object_id
                init.object_type = "player"
                self.server.output.put_nowait((client, init))
                update = self._update_from_object(object)
                self.server.output.put_nowait((client, update))

        # PlayerUpdate
        elif ptype is packets.PlayerUpdate:
            client.player.position = (packet.x, packet.z)
            client.player.rotation = packet.rotation
            if packet.move_speed > 0:
                client.player.is_moving = True
                client.player.move_speed = packet.move_speed
                client.player.move_direction = packet.move_direction
            else:
                client.player.is_moving = False
Exemple #2
0
 def on_world_object_added(self, object):
     if object.type == "player":
         # Sent a ObjectInit for the new player obeject to everyone but the player.
         init = packets.ObjectInit()
         init.object_id = object.object_id
         init.object_type = object.type
         init.element_type = object.element.type
         init.name = object.name
         self.server.output_broadcast.put_nowait((init, object))
         # Send a ObjectUpdate for the new player object to everyone.
         self._send_update(object)
Exemple #3
0
    def process_packet(self, client, packet):
        ptype = type(packet)
        #print "Processing packet=%s: %s from client=%s." % (packet.id, ptype.__name__, client.client_id)

        # JoinRequest
        if ptype is packets.JoinRequest:
            # @todo: deny conditions
            # Create the player in the world.
            player = gamestate.objects.Player(self.world)
            player.name = packet.player_name
            player.object_id = self.world.generate_id()
            player.is_dead = True
            # Listen to events.
            player.health_changed += self.on_player_status_changed
            player.power_changed += self.on_player_status_changed
            player.is_dead_changed += self.on_player_is_dead_changed
            player.score_changed += self.on_player_score_changed
            player.teleported += self.on_player_teleported
            client.player = player
            print "Creating player in world with id=%s for client id=%s." % \
                (player.object_id, client.client_id)

            # Send the JoinResponse.
            response = packets.JoinResponse()
            response.player_id = player.object_id
            self.server.output.put_nowait((client, response))

            # Send ObjectUpdate and ObjectInit to the player for each object.
            for object in self.world.objects:
                if object.type != "player":
                    continue
                if object == player:
                    continue
                init = packets.ObjectInit()
                init.object_id = object.object_id
                init.object_type = object.type
                init.element_type = object.element.type
                init.name = object.name
                self.server.output.put_nowait((client, init))
                update = self._get_update(object)
                self.server.output.put_nowait((client, update))
                score = packets.ScoreUpdate()
                score.player_id = object.object_id
                score.score = object.score
                self.server.output.put_nowait((client, score))

        # SpawnRequest
        elif ptype is packets.SpawnRequest:
            if self.is_round_active:
                # @todo: more deny conditions
                client.player.change_element(packet.element_type)
                client.player.position = self.scene.generate_spawn_position()
                client.player.rotation = 1.5707963267948966
                client.player.health = client.player.max_health
                client.player.health = client.player.max_power
                client.player.is_dead = False
                response = packets.SpawnResponse()
                response.element_type = packet.element_type
                response.x, response.z = client.player.position
                self.server.output.put_nowait((client, response))
                self._send_update(client.player, check_time=False)

        # PlayerUpdate
        elif ptype is packets.PlayerUpdate:
            client.player.position = (packet.x, packet.z)
            client.player.rotation = packet.rotation
            if packet.move_speed > 0:
                client.player.is_moving = True
                #client.player.move_speed = packet.move_speed
                client.player.move_direction = packet.move_direction
            else:
                client.player.is_moving = False

        # AbilityRequest
        elif ptype is packets.AbilityRequest:
            # @todo: deny conditions
            if client.player.use_ability(packet.ability_id):
                used = packets.AbilityUsed()
                used.object_id = client.player.object_id
                used.ability_id = packet.ability_id
                # Send an ObjectUpdate for this player so clients have most recent
                # data when using the ability.
                self._send_update(client.player,
                                  ignore=client.player,
                                  check_time=False)
                self.server.output_broadcast.put_nowait((used, None))