Class: Sportradar::Api::Baseball::Lineup

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/baseball/lineup.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, game: nil) ⇒ Lineup

Returns a new instance of Lineup.



6
7
8
# File 'lib/sportradar/api/baseball/lineup.rb', line 6

def initialize(data, game: nil)
  @game = game
end

Instance Attribute Details

#awayObject (readonly)

Returns the value of attribute away.



5
6
7
# File 'lib/sportradar/api/baseball/lineup.rb', line 5

def away
  @away
end

#gameObject (readonly)

Returns the value of attribute game.



5
6
7
# File 'lib/sportradar/api/baseball/lineup.rb', line 5

def game
  @game
end

#homeObject (readonly)

Returns the value of attribute home.



5
6
7
# File 'lib/sportradar/api/baseball/lineup.rb', line 5

def home
  @home
end

#rosterObject (readonly)

Returns the value of attribute roster.



5
6
7
# File 'lib/sportradar/api/baseball/lineup.rb', line 5

def roster
  @roster
end

Instance Method Details

#find_player(id) ⇒ Object



65
66
67
68
69
# File 'lib/sportradar/api/baseball/lineup.rb', line 65

def find_player(id)
  roster.detect do |player|
    player['id'] == id
  end
end

#full_awayObject



35
36
37
# File 'lib/sportradar/api/baseball/lineup.rb', line 35

def full_away
  @away_team_lineup
end

#full_homeObject



31
32
33
# File 'lib/sportradar/api/baseball/lineup.rb', line 31

def full_home
  @home_team_lineup
end

#initial_lineupObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sportradar/api/baseball/lineup.rb', line 71

def initial_lineup
  [
    { 'order' => 1 },
    { 'order' => 2 },
    { 'order' => 3 },
    { 'order' => 4 },
    { 'order' => 5 },
    { 'order' => 6 },
    { 'order' => 7 },
    { 'order' => 8 },
    { 'order' => 9 },
  ]
end

#next_batters(team, number_of_upcoming_batters = 3) ⇒ Object



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
# File 'lib/sportradar/api/baseball/lineup.rb', line 85

def next_batters(team, number_of_upcoming_batters = 3)
  if team == 'home'
    last_at_bat = game.at_bats.select{|at_bat| at_bat.event.half_inning.half == 'B'}.last
    if last_at_bat
      last_position = @home.detect{|htl| htl['id'] == last_at_bat.hitter_id}&.dig('order')
      upcoming = home.rotate(last_position || 0)
    else # first inning
      upcoming = home
    end
  elsif team == 'away'
    last_at_bat = game.at_bats.select{|at_bat| at_bat.event.half_inning.half == 'T'}.last
    if last_at_bat
      last_position = @away.detect{|atl| atl['id'] == last_at_bat.hitter_id}&.dig('order')
      upcoming = away.rotate(last_position || 0)
    else # first inning
      upcoming = away
    end
  end
  upcoming[0..(number_of_upcoming_batters - 1)]
# rescue => e
#   i ||= 0
#   game.get_summary
#   i += 1
#   retry unless i > 2
end

#update(data, source: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sportradar/api/baseball/lineup.rb', line 10

def update(data, source: nil)
  case source
  when :pbp
    @home = initial_lineup.dup
    @away = initial_lineup.dup
    lineups = game.events.select {|ev| ev.lineup }.map(&:lineup)
    lineups.each do |line|
      player_data = line.response
      player_data['id'] = player_data.delete('player_id')
      update_from_lineup_event(player_data)
    end
  when :summary
    @roster = (data.dig('home', 'roster') || []) + (data.dig('away', 'roster') || [])
    return unless data.dig('home', 'lineup') && data.dig('away', 'lineup')
    @home_team_lineup = data.dig('home', 'lineup')
    @away_team_lineup = data.dig('away', 'lineup')
    initialize_home
    initialize_away
  end
end

#update_away(player, order) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/sportradar/api/baseball/lineup.rb', line 56

def update_away(player, order)
  return if order == 0
  idx = away.index do |h|
    h['order'] == order
  end
  away[idx] = { 'order' => order}
  away[idx].merge!(player)
end

#update_from_lineup_event(data) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/sportradar/api/baseball/lineup.rb', line 39

def update_from_lineup_event(data)
  if data.dig('team_id') == game.home_id
    update_home(data, data.dig('order'))
  elsif data.dig('team_id') == game.away_id
    update_away(data, data.dig('order'))
  end
end

#update_home(player, order) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/sportradar/api/baseball/lineup.rb', line 47

def update_home(player, order)
  return if order == 0
  idx = home.index do |h|
    h['order'] == order
  end
  home[idx] = { 'order' => order }
  home[idx].merge!(player)
end