-
Notifications
You must be signed in to change notification settings - Fork 4
/
player_sort_steps.go
64 lines (53 loc) · 1.27 KB
/
player_sort_steps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"context"
"sort"
"sync"
)
type StepSorter struct {
p Player
}
func (ss StepSorter) Sort(ctx context.Context) []Player {
ctx, span := tracer.Start(ctx, "Player.SortPlayerByStepsRequired")
defer span.End()
var wg sync.WaitGroup
pairChan := make(chan dPair, len(ss.p.Game.LeaderBoard))
// TODO Instead of iterating over all players, use bfs??
for _, ps := range ss.p.Game.LeaderBoard {
wg.Add(1)
go func(ps PlayerState) {
defer wg.Done()
otherPlayerPt := Point{ps.X, ps.Y}
if ss.p.GetPosition().Equal(otherPlayerPt) {
return
}
aStar := NewAStar(ss.p.Game.Arena)
path, err := aStar.SearchPath(ctx, ss.p.GetPosition(), otherPlayerPt)
if err != nil {
return
}
moves := ss.p.RequiredMoves(ctx, path)
pairChan <- dPair{
distance: float64(len(moves)),
player: ps,
}
}(ps)
}
wg.Wait()
close(pairChan)
var dPairs []dPair
for elem := range pairChan {
dPairs = append(dPairs, elem)
}
if len(dPairs) == 0 {
return nil
}
sort.Sort(byDistance(dPairs))
var closestPlayers []Player
for _, dp := range dPairs {
closestPlayer := dp.player
cp := ss.p.Game.GetPlayerByPosition(Point{X: closestPlayer.X, Y: closestPlayer.Y})
closestPlayers = append(closestPlayers, *cp)
}
return closestPlayers
}