diff --git a/main.go b/main.go index da3201e..99bcac2 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "bitbucket.org/hackerbots/bot" "bitbucket.org/hackerbots/vector" "code.google.com/p/go.net/websocket" "encoding/json" @@ -26,6 +25,57 @@ type infos struct { width, height float64 } +type Stats struct { + Hp int `json:"hp"` + Speed float64 `json:"speed"` + Acceleration float64 `json:"acceleration"` + WeaponRadius int `json:"weapon_radius"` + ScannerRadius int `json:"scanner_radius"` +} + +type Scanner struct { + Position govector.Point2d `json:"position"` + Stats Stats `json:"stats"` +} + +type Robot struct { + Id string `json:"id"` + Name string `json:"name"` + Stats Stats `json:"stats"` + TargetSpeed float64 `json:"speed"` + Speed float64 `json:"speed"` + Health int `json:"health"` + Position govector.Point2d `json:"position"` + Heading govector.Vector2d `json:"heading"` + MoveTo *govector.Point2d `json:"move_to,omitempty"` + FireAt *govector.Point2d `json:"fire_at,omitempty"` + Scanners []Scanner `json:"scanners"` +} + +type Projectile struct { + Id string `json:"id"` + Position govector.Point2d `json:"position"` + MoveTo govector.Point2d `json:"move_to"` + Radius int `json:"radius"` + Speed float64 `json:"speed"` + Damage int `json:"damage"` +} + +type Splosion struct { + Id string `json:"id"` + Position govector.Point2d `json:"position"` + Radius int `json:"radius"` + MaxDamage int `json:"damage"` + MinDamage int `json:"damage"` + Lifespan int `json:"lifespan"` +} + +type Instruction struct { + MoveTo *govector.Point2d `json:"move_to,omitempty"` + FireAt *govector.Point2d `json:"fire_at,omitempty"` + Stats Stats `json:"stats"` +} + func connect() (*websocket.Conn, error) { origin := "http://localhost/" url := fmt.Sprintf("ws://%s:%d/ws/", *server, *port) @@ -50,21 +100,30 @@ func createGame() (string, error) { func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) { log.Printf("trying to connect to game %s", gameid) - err = websocket.JSON.Send(ws, bot.GameID{ + err = websocket.JSON.Send(ws, struct { + Id string `json:"id"` + }{ gameid, }) if err != nil { return nil, err } - var idreq bot.IdRequest + var idreq struct { + Type string `json:"type"` + AssignedID string `json:"id"` + } err = websocket.JSON.Receive(ws, &idreq) if err != nil || idreq.Type == "failure" { return nil, errors.New(fmt.Sprintf("failure: %+v", idreq)) } log.Printf("%+v", idreq) - err = websocket.JSON.Send(ws, bot.ClientID{ + err = websocket.JSON.Send(ws, struct { + Type string `json:"type"` + Name string `json:"name"` + Useragent string `json:"useragent"` + }{ Name: *botname, Useragent: "gobot", Type: "robot", @@ -73,7 +132,15 @@ func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) { return nil, err } - var gameparam bot.GameParam + type BoardSize struct { + Width float64 `json:"width"` + Height float64 `json:"height"` + } + var gameparam struct { + BoardSize BoardSize `json:"boardsize"` + + Type string `json:"type"` + } err = websocket.JSON.Receive(ws, &gameparam) log.Printf("gameparam: %+v", gameparam) if gameparam.Type != "gameparam" { @@ -81,10 +148,15 @@ func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) { } log.Printf("%+v", gameparam) - err = websocket.JSON.Send(ws, &bot.Config{ + err = websocket.JSON.Send(ws, &struct { + ID string `json:"id"` + Name string `json:"name"` + // TODO: candidate for embedding? + Stats Stats `json:"stats"` + }{ ID: gameid, Name: *botname, - Stats: bot.Stats{ + Stats: Stats{ Hp: *hp, Speed: *speed, WeaponRadius: *weaponRadius, @@ -92,7 +164,11 @@ func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) { }, }) - var handshake bot.Handshake + var handshake struct { + ID string `json:"id"` + Success bool `json:"success"` + Type string `json:"type"` + } websocket.JSON.Receive(ws, &handshake) if !handshake.Success { return nil, errors.New("failed to validate correct stats request") @@ -134,11 +210,18 @@ func main() { } log.Printf("%+v", moveto) - var me bot.Robot + var me Robot log.Printf("negociated well, starting loop") for { - var boardstate bot.Boardstate + var boardstate struct { + Robots []Robot `json:"robots"` + Projectiles []Projectile `json:"projectiles"` + Splosions []Splosion `json:"splosions"` + Reset bool `json:"reset"` + Type string `json:"type"` + Turn int `json:"turn"` + } err = websocket.JSON.Receive(ws, &boardstate) if err != nil { log.Fatal("Connection lost") @@ -159,7 +242,7 @@ func main() { // TODO: send instructions log.Printf("%+v: %+v", me.Position, moveto) - err = websocket.JSON.Send(ws, bot.Instruction{ + err = websocket.JSON.Send(ws, Instruction{ MoveTo: &moveto, }) if err != nil {