Class: Sportradar::Api::Basketball::Play::Base

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

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) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sportradar/api/basketball/plays/base.rb', line 9

def initialize(data, **opts)
  @response = data
  @api      = opts[:api]

  @id       = data['id']

  @quarter  = opts[:quarter].sequence.to_i  rescue opts[:quarter].to_i
  @half     = opts[:half].sequence.to_i     rescue opts[:half].to_i

  update(data, **opts)
end

Instance Attribute Details

#clockObject

Returns the value of attribute clock.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def clock
  @clock
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def description
  @description
end

#event_typeObject Also known as: type

Returns the value of attribute event_type.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def event_type
  @event_type
end

#game_secondsObject

Returns the value of attribute game_seconds.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def game_seconds
  @game_seconds
end

#halfObject

Returns the value of attribute half.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def half
  @half
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def id
  @id
end

#identifierObject

Returns the value of attribute identifier.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def identifier
  @identifier
end

#locationObject

Returns the value of attribute location.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def location
  @location
end

#on_courtObject

Returns the value of attribute on_court.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def on_court
  @on_court
end

#on_court_idsObject

Returns the value of attribute on_court_ids.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def on_court_ids
  @on_court_ids
end

#player_idObject

Returns the value of attribute player_id.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def player_id
  @player_id
end

#possessionObject

Returns the value of attribute possession.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def possession
  @possession
end

#quarterObject

Returns the value of attribute quarter.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def quarter
  @quarter
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def response
  @response
end

#scoreObject

Returns the value of attribute score.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def score
  @score
end

#statisticsObject

Returns the value of attribute statistics.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def statistics
  @statistics
end

#team_idObject

Returns the value of attribute team_id.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def team_id
  @team_id
end

#updatedObject

Returns the value of attribute updated.



6
7
8
# File 'lib/sportradar/api/basketball/plays/base.rb', line 6

def updated
  @updated
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  id == other.id && description == other.description
end

#base_keyObject



56
57
58
# File 'lib/sportradar/api/basketball/plays/base.rb', line 56

def base_key
  nil
end

#clock_secondsObject



68
69
70
71
# File 'lib/sportradar/api/basketball/plays/base.rb', line 68

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

#display_typeObject

def game

quarter.game

end



28
29
30
# File 'lib/sportradar/api/basketball/plays/base.rb', line 28

def display_type
  ''
end

#end_of_ot?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sportradar/api/basketball/plays/base.rb', line 53

def end_of_ot?
  false
end

#end_of_period?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sportradar/api/basketball/plays/base.rb', line 47

def end_of_period?
  false
end

#end_of_regulation?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/sportradar/api/basketball/plays/base.rb', line 50

def end_of_regulation?
  false
end

#halftime?Boolean

Returns:

  • (Boolean)


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

def halftime?
  false
end

#handle_on_court(data) ⇒ Object



96
97
98
99
100
# File 'lib/sportradar/api/basketball/plays/base.rb', line 96

def handle_on_court(data)
  teams = [data['home'], data['away']]
  @on_court = teams.flat_map { |hash| hash['players'].map { |pl| OpenStruct.new(pl) } }
  @on_court_ids = @on_court.map(&:id)
end

#handle_time(data, **opts) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sportradar/api/basketball/plays/base.rb', line 102

def handle_time(data, **opts)
  @game_seconds = if opts[:quarter]
    @quarter  = opts[:quarter].sequence.to_i rescue opts[:quarter].to_i
    @identifier = "#{quarter}_#{(720 - clock_seconds).to_s.rjust(3, '0')}".to_i
    nba_game_seconds
  elsif opts[:half]
    @half  = opts[:half].sequence.to_i rescue opts[:half].to_i
    @identifier = "#{half}_#{(1200 - clock_seconds).to_s.rjust(4, '0')}".to_i
    ncaa_game_seconds
  end
end

#media_timeout?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sportradar/api/basketball/plays/base.rb', line 38

def media_timeout?
  false
end

#nba_game_secondsObject



72
73
74
# File 'lib/sportradar/api/basketball/plays/base.rb', line 72

def nba_game_seconds
  ([quarter, 4].min * 720) + ([quarter - 4, 0].max * 300) - clock_seconds # seconds elapsed in game, only works for NBA
end

#parse_statistics(data) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sportradar/api/basketball/plays/base.rb', line 114

def parse_statistics(data)
  return unless data['statistics']
  @statistics = data['statistics']
  stat = @statistics.detect { |hash| hash['type'] == base_key }
  # stat = data.dig('statistics', base_key) rescue data.dig('statistics', 0, base_key)
  # stat = stat[0] if stat.is_a?(Array) # sometimes SR has an array of identical assist hashes
  @team = stat['team']
  @team_id = @team['id'] if @team
  @player = stat['player']
  @player_id = @player['id'] if @player
rescue => e
  puts e
  # binding.pry
end

#periodObject



21
22
23
# File 'lib/sportradar/api/basketball/plays/base.rb', line 21

def period
  @quarter.nonzero? || @half.nonzero?
end

#pointsObject



60
61
62
# File 'lib/sportradar/api/basketball/plays/base.rb', line 60

def points
  0
end

#quarter_break?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/sportradar/api/basketball/plays/base.rb', line 41

def quarter_break?
  false
end

#scoring_play?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/sportradar/api/basketball/plays/base.rb', line 32

def scoring_play?
  points.nonzero?
end

#timeout?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/sportradar/api/basketball/plays/base.rb', line 35

def timeout?
  false
end

#update(data, **opts) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sportradar/api/basketball/plays/base.rb', line 80

def update(data, **opts)
  @event_type  = data['event_type']  # "lineupchange",
  @clock       = data['clock']       # "12:00",
  @updated     = Time.parse(data['updated']) if data['updated']     # "2016-10-26T00:07:52+00:00",
  @description = data['description'] # "Cavaliers lineup change (Richard Jefferson, Kyrie Irving, Mike Dunleavy, Channing Frye, LeBron James)",
  @attribution = data['attribution'] # {"name"=>"Cavaliers", "market"=>"Cleveland", "id"=>"583ec773-fb46-11e1-82cb-f4ce4684ea4c", "team_basket"=>"left"},
  @team_id     = data.dig('attribution', "id")
  @location    = data['location']    # {"coord_x"=>"0", "coord_y"=>"0"},
  @possession  = data['possession']  # {"name"=>"Knicks", "market"=>"New York", "id"=>"583ec70e-fb46-11e1-82cb-f4ce4684ea4c"},
  # @on_court    = data['on_court']    # hash
  handle_on_court(data['on_court']) if data['on_court']

  handle_time(data, **opts)
  parse_statistics(data) if data['statistics']
end