point limit parametrized

This commit is contained in:
Stephen McQuay 2013-11-29 01:10:49 -07:00
parent 33aea792ed
commit 3bb710dd9c
5 changed files with 39 additions and 32 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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)

View File

@ -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