2014-04-23 14:28:06 -07:00
|
|
|
package client
|
2014-04-09 21:35:54 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2014-11-11 21:28:01 -08:00
|
|
|
"golang.org/x/net/websocket"
|
2015-04-30 23:14:17 -07:00
|
|
|
|
|
|
|
"hackerbots.us/server"
|
2014-04-09 21:35:54 -07:00
|
|
|
)
|
|
|
|
|
2016-07-13 18:40:41 -07:00
|
|
|
func connect(addr string) (*websocket.Conn, error) {
|
2014-04-09 21:35:54 -07:00
|
|
|
origin := "http://localhost/"
|
2016-07-13 18:40:41 -07:00
|
|
|
url := fmt.Sprintf("%s/ws/", addr)
|
2014-04-09 21:35:54 -07:00
|
|
|
return websocket.Dial(url, "", origin)
|
|
|
|
}
|
|
|
|
|
2014-04-09 23:11:28 -07:00
|
|
|
// Client keeps track of connection to server and has two interesting methods:
|
2016-07-13 11:31:28 -07:00
|
|
|
// Negotiate and Play. Users of this struct will likely use most everything as
|
2014-04-09 23:11:28 -07:00
|
|
|
// is while defining their own Player to specify desired game play behavior.
|
2014-04-09 21:35:54 -07:00
|
|
|
type Client struct {
|
|
|
|
ForceJSON bool
|
|
|
|
GameId string
|
|
|
|
Name string
|
|
|
|
Server string
|
2014-04-23 14:54:30 -07:00
|
|
|
Game server.GameParam
|
2014-04-09 21:35:54 -07:00
|
|
|
|
2014-05-11 23:13:00 -07:00
|
|
|
boardstate *server.Boardstate
|
2014-04-09 23:10:51 -07:00
|
|
|
enc encoder
|
|
|
|
dec decoder
|
2014-04-09 21:35:54 -07:00
|
|
|
ws *websocket.Conn
|
2015-08-31 21:48:25 -07:00
|
|
|
|
|
|
|
// visualization members
|
|
|
|
width, height float64
|
|
|
|
viewX, viewY int
|
|
|
|
StateStream chan *server.Boardstate
|
|
|
|
Die chan struct{}
|
|
|
|
|
|
|
|
Player
|
2014-04-09 21:35:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type encoder interface {
|
|
|
|
Encode(v interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type decoder interface {
|
|
|
|
Decode(v interface{}) error
|
|
|
|
}
|
|
|
|
|
2016-07-13 11:31:28 -07:00
|
|
|
// Negotiate runs through the hackerbots negociation protocol.
|
2016-07-13 15:04:48 -07:00
|
|
|
func (c *Client) Negotiate(clientType string, player Player) (err error) {
|
2016-07-13 18:40:41 -07:00
|
|
|
c.ws, err = connect(c.Server)
|
2014-04-09 21:35:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("connection failure: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = websocket.JSON.Send(c.ws, struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
|
|
|
c.GameId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var idreq struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
PlayerId string `json:"id"`
|
|
|
|
}
|
|
|
|
err = websocket.JSON.Receive(c.ws, &idreq)
|
|
|
|
if err != nil || idreq.Type == "failure" {
|
|
|
|
return errors.New(fmt.Sprintf("failure: %+v", idreq))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = websocket.JSON.Send(c.ws, struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Useragent string `json:"useragent"`
|
|
|
|
}{
|
|
|
|
Name: c.Name,
|
|
|
|
Useragent: "gobot",
|
2014-04-26 14:50:58 -07:00
|
|
|
Type: clientType,
|
2014-04-09 21:35:54 -07:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
supportedEncs := []string{"bson", "json", "gob"}
|
|
|
|
if c.ForceJSON {
|
|
|
|
supportedEncs = []string{"json"}
|
|
|
|
}
|
|
|
|
err = websocket.JSON.Send(c.ws, supportedEncs)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("failure: %+v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = websocket.JSON.Receive(c.ws, &c.Game)
|
|
|
|
if c.Game.Type != "gameparam" {
|
|
|
|
return errors.New("didn't receive a good gameparam")
|
|
|
|
}
|
|
|
|
|
2014-04-09 23:10:51 -07:00
|
|
|
if c.Game.Encoding == "json" {
|
|
|
|
c.enc = json.NewEncoder(c.ws)
|
|
|
|
c.dec = json.NewDecoder(c.ws)
|
|
|
|
} else {
|
|
|
|
c.enc = gob.NewEncoder(c.ws)
|
|
|
|
c.dec = gob.NewDecoder(c.ws)
|
|
|
|
}
|
|
|
|
|
2014-04-26 14:50:58 -07:00
|
|
|
switch clientType {
|
|
|
|
case "robot":
|
|
|
|
conf := server.ClientConfig{
|
2016-07-13 15:04:48 -07:00
|
|
|
ID: c.GameId,
|
|
|
|
Stats: player.GetStats(),
|
2014-04-26 14:50:58 -07:00
|
|
|
}
|
2014-04-09 21:35:54 -07:00
|
|
|
|
2014-04-26 14:50:58 -07:00
|
|
|
err = websocket.JSON.Send(c.ws, conf)
|
2014-04-09 21:35:54 -07:00
|
|
|
|
2014-04-26 14:50:58 -07:00
|
|
|
var handshake struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
server.Failure
|
|
|
|
}
|
|
|
|
websocket.JSON.Receive(c.ws, &handshake)
|
|
|
|
if !handshake.Success {
|
|
|
|
return errors.New(handshake.Reason)
|
|
|
|
}
|
2014-04-09 21:35:54 -07:00
|
|
|
|
2014-04-26 14:50:58 -07:00
|
|
|
// we don't do anything useful with dstats, but could be interesting to
|
|
|
|
// pass along to the player?
|
|
|
|
dstats := struct {
|
|
|
|
Stats map[string]server.Stats `json:"stats"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{}
|
|
|
|
err = websocket.JSON.Receive(c.ws, &dstats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "spectator":
|
2014-04-09 21:35:54 -07:00
|
|
|
}
|
|
|
|
|
2015-08-31 21:48:25 -07:00
|
|
|
c.width = c.Game.BoardSize.Width
|
|
|
|
c.height = c.Game.BoardSize.Height
|
|
|
|
|
2014-04-09 21:35:54 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-23 14:54:30 -07:00
|
|
|
// Play contains the main game run loop. It gets a server.Boardstate from the
|
2014-04-09 23:11:28 -07:00
|
|
|
// server, and passes this along to the embedded Player. Following this it
|
|
|
|
// sends the server the Player's instruction.
|
2014-04-09 21:35:54 -07:00
|
|
|
func (c *Client) Play() error {
|
2014-05-11 23:13:00 -07:00
|
|
|
bs := &server.Boardstate{}
|
2014-04-09 21:35:54 -07:00
|
|
|
var err error
|
|
|
|
for {
|
2014-05-11 23:13:00 -07:00
|
|
|
err = c.dec.Decode(bs)
|
2014-04-09 21:35:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("%s: Connection likely lost: %s", c.Name, err))
|
|
|
|
}
|
2016-02-23 16:57:35 -08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case c.StateStream <- bs:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:48:25 -07:00
|
|
|
err = c.enc.Encode(c.Update(bs))
|
2014-04-09 21:35:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|