From 7e4e6faa0062fb3f1ad93975f57411dda2cec4b0 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 9 Apr 2014 23:11:28 -0700 Subject: [PATCH] Added documentation --- client.go | 7 +++++++ doc.go | 2 ++ player.go | 13 ++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 doc.go diff --git a/client.go b/client.go index 451b3da..ca5d329 100644 --- a/client.go +++ b/client.go @@ -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) diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..2c890e4 --- /dev/null +++ b/doc.go @@ -0,0 +1,2 @@ +// botclient is a package used to connect to and interact with a hackebots game. +package botclient diff --git a/player.go b/player.go index a988cb0..247c3d1 100644 --- a/player.go +++ b/player.go @@ -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{