-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
67 lines (56 loc) · 2.24 KB
/
server.py
File metadata and controls
67 lines (56 loc) · 2.24 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
from stockfish import Stockfish
import os
from flask import Flask, request, jsonify
app = Flask(__name__)
mem_bytes = (os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES'))//2
print(os.cpu_count(), "CPUs", mem_bytes // (2 ** 20), "MB of memory")
stockfish = Stockfish(depth=30, parameters={
"Debug Log File": "",
# "Contempt": 0,
"Min Split Depth": 0,
# More threads will make the engine stronger, but should be kept at less than the number of logical processors on your computer.
"Threads": os.cpu_count()-1 if os.cpu_count() > 1 else 1,
"Ponder": "true",
# Default size is 16 MB. It's recommended that you increase this value, but keep it as some power of 2. E.g., if you're fine using 2 GB of RAM, set Hash to 2048 (11th power of 2).
"Hash": mem_bytes // (2 ** 20), # half the available memory
# "MultiPV": 1
# "Skill Level": 20,
# "Move Overhead": 10,
# "Minimum Thinking Time": 20,
# "Slow Mover": 100,
# "UCI_LimitStrength": "false",
"UCI_Elo": 9999}
)
print(stockfish.get_parameters())
@ app.route('/getChessMove', methods=['GET'])
def respond():
# Retrieve the name from url parameter
fen = request.args.get("fen", None)
maxtime = request.args.get("think_time", None)
# For debugging
# print(request.args.keys())
print(fen, maxtime)
response = {}
# # Check if user sent a fen at all
try:
if not fen:
response["ERROR"] = "no fen found, please send a valid board position."
# Check if fen is valid
elif not stockfish.is_fen_valid(fen):
response["ERROR"] = "invalid fen, please send a valid fen."
else:
stockfish.set_fen_position(fen)
response["BESTMOVE"] = stockfish.get_best_move_time(
maxtime if maxtime else 1000)
except Exception as e:
print(e)
response["ERROR"] = str("invalid fen, please send a valid fen.")
# Return the response in json format
return jsonify(response)
# A welcome message to test our server
@ app.route('/')
def index():
return "<h1>Uploading Virus...</h1>"
if __name__ == '__main__':
# Threaded option to enable multiple instances for multiple user access support
app.run(threaded=True, port='80', host='0.0.0.0')