Class: Sportradar::Api::Football::Play

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/football/play.rb

Direct Known Subclasses

Ncaafb::Play, Nfl::Play

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

Returns a new instance of Play.



7
8
9
10
11
12
# File 'lib/sportradar/api/football/play.rb', line 7

def initialize(data, **opts)
  @response          = data
  @id                = data["id"]

  update(data, **opts)
end

Instance Attribute Details

#alt_descriptionObject

Returns the value of attribute alt_description.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def alt_description
  @alt_description
end

#away_pointsObject

Returns the value of attribute away_points.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def away_points
  @away_points
end

#clockObject

Returns the value of attribute clock.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def clock
  @clock
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def description
  @description
end

#downObject

Returns the value of attribute down.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def down
  @down
end

#end_situationObject

Returns the value of attribute end_situation.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def end_situation
  @end_situation
end

#event_typeObject

Returns the value of attribute event_type.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def event_type
  @event_type
end

#home_pointsObject

Returns the value of attribute home_points.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def home_points
  @home_points
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def id
  @id
end

#play_clockObject

Returns the value of attribute play_clock.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def play_clock
  @play_clock
end

#play_typeObject

Returns the value of attribute play_type.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def play_type
  @play_type
end

#player_dataObject

Returns the value of attribute player_data.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def player_data
  @player_data
end

#player_idObject

Returns the value of attribute player_id.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def player_id
  @player_id
end

#playersObject

Returns the value of attribute players.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def players
  @players
end

#referenceObject

Returns the value of attribute reference.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def reference
  @reference
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def response
  @response
end

#scoreObject

Returns the value of attribute score.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def score
  @score
end

#scoring_playObject

Returns the value of attribute scoring_play.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def scoring_play
  @scoring_play
end

#sequenceObject

Returns the value of attribute sequence.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def sequence
  @sequence
end

#start_situationObject

Returns the value of attribute start_situation.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def start_situation
  @start_situation
end

#statisticsObject

Returns the value of attribute statistics.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def statistics
  @statistics
end

#team_idObject

Returns the value of attribute team_id.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def team_id
  @team_id
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def type
  @type
end

#wall_clockObject

Returns the value of attribute wall_clock.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def wall_clock
  @wall_clock
end

#yfdObject

Returns the value of attribute yfd.



5
6
7
# File 'lib/sportradar/api/football/play.rb', line 5

def yfd
  @yfd
end

Instance Method Details

#clock_secondsObject



70
71
72
73
# File 'lib/sportradar/api/football/play.rb', line 70

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

#counted_play?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sportradar/api/football/play.rb', line 91

def counted_play?
  ['rush', 'pass'].include?(self.play_type) && !self.description.include?('No Play')
end

#deleted?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/sportradar/api/football/play.rb', line 79

def deleted?
  !!@deleted
end

#down_distanceObject



95
96
97
# File 'lib/sportradar/api/football/play.rb', line 95

def down_distance
  [down, yfd].compact.join(' & ')
end

#end_of_regulation?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/sportradar/api/football/play.rb', line 99

def end_of_regulation?
  false
end

#end_spotObject



87
88
89
# File 'lib/sportradar/api/football/play.rb', line 87

def end_spot
  end_situation&.spot
end

#halftime?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/sportradar/api/football/play.rb', line 107

def halftime?
  @description == "End of 1st Half"
end

#parse_description_for_drive_endObject



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
# File 'lib/sportradar/api/football/play.rb', line 138

def parse_description_for_drive_end
  parsed_ending = case @description
  when /no play/i
    nil
  when /intercepted/i
    :interception
  when /fumbles/i
    self.statistics&.fumble&.first&.lost? && :fumble
  when /extra point is good/i
    :touchdown
  # when missed extra point
  when /punts/i
    :punt
  when /Field Goal is No Good. blocked/i
    :fg
  when /Field Goal is No Good/i
    :fg
  # when missed field goal
  when /Field Goal is Good/i
    :fg
  when "End of 1st Half"
    :end_of_half
  else
    #
  end
  if parsed_ending
    parsed_ending
  end
end

#parse_playerObject



168
169
170
171
172
173
174
175
# File 'lib/sportradar/api/football/play.rb', line 168

def parse_player
  # TODO: Currently there is an issue where we are only mapping one player_id to a play, but there are plays with multiple players involved.
  play_stats = @statistics.penalty || @statistics.rush || @statistics.return || @statistics.receive
  if play_stats.is_a?(Array)
    play_stats = play_stats.first
  end
  @player_id = play_stats&.player&.id
end

#parsed_endingObject



111
112
113
# File 'lib/sportradar/api/football/play.rb', line 111

def parsed_ending
  @parsed_ending ||= search_for_drive_end
end

#search_for_drive_endObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sportradar/api/football/play.rb', line 115

def search_for_drive_end
  case @play_type
  when 'kick'
    nil
  when 'rush'
    # check for fumble
    parse_description_for_drive_end
  when 'pass'
    # check for fumble/interception
    parse_description_for_drive_end
  when 'punt'
    parse_description_for_drive_end
  when 'penalty'
    nil
  when 'fieldgoal'
    parse_description_for_drive_end # nullified plays still have FG
  when 'extrapoint'
    :pat
  else
    parse_description_for_drive_end
  end
end

#start_spotObject



83
84
85
# File 'lib/sportradar/api/football/play.rb', line 83

def start_spot
  start_situation&.spot
end

#timeout?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/sportradar/api/football/play.rb', line 103

def timeout?
  ['timeout', 'tvtimeout', 'teamtimeout'].include? @play_type
end

#update(data, **opts) ⇒ Object



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
# File 'lib/sportradar/api/football/play.rb', line 14

def update(data, **opts)
  @api              = opts[:api] || @api
  @drive            = opts[:drive] || @drive
  @description      = data["description"] || data['summary'] || @description
  @alt_description  = data['alt_description'] if data['alt_description']
  @away_points  = data['away_points']  if data['away_points']
  @home_points  = data['home_points']  if data['home_points']

  @end_situation   = Sportradar::Api::Football::Situation.new(data["end_situation"])   if data["end_situation"]
  @start_situation = Sportradar::Api::Football::Situation.new(data["start_situation"]) if data["start_situation"]

  @team_id           = start_situation.team_id if start_situation
  @play_clock        = data["play_clock"]
  @reference         = data["reference"]
  @score             = data["score"]
  @scoring_play      = data["scoring_play"]
  @sequence          = data["sequence"]

  @clock        = data["clock"]        if data["clock"]
  @type         = data["type"]         if data["type"]
  @summary      = data["summary"]      if data["summary"]
  @updated      = Time.parse(data["updated"]) if data["updated"]
  @wall_clock   = Time.parse(data["wall_clock"]) if data["wall_clock"]
  @side         = data["side"]         if data["side"]
  @yard_line    = data["yard_line"]    if data["yard_line"]
  @down         = data["down"]         if data["down"]
  @yfd          = data["yfd"]          if data["yfd"]
  @formation    = data["formation"]    if data["formation"]
  @direction    = data["direction"]    if data["direction"]
  @distance     = data["distance"]     if data["distance"]
  @participants = data["participants"] if data["participants"]
  @play_type    = data["play_type"] || @play_type || data["event_type"]
  @sequence     = data["sequence"]     if data["sequence"]
  @event_type   = data["event_type"]   if data["event_type"]

  @deleted      = data["deleted"] || @deleted

  @details           = data["details"].gsub('.json', '') if data["details"]

  if data['statistics']
    @statistics = Sportradar::Api::Football::PlayStatistics.new(data['statistics'])
  elsif data['players']
    @player_data = data['players']
    @statistics = Sportradar::Api::Football::PlayStatistics.new(data['players'])
  else
    @statistics ||= OpenStruct.new(players: [])
  end
  parse_player

  self
end

#updatedObject



66
67
68
# File 'lib/sportradar/api/football/play.rb', line 66

def updated
  @updated || @wall_clock
end