Added ability to configure game at creation time
This commit is contained in:
parent
164c37e7d6
commit
caea45741f
37
control.go
37
control.go
@ -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,11 +18,45 @@ 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)
|
|
||||||
|
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 {
|
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)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
return
|
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 {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
|
26
game.go
26
game.go
@ -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 {
|
|
||||||
return g, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !force {
|
func (ml *MapLock) add(g *game) {
|
||||||
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 {
|
||||||
|
15
protocol.go
15
protocol.go
@ -98,15 +98,24 @@ 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)
|
||||||
|
|
||||||
|
game := games.get(gid.Id)
|
||||||
|
log.Printf("found game: %p", game)
|
||||||
|
if game == nil {
|
||||||
|
log.Println("game was nil")
|
||||||
force := *debug
|
force := *debug
|
||||||
game, err := games.get(gid.Id, force)
|
if force {
|
||||||
|
log.Println("forcing game start")
|
||||||
if err != nil {
|
game = NewGame(gid.Id, float32(*width), float32(*height))
|
||||||
|
go game.run()
|
||||||
|
games.add(game)
|
||||||
|
} else {
|
||||||
log.Printf("ERROR: game '%s' not found", gid.Id)
|
log.Printf("ERROR: game '%s' not found", gid.Id)
|
||||||
websocket.JSON.Send(ws, NewFailure("game 404"))
|
websocket.JSON.Send(ws, NewFailure("game 404"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
player_id := idg.Hash()
|
player_id := idg.Hash()
|
||||||
err = websocket.JSON.Send(ws, NewPlayerID(player_id))
|
err = websocket.JSON.Send(ws, NewPlayerID(player_id))
|
||||||
|
Loading…
Reference in New Issue
Block a user