Class: SportDb::RsssfGameReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, Models
Defined in:
lib/sportdb/rsssf_reader.rb

Overview

todo: rename to RsssfLeagueMatchReader ( or use league/cup option?) - why? why not??

Constant Summary collapse

RSSSF_FT_REGEX =
/\b
             (?<score1>\d{1,2})
 -
             (?<score2>\d{1,2})
\b/x
RSSSF_ROUND_REGEX =
/\b
               (?<round>Round)
\s
               (?<pos>\d{1,3})
\b/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_or_event_key, text) ⇒ RsssfGameReader

Returns a new instance of RsssfGameReader.



26
27
28
29
30
# File 'lib/sportdb/rsssf_reader.rb', line 26

def initialize( event_or_event_key, text )
  ## todo/fix: how to add opts={} ???
  @event_or_event_key = event_or_event_key
  @text               = text
end

Class Method Details

.from_string(event_or_event_key, text) ⇒ Object

todo: add from_file and from_zip too



22
23
24
# File 'lib/sportdb/rsssf_reader.rb', line 22

def self.from_string( event_or_event_key, text )
  self.new( event_or_event_key, text )
end

Instance Method Details

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



85
86
87
88
# File 'lib/sportdb/rsssf_reader.rb', line 85

def find_rsssf_date!( line, opts={} )
  finder = RsssfDateFinder.new
  finder.find!( line, opts )
end

#find_rsssf_round!(line) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sportdb/rsssf_reader.rb', line 101

def find_rsssf_round!( line )
  ## todo: check if \b works for run on [Apr 13] too ??
  ## todo:  allow multiple spaces after round ??

  m = RSSSF_ROUND_REGEX.match( line )
  if m
    title = m[0]     ## note: title is complete match e.g. Round 1, Round 2, etc.
    pos   = m[:pos].to_i

    logger.debug "   title: >#{title}<, pos: >#{pos}<"
    
    line.sub!( m[0], '[ROUND]' )
  else
    ## fix: add logger.warn no round pos found in line
    title = nil
    pos   = nil
  end
  
  [title,pos]   ## return array; note: [nil,nil] if nothing found
end

#find_rsssf_scores!(line) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sportdb/rsssf_reader.rb', line 64

def find_rsssf_scores!( line ) 
  # e.g. 1-1 or 0-2 or 3-3
  
  m = RSSSF_FT_REGEX.match( line )
  if m
    score1 = m[:score1].to_i
    score2 = m[:score2].to_i

    logger.debug "   score: >#{score1}-#{score2}<"
    
    line.sub!( m[0], '[SCORE]' )
  else
    score1   = nil
    score2   = nil  
  end

  scores = [score1, score2]
  scores
end

#is_rsssf_round?(line) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/sportdb/rsssf_reader.rb', line 97

def is_rsssf_round?( line )
  RSSSF_ROUND_REGEX.match( line ).nil? == false   ## match found if not nil
end

#parse_date_header(line) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/sportdb/rsssf_reader.rb', line 284

def parse_date_header( line )
  # note: returns true if parsed, false if no match
 
  # line with NO teams  plus include date e.g.
  #   [Jun 17]  etc.

  @mapper_teams.map_teams!( line )
  team_keys= @mapper_teams.find_teams!( line )
  team1_key = team_keys[0]
  team2_key = team_keys[1]

  date  = find_rsssf_date!( line, start_at: @event.start_at )

  if date && team1_key.nil? && team2_key.nil?
    logger.debug( "date header line found: >#{line}<")
    logger.debug( "    date: #{date}")
    
    @last_date = date   # keep a reference for later use
    return true
  else
    return false
  end
end

#parse_fixtures(reader) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/sportdb/rsssf_reader.rb', line 310

def parse_fixtures( reader )
    
  reader.each_line do |line|

    if is_rsssf_round?( line )
      parse_round_header( line )
    elsif try_parse_game( line )
      # do nothing here
    elsif try_parse_date_header( line )
      # do nothing here
    else
      logger.info "skipping line (no match found): >#{line}<"
    end
  end # lines.each

  ###########################
  # backtrack and patch round dates (start_at/end_at)

  unless @patch_round_ids.empty?
    ###
    # note: use uniq - to allow multiple round headers (possible?)

    Round.find( @patch_round_ids.uniq ).each do |r|
      logger.debug "patch round start_at/end_at date for #{r.title}:"

      ## note:
      ## will add "scope" pos first e.g
      #
      ## SELECT "games".* FROM "games"  WHERE "games"."round_id" = ?
      # ORDER BY pos, play_at asc  [["round_id", 7]]
      #   thus will NOT order by play_at but by pos first!!!
      # =>
      #  need to unscope pos!!! or use unordered_games - games_by_play_at_date etc.??
      #   thus use reorder()!!! - not just order('play_at asc')

      games = r.games.reorder( 'play_at asc' ).all

      ## skip rounds w/ no games

      ## todo/check/fix: what's the best way for checking assoc w/ 0 recs?
      next if games.size == 0

      # note: make sure start_at/end_at is date only (e.g. use play_at.to_date)
      #   sqlite3 saves datetime in date field as datetime, for example (will break date compares later!)

      round_attribs = {
        start_at: games[0].play_at.to_date,   # use games.first ?
        end_at:   games[-1].play_at.to_date  # use games.last ? why? why not?
      }

      logger.debug round_attribs.to_json
      r.update_attributes!( round_attribs )
    end
  end
end

#parse_game(line) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/sportdb/rsssf_reader.rb', line 194

def parse_game( line )
  logger.debug "parsing game (fixture) line: >#{line}<"

  @mapper_teams.map_teams!( line )
  team_keys = @mapper_teams.find_teams!( line ) 
  team1_key = team_keys[0]
  team2_key = team_keys[1]

  ## note: if we do NOT find two teams; return false - no match found
  if team1_key.nil? || team2_key.nil?
    logger.debug "  no game match (two teams required) found for line: >#{line}<"
    return false
  end

  date      = find_rsssf_date!( line, start_at: @event.start_at )

  ###
  # check if date found?
  #   note: ruby falsey is nil & false only (not 0 or empty array etc.)
  if date
    @last_date = date    # keep a reference for later use
  else
    date = @last_date    # no date found; (re)use last seen date
  end

  scores = find_rsssf_scores!( line )

  logger.debug "  line: >#{line}<"


  ### todo: cache team lookups in hash?
  team1 = Team.find_by!( key: team1_key )
  team2 = Team.find_by!( key: team2_key )

  round = @last_round

  ### check if games exists
  ##  with this teams in this round if yes only update
  ##
  ##  todo: add replay flag (true/false)    !!!!!!!!
  ##    allows same match fixture in round  !!!!!!!!
  game = Game.find_by( round_id: round.id,
                       team1_id: team1.id,
                       team2_id: team2.id )
                        
  game_attribs = {
    score1:    scores[0],
    score2:    scores[1],
    score1et:  scores[2],
    score2et:  scores[3],
    score1p:   scores[4],
    score2p:   scores[5],
    play_at:    date,
    play_at_v2: nil,
    postponed:  false,
    knockout:   false,  ## round.knockout, ## note: for now always use knockout flag from round - why? why not?? 
    ground_id: nil,
    group_id:  nil
  }

  if game.present?
    logger.debug "update game #{game.id}:"
  else
    logger.debug "create game:"
    game = Game.new

    ## Note: use round.games.count for pos
    ##  lets us add games out of order if later needed
    more_game_attribs = {
      round_id: round.id,
      team1_id: team1.id,
      team2_id: team2.id,
      pos:      round.games.count+1
    }
    game_attribs = game_attribs.merge( more_game_attribs )
  end

  logger.debug game_attribs.to_json
  game.update_attributes!( game_attribs )
  
  return true   # game match found
end

#parse_round_header(line) ⇒ Object



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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/sportdb/rsssf_reader.rb', line 123

def parse_round_header( line )

  ## todo/fix:
  ##   simplify - for now round number always required
  #      e.g. no auto-calculation supported here
  #       fail if round found w/o number/pos !!!
  #
  #  also remove knockout flag for now (set to always false for now)
  
  logger.debug "parsing round header line: >#{line}<"

  ## check for date in header first e.g. Round 36 [Jul 20]  !!
  ##   avoid "conflict" with getting "wrong" round number from date etc.
  date = find_rsssf_date!( line, start_at: @event.start_at )
  if date
    @last_date = date
  end
  
  title, pos = find_rsssf_round!( line )

  ## check if pos available; if not auto-number/calculate
  if pos.nil?
      logger.error( "  no round pos found in line >#{line}<; round pos required in rsssf; sorry" )
      fail( "round pos required in rsssf; sorry")
  end

  logger.debug "  line: >#{line}<"
      
  ## Note: dummy/placeholder start_at, end_at date
  ##  replace/patch after adding all games for round

  round_attribs = {
    title:    title,
    title2:   nil,
    knockout: false
  }

  round = Round.find_by( event_id: @event.id,
                         pos:      pos )

  if round.present?
    logger.debug "update round #{round.id}:"
  else
    logger.debug "create round:"
    round = Round.new
        
    round_attribs = round_attribs.merge( {
      event_id: @event.id,
      pos:      pos,
      ##  todo: add num e.g. num == pos for round 1, round 2, etc. - why? why not??
      start_at: Date.parse('1911-11-11'),
      end_at:   Date.parse('1911-11-11')
    })
  end

  logger.debug round_attribs.to_json
 
  round.update_attributes!( round_attribs )

  ### store list of round ids for patching start_at/end_at at the end
  @patch_round_ids << round.id
  @last_round = round     ## keep track of last seen round for matches that follow etc.
end

#readObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sportdb/rsssf_reader.rb', line 33

def read
  ## note: assume active activerecord connection
  
  if @event_or_event_key.kind_of?( Event )
    @event= @event_or_event_key
  else   ## assume string
    @event = Event.find_by!( key: @event_or_event_key )
  end

  logger.debug "Event #{@event.key} >#{@event.title}<"

  @mapper_teams = TeamMapper.new( @event.teams )

  ## reset cached values
  @patch_round_ids = []

  @last_round    = nil
  @last_date     = nil
  
  reader = LineReader.from_string( @text )
  parse_fixtures( reader )    
end

#try_parse_date_header(line) ⇒ Object



278
279
280
281
282
# File 'lib/sportdb/rsssf_reader.rb', line 278

def try_parse_date_header( line )
  # note: clone line; for possible test do NOT modify in place for now
  # note: returns true if parsed, false if no match
  parse_date_header( line.dup )
end

#try_parse_game(line) ⇒ Object



188
189
190
191
192
# File 'lib/sportdb/rsssf_reader.rb', line 188

def try_parse_game( line )
  # note: clone line; for possible test do NOT modify in place for now
  # note: returns true if parsed, false if no match
  parse_game( line.dup )
end