class SportDb::GoalsFinder

todo: find a better name? to avoid confusing w/ GoalsParser? use MatchGoalsParser or similar?

Public Class Methods

new() click to toggle source
# File lib/sportdb/formats/goals.rb, line 71
def initialize
  # nothing here for now
end

Public Instance Methods

find!( line, opts={} ) click to toggle source
# File lib/sportdb/formats/goals.rb, line 75
def find!( line, opts={} )
  # remove end-of-line comments
  line = line.sub( /#.*$/ ) do |_|
           logger.debug "   cutting off end of line comment - >>#{$&}<<"
           ''
         end

  # remove [] if presents e.g. [Neymar 12']
  line = line.gsub( /[\[\]]/, '' )
  # remove (single match) if line starts w/ - (allow spaces)  e.g. [-;Neymar 12'] or [ - ;Neymar 12']
  line = line.sub( /^[ ]*-[ ]*/, '' )

  # split into left hand side (lhs) for team1 and
  #            right hand side (rhs) for team2

  values = line.split( ';' )

  # note: allow empty right hand side (e.g. team2 did NOT score any goals e.g. 3-0 etc.)
  lhs = values[0]
  rhs = values[1]

  lhs = lhs.strip   unless lhs.nil?
  rhs = rhs.strip   unless rhs.nil?

  parser = GoalsParser.new
  ## todo/check: only call if not nil?

  logger.debug "  lhs (team1): >#{lhs}<"
  lhs_data = parser.parse!( lhs )
  pp lhs_data

  logger.debug "  rhs (team2): >#{rhs}<"
  rhs_data = parser.parse!( rhs )
  pp rhs_data

  ### merge into flat goal structs
  goals = []
  lhs_data.each do |player|
    player.minutes.each do |minute|
      goal = GoalStruct.new
      goal.name    = player.name
      goal.team    = 1
      goal.minute  = minute.minute
      goal.offset  = minute.offset
      goal.penalty = minute.penalty
      goal.owngoal = minute.owngoal
      goals << goal
    end
  end

  rhs_data.each do |player|
    player.minutes.each do |minute|
      goal = GoalStruct.new
      goal.name    = player.name
      goal.team    = 2
      goal.minute  = minute.minute
      goal.offset  = minute.offset
      goal.penalty = minute.penalty
      goal.owngoal = minute.owngoal
      goals << goal
    end
  end


  # sort by minute + offset
  goals = goals.sort do |l,r|
    res = l.minute <=> r.minute
    if res == 0
      res =  l.offset <=> r.offset  # pass 2: sort by offset
    end
    res
  end

  ## calc score1,score2
  score1 = 0
  score2 = 0
  goals.each do |goal|
    if goal.team == 1
      score1 += 1
    elsif goal.team == 2
      score2 += 1
    else
      # todo: should not happen: issue warning
    end
    goal.score1 = score1
    goal.score2 = score2
  end

  logger.debug "  #{goals.size} goals:"
  pp goals

  goals
end