2013-11-10 23:30:23 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bitbucket.org/hackerbots/vector"
|
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
)
|
|
|
|
|
2013-11-11 00:01:57 -08:00
|
|
|
func connect(server string, port int) (*websocket.Conn, error) {
|
2013-11-10 23:30:23 -08:00
|
|
|
origin := "http://localhost/"
|
2013-11-11 00:01:57 -08:00
|
|
|
url := fmt.Sprintf("ws://%s:%d/ws/", server, port)
|
2013-11-10 23:30:23 -08:00
|
|
|
return websocket.Dial(url, "", origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
type robot struct {
|
2013-11-11 00:01:57 -08:00
|
|
|
server string
|
|
|
|
port int
|
2013-11-10 23:30:23 -08:00
|
|
|
ws *websocket.Conn
|
|
|
|
game GameParam
|
|
|
|
playerId string
|
2013-11-10 23:37:43 -08:00
|
|
|
name string
|
2013-11-11 00:01:57 -08:00
|
|
|
stats StatsRequest
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:01:57 -08:00
|
|
|
func (r *robot) negociate() (err error) {
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: trying to connect to game '%s'", r.name, r.game.name)
|
2013-11-11 00:01:57 -08:00
|
|
|
r.ws, err = connect(r.server, r.port)
|
2013-11-10 23:30:23 -08:00
|
|
|
if err != nil {
|
2013-11-11 00:01:57 -08:00
|
|
|
return errors.New(fmt.Sprintf("connection failure: %s", err))
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = websocket.JSON.Send(r.ws, struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
2013-11-11 00:01:57 -08:00
|
|
|
r.game.name,
|
2013-11-10 23:30:23 -08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var idreq struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
PlayerId string `json:"id"`
|
|
|
|
}
|
|
|
|
err = websocket.JSON.Receive(r.ws, &idreq)
|
|
|
|
if err != nil || idreq.Type == "failure" {
|
|
|
|
return errors.New(fmt.Sprintf("failure: %+v", idreq))
|
|
|
|
}
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: idreq: %+v", r.name, idreq)
|
2013-11-10 23:30:23 -08:00
|
|
|
|
|
|
|
err = websocket.JSON.Send(r.ws, struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Useragent string `json:"useragent"`
|
|
|
|
}{
|
2013-11-10 23:37:43 -08:00
|
|
|
Name: r.name,
|
2013-11-10 23:30:23 -08:00
|
|
|
Useragent: "gobot",
|
|
|
|
Type: "robot",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = websocket.JSON.Receive(r.ws, &r.game)
|
|
|
|
if r.game.Type != "gameparam" {
|
|
|
|
return errors.New("didn't receive a good gameparam")
|
|
|
|
}
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: game parameters: %+v", r.name, r.game)
|
2013-11-10 23:30:23 -08:00
|
|
|
|
|
|
|
conf := ClientConfig{
|
2013-11-11 00:01:57 -08:00
|
|
|
ID: r.game.name,
|
2013-11-10 23:30:23 -08:00
|
|
|
Stats: map[string]StatsRequest{
|
2013-11-11 00:01:57 -08:00
|
|
|
r.name: r.stats,
|
2013-11-10 23:30:23 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = websocket.JSON.Send(r.ws, conf)
|
|
|
|
|
|
|
|
var handshake struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
websocket.JSON.Receive(r.ws, &handshake)
|
|
|
|
if !handshake.Success {
|
|
|
|
return errors.New("failed to validate correct stats request")
|
|
|
|
}
|
|
|
|
r.playerId = handshake.Id
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: handshake: %+v", r.name, handshake)
|
2013-11-10 23:30:23 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *robot) play() {
|
|
|
|
var err error
|
2013-11-11 00:01:57 -08:00
|
|
|
err = r.negociate()
|
|
|
|
if err != nil {
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: failed to negociate: %s", r.name, err)
|
|
|
|
return
|
2013-11-11 00:01:57 -08:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: %+v", r.name, r)
|
2013-11-10 23:30:23 -08:00
|
|
|
// TODO: var target govector.Point2d
|
|
|
|
moveto := govector.Point2d{
|
|
|
|
X: rand.Float32() * r.game.BoardSize.Width,
|
|
|
|
Y: rand.Float32() * r.game.BoardSize.Height,
|
|
|
|
}
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: moveto: %+v", r.name, moveto)
|
|
|
|
log.Printf("%s: starting loop", r.name)
|
2013-11-10 23:30:23 -08:00
|
|
|
for {
|
|
|
|
var boardstate Boardstate
|
|
|
|
err = websocket.JSON.Receive(r.ws, &boardstate)
|
|
|
|
if *verbose {
|
|
|
|
log.Printf("%+v", boardstate)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: Connection lost", r.name)
|
|
|
|
return
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|