From 3bb710dd9c224af3add979df2010a2e46a5d5260 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Fri, 29 Nov 2013 01:10:49 -0700 Subject: [PATCH] point limit parametrized --- config.go | 29 ++++++++++++++++------------- control.go | 21 +++++++++++---------- game.go | 5 ++++- main.go | 4 ++-- protocol.go | 12 ++++++------ 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/config.go b/config.go index 224bd2f..6497160 100644 --- a/config.go +++ b/config.go @@ -17,18 +17,27 @@ type Config struct { Width int `json:"width"` Height int `json:"height"` Obstacles int `json:"obstacles"` + MaxPoints int `json:"max_points"` } const ( - TICK = 60 - TIMESCALE = 1.0 - WIDTH = 800 - HEIGHT = 550 - OBSTACLES = 5 + TICK = 60 + TIMESCALE = 1.0 + WIDTH = 800 + HEIGHT = 550 + OBSTACLES = 5 + MAX_POINTS = 500 // allowing for 50 pts in every category ) func loadConfig(filename string) (Config, error) { - c := Config{} + c := Config{ + Tick: TICK, + Timescale: TIMESCALE, + Width: WIDTH, + Height: HEIGHT, + Obstacles: OBSTACLES, + MaxPoints: MAX_POINTS, + } u, err := user.Current() if err != nil { return c, err @@ -38,13 +47,6 @@ func loadConfig(filename string) (Config, error) { } if _, err := os.Stat(filename); os.IsNotExist(err) { log.Printf("%+v not found, using defaults", filename) - return Config{ - Tick: TICK, - Timescale: TIMESCALE, - Width: WIDTH, - Height: HEIGHT, - Obstacles: OBSTACLES, - }, nil } else { log.Printf("found config file: %s", filename) f, err := ioutil.ReadFile(filename) @@ -56,5 +58,6 @@ func loadConfig(filename string) (Config, error) { return c, errors.New(fmt.Sprintf("config parse error: %s", err)) } } + log.Printf("final config: %+v", c) return c, nil } diff --git a/control.go b/control.go index a0276aa..fde3957 100644 --- a/control.go +++ b/control.go @@ -25,6 +25,7 @@ func startGame(w http.ResponseWriter, req *http.Request) { width, height := float32(conf.Width), float32(conf.Height) obstacles := 0 tick := conf.Tick + maxPoints := conf.MaxPoints // here we determine if we are going to run with defaults or pick them off // a posted json blob @@ -35,11 +36,8 @@ func startGame(w http.ResponseWriter, req *http.Request) { } req.Body.Close() cfg := struct { - Width float32 `json:"width"` - Height float32 `json:"height"` - Name string `json:"name"` - Obstacles int `json:"obstacles"` - Tick int `json:"tick"` + Name string `json:"name"` + Config }{} err = json.Unmarshal(body, &cfg) if err != nil { @@ -49,21 +47,24 @@ func startGame(w http.ResponseWriter, req *http.Request) { return } requested_game_name = cfg.Name - width = cfg.Width - height = cfg.Height + width = float32(cfg.Width) + height = float32(cfg.Height) obstacles = cfg.Obstacles tick = cfg.Tick + maxPoints = cfg.MaxPoints } - log.Printf("game info: %v %v %v %v", requested_game_name, width, height, tick) 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, obstacles, tick) + g = NewGame(requested_game_name, width, height, obstacles, tick, maxPoints) go g.run() games.add(g) } else { - log.Printf("Game '%s' found: %p", requested_game_name, g) + log.Printf("Game '%s' already exists: %p", requested_game_name, g) + b, _ := json.Marshal(NewFailure("game already exists")) + http.Error(w, string(b), http.StatusConflict) + return } game_json := struct { diff --git a/game.go b/game.go index e6d6a25..4acc591 100644 --- a/game.go +++ b/game.go @@ -57,6 +57,7 @@ type game struct { turn int players_remaining int width, height float32 + maxPoints int spectators map[*Spectator]bool sregister chan *Spectator sunregister chan *Spectator @@ -67,7 +68,7 @@ type game struct { winners WinnerMap } -func NewGame(id string, width, height float32, obstacles, tick int) *game { +func NewGame(id string, width, height float32, obstacles, tick, maxPoints int) *game { g := &game{ id: id, register: make(chan *player), @@ -80,6 +81,7 @@ func NewGame(id string, width, height float32, obstacles, tick int) *game { turn: 0, width: width, height: height, + maxPoints: maxPoints, spectators: make(map[*Spectator]bool), sregister: make(chan *Spectator), sunregister: make(chan *Spectator), @@ -90,6 +92,7 @@ func NewGame(id string, width, height float32, obstacles, tick int) *game { players_remaining: 2, winners: WinnerMap{m: make(map[string]int)}, } + log.Printf("NewGame: %+v", g) return g } diff --git a/main.go b/main.go index daea1b8..2e06435 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "code.google.com/p/go.net/websocket" "flag" "log" "math/rand" @@ -10,6 +9,8 @@ import ( "os" "runtime/pprof" "time" + + "code.google.com/p/go.net/websocket" ) var addr = flag.String("addr", ":8666", "http service address") @@ -52,7 +53,6 @@ func main() { if err != nil { log.Fatal(err) } - log.Printf("config: %+v", conf) delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale) diff --git a/protocol.go b/protocol.go index 0b4681a..fd3393d 100644 --- a/protocol.go +++ b/protocol.go @@ -1,9 +1,10 @@ package main import ( + "log" + v "bitbucket.org/hackerbots/vector" "code.google.com/p/go.net/websocket" - "log" ) // < the name of the game we want to join @@ -45,7 +46,7 @@ type ClientConfig struct { Stats map[string]StatsRequest `json:"stats"` } -func (config ClientConfig) Valid() bool { +func (config ClientConfig) Valid(max int) bool { total := 0 for _, s := range config.Stats { total += (s.Speed + @@ -58,9 +59,7 @@ func (config ClientConfig) Valid() bool { s.WeaponDamage + s.WeaponSpeed) } - - // allowing for 50 pts in every category - if total > 500 { + if total > max { return false } return true @@ -167,6 +166,7 @@ func addPlayer(ws *websocket.Conn) { float32(conf.Height), conf.Obstacles, conf.Tick, + conf.MaxPoints, ) go game.run() games.add(game) @@ -219,7 +219,7 @@ func addPlayer(ws *websocket.Conn) { } // TODO: verify conf's type - if conf.Valid() { + if conf.Valid(game.maxPoints) { log.Printf("Config is Valid, continuing") _ = websocket.JSON.Send(ws, NewHandshake(player_id, true)) break