switch from single point to Event

This commit is contained in:
Stephen McQuay 2015-02-05 21:34:35 -08:00
parent f4748ee52c
commit 5068159a31
3 changed files with 17 additions and 23 deletions

View File

@ -3,12 +3,11 @@ package main
import (
"bufio"
"encoding/gob"
"encoding/json"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"mcquay.me/robo"
)
@ -22,7 +21,6 @@ func main() {
}
var err error
var x, y float64
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", os.Args[1], robo.Port))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
@ -32,26 +30,12 @@ func main() {
enc := gob.NewEncoder(conn)
s := bufio.NewScanner(os.Stdin)
point := robo.Point{}
e := robo.Event{}
for s.Scan() {
txt := s.Text()
parts := strings.Split(txt, ",")
if len(parts) != 2 {
err = fmt.Errorf("bad parse from stdin: %q", txt)
break
}
x, err = strconv.ParseFloat(parts[0], 32)
if err != nil {
break
}
y, err = strconv.ParseFloat(parts[1], 32)
if err != nil {
break
}
point.X, point.Y = x, y
json.Unmarshal(s.Bytes(), &e)
log.Printf("sending: %+v\n", point)
err = enc.Encode(point)
log.Printf("sending: %+v\n", e)
err = enc.Encode(e)
if err != nil {
break
}

View File

@ -29,7 +29,7 @@ func main() {
dec := gob.NewDecoder(conn)
for {
point := robo.Point{}
point := robo.Event{}
err = dec.Decode(&point)
if err != nil {
break

12
robo.go
View File

@ -4,10 +4,20 @@ import "log"
const Port = 1337
type Point struct {
type Robot struct {
X, Y, W float64
}
type Ball struct {
X, Y float64
}
type Event struct {
Players []Robot `json:"players"`
Enemies []Robot `json:"enemies"`
Ball Ball `json:"ball"`
}
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
}