Exemplo n.º 1
0
    def auth(self, message, address):
        if self.connections.get(address).get('player') is not None:
            return
        serializer = AuthSerializer(data=message)
        if not serializer.is_valid():
            self.send_error(address, serializer.errors)
            return

        state, error = yield defer_to_thread(PlayerState.objects.auth,
                                             serializer.data['name'], address)
        if not state:
            self.send(RESPONSE_AUTH_FAILURE, data=error)
            return

        logged_players = yield defer_to_thread(get_logged_players)
        if state.key in logged_players:
            self.send(RESPONSE_PLAYER_ALREADY_LOGGED,
                      data=serializer.data['name'])

        yield defer_to_thread(add_logged_player, state.key)

        self.connections[address]['player'] = Player(self, state)

        serializer = SendAuthSerializer(state)

        self.send(address, RESPONSE_AUTH_PLAYER, data=serializer.data)
        yield self.game_service.matchmake(self.connections[address]['player'])
Exemplo n.º 2
0
def queue_to_broadcast(action, data=None, exclude_sender=False, address=None, group_name=None):
    if not group_name:
        raise Exception("We need a group name to broadcast!")
    group_clients = yield defer_to_thread(get_clients_from_group, group_name)
    for _address in group_clients:
        if exclude_sender and address == _address:
            continue
        yield defer_to_thread(add_message_to_broadcast_queue, _address, action, data)
Exemplo n.º 3
0
 def refresh_state(self):
     if is_dirty(self.state.key):
         set_clean(self.state.key)
         yield defer_to_thread(self.state.refresh_from_db)
     else:
         try:
             self.state.game
         except Game.DoesNotExist:
             yield defer_to_thread(self.state.refresh_from_db)
Exemplo n.º 4
0
    def matchmake(self, player):
        Game = apps.get_model('games', 'Game')
        game, _ = yield defer_to_thread(Game.objects.get_or_create, seed=0)

        yield defer_to_thread(add_to_group, game.key, player.state.address)
        if not game.get_players():
            # self.game_instances[game.key] = yield GameInstance(self, self.protocol, game, player.state.address)
            game_instance = GameInstance(self.game_protocol, game)
            game_instance.setServiceParent(self.parent)

        player.state.game = game
        yield defer_to_thread(player.state.save)
Exemplo n.º 5
0
    def disconnnect(self, address):
        if self.connections[address].get('player'):
            yield defer_to_thread(
                remove_logged_player,
                self.connections[address]['player'].state.key)
            yield self.connections[address]['player'].on_disconnect()
            yield defer_to_thread(
                remove_broadcast_queue,
                self.connections[address]['player'].state.address)

        # lock.acquire()
        del self.connections[address]
Exemplo n.º 6
0
    def consume_broadcast_messages(self):
        for address, conn_data in list(self.connections.items()):
            if conn_data.get('player'):
                messages = yield defer_to_thread(
                    get_message_queued_for_client,
                    conn_data['player'].state.address)
                for action, data in messages:
                    self.send(address, action=action, data=data)

        call_later(0, self.consume_broadcast_messages)
Exemplo n.º 7
0
    def update_remote_addresses_in_current_game(self):
        if self.disconnected:
            return

        if self.state.game:
            addresses_in_current_game = yield defer_to_thread(
                get_clients_from_group, self.state.game.key)
            addresses_in_current_game.remove(self.state.address)
            self.remote_addresses_in_current_game = addresses_in_current_game

        call_later(1, self.update_remote_addresses_in_current_game)
Exemplo n.º 8
0
    def check_players(self):
        addresses = yield defer_to_thread(get_clients_from_group,
                                          self.state.key)
        addresses = set(addresses)

        new_players = addresses - self.addresses
        gone_players = self.addresses - addresses

        for address in new_players:
            yield self.add_address(address)
        for address in gone_players:
            yield self.remove_address(address)

        if not self.addresses:
            self.parent.removeService(self)
            return

        call_later(1, self.check_players)
Exemplo n.º 9
0
 def quit_game(self, _):
     if self.state.game:
         yield defer_to_thread(remove_from_group, self.state.game.key,
                               self.state.address)
         yield defer_to_thread(self.state.quit_game)
Exemplo n.º 10
0
 def launch_workers(self):
     for n in range(1):
         # log.msg("Launched worker %s" % n)
         defer_to_thread(self.worker_loop, n)