added score

This commit is contained in:
Stephen McQuay 2013-04-23 23:27:30 -07:00
parent 37ae2cf663
commit dc24ed7e4a
4 changed files with 19 additions and 7 deletions

View File

@ -118,6 +118,8 @@ INSTALLED_APPS = (
# 'django.contrib.admindocs',
)
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.

View File

@ -28,7 +28,6 @@ def _generate_problem():
def _validate_solution(a):
print a
f = int(a['first'])
o = a['operation']
s = int(a['second'])
@ -49,13 +48,20 @@ def home(request):
def attempt(request):
print(dict(request.session))
d = request.POST.dict()
r = _validate_solution(d)
if r:
s = request.session.get('score', 0) + 1
else:
s = request.session.get('score', 0) - 1
request.session['score'] = s
return HttpResponse(
json.dumps({'status': _validate_solution(d)}),
json.dumps({'status': r, 'score': s}),
content_type="application/json")
def problem(request):
return HttpResponse(
json.dumps(_generate_problem()),
content_type="application/json")
d = _generate_problem()
d['score'] = request.session.get('score', 0)
return HttpResponse(json.dumps(d), content_type="application/json")

View File

@ -1,12 +1,13 @@
function update_board(f, o, s) {
function update_board(f, o, s, score) {
$("#first").text(f);
$("#operation").text(o);
$("#second").text(s);
$("#score").text(score);
}
function new_problem() {
$.get("/api/v0/problem/", function(d) {
update_board(d["first"], d["operation"], d["second"]);
update_board(d["first"], d["operation"], d["second"], d["score"]);
$("#answer").val("");
})
}

View File

@ -14,6 +14,9 @@
<input id="answer" type="number" size="2" class="number" autofocus />
</div>
</section>
<footer>
score: <span id="score"></span>
</footer>
<script src="/static/jquery-2.0.0.min.js"></script>
<script src="/static/django-csrf.js"></script>
<script src="/static/math.js"></script>