simple spectator that says hi every message

This commit is contained in:
Stephen McQuay 2014-04-26 14:50:58 -07:00
parent 2d8b878f50
commit 7d77f18470
4 changed files with 114 additions and 32 deletions

50
bspect/main.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"flag"
"log"
"math/rand"
"time"
"bitbucket.org/hackerbots/client"
)
var serverHostname = flag.String("server", "localhost", "server hostname")
var port = flag.Int("port", 8666, "server port")
var verbose = flag.Bool("verbose", false, "run verbosly")
var forceJSON = flag.Bool("json", false, "force json encoding")
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
rand.Seed(time.Now().UnixNano())
var gameId string
flag.Parse()
if flag.NArg() < 1 {
gameId = "debug"
} else {
gameId = flag.Arg(0)
}
c := &client.Client{
Server: *serverHostname,
Port: *port,
Name: "bspect",
GameId: gameId,
Verbose: *verbose,
ForceJSON: *forceJSON,
}
var err error
err = c.Negociate("spectator")
if err != nil {
log.Fatalf("%s: failed to negociate: %s", c.Name, err)
}
c.Player = client.NewSimpleSpectator(
c.Game.BoardSize.Width,
c.Game.BoardSize.Height,
)
if err := c.Play(); err != nil {
log.Fatal(err)
}
}

View File

@ -46,7 +46,7 @@ type decoder interface {
} }
// Negociate runs through the hackerbots negociation protocol. // Negociate runs through the hackerbots negociation protocol.
func (c *Client) Negociate() (err error) { func (c *Client) Negociate(clientType string) (err error) {
if c.Verbose { if c.Verbose {
log.Printf("%s: trying to connect to game '%s'", c.Name, c.GameId) log.Printf("%s: trying to connect to game '%s'", c.Name, c.GameId)
} }
@ -83,7 +83,7 @@ func (c *Client) Negociate() (err error) {
}{ }{
Name: c.Name, Name: c.Name,
Useragent: "gobot", Useragent: "gobot",
Type: "robot", Type: clientType,
}) })
if err != nil { if err != nil {
return err return err
@ -114,12 +114,17 @@ func (c *Client) Negociate() (err error) {
c.dec = gob.NewDecoder(c.ws) c.dec = gob.NewDecoder(c.ws)
} }
switch clientType {
case "robot":
conf := server.ClientConfig{ conf := server.ClientConfig{
ID: c.GameId, ID: c.GameId,
Stats: map[string]server.StatsRequest{ Stats: map[string]server.StatsRequest{
c.Name: c.StatsReq, c.Name: c.StatsReq,
}, },
} }
if c.Verbose {
log.Printf("%s: client config: %+v", c.Name, conf)
}
err = websocket.JSON.Send(c.ws, conf) err = websocket.JSON.Send(c.ws, conf)
@ -147,6 +152,8 @@ func (c *Client) Negociate() (err error) {
if err != nil { if err != nil {
return err return err
} }
case "spectator":
}
return nil return nil
} }

View File

@ -60,7 +60,7 @@ func main() {
ForceJSON: *forceJSON, ForceJSON: *forceJSON,
} }
var err error var err error
err = c.Negociate() err = c.Negociate("robot")
if err != nil { if err != nil {
log.Fatalf("%s: failed to negociate: %s", c.Name, err) log.Fatalf("%s: failed to negociate: %s", c.Name, err)
} }

25
spectator.go Normal file
View File

@ -0,0 +1,25 @@
package client
import (
"log"
"bitbucket.org/hackerbots/server"
)
type SimpleSpectator struct {
width, height float64
}
// NewSimpleSpectator simply returns a populated, usable *SimplePlayer
func NewSimpleSpectator(width, height float64) *SimpleSpectator {
return &SimpleSpectator{
width: width,
height: height,
}
}
// Recv is our implementation of receiving a server.Boardstate from the server
func (p *SimpleSpectator) Update(bs *server.Boardstate) map[string]server.Instruction {
log.Println("hi")
return nil
}