/
players.go
74 lines (59 loc) · 1.64 KB
/
players.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
package players
import (
"os"
"github.com/gocarina/gocsv"
)
const fileLocation = "./data/players.csv"
type Player struct {
Name string `csv:"Player"`
Goals int `csv:"Performance_Gls"`
Assists int `csv:"Performance.1_Ast"`
PlayingTime int `csv:"Playing Time.1_Min"`
MatchesPlayed int `csv:"MP"`
}
func Compare(p1, p2 Player) bool {
if p1.Goals != p2.Goals {
return p1.Goals > p2.Goals
}
if p1.Assists != p2.Assists {
return p1.Assists > p2.Assists
}
if p1.MatchesPlayed != p2.MatchesPlayed {
return p1.MatchesPlayed > p2.MatchesPlayed
}
if p1.PlayingTime != p2.PlayingTime {
return p1.PlayingTime > p2.PlayingTime
}
return p1.Name < p2.Name
}
func FetchFromDataset() []Player {
csvFile, err := os.Open(fileLocation)
if err != nil {
panic("error opening file: " + err.Error())
}
defer csvFile.Close()
players := []Player{}
if err := gocsv.UnmarshalFile(csvFile, &players); err != nil {
panic(err)
}
// Sum accross player seasons
playerSums := map[string]Player{}
for _, player := range players {
if _, ok := playerSums[player.Name]; !ok {
playerSums[player.Name] = player
continue
}
playerSums[player.Name] = Player{
Name: player.Name,
Goals: playerSums[player.Name].Goals + player.Goals,
Assists: playerSums[player.Name].Assists + player.Assists,
PlayingTime: playerSums[player.Name].PlayingTime + player.PlayingTime,
MatchesPlayed: playerSums[player.Name].MatchesPlayed + player.MatchesPlayed,
}
}
players = make([]Player, 0, len(playerSums))
for _, player := range playerSums {
players = append(players, player)
}
return players
}