-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathlichess_commentator.py
77 lines (66 loc) · 2.99 KB
/
lichess_commentator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from threading import Thread
import chess
class Lichess_commentator(Thread):
def __init__(self, *args, **kwargs):
super(Lichess_commentator, self).__init__(*args, **kwargs)
self.stream = None
self.speech_thread = None
self.game_state = Game_state()
self.comment_me = None
self.comment_opponent = None
self.language = None
def run(self):
while not self.game_state.board.is_game_over():
is_my_turn = (self.game_state.we_play_white) == (self.game_state.board.turn == chess.WHITE)
found_move, move = self.game_state.register_move_if_needed(self.stream)
if found_move and ((self.comment_me and is_my_turn) or (self.comment_opponent and (not is_my_turn))):
self.speech_thread.put_text(self.language.comment(self.game_state.board, move))
class Game_state:
def __init__(self):
self.we_play_white = None
self.board = chess.Board()
self.registered_moves = []
self.resign_or_draw = False
self.game = None
self.variant = 'wait'
def register_move_if_needed(self, stream):
current_state = next(stream)
if 'state' in current_state:
if current_state['initialFen'] == 'startpos':
self.variant = 'standard'
else:
self.variant = 'fromPosition'
self.from_position(current_state['initialFen'])
current_state = current_state['state']
if 'moves' in current_state:
moves = current_state['moves'].split()
if len(moves) > len(self.registered_moves):
valid_move_string = moves[len(self.registered_moves)]
valid_move_UCI = chess.Move.from_uci(valid_move_string)
self.register_move(valid_move_UCI)
return True, valid_move_UCI
while len(moves) < len(self.registered_moves):
self.unregister_move()
if 'status' in current_state and current_state['status'] in ["resign", "draw"]:
self.resign_or_draw = True
return False, "No move found"
def register_move(self, move):
if move in self.board.legal_moves:
self.board.push(move)
self.registered_moves.append(move)
return True
else:
return False
def unregister_move(self):
self.board.pop()
self.registered_moves.pop()
if len(self.registered_moves) < len(self.game.executed_moves):
self.game.executed_moves.pop()
self.game.played_moves.pop()
self.game.board.pop()
self.game.internet_game.is_our_turn = not self.game.internet_game.is_our_turn
def from_position(self, fen):
self.board = chess.Board(fen)
self.game.board = chess.Board(fen)
if self.board.turn == chess.BLACK:
self.game.internet_game.is_our_turn = not self.game.internet_game.is_our_turn