Beispiel #1
0
    def __init__(self, port=8000, run_local=False):
        super(Server, self).__init__()

        self.teams = Teams()
        self.game_state = "game_running"

        self.network = NetServer(port, run_local=run_local, callbacks={
            "new_client":   self.register_client,
            "keypress":     self.handle_event,
            "keyrelease":   self.handle_event,
            "mousepress":   self.handle_event,
            "mouserelease": self.handle_event,
            "orientation":  self.handle_event
        })
        self.buffer = []
        self.last_explosion = time.time()
        self.last_laser = time.time()
        self.last_puff = time.time()
        pyglet.clock.schedule_interval(self.update, 1.0 / GAME_SPEED)
        pyglet.app.run()
Beispiel #2
0
    def __init__(self, port=8000, run_local=False):
        super(Server, self).__init__()

        self.teams = Teams()
        self.game_state = "game_running"

        self.network = NetServer(port,
                                 run_local=run_local,
                                 callbacks={
                                     "new_client": self.register_client,
                                     "keypress": self.handle_event,
                                     "keyrelease": self.handle_event,
                                     "mousepress": self.handle_event,
                                     "mouserelease": self.handle_event,
                                     "orientation": self.handle_event
                                 })
        self.buffer = []
        self.last_explosion = time.time()
        self.last_laser = time.time()
        self.last_puff = time.time()
        pyglet.clock.schedule_interval(self.update, 1.0 / GAME_SPEED)
        pyglet.app.run()
Beispiel #3
0
class Server(Manager):
    is_server = True

    def __init__(self, port=8000, run_local=False):
        super(Server, self).__init__()

        self.teams = Teams()
        self.game_state = "game_running"

        self.network = NetServer(port,
                                 run_local=run_local,
                                 callbacks={
                                     "new_client": self.register_client,
                                     "keypress": self.handle_event,
                                     "keyrelease": self.handle_event,
                                     "mousepress": self.handle_event,
                                     "mouserelease": self.handle_event,
                                     "orientation": self.handle_event
                                 })
        self.buffer = []
        self.last_explosion = time.time()
        self.last_laser = time.time()
        self.last_puff = time.time()
        pyglet.clock.schedule_interval(self.update, 1.0 / GAME_SPEED)
        pyglet.app.run()

    # callbacks
    def register_client(self, client_id, tag, data):
        print "server registered client with id %s" % client_id

        # client has already connected and has a client_id

        # add new player object
        pos = Vector3(0, 100, 100)
        fwd = Vector3(0, 0, -1)
        vel = Vector3(0, 0, 0)

        player_team = self.teams.least_players()
        spawn_point = Player._determine_spawn(player_team)
        player_team.num_players += 1

        newRoom = copy.copy(self.room)
        newRoom.elements = copy.copy(self.room.elements)
        newRoom.classes = copy.copy(self.room.classes)
        newRoom.instances = copy.copy(self.room.instances)
        newRoom.players = copy.copy(self.room.players)
        newRoom.balistics = copy.copy(self.room.balistics)

        new_player = newRoom.add_player(client_id,
                                        team=player_team,
                                        name="",
                                        pos=spawn_point.position +
                                        spawn_point.forward * 5,
                                        forward=spawn_point.forward)

        self.room = newRoom

        # send "welcome package" to client
        self.network.sendUpdate(
            client_id, "player_init", {
                "persistent_cache": self.room._persistent_cache,
                "player_room_id": new_player.id
            })
        data = {}
        data["game_state"] = "game_start"
        self.network.broadcastUpdate("game_event", data)

    def handle_event(self, client_id, tag, data):
        self.buffer.append((tag, client_id, data))

    # main update loop
    def update(self, dt):
        """Update the current room, then send the data to all the clients."""

        # for puffs for weird collissions
        # passing

        functions = self.get_scheduled_tasks()
        for func in functions:
            func()

        #manage game state
        dead_teams = self.teams.dead_teams()
        if len(dead_teams) > 0:
            self.change_game_state("game_over", dead_teams)
        else:
            if self.game_state == "game_over":
                self.change_game_state("game_start")
            elif self.game_state == "game_start":
                self.change_game_state("game_running")

        # flush buffer, run updates
        current_buffer = self.buffer
        self.buffer = []

        for update in current_buffer:
            kind, client_id, data = update
            if client_id in self.room.players:
                p = self.room.players[client_id]

                if kind in ["keypress", "keyrelease", "orientation"]:
                    # Hey, don't like how this is written?
                    # then /you/ figure out how to add instances to the room
                    # from within player!
                    if kind == "keypress" and data == key.Z and ROCKETS:
                        pos = p.position + (p.forward * 5)
                        vel = p.velocity + (p.forward * 5)
                        r = self.room.add_instance(Rocket,
                                                   position=pos,
                                                   forward=p.forward,
                                                   velocity=vel)
                    p.handle_keypress(kind, data)
                if kind in ["mousepress", "mouserelease"]:
                    p.handle_mouse(kind, data)
            else:
                print "client with id %s is not in our player list yet" % client_id

        # recursively update room and do collision detection
        self.room.update(dt)

        # send out updates
        # print self.room.pack()
        self.network.broadcastUpdate("packed_room", self.room.pack())

    def explosion(self, position):
        t = time.time()
        if t - self.last_explosion < .3:
            return
        else:
            self.last_explosion = t
        data = {"pos": position}
        self.network.broadcastUpdate("explosion", data)

    def puff(self, position, fire_vector, up_vector):
        t = time.time()
        if t - self.last_puff < .5:
            return
        else:
            self.last_puff = t
        # self.sound(position, "knock")
        data = {"pos": position, "fire": fire_vector, "up": up_vector}
        self.network.broadcastUpdate("puff", data)

    def laser(self, **kwargs):
        t = time.time()
        if t - self.last_laser < .3:
            return
        else:
            self.last_laser = t

        self.network.broadcastUpdate("laser", kwargs)

    def sound(self, position, sound_name):

        print "hey, we aren't using the sound broadcasts anymore"
        pass
        # data = {}
        # data["sound_source"] = position
        # data["sound_type"]= "explosion"
        # self.network.broadcastUpdate("sound_play", data)

    #Game States: game_running, game_over
    def change_game_state(self, state, team=None):
        if self.game_state is not state:
            #hand state change
            if state == "game_over":
                self.room.reset_room(delay=1)

            #send data to client
            self.game_state = state
            data = {}
            data["game_state"] = state
            data["team"] = team
            self.network.broadcastUpdate("game_event", data)
Beispiel #4
0
class Server(Manager):
    is_server = True

    def __init__(self, port=8000, run_local=False):
        super(Server, self).__init__()

        self.teams = Teams()
        self.game_state = "game_running"

        self.network = NetServer(port, run_local=run_local, callbacks={
            "new_client":   self.register_client,
            "keypress":     self.handle_event,
            "keyrelease":   self.handle_event,
            "mousepress":   self.handle_event,
            "mouserelease": self.handle_event,
            "orientation":  self.handle_event
        })
        self.buffer = []
        self.last_explosion = time.time()
        self.last_laser = time.time()
        self.last_puff = time.time()
        pyglet.clock.schedule_interval(self.update, 1.0 / GAME_SPEED)
        pyglet.app.run()

    # callbacks
    def register_client(self, client_id, tag, data):
        print "server registered client with id %s" % client_id

        # client has already connected and has a client_id

        # add new player object
        pos = Vector3(0,100,100)
        fwd = Vector3(0,0,-1)
        vel = Vector3(0,0,0)

        player_team=self.teams.least_players()
        spawn_point = Player._determine_spawn(player_team)
        player_team.num_players += 1

        newRoom = copy.copy(self.room)
        newRoom.elements = copy.copy(self.room.elements)
        newRoom.classes = copy.copy(self.room.classes)
        newRoom.instances = copy.copy(self.room.instances)
        newRoom.players = copy.copy(self.room.players)
        newRoom.balistics = copy.copy(self.room.balistics)

        new_player = newRoom.add_player(client_id, team=player_team, name="", pos = spawn_point.position + spawn_point.forward * 5, forward = spawn_point.forward)

        self.room = newRoom

        # send "welcome package" to client
        self.network.sendUpdate(client_id, "player_init", {
            "persistent_cache": self.room._persistent_cache,
            "player_room_id": new_player.id

        })
        data = {}
        data["game_state"] = "game_start"
        self.network.broadcastUpdate("game_event", data)

    def handle_event(self, client_id, tag, data):
        self.buffer.append((tag, client_id, data))

    # main update loop
    def update(self, dt):
        """Update the current room, then send the data to all the clients."""

        # for puffs for weird collissions
        # passing
        
        functions = self.get_scheduled_tasks()
        for func in functions:
            func()

        #manage game state
        dead_teams = self.teams.dead_teams()     
        if len(dead_teams) > 0:
            self.change_game_state("game_over", dead_teams)
        else:
            if self.game_state == "game_over":
                self.change_game_state("game_start")
            elif self.game_state == "game_start":
                self.change_game_state("game_running")

        # flush buffer, run updates
        current_buffer = self.buffer
        self.buffer = []

        for update in current_buffer:
            kind, client_id, data = update
            if client_id in self.room.players:
                p = self.room.players[client_id]

                if kind in ["keypress", "keyrelease", "orientation"]:
                    # Hey, don't like how this is written?
                    # then /you/ figure out how to add instances to the room
                    # from within player!
                    if kind == "keypress" and data == key.Z and ROCKETS:
                        pos = p.position + (p.forward * 5)
                        vel = p.velocity + (p.forward * 5)
                        r = self.room.add_instance(Rocket,
                                               position=pos,
                                               forward=p.forward,
                                               velocity=vel)
                    p.handle_keypress(kind, data)
                if kind in ["mousepress", "mouserelease"]:
                    p.handle_mouse(kind, data)
            else:
                print "client with id %s is not in our player list yet"%client_id

        # recursively update room and do collision detection
        self.room.update(dt)

        # send out updates
        # print self.room.pack()
        self.network.broadcastUpdate("packed_room", self.room.pack())

    def explosion(self, position):
        t = time.time()
        if t - self.last_explosion < .3:
            return
        else:
            self.last_explosion = t
        data = {"pos":position}
        self.network.broadcastUpdate("explosion", data)

    def puff(self, position, fire_vector, up_vector):
        t = time.time()
        if t - self.last_puff < .5:
            return
        else:
            self.last_puff = t
        # self.sound(position, "knock")
        data = {"pos":position,"fire":fire_vector,"up":up_vector}
        self.network.broadcastUpdate("puff", data)
        

    def laser(self, **kwargs):
        t = time.time()
        if t - self.last_laser < .3:
            return
        else:
            self.last_laser = t

        self.network.broadcastUpdate("laser", kwargs)

    def sound(self, position, sound_name):

        print "hey, we aren't using the sound broadcasts anymore"
        pass
        # data = {}
        # data["sound_source"] = position
        # data["sound_type"]= "explosion"
        # self.network.broadcastUpdate("sound_play", data)


    #Game States: game_running, game_over
    def change_game_state(self, state, team = None): 
        if self.game_state is not state:
            #hand state change
            if state == "game_over":
                self.room.reset_room(delay = 1)
            
            #send data to client
            self.game_state = state
            data = {}
            data["game_state"] = state
            data["team"] = team
            self.network.broadcastUpdate("game_event", data)