First pass of multibot support

This commit is contained in:
Fraser Graham
2013-11-08 21:25:42 -08:00
parent 49ff6af1f3
commit e39a97089e
6 changed files with 350 additions and 278 deletions
+44 -11
View File
@@ -40,8 +40,29 @@ func (c *ClientID) Valid() (bool, string) {
}
type ClientConfig struct {
ID string `json:"id"`
Stats StatsRequest `json:"stats"`
ID string `json:"id"`
Stats map[string]StatsRequest `json:"stats"`
}
func (config ClientConfig) Valid() bool {
total := 0
for _, s := range config.Stats {
total += (s.Speed +
s.Hp +
s.WeaponRadius +
s.ScannerRadius +
s.Acceleration +
s.TurnSpeed +
s.FireRate +
s.WeaponDamage +
s.WeaponSpeed)
}
// allowing for 50 pts in every category
if total > 1000 {
return false
}
return true
}
type BoardSize struct {
@@ -152,38 +173,50 @@ func addPlayer(ws *websocket.Conn) {
log.Printf("%s Waiting for client to send conf ...", player_id)
err = websocket.JSON.Receive(ws, &conf)
log.Printf("conf received: %+v", conf)
if err != nil {
log.Printf("%s %s config parse error", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("config parse error"))
return
}
// TODO: verify conf's type
if conf.Stats.Valid() {
if conf.Valid() {
log.Printf("Config is Valid, continuing")
_ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
break
} else {
log.Printf("Config is INVALID, abort")
_ = websocket.JSON.Send(ws, NewHandshake(player_id, false))
}
}
p := &player{
Robot: Robot{
Stats: DeriveStats(conf.Stats),
Robots: []Robot{},
send: make(chan *Boardstate),
ws: ws,
}
for _, stats := range conf.Stats {
r := Robot{
Stats: DeriveStats(stats),
Id: player_id,
Name: clientid.Name,
Health: conf.Stats.Hp,
Scanners: make([]Scanner, 0)},
send: make(chan *Boardstate),
ws: ws,
Health: 10,
Scanners: make([]Scanner, 0)}
r.Health = r.Stats.Hp
log.Printf("Adding Robot: %v", r)
p.Robots = append(p.Robots, r)
r.reset(game)
}
p.reset(game)
game.register <- p
defer func() {
game.unregister <- p
}()
go p.sender()
p.recv()
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robot.Id)
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robots[0].Id)
case "spectator":
s := &Spectator{
send: make(chan *Boardstate),