Added ability to configure game at creation time

This commit is contained in:
Stephen McQuay 2013-11-06 21:12:10 -08:00
parent 164c37e7d6
commit caea45741f
3 changed files with 61 additions and 33 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"runtime/pprof" "runtime/pprof"
@ -17,10 +18,44 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
func startGame(w http.ResponseWriter, req *http.Request) { func startGame(w http.ResponseWriter, req *http.Request) {
log.Println("asked to create a game") log.Println("asked to create a game")
g, err := games.get("", true)
if err != nil { requested_game_name := idg.Hash()
http.Error(w, err.Error(), http.StatusInternalServerError) width, height := float32(*width), float32(*height)
return
// 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 { game_json := struct {

28
game.go
View File

@ -1,8 +1,6 @@
package main package main
import ( import (
// "encoding/json"
"errors"
"log" "log"
"sort" "sort"
"sync" "sync"
@ -53,31 +51,17 @@ type MapLock struct {
// get is a function that returns a game if found, and creates one if // get is a function that returns a game if found, and creates one if
// not found and force is true. In order to get a hash (rather than use // not found and force is true. In order to get a hash (rather than use
// the string you pass) send "" for id. // the string you pass) send "" for id.
func (ml *MapLock) get(id string, force bool) (*game, error) { func (ml *MapLock) get(id string) *game {
ml.Lock() ml.Lock()
g, ok := games.m[id] g, _ := ml.m[id]
ml.Unlock() ml.Unlock()
return g
}
if ok { func (ml *MapLock) add(g *game) {
return g, nil
}
if !force {
return nil, errors.New("game not found")
}
if id == "" {
id = idg.Hash()
}
_g := NewGame(id, float32(*width), float32(*height))
go _g.run()
ml.Lock() ml.Lock()
ml.m[id] = _g ml.m[g.id] = g
ml.Unlock() ml.Unlock()
return _g, nil
} }
type game struct { type game struct {

View File

@ -98,14 +98,23 @@ func addPlayer(ws *websocket.Conn) {
log.Println("problem parsing the requested game id") log.Println("problem parsing the requested game id")
return return
} }
log.Printf("requested game id: %+v", gid)
force := *debug game := games.get(gid.Id)
game, err := games.get(gid.Id, force) log.Printf("found game: %p", game)
if game == nil {
if err != nil { log.Println("game was nil")
log.Printf("ERROR: game '%s' not found", gid.Id) force := *debug
websocket.JSON.Send(ws, NewFailure("game 404")) if force {
return log.Println("forcing game start")
game = NewGame(gid.Id, float32(*width), float32(*height))
go game.run()
games.add(game)
} else {
log.Printf("ERROR: game '%s' not found", gid.Id)
websocket.JSON.Send(ws, NewFailure("game 404"))
return
}
} }
player_id := idg.Hash() player_id := idg.Hash()