-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (62 loc) · 2.18 KB
/
app.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
import asyncio
import json
import os
from jinja2 import Environment, PackageLoader
from sanic import Sanic
from sanic.response import file, redirect, html
from bonusly import Bonusly
from db import DB
from utils import get_session_id, get_session_remaining_time
db = {}
app = Sanic(__name__)
env = Environment(loader=PackageLoader('app', 'static'))
STATIC_FOLDER = os.path.join(os.path.dirname(__file__), 'static')
app.static('/static', STATIC_FOLDER)
@app.route('/')
async def index(request):
return await file('static/templates/index.html')
@app.route('/create-session', methods=['POST'])
async def create_session(request):
session_id = get_session_id()
DB().create_session(session_id)
return redirect(f'/join/{session_id}')
@app.route('/join/<session_id>', methods=['GET', 'POST'])
async def join_room(request, session_id):
template = env.get_template('templates/create-session.html')
if request.method != 'POST':
return html(template.render())
token = request.form.get('token')
me = Bonusly(token).me()
if not me['success']:
html_content = template.render(error=True)
return html(html_content)
else:
username = me['result']['username']
DB().add_player(session_id, username)
return redirect(f'/room/{session_id}')
@app.route('/room/<session_id>', methods=['GET'])
async def room(request, session_id):
template = env.get_template('templates/spin.html')
session = DB().get_session(session_id)
if not session:
return redirect('/')
players = DB().get_players_by_session_id(session_id)
return html(
template.render(
players=players, session_id=session_id
)
)
@app.websocket('/spin/<session_id>')
async def feed(request, ws, session_id):
while True:
players = DB().get_players_by_session_id(session_id)
session = DB().get_session(session_id)
_, created = session
data = {
'players': players,
'remaining_time': get_session_remaining_time(created)
}
await ws.send(json.dumps(data))
await asyncio.sleep(1)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=4000, debug=True)