-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·98 lines (73 loc) · 2.99 KB
/
server.py
File metadata and controls
executable file
·98 lines (73 loc) · 2.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
#!/usr/bin/env python
from subprocess import Popen, PIPE
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
import signal
import sys
import json
PORT = 8000
class Engine:
def __init__(self):
self._proc = Popen(['stockfish'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
self._proc.stdin.write('setoption name Hash value 128\n')
self._proc.stdin.write('setoption name Threads value 4\n')
self._proc.stdin.write('setoption name Best Book Move value true\n')
self._proc.stdin.write('setoption name Aggressiveness value 200\n')
self._proc.stdin.write('setoption name Cowardice value 0\n')
self._proc.stdin.write('setoption name Contempt Factor value 50\n')
def update_pos(self, fen):
self._proc.stdin.write('position fen %s\n' % fen)
def get_next_move(self, fen=None, time_limit=50):
if fen is not None:
self.update_pos(fen)
self._proc.stdin.write('go movetime %d\n' % time_limit)
line = self._proc.stdout.readline()
while not line.startswith('bestmove'):
line = self._proc.stdout.readline()
print line
return line.split(' ')[1]
def close(self):
self._proc.kill()
# to anyone reading this code, i swear i would've underscores instead of camelcase, but
# the kid who wrote SimpleWebSocketServer decided he didn't like standard python naming conventions
class Client(WebSocket):
def __init__(self, *args):
WebSocket.__init__(self, *args)
self._engine = Engine()
def sendMessage(self, data):
WebSocket.sendMessage(self, json.dumps(data))
def handleMessage(self):
try:
if self.data is None:
self.sendClose()
return
print '%s: %s' % (str(self.address), self.data)
self.data = str(self.data)
self.data = json.loads(self.data)
if self.data['op'] == 'start':
self._engine.close()
self._engine = Engine()
if self.data['op'] == 'get_next_move':
params = [self.data['fen']]
if 'duration' in self.data:
params.append(self.data['duration'])
next_move = self._engine.get_next_move(*params)
src, dest = next_move[:2], next_move[2:]
self.sendMessage({
'op': 'make_move',
'move_from': src,
'move_to': dest
})
except Exception as e:
self.sendMessage({'error': str(e)})
def handleConnected(self):
print '%s: <connected>' % str(self.address)
def handleClose(self):
print '%s: <closed>' % str(self.address)
if __name__ == '__main__':
server = SimpleWebSocketServer('', PORT, Client)
def close_sig_handler(signal, frame):
server.close()
sys.exit()
signal.signal(signal.SIGINT, close_sig_handler)
print 'listening on port %d' % PORT
server.serveforever()