-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
78 lines (70 loc) · 2.43 KB
/
views.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
from django.http import HttpResponse
from django.shortcuts import render_to_response, redirect
from django.views.decorators.http import require_http_methods
from mathsBackend import *
def submitAnswer(request):
if not request.is_ajax() or request.method != 'POST':
return redirect('/play')
try: ans = int(request.POST.get('answer', None))
except: ans = None
try: id = int(request.POST.get('sessionID', -1))
except: id = -1
if id == -1:
return redirect('/play')
session = mathsSessions[id]
game = mathsGames[session.gameID]
resp = session.checkAnswer(ans)
if resp == "WRONG":
resp += ","+str(ans)
elif(resp == "LEVEL_OVER"):
resp += "," + str(session.levelScore)
elif resp == "GAME_OVER":
resp += "," + str(session.levelScore) + "," + str(session.totalScore)
elif resp == "CORRECT":
game.computeNextQuestion()
for sessionID in game.sessionIDs:
mathsSessions[sessionID].waitingForQuestion = True
return HttpResponse(resp)
def newSession(request):
sessionID = startSession()
return render_to_response('play.html', {'sessionID': sessionID} )
@require_http_methods(["POST"])
def playerReady(request):
try:
id = int(request.POST.get('sessionID', -1))
except:
id = -1
if id == -1:
return redirect('/play')
session = mathsSessions[id]
game = mathsGames[session.gameID]
session.ready = True
if game.isNextLevelReady():
game.nextLevel()
return HttpResponse('Success')
@require_http_methods(["POST"])
def pollNextLevel(request):
try: id = int(request.POST.get('sessionID', -1))
except ValueError: id = -1
if id == -1:
return redirect('/play')
session = mathsSessions[id]
game = mathsGames[session.gameID]
if not game.playing:
return HttpResponse('FAIL')
html = "SUCCESS,%d,%d" % (game.level, levelTimeLimit[game.level] )
return HttpResponse(html)
def pollNextQuestion(request):
if not request.is_ajax() or request.method != 'POST':
return redirect('/play')
try: id = int(request.POST.get('sessionID', -1))
except: id = -1
if id == -1:
return redirect('/play')
session = mathsSessions[id]
game = mathsGames[session.gameID]
if not session.waitingForQuestion:
return HttpResponse('FAIL')
session.waitingForQuestion = False
html = 'SUCCESS,'+game.nextQuestion
return HttpResponse(html)