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$ \.zip$
\.swp$ \.swp$
mmg mmg
tags

View File

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

View File

@ -17,7 +17,7 @@
<footer> <footer>
score: <span id="score"></span> score: <span id="score"></span>
</footer> </footer>
<script src="/jquery-2.0.0.min.js"></script> <script src="/jquery.js"></script>
<script src="/math.js"></script> <script src="/math.js"></script>
</body> </body>
</html> </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() { 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"]); update_board(d["First"], d["Operation"], d["Second"], d["Score"]);
$("#answer").val(""); $("#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>