-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrollers.py
95 lines (74 loc) · 2.43 KB
/
controllers.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
from aiohttp import web, WSMsgType
import aiohttp_jinja2
from settings import config
import map_generation
from teach import load_model, predict
@aiohttp_jinja2.template('index.html')
async def index(request):
return {}
async def store_sensors_data(request):
print('Storing sensors data')
collection = request.app['db'][config.COLLECTION_NAME]
request_data = await request.json()
meta = {
'gameId': request_data['gameId'],
'playerName': request_data['playerName']
}
for data in request_data['dumps']:
await collection.insert_one({
'meta': meta,
'data': data
})
print('Sensors data stored')
return web.Response(status=204, headers={
'Access-Control-Allow-Origin': '*'
})
async def generate_map(request):
seed = request.query.get('seed')
length = request.query.get('length')
rows = int(request.query.get('rows', 30))
map_data = map_generation.generate_map(seed, length, rows)
map_generation.preetify_map(map_data)
map_generation.add_finish_line(map_data)
content = '\n'.join(
','.join(str(cell) for cell in row)
for row in map_data
)
return web.Response(
text=content, content_type='text/csv',
headers={'Access-Control-Allow-Origin': '*'}
)
async def preflight(request):
return web.Response(
status=204, headers={
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type, Content-Length',
'Content-Type': ''
}
)
async def websocket_handler(request):
print('Websocket connection started')
ws = web.WebSocketResponse()
await ws.prepare(request)
print('Loading model')
model = await load_model()
print('Model loaded')
# Notify frontend that the model is loaded and we are ready for action!
await ws.send_json({
'ready': True
})
print('Waiting for prediction queries')
async for msg in ws:
if msg.type == WSMsgType.TEXT:
data = msg.json()
prediction = await predict(model, **data)
up, left, right = map(bool, prediction)
await ws.send_json({
'u': up,
'l': left,
'r': right
})
elif msg.type == WSMsgType.ERROR:
print(f"ws connection closed with exception {ws.exception()}")
print('Websocket connection closed')
return ws