-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
284 lines (262 loc) · 11.3 KB
/
player.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package gonhl
import (
"encoding/json"
"fmt"
)
const endpointPlayer = "/people/%d"
const endpointPlayerStats = "/people/%d/stats/"
const endpointStatTypes = "/statTypes"
// GetPlayer retrieves information about a single NHL player using a player ID.
func (c *Client) GetPlayer(id int) (Player, int) {
var player struct {
People []Player `json:"people"`
}
status := c.makeRequest(fmt.Sprintf(endpointPlayer, id), nil, &player)
return player.People[0], status
}
// GetPlayerStats retrieves stats about a single NHL player based on PlayerParams.
// The PlayerParams must not be nil and all fields must be set (id, season, statType).
// To determine if a skater or goalie is retrieved, use IsPlayerGoalie.
// Stats must be casted to appropriate type. Types can be determined using the DisplayName.
// Internally this method takes the retrieved player stats json from the api, unmarshals them, then reinserts into the parent struct.
// The parent struct holds an interface{} type and requires reflection to access the proper values of the stat.
// The proper types can be converted to using ConvertPlayerStatsToSkaterStats and ConvertStatsToGoalieStats.
func (c *Client) GetPlayerStats(params *PlayerParams) ([]PlayerStatsForType, int) {
var playerStats struct {
Stats []playerStatsForType `json:"stats"`
}
status := c.makeRequest(fmt.Sprintf(endpointPlayerStats, params.id), parseParams(params), &playerStats)
parsedStats := make([]PlayerStatsForType, len(playerStats.Stats))
for statType, stat := range playerStats.Stats {
parsedStats[statType].ID = params.id
parsedStats[statType].Type = stat.Type
parsedStats[statType].Splits = make([]StatsSplit, len(stat.Splits))
for splitType, split := range stat.Splits {
switch stat.Type.DisplayName {
case "regularSeasonStatRankings":
var testmap map[string]string
json.Unmarshal(*split.Stat, &testmap)
if _, ok := testmap["rankGoals"]; ok {
var skaterStat SkaterStatsByRank
json.Unmarshal(*split.Stat, &skaterStat)
parsedStats[statType].Splits[splitType].Stat = skaterStat
} else {
var goalieStat GoalieStatsByRank
json.Unmarshal(*split.Stat, &goalieStat)
parsedStats[statType].Splits[splitType].Stat = goalieStat
}
case "goalsByGameSituation":
var testmap map[string]int
json.Unmarshal(*split.Stat, &testmap)
if _, ok := testmap["gameWinningGoals"]; ok {
var skaterStat SkaterGoalsBySituation
json.Unmarshal(*split.Stat, &skaterStat)
parsedStats[statType].Splits[splitType].Stat = skaterStat
} else {
var goalieStat GoalsBySituation
json.Unmarshal(*split.Stat, &goalieStat)
parsedStats[statType].Splits[splitType].Stat = goalieStat
}
default:
var testmap map[string]interface{}
json.Unmarshal(*split.Stat, &testmap)
if _, ok := testmap["faceOffPct"]; ok {
var skaterStat SkaterStats
json.Unmarshal(*split.Stat, &skaterStat)
parsedStats[statType].Splits[splitType].Stat = skaterStat
} else {
var goalieStat GoalieStats
json.Unmarshal(*split.Stat, &goalieStat)
parsedStats[statType].Splits[splitType].Stat = goalieStat
}
}
parsedStats[statType].Splits[splitType].internalStatsSplit = split.internalStatsSplit
}
}
return parsedStats, status
}
// GetPlayerStatsTypes retrieves information about the various enums that can be used when retrieving player stats.
// Pass values retrieved from here to SetStat for PlayerParams.
func (c *Client) GetPlayerStatsTypes() ([]string, int) {
var statTypes []StatType
status := c.makeRequest(endpointStatTypes, nil, &statTypes)
statTypesString := make([]string, len(statTypes))
for index, value := range statTypes {
statTypesString[index] = value.DisplayName
}
return statTypesString, status
}
type Player struct {
ID int `json:"id"`
FullName string `json:"fullName"`
Link string `json:"link"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
PrimaryNumber int `json:"primaryNumber,string"`
BirthDate JsonDate `json:"birthDate"`
CurrentAge int `json:"currentAge"`
BirthCity string `json:"birthCity"`
BirthStateProvince string `json:"birthStateProvince"`
BirthCountry string `json:"birthCountry"`
Nationality string `json:"nationality"`
Height Height `json:"height,string"`
Weight int `json:"weight"`
Active bool `json:"active"`
AlternateCaptain bool `json:"alternateCaptain"`
Captain bool `json:"captain"`
Rookie bool `json:"rookie"`
ShootsCatches string `json:"shootsCatches"`
RosterStatus string `json:"rosterStatus"`
CurrentTeam Team `json:"currentTeam"`
PrimaryPosition Position `json:"primaryPosition"`
}
type Position struct {
Code string `json:"code"`
Name string `json:"name"`
Type string `json:"type"`
Abbreviation string `json:"abbreviation"`
}
type PlayerStatsForType struct {
ID int
Type StatType `json:"type"`
Splits []StatsSplit `json:"splits"`
}
type playerStatsForType struct {
Type StatType `json:"type"`
Splits []struct {
Stat *json.RawMessage `json:"stat"`
internalStatsSplit
} `json:"splits"`
}
type internalStatsSplit struct {
Season string `json:"season"`
IsHome *bool `json:"isHome"`
IsWin *bool `json:"isWin"`
IsOT *bool `json:"isOT"`
Month *int `json:"month"`
Opponent StatSplitIdentifier `json:"opponent"`
OpponentDivision StatSplitIdentifier `json:"opponentDivision"`
OpponentConference StatSplitIdentifier `json:"opponentConference"`
}
type StatsSplit struct {
Stat interface{} `json:"stat"`
internalStatsSplit
}
type StatSplitIdentifier struct {
ID int `json:"id"`
Name string `json:"name"`
Link string `json:"link"`
}
type StatType struct {
DisplayName string `json:"displayName"`
}
// PlayerStats holds values that are used in both SkaterStats and GoalieStats.
// Only used as an anonymous struct.
type PlayerStats struct {
TimeOnIce string `json:"timeOnIce"`
Games int `json:"games"`
TimeOnIcePerGame string `json:"timeOnIcePerGame"`
}
type SkaterStats struct {
PlayerStats
Assists int `json:"assists"`
Goals int `json:"goals"`
Pim int `json:"pim"`
Shots int `json:"shots"`
Hits int `json:"hits"`
PowerPlayGoals int `json:"powerPlayGoals"`
PowerPlayPoints int `json:"powerPlayPoints"`
PowerPlayTimeOnIce string `json:"powerPlayTimeOnIce"`
EvenTimeOnIce string `json:"evenTimeOnIce"`
PenaltyMinutes int `json:"penaltyMinutes,string"`
FaceOffPct float64 `json:"faceOffPct"`
ShotPct float64 `json:"shotPct"`
GameWinningGoals int `json:"gameWinningGoals"`
OverTimeGoals int `json:"overTimeGoals"`
ShortHandedGoals int `json:"shortHandedGoals"`
ShortHandedPoints int `json:"shortHandedPoints"`
ShortHandedTimeOnIce string `json:"shortHandedTimeOnIce"`
Blocked int `json:"blocked"`
PlusMinus int `json:"plusMinus"`
Points int `json:"points"`
Shifts int `json:"shifts"`
EvenTimeOnIcePerGame string `json:"evenTimeOnIcePerGame"`
ShortHandedTimeOnIcePerGame string `json:"shortHandedTimeOnIcePerGame"`
PowerPlayTimeOnIcePerGame string `json:"powerPlayTimeOnIcePerGame"`
}
type GoalieStats struct {
PlayerStats
Ot int `json:"ot"`
Shutouts int `json:"shutouts"`
Ties int `json:"ties"`
Wins int `json:"wins"`
Losses int `json:"losses"`
Saves int `json:"saves"`
PowerPlaySaves int `json:"powerPlaySaves"`
ShortHandedSaves int `json:"shortHandedSaves"`
EvenSaves int `json:"evenSaves"`
ShortHandedShots int `json:"shortHandedShots"`
EvenShots int `json:"evenShots"`
PowerPlayShots int `json:"powerPlayShots"`
SavePercentage float64 `json:"savePercentage"`
GoalAgainstAverage float64 `json:"goalAgainstAverage"`
GamesStarted int `json:"gamesStarted"`
ShotsAgainst int `json:"shotsAgainst"`
GoalsAgainst int `json:"goalsAgainst"`
PowerPlaySavePercentage float64 `json:"powerPlaySavePercentage"`
ShortHandedSavePercentage float64 `json:"shortHandedSavePercentage"`
EvenStrengthSavePercentage float64 `json:"evenStrengthSavePercentage"`
}
type SkaterStatsByRank struct {
RankPowerPlayGoals string `json:"rankPowerPlayGoals"`
RankBlockedShots string `json:"rankBlockedShots"`
RankAssists string `json:"rankAssists"`
RankShotPct string `json:"rankShotPct"`
RankGoals string `json:"rankGoals"`
RankHits string `json:"rankHits"`
RankPenaltyMinutes string `json:"rankPenaltyMinutes"`
RankShortHandedGoals string `json:"rankShortHandedGoals"`
RankPlusMinus string `json:"rankPlusMinus"`
RankShots string `json:"rankShots"`
RankPoints string `json:"rankPoints"`
RankOvertimeGoals string `json:"rankOvertimeGoals"`
RankGamesPlayed string `json:"rankGamesPlayed"`
}
type GoalieStatsByRank struct {
ShotsAgainst string `json:"shotsAgainst"`
Ot string `json:"ot"`
PenaltyMinutes string `json:"penaltyMinutes"`
TimeOnIce string `json:"timeOnIce"`
ShutOuts string `json:"shutOuts"`
Saves string `json:"saves"`
Losses string `json:"losses"`
GoalsAgainst string `json:"goalsAgainst"`
SavePercentage string `json:"savePercentage"`
Games string `json:"games"`
GoalsAgainstAverage string `json:"goalsAgainstAverage"`
Wins string `json:"wins"`
}
// GoalsBySituation holds all the relevant information for goalies
// and most information for skaters.
type GoalsBySituation struct {
GoalsInFirstPeriod int `json:"goalsInFirstPeriod"`
GoalsInSecondPeriod int `json:"goalsInSecondPeriod"`
GoalsInThirdPeriod int `json:"goalsInThirdPeriod"`
GoalsInOvertime int `json:"goalsInOvertime"`
ShootOutGoals int `json:"shootOutGoals"`
ShootOutShots int `json:"shootOutShots"`
GoalsTrailingByOne int `json:"goalsTrailingByOne"`
GoalsTrailingByTwo int `json:"goalsTrailingByTwo"`
GoalsTrailingByThreePlus int `json:"goalsTrailingByThreePlus"`
GoalsWhenTied int `json:"goalsWhenTied"`
GoalsLeadingByOne int `json:"goalsLeadingByOne"`
GoalsLeadingByTwo int `json:"goalsLeadingByTwo"`
GoalsLeadingByThreePlus int `json:"goalsLeadingByThreePlus"`
PenaltyGoals int `json:"penaltyGoals"`
PenaltyShots int `json:"penaltyShots"`
}
type SkaterGoalsBySituation struct {
GameWinningGoals int `json:"gameWinningGoals"`
EmptyNetGoals int `json:"emptyNetGoals"`
GoalsBySituation
}