added multiplication

This commit is contained in:
Stephen McQuay 2014-12-07 21:23:31 -08:00
parent 6349277217
commit 467cda77bf
7 changed files with 70 additions and 25 deletions

View File

@ -2,3 +2,4 @@
\.zip$
\.swp$
mmg
tags

View File

@ -2,12 +2,12 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/sessions"
"log"
"math/rand"
"net/http"
"strconv"
"github.com/gorilla/sessions"
)
type prob struct {
@ -22,13 +22,6 @@ type solution struct {
Score int
}
type JsonHandler func(http.ResponseWriter, *http.Request)
func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
h(w, req)
}
func getScore(session *sessions.Session) int {
score := session.Values["Score"]
var parsed_score int
@ -40,7 +33,8 @@ func getScore(session *sessions.Session) int {
return parsed_score
}
func problem(w http.ResponseWriter, req *http.Request) {
func addsub(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
operation := "+"
if r := rand.Intn(2); r == 0 {
operation = "-"
@ -63,16 +57,31 @@ func problem(w http.ResponseWriter, req *http.Request) {
r := prob{operation, first, second, score}
b, err := json.Marshal(r)
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,
},
)
if err != nil {
log.Fatal("issue with json marshalling", err)
}
j := string(b)
fmt.Println("problem", j)
fmt.Fprintf(w, j)
}
func attempt(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
first, err := strconv.Atoi(req.FormValue("first"))
if err != nil {
log.Fatal("cannot parse first", err)
@ -97,10 +106,13 @@ func attempt(w http.ResponseWriter, req *http.Request) {
}
var result bool
if operation == "+" {
switch operation {
case "+":
result = first+second == guess
} else if operation == "-" {
case "-":
result = first-second == guess
case "*", "x":
result = first*second == guess
}
session, _ := store.Get(req, "Score")
@ -113,11 +125,13 @@ func attempt(w http.ResponseWriter, req *http.Request) {
session.Values["Score"] = score
session.Save(req, w)
b, err := json.Marshal(solution{result, score})
err = json.NewEncoder(w).Encode(
solution{
result,
score,
},
)
if err != nil {
log.Fatal("cannot marshal solution", err)
}
j := string(b)
fmt.Println("attempt", j)
fmt.Fprintf(w, j)
}

View File

@ -2,12 +2,13 @@ package main
import (
"flag"
"github.com/gorilla/sessions"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/gorilla/sessions"
)
const MAX = 12
@ -22,8 +23,9 @@ func main() {
flag.Parse()
http.Handle("/",
http.FileServer(http.Dir(*static_files)))
http.Handle("/api/v0/problem/", JsonHandler(problem))
http.Handle("/api/v0/attempt/", JsonHandler(attempt))
http.HandleFunc("/api/v0/addsub/problem/", addsub)
http.HandleFunc("/api/v0/mul/problem/", mul)
http.HandleFunc("/api/v0/attempt/", attempt)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}

View File

@ -17,7 +17,7 @@
<footer>
score: <span id="score"></span>
</footer>
<script src="/jquery-2.0.0.min.js"></script>
<script src="/jquery.js"></script>
<script src="/math.js"></script>
</body>
</html>

4
static/jquery.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,8 @@ function update_board(f, o, s, score) {
}
function new_problem() {
$.get("/api/v0/problem/", function(d) {
var type = document.URL.split('/')[3];
$.get("/api/v0/"+type+"/problem/", function(d) {
update_board(d["First"], d["Operation"], d["Second"], d["Score"]);
$("#answer").val("");
})

23
static/mul/index.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mardson's Math Game</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/math.css" rel="stylesheet" media="screen">
</head>
<body>
<section id="content">
<span class="number" id="first">0</span>
<span class="number" id="operation">+</span>
<span class="number" id="second">0</span>
<div>
<input id="answer" type="number" size="2" class="number" autofocus />
</div>
</section>
<footer>
score: <span id="score"></span>
</footer>
<script src="/jquery.js"></script>
<script src="/math.js"></script>
</body>
</html>