Added ability to configure game at creation time

This commit is contained in:
sm
2013-11-07 22:11:54 -08:00
parent 164c37e7d6
commit caea45741f
3 changed files with 61 additions and 33 deletions
+39 -4
View File
@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"runtime/pprof"
@@ -17,10 +18,44 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
func startGame(w http.ResponseWriter, req *http.Request) {
log.Println("asked to create a game")
g, err := games.get("", true)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
requested_game_name := idg.Hash()
width, height := float32(*width), float32(*height)
// here we determine if we are going to run with defaults or pick them off
// a posted json blob
if req.Method == "POST" {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("unable to read request body:", err)
}
req.Body.Close()
conf := struct {
With float32 `json:"width"`
Height float32 `json:"height"`
Name string `json:"name"`
}{}
err = json.Unmarshal(body, &conf)
if err != nil {
NewFailure(err.Error())
if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
requested_game_name = conf.Name
width = conf.With
height = conf.Height
}
g := games.get(requested_game_name)
if g == nil {
log.Printf("Game '%s' non-existant; making it now", requested_game_name)
g = NewGame(requested_game_name, width, height)
go g.run()
games.add(g)
} else {
log.Printf("Game '%s' found: %p", requested_game_name, g)
}
game_json := struct {