removed unecessary data from payload sent to the client and filter outgoing robot list by scanner contents for each player until they die
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
v "bitbucket.org/hackerbots/vector"
|
||||
// "encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"sort"
|
||||
@@ -20,12 +21,6 @@ type Boardstate struct {
|
||||
Turn int `json:"turn"`
|
||||
}
|
||||
|
||||
func (bs *Boardstate) EmptySlices() {
|
||||
bs.Robots = bs.Robots[:0]
|
||||
bs.Projectiles = bs.Projectiles[:0]
|
||||
bs.Splosions = bs.Splosions[:0]
|
||||
}
|
||||
|
||||
func NewBoardstate() *Boardstate {
|
||||
return &Boardstate{
|
||||
Robots: []Robot{},
|
||||
@@ -35,6 +30,7 @@ func NewBoardstate() *Boardstate {
|
||||
}
|
||||
|
||||
type Scanner struct {
|
||||
Id string `json:"id"`
|
||||
Position v.Point2d `json:"position"`
|
||||
Stats Stats `json:"stats"`
|
||||
}
|
||||
@@ -110,7 +106,6 @@ func NewGame(id string, width, height float32) *game {
|
||||
|
||||
func (g *game) run() {
|
||||
var t0, t1 time.Time
|
||||
payload := NewBoardstate()
|
||||
ticker := time.NewTicker(time.Duration(*tick) * time.Millisecond)
|
||||
for {
|
||||
select {
|
||||
@@ -134,9 +129,7 @@ func (g *game) run() {
|
||||
delete(g.spectators, s)
|
||||
close(s.send)
|
||||
case <-ticker.C:
|
||||
// XXX: This is very racy!! It was at bottom of loop and empty
|
||||
// stuff was going out
|
||||
payload.EmptySlices()
|
||||
payload := NewBoardstate()
|
||||
|
||||
g.turn++
|
||||
payload.Turn = g.turn
|
||||
@@ -152,6 +145,7 @@ func (g *game) run() {
|
||||
|
||||
robots_remaining := 0
|
||||
|
||||
// UPDATE GAME STATE
|
||||
for p := range g.players {
|
||||
if p.Robot.Health > 0 {
|
||||
robots_remaining++
|
||||
@@ -199,8 +193,31 @@ func (g *game) run() {
|
||||
log.Printf("Turn Processes %v\n", t1.Sub(t0))
|
||||
}
|
||||
|
||||
// SEND THE UPDATE TO EACH PLAYER
|
||||
for p := range g.players {
|
||||
p.send <- payload
|
||||
// Copy the payload but only add the robots in scanner range
|
||||
player_payload := NewBoardstate()
|
||||
player_payload.Projectiles = payload.Projectiles
|
||||
player_payload.Splosions = payload.Splosions
|
||||
player_payload.Turn = payload.Turn
|
||||
player_payload.Reset = payload.Reset
|
||||
player_payload.Robots = append(player_payload.Robots, p.Robot)
|
||||
|
||||
if p.Robot.Health > 0 {
|
||||
for player := range g.players {
|
||||
for _, scan_entry := range p.Robot.Scanners {
|
||||
if player.Robot.Id == scan_entry.Id {
|
||||
player_payload.Robots = append(player_payload.Robots, player.Robot)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
player_payload.Robots = payload.Robots
|
||||
}
|
||||
|
||||
// x, _ := json.Marshal(player_payload)
|
||||
// log.Printf("%v", string(x))
|
||||
p.send <- player_payload
|
||||
}
|
||||
for s := range g.spectators {
|
||||
s.send <- payload
|
||||
|
||||
Reference in New Issue
Block a user