-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhost.py
More file actions
275 lines (252 loc) · 7.99 KB
/
host.py
File metadata and controls
275 lines (252 loc) · 7.99 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
import sys, os
original_sys_path = sys.path.copy()
try:
sys.path.append(os.path.join(os.path.dirname(__file__), 'build'))
import engine # type: ignore
finally:
sys.path = original_sys_path
base_dir = os.path.abspath('extern/client')
static_dir = os.path.join(base_dir, 'static')
template_dir = os.path.join(base_dir, 'templates')
app = Flask(__name__, static_folder=static_dir, template_folder=template_dir)
socketio = SocketIO(app)
t0_fen = """
[Size "8x8"]
[Board "custom"]
[r*nbqk*bnr*/p*p*p*p*p*p*p*p*/8/8/8/8/P*P*P*P*P*P*P*P*/R*NBQK*BNR*:0:0:b]
[r*nbqk*bnr*/p*p*p*p*p*p*p*p*/8/8/8/8/P*P*P*P*P*P*P*P*/R*NBQK*BNR*:0:1:w]
"""
tminf_fen = """
[Size "8x8"]
[Board "custom"]
""" + '\n'.join([f'[r*nbqk*bnr*/p*p*p*p*p*p*p*p*/8/8/8/8/P*P*P*P*P*P*P*P*/R*NBQK*BNR*:0:{i}:b][r*nbqk*bnr*/p*p*p*p*p*p*p*p*/8/8/8/8/P*P*P*P*P*P*P*P*/R*NBQK*BNR*:0:{i+1}:w]' for i in range(0, 41)])
g = engine.game.from_pgn(t0_fen)
game_data = {}
next_options = False
@app.route('/')
def index():
return render_template('index.html')
qs = []
p0 = engine.vec4(0,0,0,0)
no_more_hint = False
@socketio.on('click')
def handle_click(data):
l = int(data['l'])
t = int(data['t'])
c = int(data['c'])
x = int(data['x'])
y = int(data['y'])
c1 = "wb"[data['c']]
x1 = chr(data['x']+ord('a'))
y1 = chr(data['y']+ord('1'))
print(f"Received mouse click: ({l}T{t}{c1}){x1}{y1}")
global qs, p0, g
pos = engine.vec4(x,y,t,l)
present_t, present_c = g.get_current_present()
# print(f"present: t={present_t}, c={present_c}")
# print(pos, qs, pos in qs)
if pos in qs:
fm = engine.ext_move(p0, pos)
print("applying", fm)
flag = g.apply_move(fm)
print("finished", flag)
hl = []
if flag:
if g.currently_check():
checks = g.get_current_checks()
arrows = []
for p, q in checks:
arrows.append({
'from': {'l':p.l(), 't': p.t(), 'x':p.x(), 'y':p.y(), 'c':1-present_c},
'to': {'l':q.l(), 't': q.t(), 'x':q.x(), 'y':q.y(), 'c':1-present_c},
})
print('piece on', p, 'is checking', q)
hl = [
{
'color':'#ff1111',
'arrows':arrows
},
]
print('applying move ', fm, ' --success (checking)')
else:
print('applying move ', fm, ' --success')
else:
print('applying move ', fm, ' --failure')
qs = []
display(hl)
elif c == present_c:
qs = g.gen_move_if_playable(pos)
#print('ds = ', ds)
moves = [{'x':q.x(), 'y':q.y(), 't':q.t(), 'l':q.l(), 'c':present_c} for q in qs]
hl = [
{
'color':'#ff80c0',
'coordinates':[{'x':pos.x(), 'y':pos.y(), 't':pos.t(), 'l':pos.l(), 'c':c}]
},
{
'color': '#80ff80',
'coordinates': moves
}
]
p0 = pos
display(hl)
else:
print('no piece at click')
qs = []
display()
@socketio.on('right_click')
def handle_click(data):
print('canceled click')
global qs
qs = []
display()
@socketio.on('request_prev')
def handle_prev():
print('load previous move')
global no_more_hint
no_more_hint = False
g.visit_parent()
display()
@socketio.on('request_next')
def handle_next(data):
print('load next move')
global next_options, no_more_hint
no_more_hint = False
g.visit_child(next_options[data])
display()
@socketio.on('request_undo')
def handle_undo():
print('attempting undo', end='')
flag = g.undo()
print(' ---', 'success' if flag else 'failed')
display()
@socketio.on('request_redo')
def handle_redo():
print('attempting redo', end='')
flag = g.redo()
print(' ---', 'success' if flag else 'failed')
display()
@socketio.on('request_submit')
def handle_submit():
print('received submition request', end='')
flag = g.submit()
global no_more_hint
no_more_hint = False
print(' ---', 'success' if flag else 'failed')
display()
@socketio.on('request_hint')
def suggest_action():
print('received hint request', end='')
flag = g.suggest_action()
global no_more_hint
no_more_hint = not flag
print(' ---', 'success' if flag else 'failed')
display()
@socketio.on('request_load')
def suggest_action(data):
print('received load:')
print(data)
global g
try:
g = engine.game.from_pgn(data)
display()
except RuntimeError as e:
emit('response_load', str(e))
def convert_boards_data(boards):
def convert_board(board):
l, t, c, s = board
return {"l":l, "t":t, "c":c, "fen":s}
return list(map(convert_board, boards))
def display(hl=[]):
mandatory, optional, unplayable = g.get_current_timeline_status()
present_t, present_c = g.get_current_present()
critical = g.get_movable_pieces()
cc = [{'x':q.x(), 'y':q.y(), 't':q.t(), 'l':q.l(), 'c':present_c} for q in critical]
match_status = g.get_match_status()
comments = g.get_comments()
if comments:
emit('response_text', comments[-1])
else:
emit('response_text', "no comments")
size_x, size_y = g.get_board_size()
children = list(enumerate(g.get_child_actions()))
global next_options, no_more_hint
select_values = {str(n):s for (n,(_,s)) in children}
next_options = {str(n):a for (n,(a,_)) in children}
def show_status(ms):
if ms == engine.match_status_t.PLAYING:
if present_c:
return "Black's move"
else:
return "White's move"
elif ms == engine.match_status_t.WHITE_WINS:
return "White wins"
elif ms == engine.match_status_t.BLACK_WINS:
return "Black wins"
elif ms == engine.match_status_t.STALEMATE:
return "Stalemate"
else:
return str(ms)
new_data = {
'submit-button': 'enabled' if g.can_submit() else 'disabled',
'prev-button': 'enabled' if g.has_parent() else 'disabled',
'next-button': 'enabled' if select_values else 'disabled',
'undo-button': 'enabled' if g.can_undo() else 'disabled',
'redo-button': 'enabled' if g.can_redo() else 'disabled',
'hint-button': 'enabled' if not no_more_hint else 'disabled',
'next-options': select_values,
'metadata': {
"mode" : "odd"
},
'match-status': show_status(match_status),
'size': {
'x':size_x,
'y':size_y
},
'present': {
't': present_t,
'c': present_c,
'color': 'rgba(219,172,52,0.4)'# if not is_game_over else 'rgba(128,128,128,0.4)'
},
'focus': [
{
'l': line,
't': present_t,
'c': present_c
} for line in mandatory
],
'boards':convert_boards_data(g.get_current_boards()),
'highlights':[
{
'color':'#7070ff',
'timelines': mandatory,
},
{
'color':'#80ff80',
'timelines': optional,
},
{
'color':'#ffaaaa',
'timelines': unplayable,
},
{
'color':'#a569bd',
'coordinates': cc,
}
] + hl
}
game_data.update(new_data)
#print('displaying ', g.get_current_boards())
emit('response_data', game_data)
@socketio.on('request_data')
def handle_request(data):
display()
@socketio.on('request_pgn')
def handle_undo():
print('client requests pgn')
pgn = g.show_pgn(engine.SHOW_ALL & ~engine.SHOW_RELATIVE)
emit('response_pgn', pgn)
if __name__ == '__main__':
socketio.run(app, debug=True)