client/main.go

266 lines
6.5 KiB
Go
Raw Normal View History

package main
import (
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-28 13:10:25 -07:00
"time"
)
2013-10-19 00:30:20 -07:00
var hp = flag.Int("hp", 50, "")
var speed = flag.Float64("speed", 50, "")
var acceleration = flag.Float64("acceleration", 50, "")
var weaponRadius = flag.Int("wrad", 50, "weapon radius")
var scannerRadius = flag.Int("srad", 50, "scanner radius")
var fireRate = flag.Int("fire-rate", 50, "scanner radius")
2013-10-14 00:08:28 -07:00
var server = flag.String("server", "hackerbots.us", "server hostname")
2013-09-27 00:04:25 -07:00
var port = flag.Int("port", 8666, "server port")
var botname = flag.String("name", "gobot", "the name that other players will see")
2013-10-14 08:51:06 -07:00
var verbose = flag.Bool("verbose", false, "run verbosly")
2013-09-07 19:26:21 -07:00
2013-09-20 11:16:11 -07:00
type infos struct {
id string
2013-10-19 21:18:01 -07:00
width, height float32
2013-09-20 11:16:11 -07:00
}
type Stats struct {
Hp int `json:"hp"`
2013-10-19 21:18:01 -07:00
Speed float32 `json:"speed"`
Acceleration float32 `json:"acceleration"`
WeaponRadius int `json:"weapon_radius"`
ScannerRadius int `json:"scanner_radius"`
2013-10-19 00:30:20 -07:00
FireRate int `json:"fire_rate"`
}
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"`
2013-10-19 21:18:01 -07:00
TargetSpeed float32 `json:"speed"`
Speed float32 `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"`
2013-10-19 21:18:01 -07:00
Speed float32 `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/"
2013-09-27 00:04:25 -07:00
url := fmt.Sprintf("ws://%s:%d/ws/", *server, *port)
return websocket.Dial(url, "", origin)
}
func createGame() (string, error) {
2013-09-28 14:33:51 -07:00
url := fmt.Sprintf("http://%s:%d/game/start/", *server, *port)
resp, err := http.Get(url)
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, struct {
Id string `json:"id"`
}{
gameid,
})
if err != nil {
2013-09-20 11:16:11 -07:00
return nil, err
}
var idreq struct {
Type string `json:"type"`
AssignedID string `json:"id"`
}
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, struct {
Type string `json:"type"`
Name string `json:"name"`
Useragent string `json:"useragent"`
}{
2013-09-27 00:04:25 -07:00
Name: *botname,
Useragent: "gobot",
Type: "robot",
})
if err != nil {
2013-09-20 11:16:11 -07:00
return nil, err
}
type BoardSize struct {
2013-10-19 21:18:01 -07:00
Width float32 `json:"width"`
Height float32 `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" {
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, &struct {
ID string `json:"id"`
Name string `json:"name"`
// TODO: candidate for embedding?
Stats Stats `json:"stats"`
}{
2013-09-27 00:04:25 -07:00
ID: gameid,
Name: *botname,
Stats: Stats{
2013-09-07 19:26:21 -07:00
Hp: *hp,
2013-10-19 21:18:01 -07:00
Speed: float32(*speed),
Acceleration: float32(*acceleration),
2013-09-07 19:26:21 -07:00
WeaponRadius: *weaponRadius,
ScannerRadius: *scannerRadius,
2013-10-19 00:30:20 -07:00
FireRate: *fireRate,
},
})
var handshake struct {
ID string `json:"id"`
Success bool `json:"success"`
Type string `json:"type"`
}
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() {
2013-09-28 13:10:25 -07:00
rand.Seed(time.Now().UnixNano())
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-27 00:04:25 -07:00
// TODO: var target govector.Point2d
2013-09-20 11:16:11 -07:00
moveto := govector.Point2d{
2013-10-19 21:18:01 -07:00
X: rand.Float32() * board_info.width,
Y: rand.Float32() * board_info.height,
2013-09-20 11:16:11 -07:00
}
log.Printf("%+v", moveto)
var me Robot
2013-09-20 11:16:11 -07:00
2013-09-27 00:04:25 -07:00
log.Printf("negociated well, starting loop")
for {
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)
2013-10-14 08:51:06 -07:00
if *verbose {
log.Printf("%+v", 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
}
}
if govector.Distance(me.Position, moveto) < 3.0 {
2013-09-28 13:10:25 -07:00
log.Printf("old: %+v: %+v", me.Position, moveto)
2013-09-20 11:16:11 -07:00
moveto = govector.Point2d{
2013-10-19 21:18:01 -07:00
X: rand.Float32() * board_info.width,
Y: rand.Float32() * board_info.height,
2013-09-20 11:16:11 -07:00
}
2013-09-28 13:10:25 -07:00
log.Printf("new: %+v: %+v", me.Position, moveto)
2013-09-20 11:16:11 -07:00
}
// TODO: send instructions
err = websocket.JSON.Send(ws, Instruction{
2013-09-20 11:16:11 -07:00
MoveTo: &moveto,
})
if err != nil {
log.Fatal(err)
}
}
}