-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
game.go
287 lines (238 loc) · 6.73 KB
/
game.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
285
286
287
package nhl
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
sportboard "github.com/robbydyer/sports/internal/board/sport"
)
// LiveGameGetter retrieves a live game from a game link
type LiveGameGetter func(ctx context.Context, link string) (sportboard.Game, error)
// Game implements sportboard.Game
type Game struct {
GameGetter LiveGameGetter
ID int `json:"gamePk"`
Link string `json:"link"`
Teams *struct {
Away *gameTeam `json:"away"`
Home *gameTeam `json:"home"`
} `json:"teams"`
GameTime time.Time
GameDate string `json:"gameDate"`
GameData *struct {
DateTime *struct {
DateTime string `json:"datetime,omitempty"`
} `json:"dateTime,omitempty"`
Status *struct {
AbstractGameState string `json:"abstractGameState,omitempty"`
DetailedState string `json:"detailedState,omitempty"`
} `json:"status,omitempty"`
} `json:"gameData,omitempty"`
LiveData *struct {
Linescore *struct {
Teams *struct {
Home *gameTeam `json:"home"`
Away *gameTeam `json:"away"`
} `json:"teams"`
CurrentPeriod int `json:"currentPeriod,omitempty"`
CurrentPeriodOrdinal string `json:"currentPeriodOrdinal"`
CurrentPeriodTimeRemaining string `json:"currentPeriodTimeRemaining,omitempty"`
} `json:"linescore"`
} `json:"liveData"`
}
type gameTeam struct {
Score int `json:"score,omitempty"`
Team *Team `json:"team"`
Goals int `json:"goals,omitempty"`
ShotsOnGoal int `json:"shotsOnGoal,omitempty"`
PowerPlay bool `json:"powerPlay,omitempty"`
}
type games struct {
Dates []*struct {
Games []*Game `json:"games"`
} `json:"dates"`
}
// Games ...
func (n *NHL) Games(dateStr string) ([]*Game, error) {
games, ok := n.games[dateStr]
if !ok || len(games) == 0 {
return nil, fmt.Errorf("no games for date %s", dateStr)
}
return games, nil
}
// GetStartTime ...
func (g *Game) GetStartTime(ctx context.Context) (time.Time, error) {
return g.GameTime, nil
}
// GetID ...
func (g *Game) GetID() int {
return g.ID
}
// GetLink ...
func (g *Game) GetLink() (string, error) {
return g.Link, nil
}
// IsLive ...
func (g *Game) IsLive() (bool, error) {
complete, err := g.IsComplete()
if err != nil {
return false, err
}
if complete {
return false, nil
}
if g.LiveData != nil && g.LiveData.Linescore != nil && g.LiveData.Linescore.CurrentPeriod > 0 {
return true, nil
}
return false, nil
}
// IsComplete ...
func (g *Game) IsComplete() (bool, error) {
if g.GameData != nil &&
g.GameData.Status != nil &&
strings.Contains(strings.ToLower(g.GameData.Status.AbstractGameState), "final") {
return true, nil
}
if g.LiveData != nil &&
g.LiveData.Linescore != nil &&
strings.Contains(strings.ToLower(g.LiveData.Linescore.CurrentPeriodTimeRemaining), "final") {
return true, nil
}
return false, nil
}
// IsPostponed ...
func (g *Game) IsPostponed() (bool, error) {
if g.GameData != nil &&
g.GameData.Status != nil &&
strings.Contains(strings.ToLower(g.GameData.Status.DetailedState), "postponed") {
return true, nil
}
return false, nil
}
// HomeTeam ...
func (g *Game) HomeTeam() (sportboard.Team, error) {
if g.Teams != nil && g.Teams.Home != nil && g.Teams.Home.Team != nil {
return g.Teams.Home.Team, nil
}
if g.LiveData != nil &&
g.LiveData.Linescore != nil &&
g.LiveData.Linescore.Teams != nil &&
g.LiveData.Linescore.Teams.Home != nil {
g.LiveData.Linescore.Teams.Home.Team.score = g.LiveData.Linescore.Teams.Home.Goals
return g.LiveData.Linescore.Teams.Home.Team, nil
}
return nil, fmt.Errorf("could not locate home team in Game")
}
// AwayTeam ...
func (g *Game) AwayTeam() (sportboard.Team, error) {
if g.Teams != nil && g.Teams.Away != nil && g.Teams.Away.Team != nil {
return g.Teams.Away.Team, nil
}
if g.LiveData != nil &&
g.LiveData.Linescore != nil &&
g.LiveData.Linescore.Teams != nil &&
g.LiveData.Linescore.Teams.Away != nil {
g.LiveData.Linescore.Teams.Away.Team.score = g.LiveData.Linescore.Teams.Away.Goals
return g.LiveData.Linescore.Teams.Away.Team, nil
}
return nil, fmt.Errorf("could not locate home team in Game")
}
// GetQuarter ...
func (g *Game) GetQuarter() (string, error) {
if g.LiveData != nil && g.LiveData.Linescore != nil {
return g.LiveData.Linescore.CurrentPeriodOrdinal, nil
}
return "", nil
}
// GetClock ...
func (g *Game) GetClock() (string, error) {
if g.LiveData != nil && g.LiveData.Linescore != nil {
return g.LiveData.Linescore.CurrentPeriodTimeRemaining, nil
}
return "00:00", nil
}
// GetUpdate ...
func (g *Game) GetUpdate(ctx context.Context) (sportboard.Game, error) {
if g.GameGetter == nil {
g.GameGetter = GetLiveGame
}
return g.GameGetter(ctx, g.Link)
}
// GetOdds ...
func (g *Game) GetOdds() (string, string, error) {
return "", "", fmt.Errorf("not implemented")
}
func timeFromGameTime(gameTime string) (time.Time, error) {
t, err := time.Parse("2006-01-02T15:04:05Z", gameTime)
if err != nil {
return time.Time{}, err
}
t = t.Local()
return t, nil
}
// GetLiveGame is a LiveGameGetter
func GetLiveGame(ctx context.Context, link string) (sportboard.Game, error) {
uri := fmt.Sprintf("%s/%s", linkBase, link)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var game *Game
if err := json.Unmarshal(body, &game); err != nil {
return nil, fmt.Errorf("failed to unmarshal LiveGame: %w", err)
}
t, err := timeFromGameTime(game.GameData.DateTime.DateTime)
if err != nil {
return nil, fmt.Errorf("failed to parse game time: %w", err)
}
game.GameTime = t
return game, nil
}
func getGames(ctx context.Context, dateStr string) ([]*Game, error) {
uri := fmt.Sprintf("%s/schedule?date=%s&expand=schedule.linescore", baseURL, dateStr)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
client := http.DefaultClient
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to get games for '%s': %w", dateStr, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var gameList *games
if err := json.Unmarshal(body, &gameList); err != nil {
return nil, fmt.Errorf("failed to unmarshal games list: %w", err)
}
var retGames []*Game
for _, d := range gameList.Dates {
for _, g := range d.Games {
t, err := timeFromGameTime(g.GameDate)
if err != nil {
return nil, fmt.Errorf("failed to set GameTime: %w", err)
}
g.GameTime = t
retGames = append(retGames, g)
}
}
return retGames, nil
}