-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-api.py
512 lines (426 loc) · 16.4 KB
/
rest-api.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
from flask import Flask, render_template, redirect, url_for, request, jsonify, abort
import requests, winsound as ws, threading, service as srv, match, time, hue
from vlc import MediaPlayer
app = Flask(__name__)
m = match.Match()
previous_chal = {'id': -1}
music_player = MediaPlayer("static/music trivia/payday.mp3")
music_on = False
ready = 0
fb = {"times": 0, "rate": 0}
danceRank = list()
DANCE_POINTS = [50, 100, 250, 400, 600, 800, 1000, 1500]
RIGHT_ANSWER = 100
VOICE_HZ = {5: 1000, 50: 500, 100: 400, 150: 300, 250: 100, 350: 20}
FITNESS_CHAL_MULTIPLIER = 5
FITNESS_PLAYER = MediaPlayer("static/music trivia/eye of the tiger.mp3")
DANCE_PLAYER = MediaPlayer("static/music trivia/pump it.mp3")
MUSIC_DANCE = False
GAME_OVER_PLAYER = MediaPlayer("static/sound effects/Vittoria!.mp3")
@app.route('/')
def lobby():
return redirect(url_for('showPlayers'))
@app.route('/categories')
def categories():
r = requests.get(m.ONLINE_SERVER + "/categories")
r = r.json()
return render_template('categories.html', res=r)
@app.route('/viewChallenge/<challenge>')
def viewChallenge(challenge):
global previous_chal
result = requests.get(m.ONLINE_SERVER + "/getChallenge/" + challenge)
json = result.json()
trivia = int(json['trivia'])
if trivia == 1:
info = requests.post(m.ONLINE_SERVER + "/getQuiz/" + challenge, json={'list': m.quiz}).json()
info_list = [info['answer'], info['wrong1'], info['wrong2'], info['wrong3']]
size = len(info_list)
info_list = srv.randomize(info_list)
answer = ""
for i in range(0, size):
if info['answer'] == info_list[i]:
answer = i
m.updateTrivia({'chal_id': info['chal_id'], 'q_id': info['q_id'], 'answer': answer})
resource = info['resource']
info = [info['chal_id'], info['q_id'], info['question']] + info_list + [resource.replace(' ', '_')]
else:
m.updateTrivia(None)
info = ""
size = -1
if previous_chal['id'] != m.current_chal['id']:
previous_chal = dict(m.current_chal)
m.sendNotifications()
return render_template('challenges.html', res=json, info=info, size_info=size, players=m.player_turn, active=m.current_chal['active'],
p_number=len(m.player_turn))
@app.route('/players')
def showPlayers():
return render_template('lobby.html', players=m.getPlayersName(), admin=m.admin, n=len(m.players))
@app.route('/scores')
def scores():
return render_template('feedback.html', players=m.getPlayersScore(), n=len(m.players))
@app.route('/updateScores')
def updateScores():
m.updateScores()
return jsonify({'result': "SUCCESS"})
@app.route('/updateBestScore', methods=['POST'])
def updateBestScore():
json = request.json
if (json is not None) and ('username' in json) and ('chal_id' in json) and ('score' in json):
res = requests.post(m.ONLINE_SERVER + "/updateBestScore", json=json)
return jsonify({'result': res.text})
return jsonify({'result': "ERROR"})
@app.route('/endgame')
def endgame():
# SORT PLAYER BY SCORE
stopMusic()
GAME_OVER_PLAYER.play()
playerbyscore = m.sortedPlayerByScore()
players = list()
scores = dict()
for p in playerbyscore:
players.append(p.getName())
scores[p.getName()] = p.getScore()
return render_template('endgame.html', players=players, scores=scores)
@app.route('/playMusic')
def playMusic():
global music_on
if not music_on:
music_player.play()
music_on = True
return jsonify({"result": "SUCCESS"})
@app.route('/stopMusic')
def stopMusic():
global music_on
if music_on:
music_player.pause()
music_on = False
return jsonify({"result": "SUCCESS"})
@app.route('/playDance')
def playDance():
m.sendNotifications(title="play")
global MUSIC_DANCE
if not MUSIC_DANCE:
DANCE_PLAYER.play()
MUSIC_DANCE = True
return jsonify({"result": "SUCCESS"})
@app.route('/stopDance')
def stopDance():
m.sendNotifications(title="stop")
global MUSIC_DANCE
if MUSIC_DANCE:
DANCE_PLAYER.pause()
MUSIC_DANCE = False
return jsonify({"result": "SUCCESS"})
# MOBILE
@app.route('/categoriesM')
def categoriesM():
result = requests.get(m.ONLINE_SERVER + "/categories")
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/categories")).start()
m.setActiveMatch(True)
tmp = result.json()
tmp['result'] = 1
return jsonify(tmp)
# MOBILE
@app.route('/getChallenge/<category>')
def getChallenge(category):
global ready
r = requests.post(m.ONLINE_SERVER + "/getChallenge/" + category, json={'list': m.played_chal})
if r.text == "-1":
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/endgame")).start()
m.sendNotifications(title="gameover")
return jsonify({'result': -1})
json = r.json()
lastChal = m.getCurrentIdChal()
if (json['id'] == 1) or (json['id'] == 3):
print(m.playerTurn(len(m.players)))
print("getChallenge: fitnessChallenge")
elif (lastChal != 2) and (lastChal !=4):
print(m.playerTurn(1))
print("getChallenge")
m.updateChallenge(json)
m.setActive(m.current_chal, False)
ready = 0
m.setCategory(json['type'])
chal_id = str(json['id'])
m.played_chal.append(int(chal_id))
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/viewChallenge/" + chal_id)).start()
json['result'] = 1
return jsonify(json)
# MOBILE
@app.route('/currentChallenge')
def currentChallenge():
headers = request.headers
tmp = dict(m.current_chal)
if m.active and m.containsPlayer(headers['authorization']):
tmp['result'] = 1
return jsonify(tmp)
tmp['result'] = -1
return jsonify(tmp)
# MOBILE
@app.route('/join', methods=['POST'])
def joinMatch():
json = request.json
if(json is None) or ('username' not in json):
return jsonify({"result": "ERROR USER"})
# Check if the user already joined
if m.containsPlayer(json['username']):
if m.admin == json['username']:
m.sendNotifications([json['username']])
return jsonify({"result": "SUCCESS", "admin": True, "active": m.active})
else:
m.sendNotifications([json['username']])
return jsonify({"result": "SUCCESS", "admin": False, "active": m.active})
# Check if he's the first one
if len(m.players) == 0:
m.newPlayer(json['username'], json['token'])
m.setAdmin(json['username'])
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/players")).start()
return jsonify({"result": "SUCCESS", "admin": True, "active": m.active})
# Check if players filled the match
elif len(m.players) < m.MAX_PLAYERS:
m.newPlayer(json['username'], json['token'])
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/players")).start()
return jsonify({"result": "SUCCESS", "admin": False, "active": m.active})
else:
return jsonify({"result": "LIMITE GIOCATORI RAGGIUNTO", "active": m.active})
# MOBILE
@app.route('/answer/<int:chal_id>/<answer>')
def chooseAnswer(chal_id, answer):
global ready
correct = 'static\sound effects\Correct Answer.wav'
wrong = 'static\sound effects\Wrong Answer.wav'
if chal_id != m.current_trivia['chal_id']:
abort(409)
user = request.headers['authorization']
current_player = m.getPlayer(user)
# Calcolo indice risposta attraverso i codici ascii
a = ord(answer.lower()) - ord("a")
for p in m.player_turn:
if user == p:
if m.current_trivia['answer'] == a:
# If the player answer correctly HOMMY provides a new Quiz
ws.PlaySound(correct, ws.SND_FILENAME | ws.SND_ASYNC)
#hue.right()
threading.Thread(target=hue.right).start()
time.sleep(2)
threading.Thread(target=hue.base).start()
current_player.addPoints(RIGHT_ANSWER, 4)
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/viewChallenge/" + str(chal_id))).start()
return jsonify({'result': "CORRECT"})
else:
# If the player answer wrongly HOMMY provides a new Quiz and change player turn
current_player.resetCorrectAnswer()
print(user + ": " + str(current_player.getScore()))
ws.PlaySound(wrong, ws.SND_FILENAME | ws.SND_ASYNC)
hue.wrong()
time.sleep(2)
hue.base()
if m.playerTurn(1) == -1:
print(-1)
print("chooseAnswer")
# Manda notifica per far fare il refresh challenge
# getChallenge(m.getCategory())
m.sendNotifications(title="feedback")
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/scores")).start()
else:
print(1)
print("chooseAnswer")
ready = 0
m.setActive(m.current_chal, False)
threading.Thread(target=srv.openWebPage,args=(m.driver, m.THIS_SERVER + "/viewChallenge/" + str(chal_id))).start()
return jsonify({'result': "WRONG"})
return jsonify({'result': "NOT AUTHORIZED"})
# MOBILE
@app.route('/login', methods=['POST'])
def login():
info = request.json
result = requests.post(m.ONLINE_SERVER + "/login", json=info)
if result.text != "WRONG" and result.text != "ERROR JSON":
tmp = result.json()
tmp['result'] = 1
return jsonify(tmp)
return jsonify({"result": -1})
# MOBILE
@app.route('/signin', methods=['POST'])
def signin():
info = request.json
res = requests.post(m.ONLINE_SERVER + "/signin", json=info)
return jsonify({"result": res.text})
# MOBILE
@app.route('/startchallenge/<int:id>')
def startChallenge(id):
global ready
user = request.headers['authorization']
found = False
for p in m.player_turn:
if user == p:
found = True
ready = ready + 1
# Check if the challenge has already began
if (not m.isActive(m.current_chal)) and (ready == len(m.player_turn)):
m.setActive(m.current_chal, True)
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/viewChallenge/" + str(id))).start()
if (id == 1):
hue.fitness()
elif (id == 2):
hue.voice()
elif (id == 3):
hue.dance()
elif (id == 4):
hue.base()
stopMusic()
return jsonify({'result': 1})
if found is False:
return jsonify({'result': -1})
stopMusic()
return jsonify({'result': 2})
# MOBILE
@app.route('/do/<int:challenge>')
def do(challenge):
user = request.headers['authorization']
found = False
for p in m.player_turn:
if user == p:
found = True
if found is False:
return jsonify({"result": "NOT AUTHORIZED"})
if int(challenge) == 2:
freq = m.getVoiceHzResult(user)
if freq == -1:
freq = srv.randomFrequency()
m.voiceHzResult(user,freq)
threading.Thread(target=ws.Beep, args=(freq, 5000)).start()
elif int(challenge) == 3:
ws.PlaySound('static\sound effects\Wrong Answer.wav', ws.SND_FILENAME | ws.SND_ASYNC)
return jsonify({"result": "SUCCESS"})
# MOBILE
@app.route('/challengeResult', methods=['POST'])
def challengeResult():
global ready
res = request.json
user = request.headers['authorization']
found = False
for p in m.player_turn:
if user == p:
found = True
if user == "dancestop":
found = True
if found is False:
return jsonify({"result": "NOT AUTHORIZED"})
id = res['id']
# FITNESS CHALLENGE
if id == 1:
for key in res:
if key != 'id':
# Calcolo punteggio per tempi
current_player = m.getPlayer(key)
print(key+": " + str(res[key]))
if res[key] == 0:
current_player.addPoints(600)
else:
current_player.addPoints(int(res[key] * FITNESS_CHAL_MULTIPLIER))
print(current_player.getScore())
time.sleep(2)
# getChallenge(m.getCategory())
m.sendNotifications(title="feedback")
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/scores")).start()
# VOICE HZ DETECTOR
elif id == 2:
current_player = m.getPlayer(user)
given_freq = m.getVoiceHzResult(user)
recorded_freq = res['frequency']
diff = abs(recorded_freq-given_freq)
# Calculate the error between the two frequency and assign a score to the performance, and set a winner
points = list(VOICE_HZ)
points.sort()
for i in points:
if diff < i:
print(VOICE_HZ[i])
current_player.addPoints(VOICE_HZ[i])
break
print(user + ": " + str(current_player.getScore()))
if m.playerTurn(1) == -1:
print(-1)
print("challengeResult: VoiceHz")
# Manda notifica per far fare il refresh challenge
# getChallenge(m.getCategory())
m.sendNotifications(title="feedback")
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/scores")).start()
else:
print(1)
print("challengeResult: VoiceHz")
m.setActive(m.current_chal, False)
ready = 0
threading.Thread(target=srv.openWebPage,args=(m.driver, m.THIS_SERVER + "/viewChallenge/" + str(id))).start()
playMusic()
# DANCE AND STOP
elif id == 3:
global danceRank, MUSIC_DANCE
if user != "dancestop":
danceRank.append(user)
ws.PlaySound('static\sound effects\Wrong Answer.wav', ws.SND_FILENAME | ws.SND_ASYNC)
threading.Thread(target=srv.transition, args=(MUSIC_DANCE,)).start()
if (len(danceRank) >= len(m.player_turn)-1) or user == "dancestop":
stopDance()
i = 0
for p in danceRank:
current_player = m.getPlayer(p)
current_player.addPoints(DANCE_POINTS[i])
i = i+1
index = i-1 + (len(m.player_turn) - len(danceRank))
for p in m.player_turn:
if p not in danceRank:
m.getPlayer(p).addPoints(DANCE_POINTS[index])
m.sendNotifications(title="dancestop")
time.sleep(1)
m.sendNotifications(title="feedback")
threading.Thread(target=srv.openWebPage, args=(m.driver, m.THIS_SERVER + "/scores")).start()
# MUSIC TRIVIA
elif id == 4:
pass
else:
return jsonify({"result": "INVALID CHALLENGE"})
return jsonify({"result": "SUCCESS"})
# MOBILE
@app.route('/feedback/<int:chal_id>', methods=['POST'])
def feedback(chal_id):
global fb
user = request.headers['authorization']
found = False
for p in m.players:
if user == p:
found = True
break
if found is False:
return jsonify({"result": "NOT AUTHORIZED"})
json = request.json
fb['times'] = fb['times'] + 1
fb['rate'] = fb['rate'] + int(json['rate'])
if fb['times'] == len(m.players):
res = requests.post(m.ONLINE_SERVER + "/feedback/" + str(chal_id), json=fb)
fb['times'] = 0
fb['rate'] = 0
getChallenge(m.getCategory())
return jsonify({'result': res.text})
return jsonify({"result": "SUCCESS"})
# MOBILE
@app.route('/getRanking/<int:chal_id>')
def ranking(chal_id):
res = requests.get(m.ONLINE_SERVER+"/getRanking/" + str(chal_id))
print(res.text)
print(res.json())
if res.text != "ERROR":
return jsonify(res.json())
return jsonify({'result', "ERROR"})
#MOBILE
@app.route('/getScore')
def getScore():
header = request.headers
name = header['authorization']
for p in m.players:
if m.players[p].getName() == name:
score = m.players[p].getScore()
return jsonify({'result': "SUCCESS", "score": score})
return jsonify({'result': "ERROR"})
if __name__ == '__main__':
app.run()