Class: Sportradar::Nba::Models::Event
- Inherits:
-
Object
- Object
- Sportradar::Nba::Models::Event
- Defined in:
- lib/sportradar/nba/models/event.rb
Direct Known Subclasses
Constant Summary collapse
- COURT_LENGTH =
1128
- BASKET_CENTER_X =
57
- BASKET_CENTER_Y =
300
Instance Method Summary collapse
- #attempt_in_words ⇒ Object
- #attempt_matches ⇒ Object
- #clock ⇒ Object
- #clock_secs ⇒ Object (also: #quarter_seconds)
- #coordinate_x ⇒ Object
- #coordinate_y ⇒ Object
- #coordinates ⇒ Object
- #coordinates? ⇒ Boolean
- #description ⇒ Object
- #distance_from_scoring_basket_inches ⇒ Object
- #distance_to_scoring_basket_inches ⇒ Object
- #event_type ⇒ Object
- #field_goal ⇒ Object
- #field_goal? ⇒ Boolean
- #foul ⇒ Object
- #foul? ⇒ Boolean
- #free_throw ⇒ Object
- #free_throw? ⇒ Boolean
- #free_throw_attempt ⇒ Object
- #free_throw_attempt_of ⇒ Object
- #game_id ⇒ Object
- #has_team? ⇒ Boolean
- #id ⇒ Object
-
#initialize(quarter:, attributes:) ⇒ Event
constructor
A new instance of Event.
- #made? ⇒ Boolean
- #miss? ⇒ Boolean
- #on_court_away_players ⇒ Object
- #on_court_away_team_id ⇒ Object
- #on_court_away_team_name ⇒ Object
- #on_court_home_players ⇒ Object
- #on_court_home_team_id ⇒ Object
- #on_court_home_team_name ⇒ Object
- #on_court_players ⇒ Object
- #play_player_stats ⇒ Object
- #possession ⇒ Object
- #possession? ⇒ Boolean
- #possession_team_id ⇒ Object
- #possession_team_name ⇒ Object
- #quarter ⇒ Object
- #quarter_abbreviation ⇒ Object
- #quarter_id ⇒ Object
- #quarter_number ⇒ Object
- #quarter_sequence ⇒ Object
- #quarter_type ⇒ Object
- #scoring_play ⇒ Object
- #scoring_play? ⇒ Boolean
- #scoring_players ⇒ Object
- #statistics ⇒ Object
- #stoppage ⇒ Object
- #stoppage? ⇒ Boolean
- #team ⇒ Object
- #team_basket ⇒ Object
- #team_id ⇒ Object
- #team_name ⇒ Object
- #three_pointer ⇒ Object
- #three_pointer? ⇒ Boolean
- #time_code ⇒ Object
- #to_s ⇒ Object
- #turnover ⇒ Object
- #turnover? ⇒ Boolean
- #turnover_type ⇒ Object
- #two_pointer ⇒ Object
- #two_pointer? ⇒ Boolean
- #updated_at ⇒ Object
Constructor Details
#initialize(quarter:, attributes:) ⇒ Event
Returns a new instance of Event.
9 10 11 12 13 |
# File 'lib/sportradar/nba/models/event.rb', line 9 def initialize(quarter:, attributes:) @quarter = quarter @attributes = attributes build_statistics end |
Instance Method Details
#attempt_in_words ⇒ Object
127 128 129 |
# File 'lib/sportradar/nba/models/event.rb', line 127 def attempt_in_words @attributes['attempt'] end |
#attempt_matches ⇒ Object
131 132 133 |
# File 'lib/sportradar/nba/models/event.rb', line 131 def attempt_matches /(\d) of (\d)/.match(attempt_in_words) end |
#clock ⇒ Object
106 107 108 |
# File 'lib/sportradar/nba/models/event.rb', line 106 def clock @attributes['clock'] || '0' end |
#clock_secs ⇒ Object Also known as: quarter_seconds
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sportradar/nba/models/event.rb', line 110 def clock_secs begin if clock && clock.include?(':') mins, secs = clock.split(':').map(&:to_i) Time.parse("00:#{mins}:#{secs}"). seconds_since_midnight.to_i end rescue => e return 0 end end |
#coordinate_x ⇒ Object
199 200 201 |
# File 'lib/sportradar/nba/models/event.rb', line 199 def coordinate_x coordinates['coord_x'] end |
#coordinate_y ⇒ Object
203 204 205 |
# File 'lib/sportradar/nba/models/event.rb', line 203 def coordinate_y coordinates['coord_y'] end |
#coordinates ⇒ Object
191 192 193 |
# File 'lib/sportradar/nba/models/event.rb', line 191 def coordinates @attributes['location'] || {} end |
#coordinates? ⇒ Boolean
195 196 197 |
# File 'lib/sportradar/nba/models/event.rb', line 195 def coordinates? @attributes['location'].present? end |
#description ⇒ Object
147 148 149 |
# File 'lib/sportradar/nba/models/event.rb', line 147 def description @attributes['description'] || 0 end |
#distance_from_scoring_basket_inches ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sportradar/nba/models/event.rb', line 151 def distance_from_scoring_basket_inches if coordinates? if scoring_basket = team_basket if scoring_basket == 'left' Math.hypot(COURT_LENGTH - coordinate_x - BASKET_CENTER_X, (coordinate_y - BASKET_CENTER_Y).abs). round(3) elsif scoring_basket == 'right' Math.hypot(coordinate_x - BASKET_CENTER_X, (coordinate_y - BASKET_CENTER_Y).abs). round(3) else 0 end end end end |
#distance_to_scoring_basket_inches ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/sportradar/nba/models/event.rb', line 169 def distance_to_scoring_basket_inches if coordinates? if scoring_basket = team_basket if scoring_basket == 'left' Math.hypot(coordinate_x - BASKET_CENTER_X, (coordinate_y - BASKET_CENTER_Y).abs). round(3) elsif scoring_basket == 'right' Math.hypot(COURT_LENGTH - coordinate_x - BASKET_CENTER_X, (coordinate_y - BASKET_CENTER_Y).abs). round(3) else 0 end end end end |
#event_type ⇒ Object
187 188 189 |
# File 'lib/sportradar/nba/models/event.rb', line 187 def event_type @attributes['event_type'] end |
#field_goal ⇒ Object
223 224 225 |
# File 'lib/sportradar/nba/models/event.rb', line 223 def field_goal self if field_goal? end |
#field_goal? ⇒ Boolean
219 220 221 |
# File 'lib/sportradar/nba/models/event.rb', line 219 def field_goal? two_pointer? || three_pointer? end |
#foul ⇒ Object
247 248 249 |
# File 'lib/sportradar/nba/models/event.rb', line 247 def foul self if foul? end |
#foul? ⇒ Boolean
243 244 245 |
# File 'lib/sportradar/nba/models/event.rb', line 243 def foul? event_type.include?('foul') || event_type.include?('flagrant') end |
#free_throw ⇒ Object
255 256 257 |
# File 'lib/sportradar/nba/models/event.rb', line 255 def free_throw self if free_throw? end |
#free_throw? ⇒ Boolean
251 252 253 |
# File 'lib/sportradar/nba/models/event.rb', line 251 def free_throw? event_type.include?('free_throw') end |
#free_throw_attempt ⇒ Object
135 136 137 138 139 |
# File 'lib/sportradar/nba/models/event.rb', line 135 def free_throw_attempt if matches = attempt_matches matches[0].to_i end end |
#free_throw_attempt_of ⇒ Object
141 142 143 144 145 |
# File 'lib/sportradar/nba/models/event.rb', line 141 def free_throw_attempt_of if matches = attempt_matches matches[1].to_i end end |
#game_id ⇒ Object
37 38 39 |
# File 'lib/sportradar/nba/models/event.rb', line 37 def game_id quarter.game_id end |
#has_team? ⇒ Boolean
73 74 75 |
# File 'lib/sportradar/nba/models/event.rb', line 73 def has_team? !team_id.nil? end |
#id ⇒ Object
33 34 35 |
# File 'lib/sportradar/nba/models/event.rb', line 33 def id @attributes['id'] end |
#made? ⇒ Boolean
259 260 261 |
# File 'lib/sportradar/nba/models/event.rb', line 259 def made? event_type.include?('made') end |
#miss? ⇒ Boolean
263 264 265 |
# File 'lib/sportradar/nba/models/event.rb', line 263 def miss? event_type.include?('miss') end |
#on_court_away_players ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/sportradar/nba/models/event.rb', line 311 def on_court_away_players (@attributes.dig('on_court', 'away', 'players') || {}).map do |player| Models::OnCourtPlayer.new( player.reverse_merge({ 'event_id' => id, 'player_id' => player['id'], 'team_id' => on_court_away_team_id, 'team_name' => on_court_away_team_name, }) ) end end |
#on_court_away_team_id ⇒ Object
303 304 305 |
# File 'lib/sportradar/nba/models/event.rb', line 303 def on_court_away_team_id @on_court_away_team_id ||= @attributes.dig('on_court', 'away', 'id') end |
#on_court_away_team_name ⇒ Object
307 308 309 |
# File 'lib/sportradar/nba/models/event.rb', line 307 def on_court_away_team_name @on_court_away_team_name ||= @attributes.dig('on_court', 'away', 'name') end |
#on_court_home_players ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/sportradar/nba/models/event.rb', line 332 def on_court_home_players (@attributes.dig('on_court', 'home', 'players') || {}).map do |player| Models::OnCourtPlayer.new( player.reverse_merge({ 'event_id' => id, 'player_id' => player['id'], 'team_id' => on_court_home_team_id, 'team_name' => on_court_home_team_name, }) ) end end |
#on_court_home_team_id ⇒ Object
324 325 326 |
# File 'lib/sportradar/nba/models/event.rb', line 324 def on_court_home_team_id @on_court_home_team_id ||= @attributes.dig('on_court', 'home', 'id') end |
#on_court_home_team_name ⇒ Object
328 329 330 |
# File 'lib/sportradar/nba/models/event.rb', line 328 def on_court_home_team_name @on_court_home_team_name ||= @attributes.dig('on_court', 'home', 'name') end |
#on_court_players ⇒ Object
299 300 301 |
# File 'lib/sportradar/nba/models/event.rb', line 299 def on_court_players @on_court_players ||= on_court_away_players + on_court_home_players end |
#play_player_stats ⇒ Object
291 292 293 |
# File 'lib/sportradar/nba/models/event.rb', line 291 def play_player_stats @play_player_stats ||= [] end |
#possession ⇒ Object
45 46 47 |
# File 'lib/sportradar/nba/models/event.rb', line 45 def possession @attributes['possession'] || {} end |
#possession? ⇒ Boolean
41 42 43 |
# File 'lib/sportradar/nba/models/event.rb', line 41 def possession? possession.present? end |
#possession_team_id ⇒ Object
49 50 51 |
# File 'lib/sportradar/nba/models/event.rb', line 49 def possession_team_id possession.dig('id') end |
#possession_team_name ⇒ Object
53 54 55 |
# File 'lib/sportradar/nba/models/event.rb', line 53 def possession_team_name possession.dig('name') end |
#quarter ⇒ Object
77 78 79 |
# File 'lib/sportradar/nba/models/event.rb', line 77 def quarter @quarter end |
#quarter_abbreviation ⇒ Object
85 86 87 |
# File 'lib/sportradar/nba/models/event.rb', line 85 def quarter_abbreviation @quarter.abbreviation end |
#quarter_id ⇒ Object
81 82 83 |
# File 'lib/sportradar/nba/models/event.rb', line 81 def quarter_id @quarter.id end |
#quarter_number ⇒ Object
89 90 91 |
# File 'lib/sportradar/nba/models/event.rb', line 89 def quarter_number @quarter.number end |
#quarter_sequence ⇒ Object
93 94 95 |
# File 'lib/sportradar/nba/models/event.rb', line 93 def quarter_sequence @quarter.sequence end |
#quarter_type ⇒ Object
97 98 99 |
# File 'lib/sportradar/nba/models/event.rb', line 97 def quarter_type @quarter.type end |
#scoring_play ⇒ Object
271 272 273 |
# File 'lib/sportradar/nba/models/event.rb', line 271 def scoring_play self if scoring_play? end |
#scoring_play? ⇒ Boolean
267 268 269 |
# File 'lib/sportradar/nba/models/event.rb', line 267 def scoring_play? event_type.include?('made') end |
#scoring_players ⇒ Object
295 296 297 |
# File 'lib/sportradar/nba/models/event.rb', line 295 def scoring_players @scoring_players ||= [] end |
#statistics ⇒ Object
215 216 217 |
# File 'lib/sportradar/nba/models/event.rb', line 215 def statistics @attributes['statistics'] || [] end |
#stoppage ⇒ Object
279 280 281 |
# File 'lib/sportradar/nba/models/event.rb', line 279 def stoppage self if stoppage? end |
#stoppage? ⇒ Boolean
275 276 277 |
# File 'lib/sportradar/nba/models/event.rb', line 275 def stoppage? %w(endperiod delay officialtimeout review teamtimeout tvtimeout).include?(event_type) end |
#team ⇒ Object
57 58 59 |
# File 'lib/sportradar/nba/models/event.rb', line 57 def team @attributes['attribution'] || {} end |
#team_basket ⇒ Object
69 70 71 |
# File 'lib/sportradar/nba/models/event.rb', line 69 def team_basket team.dig('team_basket') end |
#team_id ⇒ Object
61 62 63 |
# File 'lib/sportradar/nba/models/event.rb', line 61 def team_id team.dig('id') end |
#team_name ⇒ Object
65 66 67 |
# File 'lib/sportradar/nba/models/event.rb', line 65 def team_name team.dig('name') end |
#three_pointer ⇒ Object
231 232 233 |
# File 'lib/sportradar/nba/models/event.rb', line 231 def three_pointer self if three_pointer? end |
#three_pointer? ⇒ Boolean
227 228 229 |
# File 'lib/sportradar/nba/models/event.rb', line 227 def three_pointer? event_type.include?('threepoint') end |
#time_code ⇒ Object
101 102 103 104 |
# File 'lib/sportradar/nba/models/event.rb', line 101 def time_code min, sec = clock.split(':').map(&:to_i) "PT#{min}M#{sec}S" end |
#to_s ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sportradar/nba/models/event.rb', line 15 def to_s [].tap do |sentence_parts| sentence_parts << quarter_abbreviation sentence_parts << clock sentence_parts << team_name if has_team? sentence_parts << team_basket if has_team? sentence_parts << event_type sentence_parts << description sentence_parts << free_throw_attempt sentence_parts << free_throw_attempt_of sentence_parts << turnover_type sentence_parts << "[#{coordinate_x}, #{coordinate_y}]" if coordinates? sentence_parts << distance_from_scoring_basket_inches sentence_parts << distance_to_scoring_basket_inches end.compact. join(' - ') end |
#turnover ⇒ Object
287 288 289 |
# File 'lib/sportradar/nba/models/event.rb', line 287 def turnover self if turnover? end |
#turnover? ⇒ Boolean
283 284 285 |
# File 'lib/sportradar/nba/models/event.rb', line 283 def turnover? event_type.include?('turnover') end |
#turnover_type ⇒ Object
207 208 209 |
# File 'lib/sportradar/nba/models/event.rb', line 207 def turnover_type @attributes['turnover_type'] end |
#two_pointer ⇒ Object
239 240 241 |
# File 'lib/sportradar/nba/models/event.rb', line 239 def two_pointer self if two_pointer? end |
#two_pointer? ⇒ Boolean
235 236 237 |
# File 'lib/sportradar/nba/models/event.rb', line 235 def two_pointer? event_type.include?('twopoint') end |
#updated_at ⇒ Object
211 212 213 |
# File 'lib/sportradar/nba/models/event.rb', line 211 def updated_at @attributes['updated'] end |