Skip to content

Commit

Permalink
Restore game state after restarting lichess-bot
Browse files Browse the repository at this point in the history
Should lichess-bot need to be restarted for any reason, these changes
will allow Xboard engines to resume a game that is still in progress.
All prior moves will be sent so the engine can track 50-move and
repetition information.
  • Loading branch information
MarkZH committed Mar 6, 2020
1 parent 9362b39 commit d3306fa
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion engine_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ def __init__(self, board, commands, options=None, silence_stderr=False):
if options:
self._handle_options(options)

self.engine.setboard(board)
if board.fen() != chess.STARTING_FEN:
self.engine.force()
if board.root().fen() != chess.STARTING_FEN:
self.engine.setboard(board.root())
for move in board.move_stack:
self.engine.usermove(move)
self.last_fen_seen = board.fen()

post_handler = chess.xboard.PostHandler()
self.engine.post_handlers.append(post_handler)
Expand Down Expand Up @@ -176,11 +182,15 @@ def send_time(self):
self.engine.level(0, self.minutes, self.seconds, self.inc)

def send_last_move(self, board):
if board.fen() == self.last_fen_seen:
return

self.engine.force()
try:
self.engine.usermove(board.peek())
except IndexError:
self.engine.setboard(board)
self.last_fen_seen = board.fen()

def first_search(self, board, movetime):
self.send_last_move(board)
Expand Down

0 comments on commit d3306fa

Please sign in to comment.