sort the all bots list

This commit is contained in:
Fraser Graham 2013-11-06 21:12:19 -08:00
parent 855b8518b8
commit cd9bb45ddc
2 changed files with 18 additions and 0 deletions

View File

@ -160,6 +160,7 @@ func (g *game) tick(payload *Boardstate) int {
func (g *game) send_update(payload *Boardstate) { func (g *game) send_update(payload *Boardstate) {
// Ensure that the robots are always sent in a consistent order // Ensure that the robots are always sent in a consistent order
sort.Sort(RobotSorter{Robots: payload.OtherRobots}) sort.Sort(RobotSorter{Robots: payload.OtherRobots})
sort.Sort(AllRobotSorter{Robots: payload.AllBots})
for p := range g.players { for p := range g.players {
// Copy the payload but only add the robots in scanner range // Copy the payload but only add the robots in scanner range

View File

@ -56,6 +56,23 @@ func (s RobotSorter) Less(i, j int) bool {
return s.Robots[i].Id < s.Robots[j].Id return s.Robots[i].Id < s.Robots[j].Id
} }
// TODO - how do I not duplicate this code???
type AllRobotSorter struct {
Robots []BotHealth
}
func (s AllRobotSorter) Len() int {
return len(s.Robots)
}
func (s AllRobotSorter) Swap(i, j int) {
s.Robots[i], s.Robots[j] = s.Robots[j], s.Robots[i]
}
func (s AllRobotSorter) Less(i, j int) bool {
return s.Robots[i].RobotId < s.Robots[j].RobotId
}
type Stats struct { type Stats struct {
Hp int `json:"-"` Hp int `json:"-"`
Speed float32 `json:"-"` Speed float32 `json:"-"`