-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (72 loc) · 2.83 KB
/
main.py
File metadata and controls
87 lines (72 loc) · 2.83 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
from flask import Flask, render_template, send_from_directory, session, request
from decouple import config
import random
import json
import uuid
import string
app = Flask(__name__)
app.config['SECRET_KEY'] = config('SECRET_KEY')
# rooms[questions{answers}] = {room_id: [{uuid_1: {name: 'my name', vote: 'A'}, uuid_2: {name: 'my other name', vote: 'G'}}]}
class Room():
rooms = {}
def __init__(self, name):
self.name = name.lower()
if self.name not in Room.rooms:
Room.rooms[self.name] = {
'current_question': 0,
'votes': [{}]
}
def set_current_question(self, current_question):
if current_question < 0:
return
if len(Room.rooms[self.name]['votes']) <= current_question:
for i in range(current_question + 1 - len(Room.rooms[self.name]['votes'])):
Room.rooms[self.name]['votes'].append({})
Room.rooms[self.name]['current_question'] = current_question
def get_current_question(self):
return Room.rooms[self.name]['current_question']
def set_vote(self, user_id, username, vote):
Room.rooms[self.name]['votes'][self.get_current_question()][user_id] = {'username': username, 'vote': vote}
def get_vote(self, user_id):
if user_id in Room.rooms[self.name]['votes'][self.get_current_question()]:
return Room.rooms[self.name]['votes'][self.get_current_question()][user_id].get('vote', '')
return ''
def admin_get_results(self):
return Room.rooms[self.name]
# General routes
@app.route('/res/<path:path>')
def resource_serve(path):
return send_from_directory('./res', path)
# User routes
@app.route('/', defaults={'room': ''})
@app.route('/r/<room>')
def index(room):
room = room.lower()
return render_template('index.html', room=room)
def init():
if 'uuid' not in session:
session['uuid'] = uuid.uuid4().hex
@app.route('/ws/vote/<room>', methods=['POST', 'GET'])
def ws_set_vote(room):
init()
r = Room(room.lower())
if request.method == 'POST':
r.set_vote(session['uuid'], request.form.get('name', 'no name'), request.form.get('vote', ''))
return json.dumps({'vote': r.get_vote(session['uuid']), 'question': r.get_current_question()})
# Admin routes
@app.route('/admin', defaults={'room': ''})
@app.route('/admin/', defaults={'room': ''})
@app.route('/admin/r/<room>')
def admin(room):
room = room.lower()
return render_template('admin.html', room=room)
@app.route('/ws/admin/r/<room>')
def ws_admin(room):
r = Room(room.lower())
return json.dumps(r.admin_get_results())
@app.route('/ws/admin/set_q/<room>', methods=['POST', 'GET'])
def ws_set_question(room):
if request.method == 'POST':
r = Room(room.lower())
r.set_current_question(int(request.form.get('index', '')))
return 'ok'