unstopped failure conditions

This commit is contained in:
Stephen McQuay 2013-11-11 00:25:28 -08:00
parent 2fb7ff7b20
commit e1bed61a00
2 changed files with 29 additions and 21 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"math/rand" "math/rand"
"sync" "sync"
"time" "time"
@ -61,10 +60,10 @@ func main() {
WeaponDamage: *weaponDamage, WeaponDamage: *weaponDamage,
WeaponSpeed: *weaponSpeed, WeaponSpeed: *weaponSpeed,
}, },
wg: &wg,
} }
log.Printf("my robot: %+v", r)
go r.play()
wg.Add(1) wg.Add(1)
go r.play()
} }
wg.Wait() wg.Wait()
} }

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
"sync"
) )
func connect(server string, port int) (*websocket.Conn, error) { func connect(server string, port int) (*websocket.Conn, error) {
@ -23,6 +24,10 @@ type robot struct {
playerId string playerId string
name string name string
stats StatsRequest 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) { func (r *robot) negociate() (err error) {
@ -93,6 +98,7 @@ func (r *robot) negociate() (err error) {
} }
func (r *robot) play() { func (r *robot) play() {
defer r.wg.Done()
var err error var err error
err = r.negociate() err = r.negociate()
if err != nil { if err != nil {
@ -118,26 +124,29 @@ func (r *robot) play() {
log.Printf("%s: Connection lost", r.name) log.Printf("%s: Connection lost", r.name)
return return
} }
me = boardstate.MyRobots[0] for _, me := range boardstate.MyRobots {
if govector.Distance(me.Position, moveto) < 3.0 { me = boardstate.MyRobots[0]
log.Printf("old: %+v: %+v", me.Position, moveto) if govector.Distance(me.Position, moveto) < 3.0 {
moveto = govector.Point2d{ log.Printf("%s: old: %+v: %+v", r.name, me.Position, moveto)
X: rand.Float32() * r.game.BoardSize.Width, moveto = govector.Point2d{
Y: rand.Float32() * r.game.BoardSize.Height, 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 // TODO: send instructions
instruction := map[string]Instruction{ instruction := map[string]Instruction{
me.Id: { me.Id: {
MoveTo: &moveto, MoveTo: &moveto,
FireAt: &moveto, FireAt: &moveto,
}, },
} }
err = websocket.JSON.Send(r.ws, instruction) err = websocket.JSON.Send(r.ws, instruction)
if err != nil { if err != nil {
log.Fatal(err) log.Println(err)
return
}
} }
} }
} }