Implemented multiple encoders/decoders
I really think we're close here; once we have gob server-side this might just work.
This commit is contained in:
parent
dc7db48201
commit
839b5789e3
31
robot.go
31
robot.go
@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -45,6 +47,14 @@ type robot struct {
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
type encoder interface {
|
||||
Encode(v interface{}) error
|
||||
}
|
||||
|
||||
type decoder interface {
|
||||
Decode(v interface{}) error
|
||||
}
|
||||
|
||||
func (r *robot) negociate() (err error) {
|
||||
if *verbose {
|
||||
log.Printf("%s: trying to connect to game '%s'", r.name, r.game.Name)
|
||||
@ -158,19 +168,30 @@ func (r *robot) play() {
|
||||
|
||||
log.Printf("%s: starting loop", r.name)
|
||||
|
||||
var enc encoder
|
||||
var dec decoder
|
||||
|
||||
if r.game.Encoding == "json" {
|
||||
enc = json.NewEncoder(r.ws)
|
||||
dec = json.NewDecoder(r.ws)
|
||||
} else {
|
||||
enc = gob.NewEncoder(r.ws)
|
||||
dec = gob.NewDecoder(r.ws)
|
||||
}
|
||||
|
||||
for {
|
||||
r.speed = float32(maxSpeed)
|
||||
if *verbose {
|
||||
log.Printf("%s: about to wait on boardstate", r.name)
|
||||
}
|
||||
err = websocket.JSON.Receive(r.ws, &r.boardstate)
|
||||
if *verbose {
|
||||
log.Printf("%s: one recv boardstate", r.name)
|
||||
}
|
||||
err = dec.Decode(&r.boardstate)
|
||||
if err != nil {
|
||||
log.Printf("%s: Connection lost", r.name)
|
||||
return
|
||||
}
|
||||
if *verbose {
|
||||
log.Printf("%s: one recv boardstate", r.name)
|
||||
}
|
||||
// TODO: I need a truly-verbose flag?
|
||||
// if *verbose {
|
||||
// log.Printf("\n\n%#v\n\n", r.boardstate)
|
||||
@ -199,7 +220,7 @@ func (r *robot) play() {
|
||||
FireAt: r.fireat,
|
||||
},
|
||||
}
|
||||
err = websocket.JSON.Send(r.ws, instruction)
|
||||
err = enc.Encode(instruction)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user