send the client calculated values for stats

This commit is contained in:
Stephen McQuay 2014-01-23 21:11:13 -08:00
parent c6c45ace51
commit 7bb5ae59b3
2 changed files with 24 additions and 14 deletions

View File

@ -214,12 +214,12 @@ func addPlayer(ws *websocket.Conn) {
// TODO: verify conf's type // TODO: verify conf's type
if conf.Valid(game.maxPoints) { if conf.Valid(game.maxPoints) {
log.Printf("Config is Valid, continuing")
_ = websocket.JSON.Send(ws, NewHandshake(player_id, true)) _ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
break break
} else { } else {
log.Printf("Config is INVALID, abort") log.Printf("%s: Config is INVALID, abort", player_id)
_ = websocket.JSON.Send(ws, NewHandshake(player_id, false)) _ = websocket.JSON.Send(ws, NewFailure("invalid config"))
return
} }
} }
@ -227,12 +227,15 @@ func addPlayer(ws *websocket.Conn) {
Robots: []*Robot{}, Robots: []*Robot{},
send: make(chan Message), send: make(chan Message),
ws: ws, ws: ws,
Id: idg.Hash(), Id: player_id,
} }
convertedStats := map[string]Stats{}
for name, stats := range conf.Stats { for name, stats := range conf.Stats {
dstat := DeriveStats(stats)
convertedStats[name] = dstat
r := Robot{ r := Robot{
Stats: DeriveStats(stats), Stats: dstat,
Id: idg.Hash(), Id: idg.Hash(),
Name: name, Name: name,
Health: 10, Health: 10,
@ -244,6 +247,13 @@ func addPlayer(ws *websocket.Conn) {
p.Robots = append(p.Robots, &r) p.Robots = append(p.Robots, &r)
} }
err = websocket.JSON.Send(ws, &convertedStats)
if err != nil {
log.Printf("error sending convertedStats to client: %s", err)
websocket.JSON.Send(ws, NewFailure("protocol error: convertedStats"))
return
}
game.register <- p game.register <- p
defer func() { defer func() {

View File

@ -91,15 +91,15 @@ func (s AllRobotSorter) Less(i, j int) bool {
} }
type Stats struct { type Stats struct {
Hp int `json:"-"` Hp int `json:"hp"`
Speed float32 `json:"-"` Speed float32 `json:"speed"`
Acceleration float32 `json:"-"` Acceleration float32 `json:"acceleration"`
WeaponRadius int `json:"-"` WeaponRadius int `json:"weapon_radius"`
ScannerRadius int `json:"-"` ScannerRadius int `json:"scanner_radius"`
TurnSpeed int `json:"-"` TurnSpeed int `json:"turn_speed"`
FireRate int `json:"-"` FireRate int `json:"fire_rate"`
WeaponDamage int `json:"-"` WeaponDamage int `json:"weapon_damage"`
WeaponSpeed float32 `json:"-"` WeaponSpeed float32 `json:"weapon_speed"`
} }
// We request stats using an integer between 1 and 100, the // We request stats using an integer between 1 and 100, the