client/main.go

165 lines
3.4 KiB
Go
Raw Normal View History

package main
import (
"bitbucket.org/hackerbots/bot"
2013-09-20 11:16:11 -07:00
"bitbucket.org/hackerbots/vector"
"code.google.com/p/go.net/websocket"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
2013-09-20 11:16:11 -07:00
"math/rand"
"net/http"
)
2013-09-08 09:31:35 -07:00
var hp = flag.Int("hp", 150, "")
var speed = flag.Float64("speed", 150, "")
2013-09-07 19:26:21 -07:00
var weaponRadius = flag.Int("wrad", 100, "weapon radius")
var scannerRadius = flag.Int("srad", 100, "scanner radius")
2013-09-20 11:16:11 -07:00
type infos struct {
id string
width, height float64
}
func connect() (*websocket.Conn, error) {
origin := "http://localhost/"
url := "ws://localhost:8666/ws/"
return websocket.Dial(url, "", origin)
}
func createGame() (string, error) {
resp, err := http.Get("http://localhost:8666/game/start/")
if err != nil {
return "", err
}
defer resp.Body.Close()
var g struct {
2013-09-04 23:39:24 -07:00
Id string `json:"id"`
}
if err := json.NewDecoder(resp.Body).Decode(&g); err != nil {
return "", err
}
return g.Id, err
}
2013-09-20 11:16:11 -07:00
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{
gameid,
})
if err != nil {
2013-09-20 11:16:11 -07:00
return nil, err
}
var idreq bot.IdRequest
err = websocket.JSON.Receive(ws, &idreq)
if err != nil || idreq.Type == "failure" {
2013-09-20 11:16:11 -07:00
return nil, errors.New(fmt.Sprintf("failure: %+v", idreq))
}
log.Printf("%+v", idreq)
err = websocket.JSON.Send(ws, bot.ClientID{
Name: "smcquay test robot 2",
Useragent: "gobot",
Type: "robot",
})
if err != nil {
2013-09-20 11:16:11 -07:00
return nil, err
}
var gameparam bot.GameParam
err = websocket.JSON.Receive(ws, &gameparam)
log.Printf("gameparam: %+v", gameparam)
if gameparam.Type != "gameparam" {
2013-09-20 11:16:11 -07:00
return nil, errors.New("didn't receive a good gameparam")
}
log.Printf("%+v", gameparam)
err = websocket.JSON.Send(ws, &bot.Config{
gameid,
bot.Stats{
2013-09-07 19:26:21 -07:00
Hp: *hp,
Speed: *speed,
WeaponRadius: *weaponRadius,
ScannerRadius: *scannerRadius,
},
})
var handshake bot.Handshake
websocket.JSON.Receive(ws, &handshake)
if !handshake.Success {
2013-09-20 11:16:11 -07:00
return nil, errors.New("failed to validate correct stats request")
}
log.Printf("%+v", handshake)
2013-09-20 11:16:11 -07:00
return &infos{
id: idreq.AssignedID,
width: gameparam.BoardSize.Width,
height: gameparam.BoardSize.Height,
}, err
}
func main() {
var err error
var gameid string
flag.Parse()
if flag.NArg() < 1 {
gameid, err = createGame()
if err != nil {
log.Fatal(err)
}
} else {
gameid = flag.Arg(0)
}
ws, err := connect()
if err != nil {
log.Fatal(err)
}
2013-09-20 11:16:11 -07:00
board_info, err := negociate(ws, gameid)
if err != nil {
log.Fatal(err)
}
2013-09-20 11:16:11 -07:00
moveto := govector.Point2d{
X: rand.Float64() * board_info.width,
Y: rand.Float64() * board_info.height,
}
log.Printf("%+v", moveto)
var me bot.Robot
for {
var boardstate bot.Boardstate
err = websocket.JSON.Receive(ws, &boardstate)
if err != nil {
log.Fatal("Connection lost")
}
2013-09-20 11:16:11 -07:00
for _, bot := range boardstate.Robots {
if bot.Id == board_info.id {
me = bot
break
}
}
log.Printf("%+v: %+v", me.Position, moveto)
// come up with new location if I am there
if govector.Distance(me.Position, moveto) < 3.0 {
log.Println("finally got there")
moveto = govector.Point2d{
X: rand.Float64() * board_info.width,
Y: rand.Float64() * board_info.height,
}
}
// TODO: send instructions
2013-09-20 11:16:11 -07:00
err = websocket.JSON.Send(ws, bot.Instruction{
MoveTo: &moveto,
})
if err != nil {
log.Fatal(err)
}
}
}