implemented scores
This commit is contained in:
parent
6aaa72475a
commit
8458c38468
40
handlers.go
40
handlers.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -28,6 +29,20 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
h(w, req)
|
h(w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getScore(session *sessions.Session) int {
|
||||||
|
score := session.Values["Score"]
|
||||||
|
var parsed_score int
|
||||||
|
if score == nil {
|
||||||
|
parsed_score = 0
|
||||||
|
} else {
|
||||||
|
parsed_score = score.(int)
|
||||||
|
}
|
||||||
|
return parsed_score
|
||||||
|
}
|
||||||
|
|
||||||
|
func setScore(session *sessions.Session) {
|
||||||
|
}
|
||||||
|
|
||||||
func problem(w http.ResponseWriter, req *http.Request) {
|
func problem(w http.ResponseWriter, req *http.Request) {
|
||||||
operation := "+"
|
operation := "+"
|
||||||
if r := rand.Intn(2); r == 0 {
|
if r := rand.Intn(2); r == 0 {
|
||||||
@ -46,7 +61,10 @@ func problem(w http.ResponseWriter, req *http.Request) {
|
|||||||
second = rand.Intn(MAX)
|
second = rand.Intn(MAX)
|
||||||
}
|
}
|
||||||
|
|
||||||
r := prob{operation, first, second, 66}
|
session, _ := store.Get(req, "Score")
|
||||||
|
score := getScore(session)
|
||||||
|
|
||||||
|
r := prob{operation, first, second, score}
|
||||||
|
|
||||||
b, err := json.Marshal(r)
|
b, err := json.Marshal(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,11 +80,14 @@ func attempt(w http.ResponseWriter, req *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("cannot parse first", err)
|
log.Fatal("cannot parse first", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
operation := req.FormValue("operation")
|
operation := req.FormValue("operation")
|
||||||
|
|
||||||
second, err := strconv.Atoi(req.FormValue("second"))
|
second, err := strconv.Atoi(req.FormValue("second"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("cannot parse second", err)
|
log.Fatal("cannot parse second", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var guess int
|
var guess int
|
||||||
answer := req.FormValue("answer")
|
answer := req.FormValue("answer")
|
||||||
if answer == "" {
|
if answer == "" {
|
||||||
@ -77,9 +98,6 @@ func attempt(w http.ResponseWriter, req *http.Request) {
|
|||||||
log.Fatal("cannot parser answer", err)
|
log.Fatal("cannot parser answer", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
log.Fatal("cannot parse answer", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var result bool
|
var result bool
|
||||||
if operation == "+" {
|
if operation == "+" {
|
||||||
@ -88,7 +106,19 @@ func attempt(w http.ResponseWriter, req *http.Request) {
|
|||||||
result = first-second == guess
|
result = first-second == guess
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(solution{result, 66})
|
session, _ := store.Get(req, "Score")
|
||||||
|
score := getScore(session)
|
||||||
|
if result {
|
||||||
|
score += 1
|
||||||
|
} else {
|
||||||
|
score -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
session, _ = store.Get(req, "Score")
|
||||||
|
session.Values["Score"] = score
|
||||||
|
session.Save(req, w)
|
||||||
|
|
||||||
|
b, err := json.Marshal(solution{result, score})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("cannot marshal solution", err)
|
log.Fatal("cannot marshal solution", err)
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ function deal_with_answer(e) {
|
|||||||
"answer": $("#answer").val(),
|
"answer": $("#answer").val(),
|
||||||
};
|
};
|
||||||
$.post("/api/v0/attempt/", data, function(d) {
|
$.post("/api/v0/attempt/", data, function(d) {
|
||||||
|
$("#score").text(d["Score"]);
|
||||||
if(d["Status"]) {
|
if(d["Status"]) {
|
||||||
$("body").removeClass("wrong");
|
$("body").removeClass("wrong");
|
||||||
new_problem();
|
new_problem();
|
||||||
|
Loading…
Reference in New Issue
Block a user