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 (
"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 {

28
game.go
View File

@ -1,8 +1,6 @@
package main
import (
// "encoding/json"
"errors"
"log"
"sort"
"sync"
@ -53,31 +51,17 @@ type MapLock struct {
// 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
// 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()
g, ok := games.m[id]
g, _ := ml.m[id]
ml.Unlock()
return g
}
if ok {
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()
func (ml *MapLock) add(g *game) {
ml.Lock()
ml.m[id] = _g
ml.m[g.id] = g
ml.Unlock()
return _g, nil
}
type game struct {

View File

@ -98,14 +98,23 @@ func addPlayer(ws *websocket.Conn) {
log.Println("problem parsing the requested game id")
return
}
log.Printf("requested game id: %+v", gid)
force := *debug
game, err := games.get(gid.Id, force)
if err != nil {
log.Printf("ERROR: game '%s' not found", gid.Id)
websocket.JSON.Send(ws, NewFailure("game 404"))
return
game := games.get(gid.Id)
log.Printf("found game: %p", game)
if game == nil {
log.Println("game was nil")
force := *debug
if force {
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()