Added documentation

This commit is contained in:
Stephen McQuay 2014-04-09 23:11:28 -07:00
parent 6331803887
commit 7e4e6faa00
3 changed files with 21 additions and 1 deletions

View File

@ -17,6 +17,9 @@ func connect(server string, port int) (*websocket.Conn, error) {
return websocket.Dial(url, "", origin)
}
// Client keeps track of connection to server and has two interesting methods:
// Negociate and Play. Users of this struct will likely use most everything as
// is while defining their own Player to specify desired game play behavior.
type Client struct {
ForceJSON bool
GameId string
@ -42,6 +45,7 @@ type decoder interface {
Decode(v interface{}) error
}
// Negociate runs through the hackerbots negociation protocol.
func (c *Client) Negociate() (err error) {
if c.Verbose {
log.Printf("%s: trying to connect to game '%s'", c.Name, c.GameId)
@ -147,6 +151,9 @@ func (c *Client) Negociate() (err error) {
return nil
}
// Play contains the main game run loop. It gets a botserv.Boardstate from the
// server, and passes this along to the embedded Player. Following this it
// sends the server the Player's instruction.
func (c *Client) Play() error {
log.Printf("%s: starting loop", c.Name)

2
doc.go Normal file
View File

@ -0,0 +1,2 @@
// botclient is a package used to connect to and interact with a hackebots game.
package botclient

View File

@ -9,11 +9,18 @@ import (
"bitbucket.org/hackerbots/vector"
)
// Player is the interface that is implemented when specifying non-default
// player behavior.
//
// The general case will be to implement a Player type that contains the magic
// required to slay other robots quickly while staying alive for a long time.
type Player interface {
Recv(bs *botserv.Boardstate)
Instruction() map[string]botserv.Instruction
}
// SimplePlayer is our default player and stands as a starting point for your
// own Player implementations.
type SimplePlayer struct {
me botserv.Robot
width, height float32
@ -26,6 +33,7 @@ type SimplePlayer struct {
safeDistance float32
}
// NewSimplePlayer simply returns a populated, usable *SimplePlayer
func NewSimplePlayer(width, height float32) *SimplePlayer {
return &SimplePlayer{
knownObstacles: make(map[string]botserv.Obstacle),
@ -36,6 +44,7 @@ func NewSimplePlayer(width, height float32) *SimplePlayer {
}
}
// Recv is our implementation of receiving a botserv.Boardstate from the server
func (p *SimplePlayer) Recv(bs *botserv.Boardstate) {
p.speed = p.maxSpeed
if len(bs.MyRobots) > 0 {
@ -98,6 +107,8 @@ func (p *SimplePlayer) recon(bs *botserv.Boardstate) {
}
}
// Instruction is our default implementation of preparing a map of information
// to be sent to server.
func (p *SimplePlayer) Instruction() map[string]botserv.Instruction {
return map[string]botserv.Instruction{
p.me.Id: {
@ -149,7 +160,7 @@ func (mo MiniObstacle) String() string {
return mo.Id()
}
// toObstacle is where the conversion magic happens
// ToObstacle is where the conversion magic happens
func (mo *MiniObstacle) ToObstacle() botserv.Obstacle {
return botserv.Obstacle{
Bounds: govector.AABB2d{