point limit parametrized
This commit is contained in:
parent
33aea792ed
commit
3bb710dd9c
29
config.go
29
config.go
@ -17,18 +17,27 @@ type Config struct {
|
|||||||
Width int `json:"width"`
|
Width int `json:"width"`
|
||||||
Height int `json:"height"`
|
Height int `json:"height"`
|
||||||
Obstacles int `json:"obstacles"`
|
Obstacles int `json:"obstacles"`
|
||||||
|
MaxPoints int `json:"max_points"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TICK = 60
|
TICK = 60
|
||||||
TIMESCALE = 1.0
|
TIMESCALE = 1.0
|
||||||
WIDTH = 800
|
WIDTH = 800
|
||||||
HEIGHT = 550
|
HEIGHT = 550
|
||||||
OBSTACLES = 5
|
OBSTACLES = 5
|
||||||
|
MAX_POINTS = 500 // allowing for 50 pts in every category
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadConfig(filename string) (Config, error) {
|
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()
|
u, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c, err
|
return c, err
|
||||||
@ -38,13 +47,6 @@ func loadConfig(filename string) (Config, error) {
|
|||||||
}
|
}
|
||||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
log.Printf("%+v not found, using defaults", filename)
|
log.Printf("%+v not found, using defaults", filename)
|
||||||
return Config{
|
|
||||||
Tick: TICK,
|
|
||||||
Timescale: TIMESCALE,
|
|
||||||
Width: WIDTH,
|
|
||||||
Height: HEIGHT,
|
|
||||||
Obstacles: OBSTACLES,
|
|
||||||
}, nil
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("found config file: %s", filename)
|
log.Printf("found config file: %s", filename)
|
||||||
f, err := ioutil.ReadFile(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))
|
return c, errors.New(fmt.Sprintf("config parse error: %s", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Printf("final config: %+v", c)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
21
control.go
21
control.go
@ -25,6 +25,7 @@ func startGame(w http.ResponseWriter, req *http.Request) {
|
|||||||
width, height := float32(conf.Width), float32(conf.Height)
|
width, height := float32(conf.Width), float32(conf.Height)
|
||||||
obstacles := 0
|
obstacles := 0
|
||||||
tick := conf.Tick
|
tick := conf.Tick
|
||||||
|
maxPoints := conf.MaxPoints
|
||||||
|
|
||||||
// here we determine if we are going to run with defaults or pick them off
|
// here we determine if we are going to run with defaults or pick them off
|
||||||
// a posted json blob
|
// a posted json blob
|
||||||
@ -35,11 +36,8 @@ func startGame(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
req.Body.Close()
|
req.Body.Close()
|
||||||
cfg := struct {
|
cfg := struct {
|
||||||
Width float32 `json:"width"`
|
Name string `json:"name"`
|
||||||
Height float32 `json:"height"`
|
Config
|
||||||
Name string `json:"name"`
|
|
||||||
Obstacles int `json:"obstacles"`
|
|
||||||
Tick int `json:"tick"`
|
|
||||||
}{}
|
}{}
|
||||||
err = json.Unmarshal(body, &cfg)
|
err = json.Unmarshal(body, &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -49,21 +47,24 @@ func startGame(w http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
requested_game_name = cfg.Name
|
requested_game_name = cfg.Name
|
||||||
width = cfg.Width
|
width = float32(cfg.Width)
|
||||||
height = cfg.Height
|
height = float32(cfg.Height)
|
||||||
obstacles = cfg.Obstacles
|
obstacles = cfg.Obstacles
|
||||||
tick = cfg.Tick
|
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)
|
g := games.get(requested_game_name)
|
||||||
if g == nil {
|
if g == nil {
|
||||||
log.Printf("Game '%s' non-existant; making it now", requested_game_name)
|
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()
|
go g.run()
|
||||||
games.add(g)
|
games.add(g)
|
||||||
} else {
|
} 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 {
|
game_json := struct {
|
||||||
|
5
game.go
5
game.go
@ -57,6 +57,7 @@ type game struct {
|
|||||||
turn int
|
turn int
|
||||||
players_remaining int
|
players_remaining int
|
||||||
width, height float32
|
width, height float32
|
||||||
|
maxPoints int
|
||||||
spectators map[*Spectator]bool
|
spectators map[*Spectator]bool
|
||||||
sregister chan *Spectator
|
sregister chan *Spectator
|
||||||
sunregister chan *Spectator
|
sunregister chan *Spectator
|
||||||
@ -67,7 +68,7 @@ type game struct {
|
|||||||
winners WinnerMap
|
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{
|
g := &game{
|
||||||
id: id,
|
id: id,
|
||||||
register: make(chan *player),
|
register: make(chan *player),
|
||||||
@ -80,6 +81,7 @@ func NewGame(id string, width, height float32, obstacles, tick int) *game {
|
|||||||
turn: 0,
|
turn: 0,
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
|
maxPoints: maxPoints,
|
||||||
spectators: make(map[*Spectator]bool),
|
spectators: make(map[*Spectator]bool),
|
||||||
sregister: make(chan *Spectator),
|
sregister: make(chan *Spectator),
|
||||||
sunregister: 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,
|
players_remaining: 2,
|
||||||
winners: WinnerMap{m: make(map[string]int)},
|
winners: WinnerMap{m: make(map[string]int)},
|
||||||
}
|
}
|
||||||
|
log.Printf("NewGame: %+v", g)
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
main.go
4
main.go
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/go.net/websocket"
|
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -10,6 +9,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"code.google.com/p/go.net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var addr = flag.String("addr", ":8666", "http service address")
|
var addr = flag.String("addr", ":8666", "http service address")
|
||||||
@ -52,7 +53,6 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Printf("config: %+v", conf)
|
|
||||||
|
|
||||||
delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale)
|
delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale)
|
||||||
|
|
||||||
|
12
protocol.go
12
protocol.go
@ -1,9 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
v "bitbucket.org/hackerbots/vector"
|
v "bitbucket.org/hackerbots/vector"
|
||||||
"code.google.com/p/go.net/websocket"
|
"code.google.com/p/go.net/websocket"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// < the name of the game we want to join
|
// < the name of the game we want to join
|
||||||
@ -45,7 +46,7 @@ type ClientConfig struct {
|
|||||||
Stats map[string]StatsRequest `json:"stats"`
|
Stats map[string]StatsRequest `json:"stats"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config ClientConfig) Valid() bool {
|
func (config ClientConfig) Valid(max int) bool {
|
||||||
total := 0
|
total := 0
|
||||||
for _, s := range config.Stats {
|
for _, s := range config.Stats {
|
||||||
total += (s.Speed +
|
total += (s.Speed +
|
||||||
@ -58,9 +59,7 @@ func (config ClientConfig) Valid() bool {
|
|||||||
s.WeaponDamage +
|
s.WeaponDamage +
|
||||||
s.WeaponSpeed)
|
s.WeaponSpeed)
|
||||||
}
|
}
|
||||||
|
if total > max {
|
||||||
// allowing for 50 pts in every category
|
|
||||||
if total > 500 {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -167,6 +166,7 @@ func addPlayer(ws *websocket.Conn) {
|
|||||||
float32(conf.Height),
|
float32(conf.Height),
|
||||||
conf.Obstacles,
|
conf.Obstacles,
|
||||||
conf.Tick,
|
conf.Tick,
|
||||||
|
conf.MaxPoints,
|
||||||
)
|
)
|
||||||
go game.run()
|
go game.run()
|
||||||
games.add(game)
|
games.add(game)
|
||||||
@ -219,7 +219,7 @@ func addPlayer(ws *websocket.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: verify conf's type
|
// TODO: verify conf's type
|
||||||
if conf.Valid() {
|
if conf.Valid(game.maxPoints) {
|
||||||
log.Printf("Config is Valid, continuing")
|
log.Printf("Config is Valid, continuing")
|
||||||
_ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
|
_ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
|
||||||
break
|
break
|
||||||
|
Loading…
Reference in New Issue
Block a user