-
Notifications
You must be signed in to change notification settings - Fork 0
/
players.go
91 lines (78 loc) · 2.16 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package bpi
import (
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
)
type Player struct {
FirstName string
LastName string
PersonID string
TeamID string
Jersey int
IsActive bool
Pos string
HeightFeet int
HeightInches int
// Chose to store these as strings instead of float just to avoid rounding issues
HeightMeters string
WeightPounds string
WeightKilograms string
// Store the date as a string, up to consumer to convert
DateOfBirthUTC string
NBADebutYear string
YearsPro string
CollegeName string
LastAffiliation string
Country string
// Which league the player is in. Values are: {standard, africa, sacramento, vegas, utah}
League string
// Nested structs
Teams []PlayerTeamData
Draft PlayerDraftData
}
// This struct holds the information for where the player played between SeasonStart
// and SeasonEnd years.
type PlayerTeamData struct {
TeamID string
SeasonStart string
SeasonEnd string
}
// This struct holds the information for where the player was drafted.
type PlayerDraftData struct {
TeamID string
PickNum string
RoundNum string
SeasonYear string
}
// Load all the players for a given year. Note this will load the NBA players,
// summer league players, and the African league players.
func Players(year string) ([]Player, error) {
all_players := []Player{}
raw_json, err := MakeRequest(fmt.Sprintf("/prod/v1/%s/players.json", year))
if err != nil {
return all_players, err
}
// Unpeel top level (league)
var result map[string]interface{}
json.Unmarshal([]byte(raw_json), &result)
player_json := result["league"].(map[string]interface{})
for league_type, players := range player_json {
for _, player_data := range players.([]interface{}) {
player := Player{}
mapstructure.WeakDecode(player_data, &player)
player.League = league_type
all_players = append(all_players, player)
}
}
return all_players, nil
}
//
func FilterPlayers(players []Player, test func(Player) bool) (filtered_players []Player) {
for _, player := range players {
if test(player) {
filtered_players = append(filtered_players, player)
}
}
return
}