-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
67 lines (53 loc) · 1.66 KB
/
main.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
import jsonpickle
from aiohttp import web
import socketio
import database
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
@sio.on('connect')
def connect(sid, environ):
print("joined")
sio.enter_room(sid, "scout")
@sio.on('submit_match')
async def submit_match(sid, data):
database.submit_match(data)
await sio.emit('submit_match', data, room="scout", skip_sid=sid)
@sio.on('submit_team')
async def submit_team(sid, data):
database.submit_team(data)
await sio.emit('submit_team', data, room="scout", skip_sid=sid)
@sio.on('request_update')
async def request_update(sid, data):
update = database.request_update(data)
for doc in update:
event = ""
if isinstance(doc, database.Teams):
event = "submit_team"
elif isinstance(doc, database.Matches):
event = "submit_match"
dic = doc.to_mongo().to_dict()
dic.pop('_id')
raw = jsonpickle.encode(dic, unpicklable=False)
await sio.emit(event, raw, room=sid)
@sio.on('request_rankings')
async def request_rankings(sid, data):
rankings = database.request_rankings()
arr = [t.to_mongo().to_dict() for t in rankings]
for t in arr:
t.pop('_id')
raw = jsonpickle.encode(arr, unpicklable=False)
return raw
@sio.on('request_analytics')
async def request_analytics(sid, data):
analytics = database.request_analytics(data)
dic = analytics.to_mongo().to_dict()
dic.pop('_id')
raw = jsonpickle.encode(dic, unpicklable=False)
return raw
@sio.on('disconnect')
def disconnect(sid):
print("left")
sio.leave_room(sid, "scout")
if __name__ == '__main__':
web.run_app(app)