From e7468ccaafbd654d61e3f05d2359a8e023b2674b Mon Sep 17 00:00:00 2001 From: spla Date: Mon, 23 Nov 2020 18:22:08 +0100 Subject: [PATCH] New feature! All moves saved to file in realtime with san anotation --- README.md | 3 +- mastochess.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8002757..c96845a 100644 --- a/README.md +++ b/README.md @@ -42,4 +42,5 @@ Within Python Virtual Environment: ![board](board.png) 20.11.2020 - New feature! added link to on going games in games list -21.11.2020 - New feature! Added a warning to player in turn when has been captured one of its pieces +21.11.2020 - New feature! Added a warning to player in turn when has been captured one of its pieces +23.11.2020 - New feature! Now all moves are saved to file (with san anotation). diff --git a/mastochess.py b/mastochess.py index fe7b24e..dc70582 100644 --- a/mastochess.py +++ b/mastochess.py @@ -117,6 +117,34 @@ def get_piece_name(captured_piece): return piece_name +def get_moved_piece_name(moved_piece): + + if moved_piece == 1: + + moved_piece_name = "P" + + if moved_piece == 2: + + moved_piece_name = "C" + + if moved_piece == 3: + + moved_piece_name = "A" + + if moved_piece == 4: + + moved_piece_name = "T" + + if moved_piece == 5: + + moved_piece_name = "D" + + if moved_piece == 6: + + moved_piece_name = "R" + + return moved_piece_name + def get_notification_data(): try: @@ -475,6 +503,72 @@ def update_game(board_game, toot_url): conn.close() +def save_annotation(): + + square_index = chess.SQUARE_NAMES.index(moving[2:]) + + moved_piece = board.piece_type_at(square_index) + + moved_piece_name = get_moved_piece_name(moved_piece) + + game_file = "anotations/" + str(game_id) + + if moved_piece_name == 'P': + + moved_piece_name = moved_piece_name.replace('P','') + + if capture == True: + + moved_piece_name = moved_piece_name + "X" + + if check == True: + + moved_piece_name = moved_piece_name + moving[2:] + "+" + + if checkmate == True: + + moved_piece_name = moved_piece_name + "+" + + if bool(board.turn == chess.BLACK) == True: + + if check != True and checkmate != True: + + line_data = str(board.fullmove_number) + ". " + moved_piece_name + moving[2:] + + else: + + line_data = str(board.fullmove_number) + ". " + moved_piece_name + + else: + + moved_piece_name = moved_piece_name.lower() + + if check != True and checkmate != True: + + line_data = " - " + moved_piece_name + moving[2:] + "\n" + + else: + + line_data = " - " + moved_piece_name + "\n" + + if not os.path.isfile(game_file): + + file_header = "Partida: " + str(game_id) + "\n" + white_user + " / " + black_user + "\n\n" + + with open(game_file, 'w+') as f: + + f.write(file_header) + + with open(game_file, 'a') as f: + + f.write(line_data) + + else: + + with open(game_file, 'a') as f: + + f.write(line_data) + def close_game(): now = datetime.now() @@ -844,6 +938,8 @@ def get_parameter( parameter, file_path ): def create_dir(): if not os.path.exists('games'): os.makedirs('games') + if not os.path.exists('anotations'): + os.makedirs('anotations') def usage(): @@ -1145,6 +1241,8 @@ if __name__ == '__main__': game_moves = board.ply() + save_annotation() + update_moves(username, game_moves) update_replies(status_id, username, now)