class AhlScraper::Games::TimeSplitsService

Public Class Methods

new(goals, team_id, current_state, game_properties) click to toggle source
# File lib/ahl_scraper/services/games/time_splits_service.rb, line 6
def initialize(goals, team_id, current_state, game_properties)
  @goals = goals
  @team_id = team_id
  @current_state = current_state
  @game_properties = game_properties
end

Public Instance Methods

call() click to toggle source
# File lib/ahl_scraper/services/games/time_splits_service.rb, line 13
def call
  @times = { leading: 0, trailing: 0, tied: 0 }
  @time_elapsed = @game_properties[:game_start_time_in_seconds] || 0
  calculate_time_splits
  @times
end

Private Instance Methods

add_time_based_on_score(difference, time) click to toggle source
# File lib/ahl_scraper/services/games/time_splits_service.rb, line 36
def add_time_based_on_score(difference, time)
  if difference.positive?
    @times[:leading] += time
  elsif difference.negative?
    @times[:trailing] += time
  else
    @times[:tied] += time
  end
end
add_time_remaining(time) click to toggle source
# File lib/ahl_scraper/services/games/time_splits_service.rb, line 46
def add_time_remaining(time)
  to_end_of_game = (current_time || total_game_time) - time
  add_time_based_on_score(@score_difference, to_end_of_game)
end
calculate_time_splits() click to toggle source
# File lib/ahl_scraper/services/games/time_splits_service.rb, line 22
def calculate_time_splits
  @score_difference = 0
  @goals.each.with_index do |goal, i|
    goal_time_elapsed = PeriodTimeHelper.new(goal[:time], goal[:period][:id].to_i).to_elapsed
    add_time_based_on_score(@score_difference, goal_time_elapsed - @time_elapsed)
    @score_difference += goal[:team][:id] == @team_id ? 1 : -1
    @time_elapsed = goal_time_elapsed

    next unless (i + 1) == @goals.length

    add_time_remaining(@time_elapsed) if @current_state[:shootout] || !@current_state[:overtime]
  end
end
current_time() click to toggle source
# File lib/ahl_scraper/services/games/time_splits_service.rb, line 61
def current_time
  return unless @current_state[:status] == "in_progress"

  return total_game_time if @current_state[:period] =~ /SO/

  PeriodTimeHelper.new(@current_state[:time], @current_state[:period_number]).to_elapsed
end
total_game_time() click to toggle source
# File lib/ahl_scraper/services/games/time_splits_service.rb, line 51
def total_game_time
  return @game_properties[:game_end_time_in_seconds] if @game_properties[:game_end_time_in_seconds]

  return 3600 unless @current_state[:overtime]

  return 3900 unless @current_state[:playoffs]

  3600 + (1200 * @game_properties[:overtime_periods])
end