From e1bed61a003da1925d61722c83761cb164bcac3a Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Mon, 11 Nov 2013 00:25:28 -0800 Subject: [PATCH] unstopped failure conditions --- main.go | 5 ++--- robot.go | 45 +++++++++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index c0398f8..b73cdc1 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "log" "math/rand" "sync" "time" @@ -61,10 +60,10 @@ func main() { WeaponDamage: *weaponDamage, WeaponSpeed: *weaponSpeed, }, + wg: &wg, } - log.Printf("my robot: %+v", r) - go r.play() wg.Add(1) + go r.play() } wg.Wait() } diff --git a/robot.go b/robot.go index abad77e..3ca2f63 100644 --- a/robot.go +++ b/robot.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "math/rand" + "sync" ) func connect(server string, port int) (*websocket.Conn, error) { @@ -23,6 +24,10 @@ type robot struct { playerId string name string stats StatsRequest + + // TODO: don't know if I like how this is done ... I would rather send + // a signal over a chanel + wg *sync.WaitGroup } func (r *robot) negociate() (err error) { @@ -93,6 +98,7 @@ func (r *robot) negociate() (err error) { } func (r *robot) play() { + defer r.wg.Done() var err error err = r.negociate() if err != nil { @@ -118,26 +124,29 @@ func (r *robot) play() { log.Printf("%s: Connection lost", r.name) return } - me = boardstate.MyRobots[0] - if govector.Distance(me.Position, moveto) < 3.0 { - log.Printf("old: %+v: %+v", me.Position, moveto) - moveto = govector.Point2d{ - X: rand.Float32() * r.game.BoardSize.Width, - Y: rand.Float32() * r.game.BoardSize.Height, + for _, me := range boardstate.MyRobots { + me = boardstate.MyRobots[0] + if govector.Distance(me.Position, moveto) < 3.0 { + log.Printf("%s: old: %+v: %+v", r.name, me.Position, moveto) + moveto = govector.Point2d{ + X: rand.Float32() * r.game.BoardSize.Width, + Y: rand.Float32() * r.game.BoardSize.Height, + } + log.Printf("%s: new: %+v: %+v", r.name, me.Position, moveto) } - log.Printf("new: %+v: %+v", me.Position, moveto) - } - // TODO: send instructions - instruction := map[string]Instruction{ - me.Id: { - MoveTo: &moveto, - FireAt: &moveto, - }, - } - err = websocket.JSON.Send(r.ws, instruction) - if err != nil { - log.Fatal(err) + // TODO: send instructions + instruction := map[string]Instruction{ + me.Id: { + MoveTo: &moveto, + FireAt: &moveto, + }, + } + err = websocket.JSON.Send(r.ws, instruction) + if err != nil { + log.Println(err) + return + } } } }