Class: SportDb::GoalsFinder

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/formats/goals.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeGoalsFinder

Returns a new instance of GoalsFinder.



71
72
73
# File 'lib/sportdb/formats/goals.rb', line 71

def initialize
  # nothing here for now
end

Instance Method Details

#find!(line, opts = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
167
# 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