Moved enc/dec definition out of play func
This commit is contained in:
parent
d0c3b5a117
commit
6331803887
25
client.go
25
client.go
@ -29,6 +29,8 @@ type Client struct {
|
||||
Game botserv.GameParam
|
||||
|
||||
boardstate botserv.Boardstate
|
||||
enc encoder
|
||||
dec decoder
|
||||
ws *websocket.Conn
|
||||
}
|
||||
|
||||
@ -100,6 +102,14 @@ func (c *Client) Negociate() (err error) {
|
||||
log.Printf("%s: game parameters: %+v", c.Name, c.Game)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
conf := botserv.ClientConfig{
|
||||
ID: c.GameId,
|
||||
Stats: map[string]botserv.StatsRequest{
|
||||
@ -140,27 +150,16 @@ func (c *Client) Negociate() (err error) {
|
||||
func (c *Client) Play() error {
|
||||
log.Printf("%s: starting loop", c.Name)
|
||||
|
||||
var enc encoder
|
||||
var dec decoder
|
||||
|
||||
if c.Game.Encoding == "json" {
|
||||
enc = json.NewEncoder(c.ws)
|
||||
dec = json.NewDecoder(c.ws)
|
||||
} else {
|
||||
enc = gob.NewEncoder(c.ws)
|
||||
dec = gob.NewDecoder(c.ws)
|
||||
}
|
||||
|
||||
var err error
|
||||
for {
|
||||
err = dec.Decode(&c.boardstate)
|
||||
err = c.dec.Decode(&c.boardstate)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("%s: Connection likely lost: %s", c.Name, err))
|
||||
}
|
||||
c.Player.Recv(&c.boardstate)
|
||||
|
||||
instruction := c.Player.Instruction()
|
||||
err = enc.Encode(instruction)
|
||||
err = c.enc.Encode(instruction)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user