implemented answer checking, modulo scores
This commit is contained in:
parent
4fa2e2bbd3
commit
6aaa72475a
51
handlers.go
51
handlers.go
@ -6,12 +6,19 @@ import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type prob struct {
|
||||
Operation string
|
||||
First int
|
||||
Second int
|
||||
Score int
|
||||
}
|
||||
|
||||
type solution struct {
|
||||
Status bool
|
||||
Score int
|
||||
}
|
||||
|
||||
type JsonHandler func(http.ResponseWriter, *http.Request)
|
||||
@ -39,17 +46,53 @@ func problem(w http.ResponseWriter, req *http.Request) {
|
||||
second = rand.Intn(MAX)
|
||||
}
|
||||
|
||||
r := prob{operation, first, second}
|
||||
r := prob{operation, first, second, 66}
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
log.Fatal("issue with json marshalling")
|
||||
log.Fatal("issue with json marshalling", err)
|
||||
}
|
||||
j := string(b)
|
||||
fmt.Println(j)
|
||||
fmt.Println("problem", j)
|
||||
fmt.Fprintf(w, j)
|
||||
}
|
||||
|
||||
func attempt(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Fprintf(w, "hello world")
|
||||
first, err := strconv.Atoi(req.FormValue("first"))
|
||||
if err != nil {
|
||||
log.Fatal("cannot parse first", err)
|
||||
}
|
||||
operation := req.FormValue("operation")
|
||||
second, err := strconv.Atoi(req.FormValue("second"))
|
||||
if err != nil {
|
||||
log.Fatal("cannot parse second", err)
|
||||
}
|
||||
var guess int
|
||||
answer := req.FormValue("answer")
|
||||
if answer == "" {
|
||||
guess = 0
|
||||
} else {
|
||||
guess, err = strconv.Atoi(answer)
|
||||
if err != nil {
|
||||
log.Fatal("cannot parser answer", err)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal("cannot parse answer", err)
|
||||
}
|
||||
|
||||
var result bool
|
||||
if operation == "+" {
|
||||
result = first+second == guess
|
||||
} else if operation == "-" {
|
||||
result = first-second == guess
|
||||
}
|
||||
|
||||
b, err := json.Marshal(solution{result, 66})
|
||||
if err != nil {
|
||||
log.Fatal("cannot marshal solution", err)
|
||||
}
|
||||
j := string(b)
|
||||
fmt.Println("attempt", j)
|
||||
fmt.Fprintf(w, j)
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ function deal_with_answer(e) {
|
||||
"answer": $("#answer").val(),
|
||||
};
|
||||
$.post("/api/v0/attempt/", data, function(d) {
|
||||
if(d["status"]) {
|
||||
if(d["Status"]) {
|
||||
$("body").removeClass("wrong");
|
||||
new_problem();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user