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 ( import (
"bufio" "bufio"
"encoding/gob" "encoding/gob"
"encoding/json"
"fmt" "fmt"
"log" "log"
"net" "net"
"os" "os"
"strconv"
"strings"
"mcquay.me/robo" "mcquay.me/robo"
) )
@ -22,7 +21,6 @@ func main() {
} }
var err error var err error
var x, y float64
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", os.Args[1], robo.Port)) conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", os.Args[1], robo.Port))
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
@ -32,26 +30,12 @@ func main() {
enc := gob.NewEncoder(conn) enc := gob.NewEncoder(conn)
s := bufio.NewScanner(os.Stdin) s := bufio.NewScanner(os.Stdin)
point := robo.Point{} e := robo.Event{}
for s.Scan() { for s.Scan() {
txt := s.Text() json.Unmarshal(s.Bytes(), &e)
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
log.Printf("sending: %+v\n", point) log.Printf("sending: %+v\n", e)
err = enc.Encode(point) err = enc.Encode(e)
if err != nil { if err != nil {
break break
} }

View File

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

12
robo.go
View File

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