Example #1
0
 def test_read_all_games(self):
     storage = Storage()
     game_number1 = len(storage.read_all_games())
     game = Game()
     storage.save_game_ORM(game)
     game_number2 = len(storage.read_all_games())
     self.assertEqual(game_number1 + 1, game_number2)
Example #2
0
 def test_save_empty_game(self):
     storage = Storage()
     game = Game()
     storage.save_game_ORM(game)
     id = game.id
     game1 = storage.read_game(id)
     self.assertEqual(game1, game)
Example #3
0
 def test_regular_game(self):
     storage = Storage()
     filename = os.path.join(self.path, "test_files/one_game.pgn")
     chess_file = ChessFile(filename)
     game = chess_file.next()
     game.simulate()
     storage.save_game_ORM(game)
     id = game.id
     game1 = storage.read_game(id)
     self.assertEqual(game1.moves[0].serial0, game.moves[0].serial0)
     self.assertEqual(game1, game)
Example #4
0
 def __init__(self):
     self.current_game = None
     self.current_move_num = None
     super(MainWindow, self).__init__()
     self.storage = Storage()
     self.initUI()
Example #5
0
class MainWindow(QtGui.QWidget):

    SHIFT = 50
    BUTTON_SIZE = 20
    LABEL_DIST = 30
    UP = 350

    def __init__(self):
        self.current_game = None
        self.current_move_num = None
        super(MainWindow, self).__init__()
        self.storage = Storage()
        self.initUI()

    def _display_games(self, games):
        self.games_list.clear()
        for game in games:
            self.games_list.addItem(GameItem(game))

    def _show_all_games(self):
        games = self.storage.read_all_games(True)
        self._display_games(games)

    def _import_games(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file')
        if not filename:
            return

        self.info_block.addItem('New import:')
        start_timestamp = datetime.now()

        imported_games_num = 0
        invalid_games_num = 0
        games_num = 0
        chess_file = ChessFile(filename)
        is_quick_import = self.import_check_box.isChecked()
        while 1:
            try:
                games_num += 1
                game = chess_file.next()
                if not is_quick_import:
                    game.simulate()
                self.storage.save_game(game)
                imported_games_num += 1
            except StopIteration:
                break
            except InvalidGameException as err:
                self.info_block.addItem('ERROR in %d game in file: %s' % (games_num, err.message))
                invalid_games_num += 1

        end_timestamp = datetime.now()
        time = (end_timestamp - start_timestamp).total_seconds()

        self.info_block.addItem('\t%d games were imported, %d games caused mistakes (%.2f sec)' % (imported_games_num, invalid_games_num, time))

        self._show_all_games()

    def _export_games(self):
        filename = QtGui.QFileDialog.getSaveFileName(self, 'Export file')
        if not filename:
            return
        exported_games_num = 0
        invalid_games_num = 0
        try:
            chess_file = ChessFile(filename)
            for game_item in self.games_list.selectedItems():
                try:
                    chess_file.add_game(game_item.content)
                    exported_games_num += 1
                except StopIteration:
                    break
                except Exception as err:
                    invalid_games_num += 1
        except :
            pass

        self.info_block.addItem('%d games were exported, %d games caused mistakes ' % (exported_games_num, invalid_games_num))


    def _show_game(self):
        self.current_game = self.games_list.currentItem().content
        self.current_game = self.storage.read_game(self.current_game.id)
        self.current_move_num  = -1
        try:
            self.current_game.simulate()
        except :
            pass
        self.event_label.setText('Event: \n%s' % self.current_game.event)
        self.site_label.setText('Site: \n%s' % self.current_game.site)
        self.date_label.setText('Date: \n%s' % self.current_game.date)
        self.white_label.setText('White: \n%s' % self.current_game.white)
        self.black_label.setText('Black: \n%s' % self.current_game.black)
        self.result_label.setText('Result: \n%s' % self.current_game.result)
        self.round_label.setText('Round: \n%s' % self.current_game.round)

        self.move_list.clear()
        self.move_list.addItem('')
        for move in self.current_game.moves:
            self.move_list.addItem(MoveItem(move))

        self._show_first_move()

    def _show_move(self):
        if not self.current_game is None:
            self.current_move_num = self.move_list.currentRow()
            self.board.redraw(self.current_game.board_states[self.current_move_num])

    def _show_first_move(self):
        if not self.current_game is None:
            try:
                self.current_move_num = 0
                self.move_list.setCurrentRow(self.current_move_num)
                self.board.redraw(self.current_game.board_states[self.current_move_num])
            except Exception as err:
                print err
        else:
            board_state = BoardState()
            board_state.make_initial_setup()
            self.board.redraw(board_state)

    def _show_last_move(self):
        if not self.current_game is None:
            self.current_move_num = len(self.current_game.moves)
            self.move_list.setCurrentRow(self.current_move_num)

    def _show_next_move(self):
        if not self.current_game is None:
            if self.current_move_num < len(self.current_game.moves):
                self.current_move_num += 1
                self.move_list.setCurrentRow(self.current_move_num)

    def _show_prev_move(self):
        if not self.current_game is None:
            if self.current_move_num > 0:
                self.current_move_num -= 1
                self.move_list.setCurrentRow(self.current_move_num)

    def _search_games(self):
        event = self.event_search_edit.text()
        site = self.site_search_edit.text()
        date = self.date_search_edit.text()
        white = self.white_search_edit.text()
        black = self.black_search_edit.text()
        if self.board_search_check_box.isChecked():
            board_state = self.board.board_state
        else:
            board_state = None
        games = self.storage.read_games(event, site, date, white, black, board_state)
        self._display_games(games)

    def _show_statistics(self):
        self.statistics_list.clear()
        statistics = self.storage.get_statistics(self.board.board_state)
        if len(statistics) == 0:
            self.statistics_list.addItem('No games found')
        for statistics_item in statistics:
            self.statistics_list.addItem(statistics_item.move_notation + '\t'
                                         + str(statistics_item.number) + '\t'
                                         + str(round(statistics_item.white_win, 2)) + '%')

    def _create_left_block(self):
        self.games_list = QtGui.QListWidget(self)
        self.games_list.resize(350, self.UP - self.SHIFT*1.5)
        self.games_list.move(self.SHIFT, self.SHIFT)
        self.games_list.doubleClicked.connect(self._show_game)
        self.games_list.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

        import_button = QtGui.QPushButton('Import PGN', self)
        import_button.resize(150, self.BUTTON_SIZE)
        import_button.move(self.SHIFT, self.UP - 10)
        import_button.clicked.connect(self._import_games)

        export_button = QtGui.QPushButton('Export to PGN', self)
        export_button.resize(150, self.BUTTON_SIZE)
        export_button.move(self.SHIFT + 50 + 150, self.UP - 10)
        export_button.clicked.connect(self._export_games)

        self.import_check_box = QtGui.QCheckBox('Quick import', self)
        self.import_check_box.move(self.SHIFT, self.UP + 17)
        self.import_check_box.setChecked(True)

        event_search_label = QtGui.QLabel('Event ', self)
        event_search_label.move(self.SHIFT, self.UP + 20 + self.LABEL_DIST)
        site_search_label = QtGui.QLabel('Site ', self)
        site_search_label.move(self.SHIFT, self.UP + 20 + self.LABEL_DIST*2)
        date_search_label = QtGui.QLabel('Date ', self)
        date_search_label.move(self.SHIFT, self.UP + 20 + self.LABEL_DIST*3)
        white_search_label = QtGui.QLabel('White ', self)
        white_search_label.move(self.SHIFT, self.UP + 20 + self.LABEL_DIST*4)
        black_search_label = QtGui.QLabel('Black ', self)
        black_search_label.move(self.SHIFT, self.UP + 20 + self.LABEL_DIST*5)

        self.event_search_edit = QtGui.QLineEdit('', self)
        self.event_search_edit.resize(300, self.BUTTON_SIZE)
        self.event_search_edit.move(self.SHIFT + 50, self.UP + 20 + self.LABEL_DIST)

        self.site_search_edit = QtGui.QLineEdit('', self)
        self.site_search_edit.resize(300, self.BUTTON_SIZE)
        self.site_search_edit.move(self.SHIFT + 50, self.UP + 20 + self.LABEL_DIST*2)

        self.date_search_edit = QtGui.QLineEdit('', self)
        self.date_search_edit.resize(300, self.BUTTON_SIZE)
        self.date_search_edit.move(self.SHIFT + 50, self.UP + 20 + self.LABEL_DIST*3)

        self.white_search_edit = QtGui.QLineEdit('', self)
        self.white_search_edit.resize(300, self.BUTTON_SIZE)
        self.white_search_edit.move(self.SHIFT + 50, self.UP + 20 + self.LABEL_DIST*4)

        self.black_search_edit = QtGui.QLineEdit('', self)
        self.black_search_edit.resize(300, self.BUTTON_SIZE)
        self.black_search_edit.move(self.SHIFT + 50, self.UP + 20 + self.LABEL_DIST*5)


        search_button = QtGui.QPushButton('Search', self)
        search_button.resize(150, self.BUTTON_SIZE)
        search_button.move(self.SHIFT, self.UP + 20 + self.LABEL_DIST*5 + 40)
        search_button.clicked.connect(self._search_games)


        self.board_search_check_box = QtGui.QCheckBox('Board state search', self)
        self.board_search_check_box.move(self.SHIFT, self.UP + 20 + self.LABEL_DIST*5 + 40 + 27)
        self.board_search_check_box.setChecked(False)


        all_games_button = QtGui.QPushButton('Show all games', self)
        all_games_button.resize(150, self.BUTTON_SIZE)
        all_games_button.move(self.SHIFT + 50 + 150, self.UP + 20 + self.LABEL_DIST*5 + 40)
        all_games_button.clicked.connect(self._show_all_games)

    def _create_central_block(self):
        self.board = BoardScene()
        self.chess_board = QtGui.QGraphicsView(self.board, parent = self)
        self.chess_board.setAcceptDrops(True)
        self.chess_board.resize(402, 402)
        self.chess_board.move(self.SHIFT + 350 + self.SHIFT, self.SHIFT)

        start_move_button = QtGui.QPushButton('<<', self)
        start_move_button.resize(70, self.BUTTON_SIZE)
        start_move_button.move(self.SHIFT*2 + 350 + 15, self.SHIFT + 400 + 20)
        start_move_button.clicked.connect(self._show_first_move)

        prev_move_button = QtGui.QPushButton('<', self)
        prev_move_button.resize(70, self.BUTTON_SIZE)
        prev_move_button.move(self.SHIFT*2 + 350 + 15 + 100, self.SHIFT + 400 + 20)
        prev_move_button.clicked.connect(self._show_prev_move)

        next_move_button = QtGui.QPushButton('>', self)
        next_move_button.resize(70, self.BUTTON_SIZE)
        next_move_button.move(self.SHIFT*2 + 350 + 15 + 200, self.SHIFT + 400 + 20)
        next_move_button.clicked.connect(self._show_next_move)

        last_move_button = QtGui.QPushButton('>>', self)
        last_move_button.resize(70, self.BUTTON_SIZE)
        last_move_button.move(self.SHIFT*2 + 350 + 15 + 300, self.SHIFT + 400 + 20)
        last_move_button.clicked.connect(self._show_last_move)

        self.info_block = QtGui.QListWidget(self)
        self.info_block.resize(400, 100)
        self.info_block.move(self.SHIFT*2 + 350, 520)


    def _create_right_block(self):

        self.move_list = QtGui.QListWidget(self)
        self.move_list.resize(100, 300)
        self.move_list.move(self.SHIFT*3 + 350 + 400, self.SHIFT)
        self.move_list.currentItemChanged.connect(self._show_move)

        self.event_label = QtGui.QLabel('Event: ', self)
        self.event_label.move(self.SHIFT*3 + 350 + 400 + 100 + 20, self.SHIFT)
        self.event_label.resize(200, 30)

        self.site_label = QtGui.QLabel('Site: ', self)
        self.site_label.move(self.SHIFT*3 + 350 + 400 + 100 + 20, self.SHIFT + self.LABEL_DIST*1.5)
        self.site_label.resize(200, 30)

        self.date_label = QtGui.QLabel('Date: ', self)
        self.date_label.move(self.SHIFT*3 + 350 + 420 + 100, self.SHIFT + self.LABEL_DIST*3)
        self.date_label.resize(200, 30)

        self.white_label = QtGui.QLabel('White: ', self)
        self.white_label.move(self.SHIFT*3 + 350 + 420 + 100, self.SHIFT + self.LABEL_DIST*4.5)
        self.white_label.resize(200, 30)

        self.black_label = QtGui.QLabel('Black: ', self)
        self.black_label.move(self.SHIFT*3 + 350 + 420 + 100, self.SHIFT + self.LABEL_DIST*6)
        self.black_label.resize(200, 30)

        self.round_label = QtGui.QLabel('Round: ', self)
        self.round_label.move(self.SHIFT*3 + 350 + 420 + 100, self.SHIFT + self.LABEL_DIST*7.5)
        self.round_label.resize(200, 30)

        self.result_label = QtGui.QLabel('Result: ', self)
        self.result_label.move(self.SHIFT*3 + 350 + 420 + 100, self.SHIFT + self.LABEL_DIST*9)
        self.result_label.resize(200, 30)

        get_statistics_button = QtGui.QPushButton('Get move statistics', self)
        get_statistics_button.resize(250, self.BUTTON_SIZE)
        get_statistics_button.move(self.SHIFT*3 + 350 + 400, self.SHIFT + 320)
        get_statistics_button.clicked.connect(self._show_statistics)


        self.statistics_list = QtGui.QListWidget(self)
        self.statistics_list.resize(250, 180)
        self.statistics_list.move(self.SHIFT*3 + 350 + 400, self.SHIFT + 350)


    def initUI(self):

        self._create_left_block()
        self._create_central_block()
        self._create_right_block()

        self.resize(1220, 650)
        self.center()
        self.setWindowTitle('ChessMaster')
        self.show()

    def center(self):

        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())