forked from HenriqueToledo/broken-speech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (108 loc) · 3.49 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
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
import os, random, re, base64, io, json
from flask import Flask, session, render_template, request, redirect
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
app = Flask(__name__)
app.DEBUG = True
app.config['SECRET_KEY'] = 'notveryseCretKey'
def read_json():
with open('rooms.json', 'r') as fp:
return json.load(fp)
def write_json(d):
with open('rooms.json', 'w') as fp:
json.dump(d, fp)
def all_sent(room_id):
a = True
rooms = read_json()
for p in rooms[room_id]['players']:
if rooms[room_id]['players'][p]['uploaded'] == False:
a = False
break
return a
def get_uindex(room_id, uid):
rooms = read_json()
for p in rooms[room_id]['players']:
if rooms[room_id]['players'][p]['uid'] == uid:
return p
@app.route('/')
def home():
return render_template('home.html')
@app.route('/create')
def create():
rooms = read_json()
room_id = str(random.randint(100000,999999))
session['uid'] = str(os.urandom(32))
session['name'] = request.args.get('name')
session['room_id'] = room_id
rooms[room_id] = {
'started': 0,
'players': {0: {
'name': request.args.get('name'),
'uid': session['uid'],
'uploaded': False,
}}
}
write_json(rooms)
return redirect('/control_panel')
@app.route('/control_panel')
def control_panel():
rooms = read_json()
return render_template('admin_panel.html', users=rooms[session['room_id']]['players'], room_id=session['room_id'])
@app.route('/player_panel')
def player_panel():
rooms = read_json()
room_id = session['room_id']
if rooms[room_id]['started'] == 0:
return render_template('player_panel.html', users=rooms[room_id]['players'], room_id=room_id)
elif rooms[room_id]['started'] == 1:
return render_template('drawing.html')
@app.route('/join')
def join():
rooms = read_json()
try:
room_id = request.args.get('roomID')
except:
return redirect('/')
session['uid'] = str(os.urandom(32))
session['name'] = request.args.get('name')
session['room_id'] = room_id
if session['room_id'] not in rooms:
return redirect('/')
rooms[room_id]['players'][len(rooms[room_id]['players'])] = {
'name': request.args.get('name'),
'uid': session['uid'],
'uploaded': False,
}
write_json(rooms)
return redirect('/player_panel')
@app.route('/start')
def start():
rooms = read_json()
rooms[session['room_id']]['started'] = 1
write_json(rooms)
return redirect('/player_panel')
@app.route('/upload', methods=["POST"])
def upload():
rooms = read_json()
room_id = session['room_id']
uid = session['uid']
data = request.form
for d in data:
received = d
received = received.split(',')[1] + '='
missing_pad = len(received)%4
if missing_pad:
received += ('=' * (4-missing_pad))
received = received.replace(' ', '+')
img = Image.open(io.BytesIO(base64.b64decode(received)))
img.save(f"static/images/{session['room_id']}-{session['name']}.png", "PNG")
rooms[room_id]['players'][get_uindex(room_id, uid)]['uploaded'] = True
write_json(rooms)
return redirect('/results')
@app.route('/results')
def results():
rooms = read_json()
if(all_sent(session['room_id'])):
return render_template('show_results.html', room=rooms[session['room_id']], room_id=session['room_id'])
return render_template('waiting.html')