コード例 #1
0
    def matchmaking(self, **kwargs):
        response = {
        }  # an object used to return information to the client directly.
        if 'action' not in kwargs:
            return self.send({"error": "no action given"})

        session_data = SessionStore(session_key=kwargs['session_key'])

        if kwargs['action'] == "flush_db":
            ww_redis_db.flushall(
            )  # use to clear redis of all keys to start afresh
            session_data.flush()
            self.send("flushed!")
            return

            ############################################################
            # Initialise player
            ############################################################
        if "p_id" in session_data:
            player = Player(session_data['p_id'])
        else:
            print("error: p_id not been assigned to session")
            raise TypeError

            ############################################################
            # Join game
            ############################################################
        if kwargs['action'] == "join_game":
            response = player.find_game(
                **kwargs)  # this should really be done throug the game manager

            ############################################################
            # Leave game
            ############################################################
        elif kwargs['action'] == "leave_game":
            try:
                if 'g_id' in kwargs:
                    player_game = Game(kwargs['g_id'])
                elif hasattr(player, "g_id"):
                    player_game = Game(player.g_id)
                elif 'g_id' in self.session:
                    player_game = Game(player.session['g_id'])
                    log_handler.log(
                        log_type="INFO",
                        log_code="Player",
                        log_message=
                        "g_id was passed via session and not via kwargs",
                        log_detail=7,
                        context_id=player.p_id)
                elif not player.is_ingame():
                    response['error'] = "Player not currently in a game."
                    log_handler.log(
                        log_type="ERROR",
                        log_code="Player",
                        log_message=
                        "This player tried to leave their game when they weren't in any game",
                        log_detail=3,
                        context_id=player.p_id)
                    raise ValueError("Player not current in a game")
                else:
                    response['error'] = "Game not found"
                    log_handler.log(
                        log_type="ERROR",
                        log_code="Player",
                        log_message=
                        "No game could be found that is associate with this player",
                        log_detail=3,
                        context_id=player.p_id)
                    raise ValueError("Game not found")

                if player.p_id in player_game.players:
                    player_game.remove_player(leaving_player=player)

                    response['response'] = "Player removed from game"
                else:
                    response[
                        'error'] = "Player not found in game: " + player_game.g_id
                    log_handler.log(
                        log_type="ERROR",
                        log_code="Player",
                        log_message="Could not find this player in the game " +
                        player_game.g_id,
                        log_detail=4,
                        context_id=player.p_id)
            except ValueError:
                log_handler.log(
                    log_type="ERROR",
                    log_code="Player",
                    log_message="Could not find this player in the game " +
                    player_game.g_id,
                    log_detail=4,
                    context_id=player.p_id)

                ############################################################
                # Create game
                ############################################################
        elif kwargs['action'] == "create_game":
            user_config = {}
            if 'config' in kwargs:
                user_config = kwargs['config']

            newGame = Game(config=user_config)

            if player.is_ingame():
                Game(player.g_id).remove_player(player.p_id)

            response = newGame.add_player(player.p_id)

            ############################################################
            # Send response to client
            ############################################################
        self.send(response)
コード例 #2
0
ファイル: routers.py プロジェクト: Jaeger2305/werewolves-site
    def matchmaking(self, **kwargs):
        response = {}   # an object used to return information to the client directly.
        if 'action' not in kwargs:
            return self.send({"error":"no action given"})

        session_data = SessionStore(session_key=kwargs['session_key'])

        if kwargs['action'] == "flush_db":
            ww_redis_db.flushall()     # use to clear redis of all keys to start afresh
            session_data.flush()
            self.send("flushed!")
            return

                                                            ############################################################
                                                            # Initialise player
                                                            ############################################################
        if "p_id" in session_data:
            player = Player(session_data['p_id'])
        else:
            print("error: p_id not been assigned to session")
            raise TypeError

                                                            ############################################################
                                                            # Join game
                                                            ############################################################
        if kwargs['action'] == "join_game":
            response = player.find_game(**kwargs)   # this should really be done throug the game manager

                                                            ############################################################
                                                            # Leave game
                                                            ############################################################
        elif kwargs['action'] == "leave_game":
            try:
                if 'g_id' in kwargs:
                    player_game = Game(kwargs['g_id'])
                elif hasattr(player, "g_id"):
                    player_game = Game(player.g_id)
                elif 'g_id' in self.session:
                    player_game = Game(player.session['g_id'])
                    log_handler.log(
                        log_type    = "INFO",
                        log_code    = "Player",
                        log_message = "g_id was passed via session and not via kwargs",
                        log_detail  = 7,
                        context_id  = player.p_id
                    )
                elif not player.is_ingame():
                    response['error'] = "Player not currently in a game."
                    log_handler.log(
                        log_type    = "ERROR",
                        log_code    = "Player",
                        log_message = "This player tried to leave their game when they weren't in any game",
                        log_detail  = 3,
                        context_id  = player.p_id
                    )
                    raise ValueError ("Player not current in a game")
                else:
                    response['error'] = "Game not found"
                    log_handler.log(
                        log_type    = "ERROR",
                        log_code    = "Player",
                        log_message = "No game could be found that is associate with this player",
                        log_detail  = 3,
                        context_id  = player.p_id
                    )
                    raise ValueError ("Game not found")

                if player.p_id in player_game.players:
                    player_game.remove_player(leaving_player=player)

                    response['response'] = "Player removed from game"
                else:
                    response['error'] = "Player not found in game: "+player_game.g_id
                    log_handler.log(
                        log_type        = "ERROR",
                        log_code        = "Player",
                        log_message     = "Could not find this player in the game "+player_game.g_id,
                        log_detail      = 4,
                        context_id      = player.p_id
                    )
            except ValueError:
                log_handler.log(
                    log_type        = "ERROR",
                    log_code        = "Player",
                    log_message     = "Could not find this player in the game "+player_game.g_id,
                    log_detail      = 4,
                    context_id      = player.p_id
                )

                                                            ############################################################
                                                            # Create game
                                                            ############################################################
        elif kwargs['action'] == "create_game":
            user_config = {}
            if 'config' in kwargs:
                user_config = kwargs['config']

            newGame = Game(config=user_config)

            if player.is_ingame():
                Game(player.g_id).remove_player(player.p_id)

            response = newGame.add_player(player.p_id)


                                                            ############################################################
                                                            # Send response to client
                                                            ############################################################
        self.send(response)