Browse Source

switch from single point to Event

master
Stephen McQuay 6 years ago
parent
commit
5068159a31
3 changed files with 17 additions and 23 deletions
  1. +5
    -21
      cmd/rclient/main.go
  2. +1
    -1
      cmd/rserver/main.go
  3. +11
    -1
      robo.go

+ 5
- 21
cmd/rclient/main.go 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
}

+ 1
- 1
cmd/rserver/main.go 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

+ 11
- 1
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)
}

Loading…
Cancel
Save