-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·454 lines (375 loc) · 14.8 KB
/
server.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env python
from flask import Flask
from flask import render_template
from flask import current_app
from flask import redirect
from flask import request
from flask import url_for
from flask import jsonify
from flask import send_from_directory
from werkzeug.exceptions import BadRequest
import chess
import chess.syzygy
import chess.gaviota
import functools
import os.path
import warnings
import json
try:
from htmlmin import minify as html_minify
except ImportError:
warnings.warn("Not using HTML minification, htmlmin not imported.")
def html_minify(html):
return html
DEFAULT_FEN = "4k3/8/8/8/8/8/8/4K3 w - - 0 1"
app = Flask(__name__)
syzygy = chess.syzygy.Tablebases()
num = 0
num += syzygy.open_directory(os.path.join(os.path.dirname(__file__), "four-men"))
num += syzygy.open_directory(os.path.join(os.path.dirname(__file__), "five-men"))
num += syzygy.open_directory(os.path.join(os.path.dirname(__file__), "six-men", "wdl"), load_dtz=False)
num += syzygy.open_directory(os.path.join(os.path.dirname(__file__), "six-men", "dtz"), load_wdl=False)
app.logger.info("Loaded %d tablebase files.", num)
gaviota = chess.gaviota.open_tablebases(os.path.join(os.path.dirname(__file__), "gaviota"))
def swap_colors(fen):
parts = fen.split()
return parts[0].swapcase() + " " + parts[1] + " - - 0 1"
def mirror_vertical(fen):
parts = fen.split()
position_parts = "/".join(reversed(parts[0].split("/")))
return position_parts + " " + parts[1] + " - - 0 1"
def mirror_horizontal(fen):
parts = fen.split()
position_parts = "/".join("".join(reversed(position_part)) for position_part in parts[0].split("/"))
return position_parts + " " + parts[1] + " - - 0 1"
def clear_fen(fen):
parts = fen.split()
return DEFAULT_FEN.replace("w", parts[1])
def material(board):
name = ""
name += "K" * chess.pop_count(board.kings & board.occupied_co[chess.WHITE])
name += "Q" * chess.pop_count(board.queens & board.occupied_co[chess.WHITE])
name += "R" * chess.pop_count(board.rooks & board.occupied_co[chess.WHITE])
name += "B" * chess.pop_count(board.bishops & board.occupied_co[chess.WHITE])
name += "N" * chess.pop_count(board.knights & board.occupied_co[chess.WHITE])
name += "P" * chess.pop_count(board.pawns & board.occupied_co[chess.WHITE])
name += "v"
name += "K" * chess.pop_count(board.kings & board.occupied_co[chess.BLACK])
name += "Q" * chess.pop_count(board.queens & board.occupied_co[chess.BLACK])
name += "R" * chess.pop_count(board.rooks & board.occupied_co[chess.BLACK])
name += "B" * chess.pop_count(board.bishops & board.occupied_co[chess.BLACK])
name += "N" * chess.pop_count(board.knights & board.occupied_co[chess.BLACK])
name += "P" * chess.pop_count(board.pawns & board.occupied_co[chess.BLACK])
return name
def jsonp(func):
"""Wraps JSONified output for JSONP requests."""
@functools.wraps(func)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
data = str(func(*args, **kwargs).data)
content = str(callback) + '(' + data + ');'
mimetype = 'application/javascript'
return current_app.response_class(content, mimetype=mimetype)
else:
return func(*args, **kwargs)
return decorated_function
def probe(board):
moves = {}
# The best move will be determined in this order.
mating_move = None
zeroing_move = None
winning_move, winning_dtz = None, -9999
stalemating_move = None
insuff_material_move = None
drawing_move = None
losing_move, losing_dtz = None, -9999
losing_zeroing_move, losing_zeroing_dtz = None, -9999
# Look at all moves and probe for the result position.
for move in board.legal_moves:
uci_move = board.uci(move)
board.push(move)
dtz = syzygy.probe_dtz(board)
dtm = gaviota.probe_dtm(board)
moves[uci_move] = {
"dtz": dtz,
"dtm": dtm,
}
# Mate.
if board.is_checkmate():
mating_move = uci_move
# Winning zeroing move.
if dtz is not None and dtz < 0 and board.halfmove_clock == 0:
zeroing_move = uci_move
# Winning move.
if dtz is not None and dtz < 0 and dtz > winning_dtz:
winning_move = uci_move
winning_dtz = dtz
# Stalemating move.
if board.is_stalemate():
stalemating_move = uci_move
# Insufficient material.
if board.is_insufficient_material():
insuff_material_move = uci_move
# Drawing move.
if dtz is not None and dtz == 0:
drawing_move = uci_move
# Losing move.
if dtz is not None and board.halfmove_clock != 0 and dtz > losing_dtz:
losing_move = uci_move
losing_dtz = dtz
# Losing move.
if dtz is not None and dtz > losing_zeroing_dtz:
losing_zeroing_move = uci_move
losing_zeroing_dtz = dtz
board.pop()
return {
"dtz": syzygy.probe_dtz(board),
"wdl": syzygy.probe_wdl(board),
"dtm": gaviota.probe_dtm(board),
"bestmove": mating_move or zeroing_move or winning_move or stalemating_move or insuff_material_move or drawing_move or losing_move or losing_zeroing_move,
"moves": moves,
}
@app.route("/api")
@app.route("/api/")
def api():
return redirect(url_for(".api_v1", fen=request.args.get("fen")))
@app.route("/api/v1")
@jsonp
def api_v1():
# Get required FEN argument.
fen = request.args.get("fen")
if not fen:
raise BadRequest("fen required")
# Setup a board with the given FEN.
try:
board = chess.Board(fen)
except ValueError:
raise BadRequest("invalid fen")
# Check the position for validity.
if not board.is_valid():
raise BadRequest("illegal fen")
# Remove DTM information to produce legacy API output.
result = probe(board)
for move in result["moves"]:
result["moves"][move] = result["moves"][move]["dtz"]
return jsonify(**result)
@app.route("/api/v2")
@jsonp
def api_v2():
# Get required FEN argument.
fen = request.args.get("fen")
if not fen:
raise BadRequest("fen required")
# Setup a board with the given FEN.
try:
board = chess.Board(fen)
except ValueError:
raise BadRequest("invalid fen")
# Check the position for validity.
if not board.is_valid():
raise BadRequest("illegal fen")
return jsonify(**probe(board))
@app.route("/")
def index():
# Setup a board from the given valid FEN or fall back to the default FEN.
try:
board = chess.Board(request.args.get("fen", DEFAULT_FEN))
except ValueError:
try:
board, _ = chess.Board.from_epd(request.args.get("fen", DEFAULT_FEN))
except ValueError:
board = chess.Board(DEFAULT_FEN)
# Get FENs with the current side to move, black and white to move.
original_turn = board.turn
board.turn = chess.WHITE
white_fen = board.fen()
board.turn = chess.BLACK
black_fen = board.fen()
board.turn = original_turn
fen = board.fen()
wdl = None
winning_side = None
winning_moves = []
drawing_moves = []
losing_moves = []
if not board.is_valid():
status = "Invalid position"
elif board.is_stalemate():
status = "Draw by stalemate"
wdl = 0
elif board.is_checkmate():
wdl = 2
if board.turn == chess.WHITE:
status = "Black won by checkmate"
winning_side = "black"
else:
status = "White won by checkmate"
winning_side = "white"
else:
wdl = syzygy.probe_wdl(board)
dtz = syzygy.probe_dtz(board)
if board.is_insufficient_material():
status = "Draw by insufficient material"
wdl = 0
elif dtz is None:
status = "Position not found in tablebases"
elif dtz == 0:
status = "Tablebase draw"
elif dtz > 0 and board.turn == chess.WHITE:
status = "White is winning with DTZ %d" % (abs(dtz), )
winning_side = "white"
losing_side = "black"
elif dtz < 0 and board.turn == chess.WHITE:
status = "White is losing with DTZ %d" % (abs(dtz), )
winning_side = "black"
losing_side = "white"
elif dtz > 0 and board.turn == chess.BLACK:
status = "Black is winning with DTZ %d" % (abs(dtz), )
winning_side = "black"
losing_side = "white"
elif dtz < 0 and board.turn == chess.BLACK:
status = "Black is losing with DTZ %d" % (abs(dtz), )
winning_side = "white"
losing_side = "black"
for move in board.legal_moves:
san = board.san(move)
uci = board.uci(move)
board.push(move)
move_info = {
"uci": uci,
"san": san,
"fen": board.epd() + " 0 1",
"dtz": syzygy.probe_dtz(board),
"dtm": gaviota.probe_dtm(board),
"zeroing": board.halfmove_clock == 0,
"checkmate": board.is_checkmate(),
"stalemate": board.is_stalemate(),
"insufficient_material": board.is_insufficient_material(),
}
move_info["dtm"] = abs(move_info["dtm"]) if move_info["dtm"] is not None else None
move_info["winning"] = move_info["checkmate"] or (move_info["dtz"] is not None and move_info["dtz"] < 0)
move_info["drawing"] = move_info["stalemate"] or move_info["insufficient_material"] or (move_info["dtz"] == 0 or (move_info["dtz"] is None and wdl is not None and wdl < 0))
if move_info["winning"]:
if move_info["checkmate"]:
move_info["badge"] = "Checkmate"
elif move_info["zeroing"]:
move_info["badge"] = "Zeroing"
else:
move_info["badge"] = "Win with DTZ %d" % (abs(move_info["dtz"]), )
winning_moves.append(move_info)
elif move_info["drawing"]:
if move_info["stalemate"]:
move_info["badge"] = "Stalemate"
elif move_info["insufficient_material"]:
move_info["badge"] = "Insufficient material"
elif move_info["dtz"] == 0:
move_info["badge"] = "Draw"
else:
move_info["badge"] = "Unknown"
drawing_moves.append(move_info)
else:
if move_info["dtz"] is None:
move_info["badge"] = "Unknown"
elif move_info["zeroing"]:
move_info["badge"] = "Zeroing"
else:
move_info["badge"] = "Loss with DTZ %d" % (abs(move_info["dtz"]), )
losing_moves.append(move_info)
board.pop()
winning_moves.sort(key=lambda move: move["uci"])
winning_moves.sort(key=lambda move: (move["dtm"] is None, move["dtm"]))
winning_moves.sort(key=lambda move: (move["dtz"] is None, move["dtz"]), reverse=True)
winning_moves.sort(key=lambda move: move["zeroing"], reverse=True)
winning_moves.sort(key=lambda move: move["checkmate"], reverse=True)
drawing_moves.sort(key=lambda move: move["uci"])
drawing_moves.sort(key=lambda move: move["insufficient_material"], reverse=True)
drawing_moves.sort(key=lambda move: move["stalemate"], reverse=True)
losing_moves.sort(key=lambda move: move["uci"])
losing_moves.sort(key=lambda move: (move["dtm"] is not None, move["dtm"]), reverse=True)
losing_moves.sort(key=lambda move: (move["dtz"] is None, move["dtz"]), reverse=True)
losing_moves.sort(key=lambda move: move["zeroing"])
return html_minify(render_template("index.html",
fen_input=board.epd() + " 0 1" if board.epd() + " 0 1" != DEFAULT_FEN else "",
fen=fen,
status=status,
insufficient_material=board.is_insufficient_material(),
winning_side=winning_side,
winning_moves=winning_moves,
drawing_moves=drawing_moves,
losing_moves=losing_moves,
blessed_loss=wdl == -1,
cursed_win=wdl == 1,
illegal=not board.is_valid(),
not_yet_solved=board.epd() + " 0 1" == chess.STARTING_FEN,
unknown=wdl is None,
turn="white" if board.turn == chess.WHITE else "black",
white_fen=white_fen,
black_fen=black_fen,
horizontal_fen=mirror_horizontal(fen),
vertical_fen=mirror_vertical(fen),
swapped_fen=swap_colors(fen),
clear_fen=clear_fen(fen),
DEFAULT_FEN=DEFAULT_FEN,
material=material(board)
))
@app.route("/legal")
def imprint():
return html_minify(render_template("legal.html"))
@app.route("/apidoc")
def apidoc():
render = {}
render["DEFAULT_FEN"] = DEFAULT_FEN
render["status"] = 200
# Parse the FEN.
fen = request.args.get("fen")
if not fen:
render["status"] = 400
render["error"] = "fen required"
else:
try:
board = chess.Board(fen)
except ValueError:
render["status"] = 400
render["error"] = "invalid fen"
# Set the exact given FEN and a sanitized FEN for result.
if fen is not None:
render["fen"] = fen
if render["status"] == 400:
render["sanitized_fen"] = "8/8/8/8/8/8/8/8 w - - 0 1"
else:
render["sanitized_fen"] = board.fen()
if board.is_valid():
render["request_body"] = json.dumps(probe(board), indent=2, sort_keys=True)
else:
render["status"] = 400
render["error"] = "illegal fen"
# Render.
return html_minify(render_template("apidoc.html", **render))
@app.route("/favicon.ico")
def favicon():
return send_from_directory(app.root_path, "favicon.ico", mimetype="image/vnd.microsoft.icon")
@app.route("/sitemap.txt")
def sitemap():
entries = [
"",
"?fen=6N1/5KR1/2n5/8/8/8/2n5/1k6%20w%20-%20-%200%201",
"?fen=4r3/1K6/8/8/5p2/3k4/8/7Q%20b%20-%20-%200%201",
"apidoc?fen=6N1/5KR1/2n5/8/8/8/2n5/1k6%20w%20-%20-%200%201",
"legal",
]
return current_app.response_class("\n".join("https://syzygy-tables.info/" + entry for entry in entries), mimetype="text/plain")
if __name__ == "__main__":
try:
import tornado
import tornado.httpserver
import tornado.wsgi
import tornado.ioloop
except ImportError:
warnings.warn("Using builtin webserver, tornado not imported.")
app.run()
else:
http_server = tornado.httpserver.HTTPServer(tornado.wsgi.WSGIContainer(app))
http_server.listen(5000)
print("Listening on http://127.0.0.1:5000/ ...")
tornado.ioloop.IOLoop.instance().start()