class Sportradar::Nba::Models::Event

Constants

BASKET_CENTER_X
BASKET_CENTER_Y
COURT_LENGTH

Public Class Methods

new(quarter:, attributes:) click to toggle source
# File lib/sportradar/nba/models/event.rb, line 9
def initialize(quarter:, attributes:)
  @quarter = quarter
  @attributes = attributes
  build_statistics
end

Public Instance Methods

attempt_in_words() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 127
def attempt_in_words
  @attributes['attempt']
end
attempt_matches() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 131
def attempt_matches
  /(\d) of (\d)/.match(attempt_in_words)
end
clock() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 106
def clock
  @attributes['clock'] || '0'
end
clock_secs() click to toggle source
# 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
Also aliased as: quarter_seconds
coordinate_x() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 199
def coordinate_x
  coordinates['coord_x']
end
coordinate_y() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 203
def coordinate_y
  coordinates['coord_y']
end
coordinates() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 191
def coordinates
  @attributes['location'] || {}
end
coordinates?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 195
def coordinates?
  @attributes['location'].present?
end
description() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 147
def description
  @attributes['description'] || 0
end
distance_from_scoring_basket_inches() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 187
def event_type
  @attributes['event_type']
end
field_goal() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 223
def field_goal
  self if field_goal?
end
field_goal?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 219
def field_goal?
  two_pointer? || three_pointer?
end
foul() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 247
def foul
  self if foul?
end
foul?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 243
def foul?
  event_type.include?('foul') || event_type.include?('flagrant')
end
free_throw() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 255
def free_throw
  self if free_throw?
end
free_throw?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 251
def free_throw?
  event_type.include?('free_throw')
end
free_throw_attempt() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 37
def game_id
  quarter.game_id
end
has_team?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 73
def has_team?
  !team_id.nil?
end
id() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 33
def id
  @attributes['id']
end
made?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 259
def made?
  event_type.include?('made')
end
miss?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 263
def miss?
  event_type.include?('miss')
end
on_court_away_players() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 291
def play_player_stats
  @play_player_stats ||= []
end
possession() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 45
def possession
  @attributes['possession'] || {}
end
possession?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 41
def possession?
  possession.present?
end
possession_team_id() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 49
def possession_team_id
  possession.dig('id')
end
possession_team_name() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 53
def possession_team_name
  possession.dig('name')
end
quarter() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 77
def quarter
  @quarter
end
quarter_abbreviation() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 85
def quarter_abbreviation
  @quarter.abbreviation
end
quarter_id() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 81
def quarter_id
  @quarter.id
end
quarter_number() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 89
def quarter_number
  @quarter.number
end
quarter_seconds()
Alias for: clock_secs
quarter_sequence() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 93
def quarter_sequence
  @quarter.sequence
end
quarter_type() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 97
def quarter_type
  @quarter.type
end
scoring_play() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 271
def scoring_play
  self if scoring_play?
end
scoring_play?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 267
def scoring_play?
  event_type.include?('made')
end
scoring_players() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 295
def scoring_players
  @scoring_players ||= []
end
statistics() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 215
def statistics
  @attributes['statistics'] || []
end
stoppage() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 279
def stoppage
  self if stoppage?
end
stoppage?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 275
def stoppage?
  %w(endperiod delay officialtimeout review teamtimeout tvtimeout).include?(event_type)
end
team() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 57
def team
  @attributes['attribution'] || {}
end
team_basket() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 69
def team_basket
  team.dig('team_basket')
end
team_id() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 61
def team_id
  team.dig('id')
end
team_name() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 65
def team_name
  team.dig('name')
end
three_pointer() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 231
def three_pointer
  self if three_pointer?
end
three_pointer?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 227
def three_pointer?
  event_type.include?('threepoint')
end
time_code() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 287
def turnover
  self if turnover?
end
turnover?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 283
def turnover?
  event_type.include?('turnover')
end
turnover_type() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 207
def turnover_type
  @attributes['turnover_type']
end
two_pointer() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 239
def two_pointer
  self if two_pointer?
end
two_pointer?() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 235
def two_pointer?
  event_type.include?('twopoint')
end
updated_at() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 211
def updated_at
  @attributes['updated']
end

Private Instance Methods

build_statistics() click to toggle source
# File lib/sportradar/nba/models/event.rb, line 347
def build_statistics
  statistics.each do |statistic|
    play_player_stats << Models::PlayPlayerStat.new(event: self, attributes: statistic)
    scoring_players << Models::ScoringPlayer.new(event: self, attributes: statistic) if scoring_play?
  end
end