From 7bb5ae59b35c4fdaa86614b8aa8024b1657f1aac Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 23 Jan 2014 21:11:13 -0800 Subject: [PATCH] send the client calculated values for stats --- protocol.go | 20 +++++++++++++++----- robot.go | 18 +++++++++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/protocol.go b/protocol.go index 01569b5..9abaad3 100644 --- a/protocol.go +++ b/protocol.go @@ -214,12 +214,12 @@ func addPlayer(ws *websocket.Conn) { // TODO: verify conf's type if conf.Valid(game.maxPoints) { - 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)) + log.Printf("%s: Config is INVALID, abort", player_id) + _ = websocket.JSON.Send(ws, NewFailure("invalid config")) + return } } @@ -227,12 +227,15 @@ func addPlayer(ws *websocket.Conn) { Robots: []*Robot{}, send: make(chan Message), ws: ws, - Id: idg.Hash(), + Id: player_id, } + convertedStats := map[string]Stats{} for name, stats := range conf.Stats { + dstat := DeriveStats(stats) + convertedStats[name] = dstat r := Robot{ - Stats: DeriveStats(stats), + Stats: dstat, Id: idg.Hash(), Name: name, Health: 10, @@ -244,6 +247,13 @@ func addPlayer(ws *websocket.Conn) { 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 defer func() { diff --git a/robot.go b/robot.go index 186e39e..1a5d877 100644 --- a/robot.go +++ b/robot.go @@ -91,15 +91,15 @@ func (s AllRobotSorter) Less(i, j int) bool { } type Stats struct { - Hp int `json:"-"` - Speed float32 `json:"-"` - Acceleration float32 `json:"-"` - WeaponRadius int `json:"-"` - ScannerRadius int `json:"-"` - TurnSpeed int `json:"-"` - FireRate int `json:"-"` - WeaponDamage int `json:"-"` - WeaponSpeed float32 `json:"-"` + Hp int `json:"hp"` + Speed float32 `json:"speed"` + Acceleration float32 `json:"acceleration"` + WeaponRadius int `json:"weapon_radius"` + ScannerRadius int `json:"scanner_radius"` + TurnSpeed int `json:"turn_speed"` + FireRate int `json:"fire_rate"` + WeaponDamage int `json:"weapon_damage"` + WeaponSpeed float32 `json:"weapon_speed"` } // We request stats using an integer between 1 and 100, the