def test_load_games_entries_exist(self):
        warnings.simplefilter('ignore')
        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)
        asyncio.run(game2.save())
        game3 = Game()
        game3.board = chess.Board(
            'rnbqkbnr/ppppp2p/8/5ppQ/4P3/2N5/PPPP1PPP/R1B1KBNR b KQkq - 0 1')
        game3.player1 = FakeDiscordUser(id=2)
        game3.player2 = FakeDiscordUser(id=3)
        game3.result = '1-0'
        asyncio.run(game3.save())

        expected_games = [game1, game2]

        chess_bot = Chess()

        actual = asyncio.run(chess_bot.load_games())

        self.assertListEqual(expected_games, actual)
Example #2
0
    async def make_move(self, game: Game, move: str) -> Game:
        """
        Makes a move in given game, if valid

        :param game: Game to be played
        :type game: Game
        :param move: Chess move in SAN or UCI notation
        :type move: str
        :return: Updated game
        :rtype: Game
        """
        chess_move = self._parse_str_move(game, move)
        if not chess_move:
            raise InvalidMove()
        game.board.push(chess_move)

        game.current_player = game.player1 if game.current_player == game.player2 else game.player2

        if self.is_pve_game(game) and not self.is_game_over(game):
            stockfish_result = await self._play_move(game)
            game.board.push(stockfish_result.move)
            game.current_player = game.player1

        if self.is_game_over(game):
            game.result = game.board.result(claim_draw=True)
            self.games.remove(game)

        await game.save()
        return game
Example #3
0
    async def resign(self, game: Game) -> Game:
        """
        Resigns the given game. Only the next player to move can resign their game.

        :param game: Game to be resigned
        :type game: Game
        :return: Updated game
        :rtype: Game
        """
        board_png_bytes = self.build_png_board(game)
        game.result = '0-1' if game.board.turn == chess.WHITE else '1-0'
        await game.save()
        self.games.remove(game)
        return game
    def test_generate_pgn(self):
        board = chess.Board()
        board.push_san("g4")
        board.push_san("e5")
        board.push_san("f4")
        game = Game()
        game.board = board
        game.player1 = FakeDiscordUser(id=1, name='Player1')
        game.player2 = FakeDiscordUser(id=2, name='Player2')
        game.current_player = game.player1
        game.result = game.board.result()

        chess_bot = Chess()
        chess_bot.games.append(game)
        result = chess_bot.generate_pgn(game)

        self.assertIn('[White "Player1"]', result)
        self.assertIn('[Black "Player2"]', result)
        self.assertIn('1. g4 e5 2. f4 *', result)
Example #5
0
    def new_game(self, user1, user2, color_schema=None, cpu_level=None):
        player1, player2 = convert_users_to_players(user1, user2)
        current_players_pairs = map(lambda x: [x.player1, x.player2],
                                    self.games)
        given_players_pairs = [player1, player2]

        if given_players_pairs in current_players_pairs:
            raise GameAlreadyInProgress()

        game = Game()
        game.board = chess.Board()
        game.player1 = player1
        game.player2 = player2
        game.current_player = player1
        game.result = game.board.result()
        game.color_schema = color_schema
        game.cpu_level = cpu_level

        self.games.append(game)
        return game