Skip to content

Commit 7b5fee7

Browse files
committed
Refactor updating of game moves, clock and comments.
* Add variable to save comments in the pgn. Not settable via gui at the moment. * Add variable to save time left in the pgn. Not settable via gui at the moment.
1 parent cc8432a commit 7b5fee7

1 file changed

Lines changed: 65 additions & 56 deletions

File tree

python_easy_chess_gui.py

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,57 @@ def __init__(self, theme, engine_config_file, user_config_file,
677677

678678
self.gui_theme = 'Reddit'
679679

680+
self.is_save_time_left = True
681+
self.is_save_user_comment = True
682+
683+
def update_game(self, mc, user_move, time_left, user_comment):
684+
"""
685+
Used for saving moves in the game.
686+
687+
:param mc: move count
688+
:param user_move:
689+
:param time_left:
690+
:param user_comment: Can be a 'book' from the engine
691+
:return:
692+
"""
693+
# Save user comment
694+
if self.is_save_user_comment:
695+
# If comment is empty
696+
if not (user_comment and user_comment.strip()):
697+
if mc == 1:
698+
self.node = self.game.add_variation(user_move)
699+
else:
700+
self.node = self.node.add_variation(user_move)
701+
702+
# Save clock (time left after a move) as move comment
703+
if self.is_save_time_left:
704+
rem_time = self.get_time_h_mm_ss(time_left, False)
705+
self.node.comment = '[%clk {}]'.format(rem_time)
706+
else:
707+
if mc == 1:
708+
self.node = self.game.add_variation(user_move)
709+
else:
710+
self.node = self.node.add_variation(user_move)
711+
712+
# Save clock, add clock as comment after a move
713+
if self.is_save_time_left:
714+
rem_time = self.get_time_h_mm_ss(time_left, False)
715+
self.node.comment = '[%clk {}] {}'.format(rem_time,
716+
user_comment)
717+
else:
718+
self.node.comment = user_comment
719+
# Do not save user comment
720+
else:
721+
if mc == 1:
722+
self.node = self.game.add_variation(user_move)
723+
else:
724+
self.node = self.node.add_variation(user_move)
725+
726+
# Save clock, add clock as comment after a move
727+
if self.is_save_time_left:
728+
rem_time = self.get_time_h_mm_ss(time_left, False)
729+
self.node.comment = '[%clk {}]'.format(rem_time)
730+
680731
def create_new_window(self, window, flip=False):
681732
""" Close the window param just before turning the new window """
682733

@@ -1162,8 +1213,10 @@ def get_time_mm_ss_ms(self, time_ms):
11621213

11631214
def get_time_h_mm_ss(self, time_ms, symbol=True):
11641215
"""
1165-
Returns time in h:mmm:ss format
1216+
Returns time in h:mm:ss format.
1217+
11661218
:param time_ms:
1219+
:param symbol:
11671220
:return:
11681221
"""
11691222
s, ms = divmod(int(time_ms), 1000)
@@ -1208,6 +1261,7 @@ def get_tag_date(self):
12081261
def init_game(self):
12091262
""" Initialize game with initial pgn tag values """
12101263
self.game = chess.pgn.Game()
1264+
self.node = None
12111265
self.game.headers['Event'] = INIT_PGN_TAG['Event']
12121266
self.game.headers['Date'] = self.get_tag_date()
12131267
self.game.headers['White'] = INIT_PGN_TAG['White']
@@ -2004,48 +2058,10 @@ def play_game(self, window, engine_id_name, board):
20042058
# Update clock, reset elapse to zero
20052059
human_timer.update_base()
20062060

2007-
if move_cnt == 1:
2008-
# Save comment from comment box
2009-
if True:
2010-
user_comment = value['comment_k']
2011-
2012-
# If comment is empty
2013-
if not (user_comment and user_comment.strip()):
2014-
node = self.game.add_variation(user_move)
2015-
else:
2016-
node = self.game.add_variation(user_move)
2017-
node.comment = user_comment
2018-
else:
2019-
node = self.game.add_variation(user_move)
2020-
else:
2021-
# Save comment from comment box
2022-
if True:
2023-
user_comment = value['comment_k']
2024-
if not (user_comment and user_comment.strip()):
2025-
node = node.add_variation(user_move)
2026-
rem_time = self.get_time_h_mm_ss(
2027-
human_timer.base, False)
2028-
node.comment = '[%clk {}]'.format(
2029-
rem_time, user_comment)
2030-
else:
2031-
node = node.add_variation(user_move)
2032-
2033-
# Combine user and clk comment
2034-
2035-
# Add comment after the move
2036-
node.comment = user_comment
2037-
2038-
# Add clk as comment after a move
2039-
rem_time = self.get_time_h_mm_ss(
2040-
human_timer.base, False)
2041-
node.comment = '[%clk {}] {}'.format(
2042-
rem_time, user_comment)
2043-
else:
2044-
node = node.add_variation(user_move)
2045-
rem_time = self.get_time_h_mm_ss(
2046-
human_timer.base, False)
2047-
node.comment = '[%clk {}]'.format(
2048-
rem_time, user_comment)
2061+
# Update game, move from human
2062+
time_left = human_timer.base
2063+
user_comment = value['comment_k']
2064+
self.update_game(move_cnt, user_move, time_left, user_comment)
20492065

20502066
window.FindElement('_movelist_').Update(disabled=False)
20512067
window.FindElement('_movelist_').Update('')
@@ -2268,20 +2284,13 @@ def play_game(self, window, engine_id_name, board):
22682284
# Update timer
22692285
engine_timer.update_base()
22702286

2271-
if move_cnt == 1:
2272-
node = self.game.add_variation(best_move)
2273-
if is_book_from_gui:
2274-
node.comment = 'book'
2287+
# Update game, move from engine
2288+
time_left = engine_timer.base
2289+
if is_book_from_gui:
2290+
engine_comment = 'book'
22752291
else:
2276-
node = node.add_variation(best_move)
2277-
if is_book_from_gui:
2278-
node.comment = 'book'
2279-
2280-
# Add clk as comment after a move
2281-
if not is_book_from_gui:
2282-
rem_time = self.get_time_h_mm_ss(
2283-
engine_timer.base, False)
2284-
node.comment = '[%clk {}]'.format(rem_time)
2292+
engine_comment = ''
2293+
self.update_game(move_cnt, best_move, time_left, engine_comment)
22852294

22862295
window.FindElement('_movelist_').Update(disabled=False)
22872296
window.FindElement('_movelist_').Update('')

0 commit comments

Comments
 (0)