Class: Sportradar::Api::Basketball::Game

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/basketball/game.rb

Direct Known Subclasses

Nba::Game, Ncaamb::Game

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#all_attributes, #attributes, #create_data, #parse_into_array, #parse_into_array_with_options, #parse_out_hashes, #structure_links, #update_data

Constructor Details

#initialize(data, **opts) ⇒ Game

def self.new(data, **opts)

existing = @all_hash[data['id']]
if existing
  existing.update(data, **opts)
  existing
else
  @all_hash[data['id']] = super
end

end def self.all

@all_hash.values

end



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sportradar/api/basketball/game.rb', line 22

def initialize(data, **opts)
  @response = data
  @api      = opts[:api]
  # @season   = opts[:season]
  @updates  = {}
  @changes  = {}

  @score          = {}
  @team_stats     = {}
  @player_stats   = {}
  @scoring_raw    = Scoring.new(data, game: self)
  @teams_hash     = {}
  @periods_hash   = {}
  @home_points    = nil
  @away_points    = nil
  @home_id        = nil
  @away_id        = nil

  @id = data['id']

  update(data, **opts)
end

Instance Attribute Details

#attendanceObject

Returns the value of attribute attendance.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def attendance
  @attendance
end

#away_idObject

Returns the value of attribute away_id.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def away_id
  @away_id
end

#broadcastObject

Returns the value of attribute broadcast.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def broadcast
  @broadcast
end

#changesObject

Returns the value of attribute changes.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def changes
  @changes
end

#clockObject

Returns the value of attribute clock.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def clock
  @clock
end

#coverageObject

Returns the value of attribute coverage.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def coverage
  @coverage
end

#durationObject

Returns the value of attribute duration.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def duration
  @duration
end

#home_idObject

Returns the value of attribute home_id.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def home_id
  @home_id
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def id
  @id
end

#media_timeoutsObject

Returns the value of attribute media_timeouts.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def media_timeouts
  @media_timeouts
end

#periodObject

Returns the value of attribute period.



7
8
9
# File 'lib/sportradar/api/basketball/game.rb', line 7

def period
  @period
end

#player_statsObject

Returns the value of attribute player_stats.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def player_stats
  @player_stats
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def response
  @response
end

#scheduledObject

Returns the value of attribute scheduled.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def scheduled
  @scheduled
end

#scoreObject

Returns the value of attribute score.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def score
  @score
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def status
  @status
end

#team_statsObject

Returns the value of attribute team_stats.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def team_stats
  @team_stats
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def title
  @title
end

#venueObject

Returns the value of attribute venue.



5
6
7
# File 'lib/sportradar/api/basketball/game.rb', line 5

def venue
  @venue
end

Instance Method Details

#apiObject

The base for the requests needed for a subclass



# File 'lib/sportradar/api/basketball/game.rb', line 345

#assign_away(team) ⇒ Object



135
136
137
138
# File 'lib/sportradar/api/basketball/game.rb', line 135

def assign_away(team)
  @away_id = team.id
  @teams_hash[team.id] = team
end

#assign_home(team) ⇒ Object



131
132
133
134
# File 'lib/sportradar/api/basketball/game.rb', line 131

def assign_home(team)
  @home_id = team.id
  @teams_hash[team.id] = team
end

#awayObject



117
118
119
# File 'lib/sportradar/api/basketball/game.rb', line 117

def away
  @teams_hash[@away_id] || @away
end

#boxObject



140
141
142
# File 'lib/sportradar/api/basketball/game.rb', line 140

def box
  @box ||= get_box
end

#cancelled?Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/sportradar/api/basketball/game.rb', line 247

def cancelled?
  ['unnecessary', 'postponed'].include? status
end

#changed?(key) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/sportradar/api/basketball/game.rb', line 177

def changed?(key)
  @changes[key]
end

#check_newness(key, new_object) ⇒ Object



180
181
182
183
# File 'lib/sportradar/api/basketball/game.rb', line 180

def check_newness(key, new_object)
  @changes[key] = !not_updated?(key, new_object)
  remember(key, new_object)
end

#clock_displayObject



217
218
219
220
221
# File 'lib/sportradar/api/basketball/game.rb', line 217

def clock_display
  if clock && period
    "#{clock} #{period_display}"
  end
end

#clock_secondsObject



75
76
77
78
79
# File 'lib/sportradar/api/basketball/game.rb', line 75

def clock_seconds
  return unless @clock
  m,s = @clock.split(':')
  m.to_i * 60 + s.to_i
end

#closed?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/sportradar/api/basketball/game.rb', line 262

def closed?
  'closed' == status
end

#completed?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/sportradar/api/basketball/game.rb', line 259

def completed?
  'complete' == status
end

#finished?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/sportradar/api/basketball/game.rb', line 256

def finished?
  ['complete', 'closed'].include? status
end

#future?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/sportradar/api/basketball/game.rb', line 250

def future?
  ['scheduled', 'delayed', 'created', 'time-tbd', 'if-necessary'].include? status
end

#get_boxObject

data retrieval



268
269
270
271
# File 'lib/sportradar/api/basketball/game.rb', line 268

def get_box
  data = api.get_data(path_box).to_h
  ingest_box(data)
end

#get_pbpObject



285
286
287
288
# File 'lib/sportradar/api/basketball/game.rb', line 285

def get_pbp
  data = api.get_data(path_pbp).to_h
  ingest_pbp(data)
end

#get_summaryObject



313
314
315
316
# File 'lib/sportradar/api/basketball/game.rb', line 313

def get_summary
  data = api.get_data(path_summary).to_h
  ingest_summary(data)
end

#halftime?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/sportradar/api/basketball/game.rb', line 214

def halftime?
  status == 'halftime' || clock == '00:00' && quarter == 2
end

#homeObject



113
114
115
# File 'lib/sportradar/api/basketball/game.rb', line 113

def home
  @teams_hash[@home_id] || @home
end

#ingest_box(data) ⇒ Object



273
274
275
276
277
278
# File 'lib/sportradar/api/basketball/game.rb', line 273

def ingest_box(data)
  update(data, source: :box)
  @period = data.delete(period_name).to_i
  check_newness(:box, @clock)
  data
end

#ingest_pbp(data) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/sportradar/api/basketball/game.rb', line 290

def ingest_pbp(data)
  period_name = 'periods'
  update(data, source: :pbp)
  period_data = if data[period_name] && !data[period_name].empty?
    @period = data[period_name].last['sequence'].to_i
    pers = data[period_name]
    pers.is_a?(Array) && (pers.size == 1) ? pers[0] : pers
  else
    @period = nil
    []
  end
  if data['overtime']
    extra_periods = data['overtime'].is_a?(Hash) ? [data['overtime']] : data['overtime']
    period_data.concat(extra_periods)
  end
  set_pbp(period_data)
  @pbp = @periods_hash.values
  check_newness(:pbp, plays.last&.updated)
  check_newness(:clock, @clock)
  check_newness(:score, @score)
  data
end

#ingest_summary(data) ⇒ Object



323
324
325
326
327
328
329
# File 'lib/sportradar/api/basketball/game.rb', line 323

def ingest_summary(data)
  update(data, source: :summary)
  @period = data.delete(period_name).to_i
  check_newness(:box, @clock)
  check_newness(:score, @score)
  data
end

#leading_teamObject



125
126
127
# File 'lib/sportradar/api/basketball/game.rb', line 125

def leading_team
  @teams_hash[leading_team_id] || (@away_id == leading_team_id && away) || (@home_id == leading_team_id && home)
end

#leading_team_idObject



121
122
123
124
# File 'lib/sportradar/api/basketball/game.rb', line 121

def leading_team_id
  return nil if score.values.uniq.size == 1
  score.max_by(&:last).first
end

#not_updated?(key, object) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/sportradar/api/basketball/game.rb', line 174

def not_updated?(key, object)
  @updates[key] == object
end

#parse_score(data) ⇒ Object



71
72
73
74
# File 'lib/sportradar/api/basketball/game.rb', line 71

def parse_score(data)
  update_score(data.dig('home', 'id') => data.dig('home', 'points').to_i)
  update_score(data.dig('away', 'id') => data.dig('away', 'points').to_i)
end

#path_baseObject

url paths



186
187
188
# File 'lib/sportradar/api/basketball/game.rb', line 186

def path_base
  "games/#{ id }"
end

#path_boxObject



189
190
191
# File 'lib/sportradar/api/basketball/game.rb', line 189

def path_box
  "#{ path_base }/boxscore"
end

#path_pbpObject



192
193
194
# File 'lib/sportradar/api/basketball/game.rb', line 192

def path_pbp
  "#{ path_base }/pbp"
end

#path_summaryObject



195
196
197
# File 'lib/sportradar/api/basketball/game.rb', line 195

def path_summary
  "#{ path_base }/summary"
end

#pbpObject



143
144
145
146
147
148
# File 'lib/sportradar/api/basketball/game.rb', line 143

def pbp
  if !future? && periods.empty?
    get_pbp
  end
  @pbp ||= periods
end

#period_classObject

The class used for game periods



# File 'lib/sportradar/api/basketball/game.rb', line 337

#period_displayObject



231
232
233
234
235
236
237
238
239
# File 'lib/sportradar/api/basketball/game.rb', line 231

def period_display
  if period > 5
    "#{period - 4}OT"
  elsif period == 5
    'OT'
  else
    "#{period}Q"
  end
end

#period_display_longObject



222
223
224
225
226
227
228
229
230
# File 'lib/sportradar/api/basketball/game.rb', line 222

def period_display_long
  if period > 5
    "Overtime #{period - 4}"
  elsif period == 5
    'Overtime'
  else
    "#{Sportradar.ordinalize_period(period)} Quarter"
  end
end

#period_nameObject

The string name used for game periods



# File 'lib/sportradar/api/basketball/game.rb', line 341

#periodsObject



165
166
167
# File 'lib/sportradar/api/basketball/game.rb', line 165

def periods
  @periods_hash.values
end

#playsObject Also known as: events



149
150
151
# File 'lib/sportradar/api/basketball/game.rb', line 149

def plays
  periods.flat_map(&:plays)
end

#plays_by_type(play_type, *types) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/sportradar/api/basketball/game.rb', line 152

def plays_by_type(play_type, *types)
  if types.empty?
    plays.grep(Play.subclass(play_type.delete('_')))
  else
    play_classes = [play_type, *types].map { |type| Play.subclass(type.delete('_')) }
    plays.select { |play| play_classes.any? { |klass| play.kind_of?(klass) } }
  end
end

#points(team_id) ⇒ Object



51
52
53
# File 'lib/sportradar/api/basketball/game.rb', line 51

def points(team_id)
  team_id.is_a?(Symbol) ? @score[@team_ids[team_id]].to_i : @score[team_id].to_i
end

#postponed?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/sportradar/api/basketball/game.rb', line 241

def postponed?
  'postponed' == status
end

#queue_pbpObject



280
281
282
283
# File 'lib/sportradar/api/basketball/game.rb', line 280

def queue_pbp
  url, headers, options, timeout = api.get_request_info(path_pbp)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_pbp)}
end

#queue_summaryObject



318
319
320
321
# File 'lib/sportradar/api/basketball/game.rb', line 318

def queue_summary
  url, headers, options, timeout = api.get_request_info(path_summary)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_summary)}
end

#realtime_stateObject

status helpers



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/sportradar/api/basketball/game.rb', line 200

def realtime_state
  if future?
    'Scheduled'
  elsif finished?
    'Final'
  elsif postponed?
    'Postponed'
  elsif halftime?
    'Halftime'
  else
    clock_display
  end
end

#remember(key, object) ⇒ Object

tracking updates



171
172
173
# File 'lib/sportradar/api/basketball/game.rb', line 171

def remember(key, object)
  @updates[key] = object&.dup
end

#scoringObject



58
59
60
# File 'lib/sportradar/api/basketball/game.rb', line 58

def scoring
  @scoring_raw.scores
end

#set_pbp(data) ⇒ Object



331
332
333
334
335
# File 'lib/sportradar/api/basketball/game.rb', line 331

def set_pbp(data)
  create_data(@periods_hash, data, klass: period_class, api: api, game: self)
  @plays  = nil # to clear empty array empty
  @periods_hash
end

#started?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/sportradar/api/basketball/game.rb', line 253

def started?
  ['inprogress', 'halftime', 'delayed'].include? status
end

#stats(team_id) ⇒ Object



54
55
56
# File 'lib/sportradar/api/basketball/game.rb', line 54

def stats(team_id)
  team_id.is_a?(Symbol) ? @team_stats[@team_ids[team_id]].to_i : @team_stats[team_id].to_i
end

#summaryObject



160
161
162
# File 'lib/sportradar/api/basketball/game.rb', line 160

def summary
  @summary ||= get_summary
end

#team(team_id) ⇒ Object



128
129
130
# File 'lib/sportradar/api/basketball/game.rb', line 128

def team(team_id)
  @teams_hash[team_id]
end

#tied?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/sportradar/api/basketball/game.rb', line 48

def tied?
  @score[away_id].to_i == @score[home_id].to_i
end

#timeoutsObject



44
45
46
# File 'lib/sportradar/api/basketball/game.rb', line 44

def timeouts
  {}
end

#unnecessary?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/sportradar/api/basketball/game.rb', line 244

def unnecessary?
  'unnecessary' == status
end

#update(data, source: nil, **opts) ⇒ Object



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
# File 'lib/sportradar/api/basketball/game.rb', line 81

def update(data, source: nil, **opts)
  # via pbp
  @status       = data['status']                if data['status']
  @coverage     = data['coverage']              if data['coverage']
  @home_id      = data['home_team'] || data.dig('home', 'id')   if data['home_team'] || data.dig('home', 'id')
  @away_id      = data['away_team'] || data.dig('away', 'id')   if data['away_team'] || data.dig('away', 'id')
  @home_points  = data['home_points'].to_i      if data['home_points']
  @away_points  = data['away_points'].to_i      if data['away_points']

  @scheduled    = Time.parse(data["scheduled"]) if data["scheduled"]
  @venue        = Venue.new(data['venue']) if data['venue']
  @broadcast    = Broadcast.new(data['broadcast']) if !data['broadcast'].to_h.empty?
  @home         = team_class.new(data['home'], api: api, game: self) if data['home']
  @away         = team_class.new(data['away'], api: api, game: self) if data['away']
  @title        = data['title'] || @title || (home && away && "#{home.full_name} vs #{away.full_name}")

  @duration     = data['duration']              if data['duration']
  @clock        = data['clock']                 if data['clock']
  @attendance   = data['attendance']            if data['attendance']
  @lead_changes = data['lead_changes']          if data['lead_changes']
  @times_tied   = data['times_tied']            if data['times_tied']

  @team_ids     = { home: @home_id, away: @away_id}

  update_score(@home_id => @home_points.to_i) if @home_points
  update_score(@away_id => @away_points.to_i) if @away_points
  parse_score(data['scoring']) if data['scoring']
  @scoring_raw.update(data, source: source)

  create_data(@teams_hash, data['team'], klass: team_class, api: api, game: self) if data['team']
end

#update_player_stats(player, stats) ⇒ Object



67
68
69
# File 'lib/sportradar/api/basketball/game.rb', line 67

def update_player_stats(player, stats)
  @player_stats.merge!(player.id => stats.merge!(player: player))
end

#update_score(score) ⇒ Object



61
62
63
# File 'lib/sportradar/api/basketball/game.rb', line 61

def update_score(score)
  @score.merge!(score)
end

#update_stats(team, stats) ⇒ Object



64
65
66
# File 'lib/sportradar/api/basketball/game.rb', line 64

def update_stats(team, stats)
  @team_stats.merge!(team.id => stats.merge!(team: team))
end