2013-05-08 22:57:52 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-05-08 23:11:03 -07:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
2013-05-08 22:57:52 -07:00
|
|
|
"net/http"
|
2013-05-09 00:16:54 -07:00
|
|
|
"strconv"
|
2014-12-07 21:23:31 -08:00
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
2013-05-08 22:57:52 -07:00
|
|
|
)
|
|
|
|
|
2013-05-08 23:21:18 -07:00
|
|
|
type prob struct {
|
|
|
|
Operation string
|
|
|
|
First int
|
2013-05-09 00:16:54 -07:00
|
|
|
Second int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
|
|
|
|
type solution struct {
|
|
|
|
Status bool
|
|
|
|
Score int
|
2013-05-08 23:21:18 -07:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:37:35 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-07 21:23:31 -08:00
|
|
|
func addsub(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2013-05-08 23:11:03 -07:00
|
|
|
operation := "+"
|
|
|
|
if r := rand.Intn(2); r == 0 {
|
|
|
|
operation = "-"
|
|
|
|
}
|
|
|
|
|
2013-05-08 23:21:18 -07:00
|
|
|
first := rand.Intn(MAX)
|
|
|
|
var second int
|
|
|
|
if operation == "-" {
|
|
|
|
if first == 0 {
|
|
|
|
second = 0
|
|
|
|
} else {
|
|
|
|
second = rand.Intn(first)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
second = rand.Intn(MAX)
|
2013-05-08 23:11:03 -07:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:37:35 -07:00
|
|
|
session, _ := store.Get(req, "Score")
|
|
|
|
score := getScore(session)
|
|
|
|
|
|
|
|
r := prob{operation, first, second, score}
|
2013-05-08 23:21:18 -07:00
|
|
|
|
2014-12-07 21:23:31 -08:00
|
|
|
err := json.NewEncoder(w).Encode(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("issue with json marshalling", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mul(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
session, _ := store.Get(req, "Score")
|
|
|
|
score := getScore(session)
|
|
|
|
err := json.NewEncoder(w).Encode(
|
|
|
|
prob{
|
|
|
|
"x",
|
|
|
|
rand.Intn(11),
|
|
|
|
rand.Intn(11),
|
|
|
|
score,
|
|
|
|
},
|
|
|
|
)
|
2013-05-08 23:11:03 -07:00
|
|
|
if err != nil {
|
2013-05-09 00:16:54 -07:00
|
|
|
log.Fatal("issue with json marshalling", err)
|
2013-05-08 23:11:03 -07:00
|
|
|
}
|
2013-05-08 22:57:52 -07:00
|
|
|
}
|
|
|
|
|
2013-05-08 23:21:42 -07:00
|
|
|
func attempt(w http.ResponseWriter, req *http.Request) {
|
2014-12-07 21:23:31 -08:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2013-05-09 00:16:54 -07:00
|
|
|
first, err := strconv.Atoi(req.FormValue("first"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("cannot parse first", err)
|
|
|
|
}
|
2013-05-09 00:37:35 -07:00
|
|
|
|
2013-05-09 00:16:54 -07:00
|
|
|
operation := req.FormValue("operation")
|
2013-05-09 00:37:35 -07:00
|
|
|
|
2013-05-09 00:16:54 -07:00
|
|
|
second, err := strconv.Atoi(req.FormValue("second"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("cannot parse second", err)
|
|
|
|
}
|
2013-05-09 00:37:35 -07:00
|
|
|
|
2013-05-09 00:16:54 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result bool
|
2014-12-07 21:23:31 -08:00
|
|
|
switch operation {
|
|
|
|
case "+":
|
2013-05-09 00:16:54 -07:00
|
|
|
result = first+second == guess
|
2014-12-07 21:23:31 -08:00
|
|
|
case "-":
|
2013-05-09 00:16:54 -07:00
|
|
|
result = first-second == guess
|
2014-12-07 21:23:31 -08:00
|
|
|
case "*", "x":
|
|
|
|
result = first*second == guess
|
2013-05-09 00:16:54 -07:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:37:35 -07:00
|
|
|
session, _ := store.Get(req, "Score")
|
|
|
|
score := getScore(session)
|
2013-05-09 00:48:56 -07:00
|
|
|
if result {
|
|
|
|
score += 1
|
|
|
|
} else {
|
|
|
|
score -= 1
|
|
|
|
}
|
|
|
|
session.Values["Score"] = score
|
|
|
|
session.Save(req, w)
|
2013-05-09 00:37:35 -07:00
|
|
|
|
2014-12-07 21:23:31 -08:00
|
|
|
err = json.NewEncoder(w).Encode(
|
|
|
|
solution{
|
|
|
|
result,
|
|
|
|
score,
|
|
|
|
},
|
|
|
|
)
|
2013-05-09 00:16:54 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("cannot marshal solution", err)
|
|
|
|
}
|
2013-05-08 22:57:52 -07:00
|
|
|
}
|
2014-12-07 21:57:41 -08:00
|
|
|
|
|
|
|
func reset(w http.ResponseWriter, req *http.Request) {
|
|
|
|
session, _ := store.Get(req, "Score")
|
|
|
|
session.Values["Score"] = 0
|
|
|
|
session.Save(req, w)
|
|
|
|
http.Redirect(w, req, "/", http.StatusFound)
|
|
|
|
}
|