forked from jpillora/ssh-tron
/
score.go
75 lines (69 loc) · 1.53 KB
/
score.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
64
65
66
67
68
69
70
71
72
73
74
75
package tron
import "sort"
const (
sidebarWidth = 14
sidebarEntryHeight = 4
)
//scoreboard
type scoreboard struct {
g *Game
changed bool
allPlayersSorted []*Player
}
//compute the score
func (s *scoreboard) compute() {
// pull in player list and sort
sorted := make([]*Player, len(s.g.allPlayers))
i := 0
for _, p := range s.g.allPlayers {
sorted[i] = p
i++
}
sort.Sort(byScore(sorted))
if s.allPlayersSorted == nil {
s.changed = true
}
if len(sorted) > 0 {
//place a rank on all players
last := sorted[0]
if last.index != 0 {
s.changed = true
}
last.rank = 1
for i = 1; i < len(sorted); i++ {
p := sorted[i]
if p.Kills == last.Kills &&
p.Deaths == last.Deaths {
p.rank = last.rank
} else {
p.rank = last.rank + 1
}
if p.index != i {
s.changed = true
}
p.index = i
last = p
}
}
if s.g.bot.connected && s.changed {
s.g.bot.scoreChange(sorted)
}
s.allPlayersSorted = sorted
}
// byScore implements the sort.Interface to sort the players by score.
// The scores are first influenced by kills, then deaths and then names.
type byScore []*Player
func (ps byScore) Len() int { return len(ps) }
func (ps byScore) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] }
func (ps byScore) Less(i, j int) bool {
if ps[i].Kills > ps[j].Kills {
return true
} else if ps[i].Kills < ps[j].Kills {
return false
} else if ps[i].Deaths < ps[j].Deaths {
return true
} else if ps[i].Deaths > ps[j].Deaths {
return false
}
return ps[i].hash < ps[j].hash
}