-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.go
87 lines (70 loc) · 1.52 KB
/
statistics.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
76
77
78
79
80
81
82
83
84
85
86
87
package main
import "sort"
type Statistics struct {
wins map[string]uint64
loses map[string]uint64
}
type Statistic struct {
Username string
Wins uint64
Loses uint64
}
func NewStatistics() *Statistics {
return &Statistics{
wins: make(map[string]uint64),
loses: make(map[string]uint64),
}
}
func (s *Statistics) AddStatistic(winner, loser string) {
if val, ok := s.wins[winner]; ok {
s.wins[winner] = val + 1
} else {
s.wins[winner] = 1
}
if val, ok := s.loses[loser]; ok {
s.loses[loser] = val + 1
} else {
s.loses[loser] = 1
}
}
func (s *Statistics) GetStatistics() []Statistic {
stats := make(map[string]Statistic, 0)
for username, winCount := range s.wins {
if _, ok := stats[username]; ok {
stats[username] = Statistic{
Username: username,
Wins: winCount,
Loses: 0,
}
} else {
stat := stats[username]
stat.Username = username
stat.Wins = winCount
stats[username] = stat
}
}
for username, loseCount := range s.loses {
if _, ok := stats[username]; ok {
stats[username] = Statistic{
Username: username,
Wins: 0,
Loses: loseCount,
}
} else {
stat := stats[username]
stat.Loses = loseCount
stat.Username = username
stats[username] = stat
}
}
// only return values from stats, not the keys
result := make([]Statistic, 0)
for _, v := range stats {
result = append(result, v)
}
// sort result for highest wins
sort.Slice(result, func(i, j int) bool {
return result[i].Wins > result[j].Wins
})
return result
}