Class: SportDb::GameReader
- Inherits:
-
Object
- Object
- SportDb::GameReader
show all
- Includes:
- LogUtils::Logging, FixtureHelpers, Model, TextUtils::ValueHelper
- Defined in:
- lib/sportdb/readers/game.rb
Constant Summary
Constants included
from Model
Model::City, Model::Continent, Model::Country, Model::Prop, Model::Region
Instance Attribute Summary collapse
Instance Method Summary
collapse
#cut_off_end_of_line_comment!, #find_date!, #find_game_pos!, #find_ground!, #find_group_title_and_pos!, #find_leading_pos!, #find_person!, #find_record_comment!, #find_record_laps!, #find_record_leading_state!, #find_record_timeline!, #find_round_def_title!, #find_round_header_title!, #find_round_header_title2!, #find_round_pos!, #find_scores!, #find_team!, #find_team1!, #find_team2!, #find_teams!, #find_track!, #is_group?, #is_group_def?, #is_knockout_round?, #is_postponed?, #is_round?, #is_round_def?, #map_ground!, #map_person!, #map_team!, #map_teams!, #map_track!, #match_teams!, #match_track!
Constructor Details
#initialize(include_path, opts = {}) ⇒ GameReader
Returns a new instance of GameReader.
23
24
25
|
# File 'lib/sportdb/readers/game.rb', line 23
def initialize( include_path, opts = {} )
@include_path = include_path
end
|
Instance Attribute Details
#include_path ⇒ Object
Returns the value of attribute include_path.
20
21
22
|
# File 'lib/sportdb/readers/game.rb', line 20
def include_path
@include_path
end
|
Instance Method Details
#load_fixtures_worker(event_key, reader) ⇒ Object
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
|
# File 'lib/sportdb/readers/game.rb', line 88
def load_fixtures_worker( event_key, reader )
@patch_round_ids_dates = []
@patch_round_ids_pos = []
@round = nil @group = nil @last_date = nil
@event = Event.find_by_key!( event_key )
logger.debug "Event #{@event.key} >#{@event.title}<"
@known_teams = @event.known_teams_table
@known_grounds = TextUtils.build_title_table_for( @event.grounds )
parse_fixtures( reader )
end
|
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
# File 'lib/sportdb/readers/game.rb', line 557
def ( line )
match_teams!( line )
team1_key = find_team1!( line )
team2_key = find_team2!( line )
date = find_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 return true
else
return false
end
end
|
#parse_fixtures(reader) ⇒ Object
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
# File 'lib/sportdb/readers/game.rb', line 585
def parse_fixtures( reader )
reader.each_line do |line|
if is_round_def?( line )
parse_round_def( line )
elsif is_round?( line )
( line )
elsif is_group_def?( line ) parse_group_def( line )
elsif is_group?( line )
( line )
elsif try_parse_game( line )
elsif ( line )
else
logger.info "skipping line (no match found): >#{line}<"
end
end
unless @patch_round_ids_dates.empty?
Round.find( @patch_round_ids_dates.uniq ).each do |r|
logger.debug "patch round start_at/end_at date for #{r.title}:"
games = r.games.reorder( 'play_at asc' ).all
next if games.size == 0
round_attribs = {
start_at: games[0].play_at.to_date, end_at: games[-1].play_at.to_date }
logger.debug round_attribs.to_json
r.update_attributes!( round_attribs )
end
end
unless @patch_round_ids_pos.empty?
if @last_round_pos
offset = @last_round_pos
logger.info " +++ patch round pos - use offset; start w/ #{offset}"
else
offset = 0
logger.debug " patch round pos - no offset; start w/ 0"
end
Round.order( 'start_at asc').find( @patch_round_ids_pos.uniq ).each_with_index do |r,idx|
logger.debug "[#{idx+1}] patch round pos >#{offset+idx+1}< for #{r.title}:"
round_attribs = {
pos: offset+idx+1
}
if r.title.starts_with?('Matchday')
round_attribs[:title] = "Matchday #{offset+idx+1}"
end
logger.debug round_attribs.to_json
r.update_attributes!( round_attribs )
@last_round_pos = [offset+idx+1,@last_round_pos||0].max
end
end
end
|
#parse_game(line) ⇒ Object
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
# File 'lib/sportdb/readers/game.rb', line 351
def parse_game( line )
logger.debug "parsing game (fixture) line: >#{line}<"
match_teams!( line )
team1_key = find_team1!( line )
team2_key = find_team2!( line )
if team1_key.nil? || team2_key.nil?
logger.debug " no game match (two teams required) found for line: >#{line}<"
return false
end
pos = find_game_pos!( line )
if is_postponed?( line )
postponed = true
date_v2 = find_date!( line, start_at: @event.start_at )
date = find_date!( line, start_at: @event.start_at )
else
postponed = false
date_v2 = nil
date = find_date!( line, start_at: @event.start_at )
end
if date
@last_date = date else
date = @last_date end
scores = find_scores!( line )
if @event.grounds.count > 0
map_ground!( line )
ground_key = find_ground!( line )
ground = ground_key.nil? ? nil : Ground.find_by_key!( ground_key )
else
ground = nil
end
logger.debug " line: >#{line}<"
team1 = Team.find_by_key!( team1_key )
team2 = Team.find_by_key!( team2_key )
if @round.nil?
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with?( 'sqlite' )
logger.debug( " [sqlite] using sqlite-specific query for date compare for rounds finder" )
round = Round.where( 'event_id = ? AND ( julianday(start_at) <= julianday(?)'+
'AND julianday(end_at) >= julianday(?))',
@event.id, date.to_date, date.to_date).first
else round = Round.where( 'event_id = ? AND (start_at <= ? AND end_at >= ?)',
@event.id, date.to_date, date.to_date).first
end
pp round
if round.nil?
logger.warn( " !!!! no round match found for date #{date}" )
pp Round.all
round = Round.new
round_attribs = {
event_id: @event.id,
title: "Matchday #{date.to_date}",
pos: 999001+@patch_round_ids_pos.length, start_at: date.to_date,
end_at: date.to_date
}
logger.info( " auto-add round >Matchday #{date.to_date}<" )
logger.debug round_attribs.to_json
round.update_attributes!( round_attribs )
@patch_round_ids_pos << round.id end
if round.pos > 999000
else
@last_round_pos = [round.pos,@last_round_pos||0].max
end
logger.debug( " using round #{round.pos} >#{round.title}< start_at: #{round.start_at}, end_at: #{round.end_at}" )
else
round = @round
end
game = Game.find_by_round_id_and_team1_id_and_team2_id(
round.id, team1.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: date_v2,
postponed: postponed,
knockout: round.knockout, ground_id: ground.present? ? ground.id : nil,
group_id: @group.present? ? @group.id : nil
}
game_attribs[ :pos ] = pos if pos.present?
if game.present? &&
game.check_for_changes( game_attribs ) == false
logger.debug " skip update game #{game.id}; no changes found"
else
if game.present?
logger.debug "update game #{game.id}:"
else
logger.debug "create game:"
game = Game.new
more_game_attribs = {
round_id: round.id,
team1_id: team1.id,
team2_id: team2.id
}
more_game_attribs[ :pos ] = round.games.count+1 if pos.nil?
game_attribs = game_attribs.merge( more_game_attribs )
end
logger.debug game_attribs.to_json
game.update_attributes!( game_attribs )
end
return true end
|
#parse_group_def(line) ⇒ Object
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
|
# File 'lib/sportdb/readers/game.rb', line 142
def parse_group_def( line )
logger.debug "parsing group def line: >#{line}<"
match_teams!( line )
team_keys = find_teams!( line )
title, pos = find_group_title_and_pos!( line )
logger.debug " line: >#{line}<"
group_attribs = {
title: title
}
group = Group.find_by_event_id_and_pos( @event.id, pos )
if group.present?
logger.debug "update group #{group.id}:"
else
logger.debug "create group:"
group = Group.new
group_attribs = group_attribs.merge( {
event_id: @event.id,
pos: pos
})
end
logger.debug group_attribs.to_json
group.update_attributes!( group_attribs )
group.teams.clear team_keys.each do |team_key|
team = Team.find_by_key!( team_key )
logger.debug " adding team #{team.title} (#{team.code})"
group.teams << team
end
end
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/sportdb/readers/game.rb', line 120
def ( line )
logger.debug "parsing group header line: >#{line}<"
@round = nil
title, pos = find_group_title_and_pos!( line )
logger.debug " title: >#{title}<"
logger.debug " pos: >#{pos}<"
logger.debug " line: >#{line}<"
@group = Group.find_by_event_id_and_pos!( @event.id, pos )
end
|
#parse_round_def(line) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
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
|
# File 'lib/sportdb/readers/game.rb', line 182
def parse_round_def( line )
logger.debug "parsing round def line: >#{line}<"
( line )
start_at = find_date!( line, start_at: @event.start_at )
end_at = find_date!( line, start_at: @event.start_at )
end_at = start_at if end_at.nil?
start_at = start_at.to_date
end_at = end_at.to_date
pos = find_round_pos!( line )
title = find_round_def_title!( line )
knockout_flag = is_knockout_round?( title )
logger.debug " start_at: #{start_at}"
logger.debug " end_at: #{end_at}"
logger.debug " pos: #{pos}"
logger.debug " title: >#{title}<"
logger.debug " knockout_flag: #{knockout_flag}"
logger.debug " line: >#{line}<"
round_attribs = {
title: title,
knockout: knockout_flag,
start_at: start_at,
end_at: end_at
}
round = Round.find_by_event_id_and_pos( @event.id, 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
})
end
logger.debug round_attribs.to_json
round.update_attributes!( round_attribs )
end
|
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
|
# File 'lib/sportdb/readers/game.rb', line 249
def ( line )
logger.debug "parsing round header line: >#{line}<"
( line )
title2 = ( line )
group_title, group_pos = find_group_title_and_pos!( line )
pos = find_round_pos!( line )
if pos.nil?
if @patch_round_ids_pos.empty?
pos = (@last_round_pos||0)+1
logger.debug( " no round pos found; auto-number round - use (#{pos})" )
else
pos = 999001+@patch_round_ids_pos.length logger.debug( " no round pos found; auto-number round w/ patch (backtrack) at the end" )
end
end
if pos > 999000
else
@last_round_pos = [pos,@last_round_pos||0].max
end
title = ( line )
knockout_flag = is_knockout_round?( title )
if group_pos.present?
@group = Group.find_by_event_id_and_pos!( @event.id, group_pos )
else
@group = nil end
logger.debug " line: >#{line}<"
round_attribs = {
title: title,
title2: title2,
knockout: knockout_flag
}
if pos > 999000
@round = Round.find_by_event_id_and_title( @event.id, title )
else
@round = Round.find_by_event_id_and_pos( @event.id, pos )
end
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,
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 )
@patch_round_ids_pos << @round.id if pos > 999000
@patch_round_ids_dates << @round.id end
|
#read(name, more_attribs = {}) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/sportdb/readers/game.rb', line 28
def read( name, more_attribs={} )
reader = EventReader.new( @include_path )
reader.read( name )
event = reader.event fixtures = reader.fixtures
@last_round_pos = nil
fixtures.each do |fixture|
read_fixtures( event.key, fixture ) end
end
|
#read_fixtures(event_key, name) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/sportdb/readers/game.rb', line 60
def read_fixtures( event_key, name )
pos = name.index( '!/')
if pos.nil?
name_real_path = name else
name_real_path = name[ (pos+2)..-1 ]
end
path = "#{include_path}/#{name_real_path}.txt"
logger.info "parsing data '#{name}' (#{path})..."
SportDb.lang.lang = SportDb.lang.classify_file( path )
reader = LineReader.new( path )
load_fixtures_worker( event_key, reader )
Prop.create_from_fixture!( name, path )
end
|
#read_fixtures_from_string(event_key, text) ⇒ Object
load from string (e.g. passed in via web form)
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/sportdb/readers/game.rb', line 46
def read_fixtures_from_string( event_key, text )
SportDb.lang.lang = SportDb.lang.classify( text )
reader = StringLineReader.new( text )
read_fixtures_worker( event_key, reader )
end
|
551
552
553
554
555
|
# File 'lib/sportdb/readers/game.rb', line 551
def ( line )
( line.dup )
end
|
#try_parse_game(line) ⇒ Object
345
346
347
348
349
|
# File 'lib/sportdb/readers/game.rb', line 345
def try_parse_game( line )
parse_game( line.dup )
end
|