This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckmateServer.py
More file actions
462 lines (414 loc) · 18 KB
/
CheckmateServer.py
File metadata and controls
462 lines (414 loc) · 18 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
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
455
456
457
458
459
460
461
462
# This file is part of Checkmate.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2015 Ozge Lule(ozge.lule@ceng.metu.edu.tr),
# Esref Ozturk(esref.ozturk@ceng.metu.edu.tr)
from threading import *
from socket import *
from json import *
import sys
from Checkmate import *
"""
PROTOCOL
--------------
- JSON Objects
- Dict of following fields
* op : String, one of the followings:
= start : starts new game according to params
= connect : connects game with given gameid
= exit : detaches from game
= kill : kills game
= play : plays the command at params[0] according to rest of params
* color : White or Black, assigned to agent
* gameid : int
* params : list of command and parameters will be given to command
Sample examples:
- {"op":"start" , "color":"White","params":["multi","None","None"]}
- {"op":"connect" , "color":"Black","gameid":"1"}
- {"op":"play","params":["nextmove","White","e2 e4"]}
- {"op":"kill"}
- {"op":"play","params":["setbookmode","random"]}
"""
GAMEID = 1
class Game(Checkmate):
def __init__(self, params):
Checkmate.__init__(self, params[0], params[1], params[2])
global GAMEID
self.id = GAMEID
self.lock = Lock()
self.cv = Condition(self.lock)
GAMEID += 1
self.activeplayers = 1
self.active = True
self.nextcolor = 'White'
if params[0] == 'multi':
self.capacity = 2
else:
self.capacity = 1
class Agent(Thread):
'''
Handles each clients requests by receiving and sending JSON objects.
'''
def __init__(self, conn, addr, checkmateserver):
'''
Take information needed to handle jobs.
@conn : socket object, connection to client
@addr : string, client's address
@checkmateserver : CheckmateServer object
'''
Thread.__init__(self)
self.conn = conn
self.addr = addr
self.checkmateserver = checkmateserver
self.game = None
self.color = 'White'
def run(self):
'''
receives commands as JSON objects, does the job and sends feedbacks to clients
'''
rawdata = self.conn.recv(4096)
self.checkmateserver.printlock.acquire()
print self.name, rawdata, 'received'
self.checkmateserver.printlock.release()
if not rawdata:
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
self.checkmateserver.printlock.acquire()
print self.name, rawdata, "not valid", "returning"
self.checkmateserver.printlock.release()
return
data = loads(rawdata.strip())
if data['op'] == 'start':
self.checkmateserver.printlock.acquire()
print self.name, "starting a new game with", data['params']
self.checkmateserver.printlock.release()
self.game = Game(data['params'])
self.checkmateserver.l.acquire()
self.checkmateserver.games[self.game.id] = self.game
self.checkmateserver.l.release()
self.game.cv.acquire()
gamemode = self.game.mode
board = self.game.board
self.game.setdepth(3)
self.game.cv.release()
if gamemode == 'multi':
self.color = data['color']
self.conn.send(dumps({'gameid': self.game.id, 'board': board}))
elif data['op'] == 'connect':
self.checkmateserver.printlock.acquire()
print self.name, "connecting to game with id=", data['gameid']
self.checkmateserver.printlock.release()
self.checkmateserver.l.acquire()
game = self.checkmateserver.games[int(data['gameid'])]
self.checkmateserver.l.release()
if game.capacity - game.activeplayers > 0:
self.checkmateserver.printlock.acquire()
print self.name, "connected to game with id=", data['gameid']
self.checkmateserver.printlock.release()
game.cv.acquire()
game.activeplayers += 1
gamemode = game.mode
self.game = game
board = self.game.board
game.cv.release()
if gamemode == 'multi':
self.color = data['color']
self.conn.send(dumps({'success': True, 'board': board}))
self.game.cv.acquire()
self.game.cv.notifyAll()
self.game.cv.release()
else:
self.checkmateserver.printlock.acquire()
print self.name, "cannot connect to game with id=", data['gameid']
self.checkmateserver.printlock.release()
self.conn.send(dumps({'success': False}))
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
return
else:
self.checkmateserver.printlock.acquire()
print self.name, data, "Wrong format"
self.checkmateserver.printlock.release()
self.conn.send(dumps({'message': 'Wrong format'}))
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
return
while True:
self.game.cv.acquire()
if self.game.mode == 'multi':
if self.game.activeplayers < 2:
while self.game.active and self.game.activeplayers < 2:
self.checkmateserver.printlock.acquire()
print self.name, "waiting for another player"
self.checkmateserver.printlock.release()
self.game.cv.wait()
else:
while self.game.active and self.game.nextcolor != self.color:
self.checkmateserver.printlock.acquire()
print self.name, "waiting for its turn"
self.checkmateserver.printlock.release()
self.game.cv.wait()
self.game.cv.release()
self.game.cv.acquire()
gameactive = self.game.active
self.game.cv.release()
if not gameactive:
self.checkmateserver.printlock.acquire()
print self.name, "Game is already killed. Returning..."
self.checkmateserver.printlock.release()
self.game.lock.acquire()
self.game.quit()
self.game.lock.release()
self.checkmateserver.l.acquire()
del self.checkmateserver.games[self.game.id]
self.checkmateserver.l.release()
self.conn.send(dumps({'message': 'Game is killed'}))
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
self.game.cv.acquire()
self.game.cv.notifyAll()
self.game.cv.release()
return
rawdata = self.conn.recv(4096)
self.checkmateserver.printlock.acquire()
print self.name, rawdata, 'received'
self.checkmateserver.printlock.release()
if not rawdata:
self.checkmateserver.printlock.acquire()
print self.name, rawdata, 'not valid', 'returning'
self.checkmateserver.printlock.release()
self.game.lock.acquire()
self.game.activeplayers -= 1
self.game.lock.release()
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
self.game.cv.acquire()
self.game.cv.notifyAll()
self.game.cv.release()
return
data = loads(rawdata.strip())
if data['op'] == 'exit':
self.checkmateserver.printlock.acquire()
print self.name, "Detaching the game"
self.checkmateserver.printlock.release()
self.game.lock.acquire()
self.game.activeplayers -= 1
self.game.lock.release()
self.conn.send(dumps({'message': 'You are detached'}))
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
self.game.cv.acquire()
self.game.cv.notifyAll()
self.game.cv.release()
return
elif data['op'] == 'kill':
self.checkmateserver.printlock.acquire()
print self.name, "Killing the game"
self.checkmateserver.printlock.release()
self.game.lock.acquire()
self.game.active = False
if self.game.activeplayers == 1:
self.game.quit()
self.checkmateserver.l.acquire()
del self.checkmateserver.games[self.game.id]
self.checkmateserver.l.release()
else:
self.game.activeplayers -= 1
self.game.lock.release()
self.conn.send(dumps({'success': True}))
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
self.game.cv.acquire()
self.game.cv.notifyAll()
self.game.cv.release()
return
elif data['op'] == 'play':
function = data['params'][0]
params = data['params'][1:]
self.checkmateserver.printlock.acquire()
print self.name, "Playing", function, "with", params
self.checkmateserver.printlock.release()
if function == 'nextmove':
self.game.cv.acquire()
success = self.game.nextmove(params[0], params[1])
board = self.game.getboard()
history = self.game.history()
isfinished = self.game.isfinished()
if success:
if self.game.mode == 'multi':
self.game.nextcolor = 'White' if self.game.nextcolor == 'Black' else 'Black'
self.game.cv.notifyAll()
self.game.cv.release()
if isfinished:
self.conn.send(dumps({'board': board, 'success': success, 'history': history,
'currentplayer': self.game.nextcolor, 'isfinished': isfinished}))
else:
self.conn.send(dumps({'board': board, 'success': success, 'history': history,
'currentplayer': self.game.nextcolor}))
elif function == 'save':
self.game.lock.acquire()
success = self.game.save(params[0])
self.game.lock.release()
self.conn.send(dumps({'success': success}))
elif function == 'load':
self.game.lock.acquire()
success = self.game.load(params[0])
board = self.game.getboard()
self.game.setdepth(3)
history = self.game.history()
self.game.lock.release()
self.conn.send(dumps({'success': success, 'board': board, 'history': history}))
elif function == 'hint':
self.game.lock.acquire()
hint = self.game.hint()
self.game.lock.release()
self.conn.send(dumps({'hint': hint}))
elif function == 'addbook':
self.game.lock.acquire()
success = self.game.addbook(params[0])
self.game.lock.release()
self.conn.send(dumps({'success': success}))
elif function == 'enablebook':
self.game.lock.acquire()
success = self.game.enablebook(params[0])
self.game.lock.release()
self.conn.send(dumps({'success': success}))
elif function == 'setbookmode':
self.game.lock.acquire()
success = self.game.setbookmode(params[0])
self.game.lock.release()
self.conn.send(dumps({'success': success}))
elif function == 'getboard':
self.game.lock.acquire()
board = self.game.getboard()
self.game.lock.release()
self.conn.send(dumps({'board': board}))
elif function == 'history':
self.game.lock.acquire()
history = self.game.history()
self.game.lock.release()
self.conn.send(dumps({'history': history}))
elif function == 'quit':
self.game.lock.acquire()
self.game.active = False
if self.game.activeplayers == 1:
self.game.quit()
self.checkmateserver.l.acquire()
del self.checkmateserver.games[self.game.id]
self.checkmateserver.l.release()
else:
self.game.activeplayers -= 1
self.game.lock.release()
self.conn.send(dumps({'success': True}))
self.conn.shutdown(SHUT_RDWR)
self.conn.close()
self.game.cv.acquire()
self.game.cv.notifyAll()
self.game.cv.release()
return
elif function == 'isfinished':
self.game.lock.acquire()
isfinished = self.game.isfinished()
self.game.lock.release()
self.conn.send(dumps({'isfinished': isfinished}))
elif function == 'getwinner':
self.game.lock.acquire()
winner = self.game.getwinner()
self.game.lock.release()
self.conn.send(dumps({'winner': winner}))
elif function == 'undo':
self.game.lock.acquire()
success = self.game.undo()
self.game.lock.release()
self.conn.send(dumps({'success': success}))
elif function == 'setdepth':
self.game.lock.acquire()
self.game.setdepth(int(params[0]))
self.game.lock.release()
self.conn.send(dumps({'success': True}))
elif function == 'getdepth':
self.game.lock.acquire()
depth = self.game.getdepth()
self.game.lock.release()
self.conn.send(dumps({'depth': depth}))
elif function == 'getbookmode':
self.game.lock.acquire()
bookmode = self.game.getbookmode()
self.game.lock.release()
self.conn.send(dumps({'bookmode': bookmode}))
elif function == 'newgame':
self.game.lock.acquire()
self.game.newgame()
board = self.game.getboard()
history = self.game.history()
self.game.setdepth(3)
self.game.lock.release()
self.conn.send(dumps({'board': board, 'history': history}))
elif function == 'changemode':
self.game.lock.acquire()
if self.game.mode == 'multi' and params[0] == 'single':
success = False
else:
success = self.game.changemode(params[0])
if success and params[0] == 'multi':
self.game.capacity = 2
self.game.lock.release()
self.conn.send(dumps({'success': success}))
elif function == 'getmode':
self.game.lock.acquire()
mode = self.game.getmode()
self.game.lock.release()
self.conn.send(dumps({'mode': mode}))
elif function == 'currentplayer':
self.game.lock.acquire()
currentplayer = self.game.currentplayer()
self.game.lock.release()
self.conn.send(dumps({'currentplayer': currentplayer}))
class CheckmateServer():
'''
CheckmateServer is a server for Checkmate application.
'''
def __init__(self, host, port):
'''
binds given host and port
@host : string
@param : int
'''
self.host = host
self.port = port
self.games = {}
self.l = Lock()
self.sock = None
self.agents = []
self.printlock = Lock()
def start(self):
'''
listens and starts a new agent for each connection.
'''
self.sock = socket(AF_INET, SOCK_STREAM)
self.sock.bind((self.host, self.port))
self.sock.listen(1)
while True:
conn, addr = self.sock.accept()
agent = Agent(conn, addr, self)
self.agents.append(agent)
agent.start()
if __name__ == '__main__':
checkmateserver = CheckmateServer(sys.argv[1], int(sys.argv[2]))
try:
checkmateserver.start()
except KeyboardInterrupt:
checkmateserver.sock.close()
print 'Server Shutdown'