initial implementation, no gameplay, but working handshake
This commit is contained in:
parent
5f4660c827
commit
e7529e43d1
121
main.go
Normal file
121
main.go
Normal file
@ -0,0 +1,121 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bitbucket.org/hackerbots/bot"
|
||||
"code.google.com/p/go.net/websocket"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
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 {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&g); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return g.Id, err
|
||||
}
|
||||
|
||||
func negociate(ws *websocket.Conn, gameid string) (err error) {
|
||||
log.Printf("trying to connect to game %s", gameid)
|
||||
err = websocket.JSON.Send(ws, bot.GameID{
|
||||
gameid,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var idreq bot.IdRequest
|
||||
err = websocket.JSON.Receive(ws, &idreq)
|
||||
if err != nil || idreq.Type == "failure" {
|
||||
return 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 {
|
||||
return err
|
||||
}
|
||||
|
||||
var gameparam bot.GameParam
|
||||
err = websocket.JSON.Receive(ws, &gameparam)
|
||||
log.Printf("gameparam: %+v", gameparam)
|
||||
if gameparam.Type != "gameparam" {
|
||||
return errors.New("didn't receive a good gameparam")
|
||||
}
|
||||
log.Printf("%+v", gameparam)
|
||||
|
||||
err = websocket.JSON.Send(ws, &bot.Config{
|
||||
gameid,
|
||||
bot.Stats{
|
||||
// TODO: make these flags
|
||||
Hp: 200,
|
||||
Speed: 100,
|
||||
WeaponRadius: 50,
|
||||
ScannerRadius: 50,
|
||||
},
|
||||
})
|
||||
|
||||
var handshake bot.Handshake
|
||||
websocket.JSON.Receive(ws, &handshake)
|
||||
if !handshake.Success {
|
||||
return errors.New("failed to validate correct stats request")
|
||||
}
|
||||
log.Printf("%+v", handshake)
|
||||
return 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)
|
||||
}
|
||||
if err = negociate(ws, gameid); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
var boardstate bot.Boardstate
|
||||
err = websocket.JSON.Receive(ws, &boardstate)
|
||||
if err != nil {
|
||||
log.Fatal("Connection lost")
|
||||
}
|
||||
// log.Printf("%+v", boardstate)
|
||||
// TODO: logic
|
||||
// TODO: send instructions
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user