simple spectator that says hi every message

This commit is contained in:
sm
2014-05-11 21:45:26 -07:00
parent 2d8b878f50
commit 7d77f18470
4 changed files with 114 additions and 32 deletions
+38 -31
View File
@@ -46,7 +46,7 @@ type decoder interface {
}
// Negociate runs through the hackerbots negociation protocol.
func (c *Client) Negociate() (err error) {
func (c *Client) Negociate(clientType string) (err error) {
if c.Verbose {
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,
Useragent: "gobot",
Type: "robot",
Type: clientType,
})
if err != nil {
return err
@@ -114,38 +114,45 @@ func (c *Client) Negociate() (err error) {
c.dec = gob.NewDecoder(c.ws)
}
conf := server.ClientConfig{
ID: c.GameId,
Stats: map[string]server.StatsRequest{
c.Name: c.StatsReq,
},
}
switch clientType {
case "robot":
conf := server.ClientConfig{
ID: c.GameId,
Stats: map[string]server.StatsRequest{
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)
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)
}
if c.Verbose {
log.Printf("%s: handshake: %+v", c.Name, handshake)
}
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)
}
if c.Verbose {
log.Printf("%s: handshake: %+v", c.Name, handshake)
}
// 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
// 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":
}
return nil