-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrealtimechess.py
447 lines (365 loc) · 12.8 KB
/
realtimechess.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
#!/usr/bin/python3
import asyncio
import base64
import cProfile
import datetime
import io
import json
import logging
import os
import pstats
import signal
import sys
import urllib.parse
import aiohttp.web
from jinja2 import Template
import auth
import constants
import game_storage
import util
HTTP_PORT = 8080
index_template = Template(
open(os.path.join(os.path.dirname(__file__), 'index.html')).read())
login_template = Template(
open(os.path.join(os.path.dirname(__file__), 'login.html')).read())
error_template = Template(
open(os.path.join(os.path.dirname(__file__), 'error.html')).read())
# The HTML files are static for the duration of the server, so
# keep the version static while the server is running for the
# files in the game/ folder as well. The version is needed to
# keep the files out of the browser cache.
GAME_FILE_VERSION = base64.urlsafe_b64encode(
str(datetime.datetime.now()).encode("ascii")).decode('ascii')
def game_file_url(filename):
return "/game/{}?v={}".format(filename, GAME_FILE_VERSION)
def user_and_game(request):
user = request.app["user_manager"].get_current_user(request)
game_key = request.query.get('g')
game = request.app["game_manager"].get(game_key)
if not user or not game:
raise aiohttp.web.HTTPNotFound(text="No such game.")
return user, game
def error_response(status, message):
html = error_template.render({
"status": status,
"message": message,
"game_css": game_file_url("game.css"),
})
return aiohttp.web.Response(
status=status, text=html, content_type="text/html")
async def anonymous_login_handler(request):
try:
return await auth.anonymous_login_handler(request)
except aiohttp.web.HTTPError as ex:
return error_response(ex.status, ex.text)
@auth.authenticated
async def getplayer_page(request):
user = request.app["user_manager"].get_current_user(request)
return aiohttp.web.Response(
text=json.dumps({
"rating": user.rating,
"wins": user.wins,
"losses": user.losses
}))
async def login_page(request):
game_key = request.query.get('g')
if game_key is not None:
destination = "/?g=" + str(game_key)
else:
destination = "/"
template_values = {
'destination': destination,
'login_link': "",
'top_players': "",
'game_css': game_file_url("game.css"),
'constants_js': game_file_url("constants.js")
}
return aiohttp.web.Response(
text=login_template.render(**template_values),
content_type="text/html")
async def main_page(request):
"""Renders the main page. When this page is shown, we create a new
channel to push asynchronous updates to the client."""
user_manager = request.app["user_manager"]
game_manager = request.app["game_manager"]
user = user_manager.get_current_user(request)
game_key = request.query.get('g')
original_game_key = game_key
logging.info("Main page: %s %s", user, game_key)
if not user:
if game_key is not None:
raise aiohttp.web.HTTPFound("/loginpage?g=" + str(game_key))
else:
raise aiohttp.web.HTTPFound("/loginpage")
recent_games = game_manager.get_recent(user, game_key)
if not game_key:
game, game_key = game_manager.new(user)
logging.info("New game from %s." + str(user))
else:
game = game_manager.get(game_key)
if not game:
return error_response(404, "Game not found")
logging.info("Game userX: %s.", game.userX)
logging.info("User : %s.", user.id)
if game.userX.id == user.id:
# Same player tried to join.
pass
elif not game.userO:
# Current user joins this game as the second player.
game.userO = user
game.userO.id = user.id
logging.info("User %s joins the game.", user)
elif (user.id != game.userO.id and user.id != game.userX.id):
logging.info("Observer %s joined %s.", user, game.key)
game_link = '/?g=' + game_key
if not original_game_key:
raise aiohttp.web.HTTPFound(game_link)
joinable_games_html = recent_games.joinable_html(game_key)
if len(joinable_games_html) > 0:
joinable_games_html = "Or join another available game below:<br />" + joinable_games_html
observable_games_html = recent_games.observable_html(game_key)
if len(observable_games_html) > 0:
observable_games_html = "<p />Observe an existing game:<br />" + observable_games_html
returnable_games_html = recent_games.returnable_html(game_key)
if len(returnable_games_html) > 0:
returnable_games_html = "<p />Return to your existing game:<br />" + returnable_games_html
recent_games = (returnable_games_html + "\n" + joinable_games_html + "\n" +
observable_games_html)
template_values = {
'me': user,
'game_key': game_key,
'game_link': game_link,
'initial_message': game.get_game_message(),
'recent_games': recent_games,
'rating': user.rating,
'top_players': user_manager.top_players_html(),
'wins': user.wins,
'losses': user.losses,
'game_css': game_file_url("game.css"),
'game_js': game_file_url("game.js"),
'constants_js': game_file_url("constants.js"),
}
return aiohttp.web.Response(
text=index_template.render(**template_values),
content_type="text/html")
@auth.authenticated
async def error_handler(request):
user, game = user_and_game(request)
data = await request.post()
logging.error("JavaScript error: %s %s %s.", user, request.query, data)
return aiohttp.web.Response(text="OK")
@auth.authenticated
async def getstate_handler(request):
user, game = user_and_game(request)
json_data = game.get_game_message()
return aiohttp.web.Response(text=json_data)
@auth.authenticated
async def move_handler(request):
user, game = user_and_game(request)
from_id = request.query.get('from')
to_id = request.query.get('to')
if from_id and to_id:
if game.move(user, from_id, to_id):
await game.send_update()
else:
raise aiohttp.web.HTTPBadRequest(text="Need from and to IDs.")
return aiohttp.web.Response(text="OK")
async def move_websocket_handler(user, game, query):
from_id = query.get('from')
to_id = query.get('to')
if from_id and to_id:
res = False
try:
res = game.move(user, from_id[0], to_id[0])
except util.HttpCodeException as ex:
# We can not return a code because we need the socket
# to stay open.
logging.warning("Move error: %s %s.", ex.status, ex.text)
pass
# TODO: Always send an update, since that makes testing for
# invalid moves via websocket easier.
if res:
await game.send_update()
@auth.authenticated
async def newgame_handler(request):
game_manager = request.app["game_manager"]
user, game = user_and_game(request)
if game.state != constants.STATE_GAMEOVER:
raise aiohttp.web.HTTPForbidden(text="Game is not finished.")
if game.userX == user or game.userO == user:
# Create a new game.
oldgame = game
game, _ = game_manager.new(user, game.key)
# Set properties.
game.userX = oldgame.userX
game.userO = oldgame.userO
game.observers = oldgame.observers
await game.send_update()
else:
raise aiohttp.web.HTTPForbidden(
text="Modifying this game not allowed.")
return aiohttp.web.Response(text="OK")
@auth.authenticated
async def opened_handler(request):
user, game = user_and_game(request)
logging.info("Opened: %s %s.", user, game.key)
await game.send_update()
return aiohttp.web.Response(text="OK")
@auth.authenticated
async def ping_handler(request):
user, game = user_and_game(request)
user_manager = request.app["user_manager"]
await ping_websocket_handler(user_manager, user, game)
return aiohttp.web.Response(text="OK")
async def ping_websocket_handler(user_manager, user, game):
logging.info("Ping: %s %s", user, game.key)
await game.send_update()
if game.state == constants.STATE_GAMEOVER and not game.results_are_written:
if game.winner == constants.WHITE:
user_manager.change_ratings(game.userX, game.userO)
else:
user_manager.change_ratings(game.userO, game.userX)
logging.info("White player after update %s.", game.userX)
logging.info("Black player after update %s.", game.userO)
game.results_are_written = True
game.put()
@auth.authenticated
async def randomize_handler(request):
user, game = user_and_game(request)
await request.post()
if user == game.userX or user == game.userO:
game.randomize()
await game.send_update()
return aiohttp.web.Response(text="OK")
@auth.authenticated
async def ready_handler(request):
user, game = user_and_game(request)
await request.post()
ready = request.query.get("ready")
if ready is None:
return aiohttp.web.Response(text="OK")
logging.info("User %s ready: %s.", user, ready)
await game.set_ready(user.id, ready)
return aiohttp.web.Response(text="OK")
async def websocket_handler(request):
# Anyone can listen to updates for a game.
key = request.query.get('g')
game_manager = request.app["game_manager"]
game = game_manager.get(key)
if not game:
raise aiohttp.web.HTTPNotFound(text="Game not found.")
user_manager = request.app["user_manager"]
user = user_manager.get_current_user(request)
logging.info('Websocket connection starting')
ws = aiohttp.web.WebSocketResponse()
await ws.prepare(request)
logging.info('Websocket connection ready')
game.observers.append(ws)
async for msg in ws:
logging.info("Received %s over websocket.", msg)
if msg.type == aiohttp.WSMsgType.TEXT:
# Update the game to resolve the moving pieces and
# in case it has been recreated.
game = game_manager.get(key)
url = urllib.parse.urlparse(msg.data)
query = urllib.parse.parse_qs(url.query)
if user and url.path == '/move':
await move_websocket_handler(user, game, query)
elif url.path == '/ping':
await ping_websocket_handler(user_manager, user, game)
else:
logging.error("Invalid Websocket command: %s %s %s.", user,
url, query)
logging.info('Websocket connection closed')
return ws
@auth.debug_authenticated
async def setdebug_handler(request):
user, game = user_and_game(request)
data = await request.post()
debug = data.get("debug")
if debug is None or debug == "" or int(debug) == 1:
game.debug_no_time = True
logging.info("Debug mode on for game %s.", game.key)
else:
game.debug_no_time = False
logging.info("Debug mode off for game %s.", game.key)
return aiohttp.web.Response(text="OK")
def make_app(is_debug):
app = aiohttp.web.Application(debug=is_debug)
app.router.add_get('/', main_page)
app.router.add_get('/getplayer', getplayer_page)
app.router.add_get('/loginpage', login_page)
app.router.add_static('/game',
os.path.join(os.path.dirname(__file__), "game"))
app.router.add_post('/anonymous_login', anonymous_login_handler)
app.router.add_post('/error', error_handler)
app.router.add_post('/getstate', getstate_handler)
app.router.add_post('/move', move_handler)
app.router.add_post('/newgame', newgame_handler)
app.router.add_post('/opened', opened_handler)
app.router.add_post('/ping', ping_handler)
app.router.add_post('/randomize', randomize_handler)
app.router.add_post('/ready', ready_handler)
app.router.add_route('GET', '/websocket', websocket_handler)
app["user_manager"] = auth.UserManager(unsafe_debug=is_debug)
app["game_manager"] = game_storage.GameManager()
if is_debug:
app.router.add_post('/setdebug', setdebug_handler)
return app
def setup_loop(loop, is_debug=False): # pragma: no cover
if is_debug:
loop.set_debug(is_debug)
app = make_app(is_debug)
handler = app.make_handler(access_log=logging.getLogger())
web_server = loop.run_until_complete(
loop.create_server(handler, '0.0.0.0', HTTP_PORT))
def every_second():
"""Useful for catching Ctrl+C on Windows."""
loop.call_later(1.0, every_second)
loop.call_soon(every_second)
def stop():
web_server.close()
loop.run_until_complete(web_server.wait_closed())
loop.run_until_complete(app.shutdown())
loop.run_until_complete(handler.shutdown(1.0))
loop.run_until_complete(app.cleanup())
return stop
if __name__ == '__main__': # pragma: no cover
use_profiling = False
if len(sys.argv) < 2 or (sys.argv[1] != "run" and sys.argv[1] != "debug"):
print("Specify", sys.argv[0], " run/debug to run.")
sys.exit(0)
logging.basicConfig(
format=
'[%(levelname)-8s] %(asctime)s %(name)s %(filename)s:%(lineno)d: %(message)s',
level=logging.WARNING,
datefmt='%Y-%m-%d %H:%M:%S')
is_debug = False
if sys.argv[1] == "debug":
is_debug = True
logging.getLogger().setLevel(logging.DEBUG)
loop = asyncio.get_event_loop()
stop = setup_loop(loop, is_debug)
if os.name != "nt":
loop.add_signal_handler(signal.SIGTERM, loop.stop)
logging.info("Server started.")
print("Server started.")
if use_profiling:
pr = cProfile.Profile()
pr.enable()
try:
loop.run_forever()
except KeyboardInterrupt:
print("KeyboardInterrupt.")
pass
if use_profiling:
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
# This file can be visualized nicely with
# $ gprof2dot -f pstats cProfile.log | dot -Tpdf > cProfile.pdf
ps.dump_stats('cProfile.log')
stop()
loop.close()
logging.info("Server closed.")