This commit is contained in:
Stephen McQuay 2013-04-23 23:27:42 -07:00
commit ca8bd88617
5 changed files with 33 additions and 33 deletions

View File

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

View File

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

View File

@ -1,21 +1,7 @@
.number { .number {
font-size: 100px;
height: 80px;
font-family: helvetica; font-family: helvetica;
} }
.question {
padding-left: 30px;
}
.answer {
padding-top: 30px;
}
#content {
padding-top: 30px;
}
.wrong { .wrong {
background: red; background: red;
} }

View File

@ -1,12 +1,13 @@
function update_board(f, o, s) { function update_board(f, o, s, score) {
$("#first").text(f); $("#first").text(f);
$("#operation").text(o); $("#operation").text(o);
$("#second").text(s); $("#second").text(s);
$("#score").text(score);
} }
function new_problem() { function new_problem() {
$.get("/api/v0/problem/", function(d) { $.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(""); $("#answer").val("");
}) })
} }
@ -18,19 +19,24 @@ function deal_with_answer(e) {
"second": $("#second").text(), "second": $("#second").text(),
"answer": $("#answer").val(), "answer": $("#answer").val(),
}; };
console.log(data);
$.post("/api/v0/attempt/", data, function(d) { $.post("/api/v0/attempt/", data, function(d) {
if(d["status"]) { if(d["status"]) {
$("#content").removeClass("wrong"); $("body").removeClass("wrong");
new_problem(); new_problem();
} }
else { else {
$("#content").addClass("wrong"); $("body").addClass("wrong");
$("#answer").val("");
} }
$("#answer").focus();
}); });
} }
$(function() { $(function() {
new_problem(); new_problem();
$("#check").click(deal_with_answer); $("#answer").keypress(function(e) {
if( e.which == 13) {
deal_with_answer(e);
}
});
}); });

View File

@ -7,16 +7,16 @@
</head> </head>
<body> <body>
<section id="content"> <section id="content">
<div class="number question"> <span class="number" id="first">0</span>
<span class="number" id="first">0</span> <span class="number" id="operation">+</span>
<span class="number" id="operation">+</span> <span class="number" id="second">0</span>
<span class="number" id="second">0</span> <div>
</div> <input id="answer" type="number" size="2" class="number" autofocus />
<div class="number answer">
<input id="answer" type="number" size="2" class="number"></div>
<input id="check" type="submit" name="check" value="check"/>
</div> </div>
</section> </section>
<footer>
score: <span id="score"></span>
</footer>
<script src="/static/jquery-2.0.0.min.js"></script> <script src="/static/jquery-2.0.0.min.js"></script>
<script src="/static/django-csrf.js"></script> <script src="/static/django-csrf.js"></script>
<script src="/static/math.js"></script> <script src="/static/math.js"></script>