def test_save_games(self):
        chess_bot = Chess()
        board1 = chess.Board()
        board1.push_san("e4")
        board1.push_san("e5")
        game1 = Game()
        game1.board = board1
        game1.player1 = FakeDiscordUser(id=1)
        game1.player2 = FakeDiscordUser(id=2)
        asyncio.run(game1.save())
        board2 = chess.Board()
        board2.push_san("Nf3")
        game2 = Game()
        game2.board = board2
        game2.player1 = FakeDiscordUser(id=2)
        game2.player2 = FakeDiscordUser(id=3)
        chess_bot.games = [game1, game2]

        asyncio.run(chess_bot.save_games())

        expected = [
            Game.from_chess_game_model(x)
            for x in self.db_session.query(ChessGame)
        ]

        self.assertEqual(chess_bot.games, expected)
        self.assertEqual(game1.color_schema, expected[0].color_schema)
        self.assertEqual(game1.cpu_level, expected[0].cpu_level)
        self.assertEqual(game2.color_schema, expected[1].color_schema)
        self.assertEqual(game2.cpu_level, expected[1].cpu_level)
    def test_make_move_legal_move_pve(self):
        board = chess.Board(
            'rn2kb1r/pp1qpppp/2ppbn2/1B6/3PP3/2N2N2/PPP2PPP/R1BQK2R w KQkq - 0 6'
        )
        game = Game()
        game.board = board
        game.player1 = FakeDiscordUser(id=1)
        game.player2 = FakeDiscordUser(id=2)
        game.current_player = game.player1
        game.cpu_level = 0

        chess_bot = Chess()
        chess_bot.games.append(game)
        chess_bot.stockfish_limit['time'] = 1
        result = asyncio.run(chess_bot.make_move(game, 'b5d3'))

        self.assertIsInstance(result, Game)
        if chess_bot.is_stockfish_enabled():
            self.assertEqual(len(result.board.move_stack), 2)
            self.assertEqual(result.current_player, game.player1)

        updated_chess_game = self.db_session.query(ChessGame).filter_by(
            player1_id=result.player1.id).first()
        updated_game_from_db = Game.from_chess_game_model(updated_chess_game)
        self.assertEqual(updated_game_from_db.board.move_stack,
                         result.board.move_stack)
Exemple #3
0
    async def load_games(self):
        """
        Load all ongoing games from database

        :return: List of ongoing games
        :rtype: List[Games]
        """
        try:
            chess_games_models = await ChessGameModel.get_all_ongoing_games()
            self.games = [
                Game.from_chess_game_model(x) for x in chess_games_models
            ]
        except Exception as e:
            logging.warning(e, exc_info=True)
        finally:
            return self.games
Exemple #4
0
    async def get_game_by_id(self, chess_game_id: str) -> Game:
        """
        Fetches from database chess game by given chess game id

        :param chess_game_id: Chess game's UUID
        :type chess_game_id: str
        :return: Chess game
        :rtype: Game
        """
        try:
            chess_game_model = await ChessGameModel.get(chess_game_id,
                                                        preload_players=True)
            if not chess_game_model:
                return None
        except:
            return None
        return Game.from_chess_game_model(chess_game_model)
    def test_make_move_legal_san_move_in_players_turn(self):
        board = chess.Board()
        board.push_san("e4")
        board.push_san("e5")
        game = Game()
        game.board = board
        game.player1 = FakeDiscordUser(id=1)
        game.player2 = FakeDiscordUser(id=2)
        game.current_player = game.player1

        chess_bot = Chess()
        chess_bot.games.append(game)
        result = asyncio.run(chess_bot.make_move(game, 'Nf3'))

        self.assertIsInstance(result, Game)
        self.assertEqual(len(result.board.move_stack), 3)
        self.assertEqual(result.current_player, game.player2)

        updated_chess_game = self.db_session.query(ChessGame).filter_by(
            player1_id=result.player1.id).first()
        updated_game_from_db = Game.from_chess_game_model(updated_chess_game)
        self.assertEqual(updated_game_from_db.board.move_stack,
                         result.board.move_stack)