diff --git a/main.go b/main.go index 8370130..50c8bc3 100644 --- a/main.go +++ b/main.go @@ -36,24 +36,28 @@ func main() { } else { gameName = flag.Arg(0) } - // XXX: add TurnSpeed, WeaponDamage, WeaponSpeed - var wg sync.WaitGroup + var wg sync.WaitGroup for i := 0; i < *botcount; i++ { - r, err := NewRobot( - *server, - *port, - gameName, - fmt.Sprintf("%s%d", *botname, i), - *hp, - *speed, - *acceleration, - *weaponRadius, - *scannerRadius, - *fireRate, - ) - if err != nil { - log.Fatal(err) + r := &robot{ + server: *server, + port: *port, + name: fmt.Sprintf("%s%d", *botname, i), + game: GameParam{ + name: gameName, + }, + // XXX: update with missing fields + stats: StatsRequest{ + Hp: *hp, + Speed: *speed, + Acceleration: *acceleration, + WeaponRadius: *weaponRadius, + ScannerRadius: *scannerRadius, + TurnSpeed: 50, + FireRate: *fireRate, + WeaponDamage: 50, + WeaponSpeed: 50, + }, } log.Printf("my robot: %+v", r) go r.play() diff --git a/robot.go b/robot.go index 9e12dae..cbe2b4b 100644 --- a/robot.go +++ b/robot.go @@ -9,64 +9,33 @@ import ( "math/rand" ) -func connect() (*websocket.Conn, error) { +func connect(server string, port int) (*websocket.Conn, error) { origin := "http://localhost/" - url := fmt.Sprintf("ws://%s:%d/ws/", *server, *port) + url := fmt.Sprintf("ws://%s:%d/ws/", server, port) return websocket.Dial(url, "", origin) } type robot struct { + server string + port int ws *websocket.Conn game GameParam playerId string name string + stats StatsRequest } -func NewRobot( - server string, - port int, - gameName string, - botName string, - hp, - speed, - acceleration, - weaponRadius, - scannerRadius, - fireRate int) (*robot, error) { - ws, err := connect() +func (r *robot) negociate() (err error) { + log.Printf("trying to connect to game '%s'", r.game.name) + r.ws, err = connect(r.server, r.port) if err != nil { - return nil, errors.New(fmt.Sprintf("connection failure: %s", err)) + return errors.New(fmt.Sprintf("connection failure: %s", err)) } - r := &robot{ - ws: ws, - name: botName, - } - - // XXX: update with missing fields - statsReq := StatsRequest{ - Hp: hp, - Speed: speed, - Acceleration: acceleration, - WeaponRadius: weaponRadius, - ScannerRadius: scannerRadius, - TurnSpeed: 50, - FireRate: fireRate, - WeaponDamage: 50, - WeaponSpeed: 50, - } - - err = r.negociate(statsReq, gameName) - - return r, nil -} - -func (r *robot) negociate(req StatsRequest, gameName string) (err error) { - log.Printf("trying to connect to game '%s'", gameName) err = websocket.JSON.Send(r.ws, struct { Id string `json:"id"` }{ - gameName, + r.game.name, }) if err != nil { return err @@ -98,13 +67,12 @@ func (r *robot) negociate(req StatsRequest, gameName string) (err error) { if r.game.Type != "gameparam" { return errors.New("didn't receive a good gameparam") } - r.game.name = gameName log.Printf("game parameters: %+v", r.game) conf := ClientConfig{ - ID: gameName, + ID: r.game.name, Stats: map[string]StatsRequest{ - r.name: req, + r.name: r.stats, }, } @@ -126,6 +94,12 @@ func (r *robot) negociate(req StatsRequest, gameName string) (err error) { func (r *robot) play() { var err error + err = r.negociate() + if err != nil { + log.Fatal("failed to negociate:", err) + } + + log.Printf("me: %+v", r) // TODO: var target govector.Point2d moveto := govector.Point2d{ X: rand.Float32() * r.game.BoardSize.Width,