Class: SportDb::GameReader
- Inherits:
-
Object
- Object
- SportDb::GameReader
- Includes:
- LogUtils::Logging, Models, FixtureHelpers, TextUtils::ValueHelper
- Defined in:
- lib/sportdb/readers/game.rb
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
returns event record.
Class Method Summary collapse
- .from_file(path, more_attribs = {}) ⇒ Object
- .from_string(event, text_or_text_ary, more_attribs = {}) ⇒ Object
- .from_zip(zip_file, entry_path, more_attribs = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(event, text_or_text_ary, more_attribs = {}) ⇒ GameReader
constructor
A new instance of GameReader.
- #parse_date_header(line) ⇒ Object
- #parse_fixtures(reader) ⇒ Object
- #parse_game(line) ⇒ Object
- #parse_goals(line) ⇒ Object
- #parse_group_def(line) ⇒ Object
- #parse_group_header(line) ⇒ Object
- #parse_round_def(line) ⇒ Object
- #parse_round_header(line) ⇒ Object
- #read ⇒ Object
- #read_fixtures_worker(event_key, reader) ⇒ Object
- #try_parse_date_header(line) ⇒ Object
- #try_parse_game(line) ⇒ Object
Methods included from FixtureHelpers
#cut_off_end_of_line_comment!, #find_date!, #find_game_pos!, #find_ground!, #find_group_title_and_pos!, #find_leading_num!, #find_leading_pos!, #find_nationality!, #find_person!, #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!, #is_goals?, #is_group?, #is_group_def?, #is_knockout_round?, #is_postponed?, #is_round?, #is_round_def?, #map_ground!, #map_person!, #map_team!, #map_teams!, #match_teams!
Constructor Details
#initialize(event, text_or_text_ary, more_attribs = {}) ⇒ GameReader
Returns a new instance of GameReader.
141 142 143 144 145 146 147 148 149 |
# File 'lib/sportdb/readers/game.rb', line 141 def initialize( event, text_or_text_ary, more_attribs={} ) ### fix - fix -fix: ## change event to event_or_event_key !!!!! - allow event_key as string passed in ## todo/fix: how to add opts={} ??? @event = event @text_or_text_ary = text_or_text_ary @more_attribs = more_attribs end |
Instance Attribute Details
#event ⇒ Object (readonly)
returns event record
13 14 15 |
# File 'lib/sportdb/readers/game.rb', line 13 def event @event end |
Class Method Details
.from_file(path, more_attribs = {}) ⇒ Object
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 |
# File 'lib/sportdb/readers/game.rb', line 76 def self.from_file( path, more_attribs={} ) logger = LogKernel::Logger.root ### NOTE: fix-fix-fix - pass in event path!!!!!!! (not fixture path!!!!) ## - ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark) ## - ## - see textutils/utils.rb ## - text = File.read_utf8( path ) event_text = File.read_utf8( path ) ## hack: ## support old event config format for now (will get removed later) ## e.g. check for ## teams: ## 12 teams: etc. if event_text =~ /^teams:/i || event_text =~ /^\d{1,2} teams:/i || event_text =~ /^start_at:/i ## old format puts "*** [DEPRECATED] old event config format w/ yaml, please use new plain text format >#{path}<" reader = EventReader.from_file( path ) reader.read() else ## new format reader = EventMetaReader.from_file( path ) reader.read() end event = reader.event ## was fetch_event( name ) fixtures = reader.fixtures ## was fetch_event_fixtures( name ) ## add path to fixtures (use path from event e.g) # - bl + at-austria!/2012_13/bl -> at-austria!/2012_13/bl # - bl_ii + at-austria!/2012_13/bl -> at-austria!/2012_13/bl_ii dir = File.dirname( path ) # use dir for fixtures fixtures_with_path = fixtures.map do |fx| fx_new = "#{dir}/#{fx}.txt" # add path upfront logger.debug "fx: #{fx_new} | >#{fx}< + >#{dir}<" fx_new end ## fix-fix-fix: change file extension to ?? text_ary = [] fixtures_with_path.each do |fixture_path| text_ary << File.read_utf8( fixture_path ) end self.from_string( event, text_ary, more_attribs ) end |
.from_string(event, text_or_text_ary, more_attribs = {}) ⇒ Object
134 135 136 137 138 |
# File 'lib/sportdb/readers/game.rb', line 134 def self.from_string( event, text_or_text_ary, more_attribs={} ) ### fix - fix -fix: ## change event to event_or_event_key !!!!! - allow event_key as string passed in self.new( event, text_or_text_ary, more_attribs ) end |
.from_zip(zip_file, entry_path, more_attribs = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sportdb/readers/game.rb', line 20 def self.from_zip( zip_file, entry_path, more_attribs={} ) logger = LogKernel::Logger.root ## get text content from zip entry = zip_file.find_entry( entry_path ) event_text = entry.get_input_stream().read() event_text = event_text.force_encoding( Encoding::UTF_8 ) ## hack: ## support old event config format for now (will get removed later) ## e.g. check for ## teams: ## 12 teams: etc. if event_text =~ /^teams:/i || event_text =~ /^\d{1,2} teams:/i || event_text =~ /^start_at:/i ## old format puts "*** [DEPRECATED] old event config format w/ yaml, please use new plain text format >#{entry_path}<" reader = EventReader.from_zip( zip_file, entry_path ) reader.read() else ## new format reader = EventMetaReader.from_zip( zip_file, entry_path ) reader.read() end event = reader.event ## was fetch_event( name ) fixtures = reader.fixtures ## was fetch_event_fixtures( name ) ## add path to fixtures (use path from event e.g) # - bl + at-austria!/2012_13/bl -> at-austria!/2012_13/bl # - bl_ii + at-austria!/2012_13/bl -> at-austria!/2012_13/bl_ii dir = File.dirname( entry_path ) # use dir for fixtures fixtures_with_path = fixtures.map do |fx| fx_new = "#{dir}/#{fx}.txt" # add path upfront logger.debug "fx: #{fx_new} | >#{fx}< + >#{dir}<" fx_new end ## fix-fix-fix: change file extension to ?? text_ary = [] fixtures_with_path.each do |fixture_path| entry = zip_file.find_entry( fixture_path ) text = entry.get_input_stream().read() text = text.force_encoding( Encoding::UTF_8 ) text_ary << text end self.from_string( event, text_ary, more_attribs ) end |
Instance Method Details
#parse_date_header(line) ⇒ Object
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 |
# File 'lib/sportdb/readers/game.rb', line 662 def parse_date_header( line ) # note: returns true if parsed, false if no match # line with NO teams plus include date e.g. # [Fri Jun/17] or # Jun/17 or # 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_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
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
# File 'lib/sportdb/readers/game.rb', line 803 def parse_fixtures( reader ) reader.each_line do |line| if is_goals?( line ) parse_goals( line ) elsif is_round_def?( line ) ## todo/fix: add round definition (w begin n end date) ## todo: do not patch rounds with definition (already assume begin/end date is good) ## -- how to deal with matches that get rescheduled/postponed? parse_round_def( line ) elsif is_round?( line ) parse_round_header( line ) elsif is_group_def?( line ) ## NB: group goes after round (round may contain group marker too) ### todo: add pipe (|) marker (required) parse_group_def( line ) elsif is_group?( line ) ## -- lets you set group e.g. Group A etc. parse_group_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 pos and round dates (start_at/end_at) # note: patch dates must go first! (otherwise sort_by_date will not work for round pos) unless @patch_round_ids_dates.empty? ### # fix: do NOT patch if auto flag is set to false !!! # e.g. rounds got added w/ round def (not w/ round header) # note: use uniq - to allow multiple round headers (possible?) Round.find( @patch_round_ids_dates.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 unless @patch_round_ids_pos.empty? # step 0: check for offset (last_round_pos) 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 # step 1: sort by date # step 2: update pos # note: use uniq - to allow multiple round headers (possible?) Round.order( 'start_at asc').find( @patch_round_ids_pos.uniq ).each_with_index do |r,idx| # note: starts counting w/ zero(0) logger.debug "[#{idx+1}] patch round pos >#{offset+idx+1}< for #{r.title}:" round_attribs = { pos: offset+idx+1 } # update title if Matchday XXXX e.g. use Matchday 1 etc. 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 ) # update last_round_pos offset too @last_round_pos = [offset+idx+1,@last_round_pos||0].max end end end |
#parse_game(line) ⇒ Object
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 549 550 551 552 553 554 555 556 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 582 583 584 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 |
# File 'lib/sportdb/readers/game.rb', line 448 def parse_game( line ) logger.debug "parsing game (fixture) line: >#{line}<" @mapper_teams.map_teams!( line ) ### todo/fix: limit mapping to two(2) teams - why? why not? might avoid matching @ Barcelona ?? 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 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 ### # check if date found? # NB: ruby falsey is nil & false only (not 0 or empty array etc.) if date ### check: use date_v2 if present? why? why not? @last_date = date # keep a reference for later use else date = @last_date # no date found; (re)use last seen date end scores = find_scores!( line ) #### # note: # only map ground if we got any grounds (setup/configured in event) if @event.grounds.count > 0 ## todo/check: use @known_grounds for check?? why? why not?? ## use in @known_grounds = TextUtils.build_title_table_for( @event.grounds ) ## # fix: mark mapped title w/ type (ground-) or such!! - too avoid fallthrough match # e.g. three teams match - but only two get mapped, third team gets match for ground # e.g Somalia v Djibouti @ Djibouti map_ground!( line ) ground_key = find_ground!( line ) ground = ground_key.nil? ? nil : Ground.find_by_key!( ground_key ) else # no grounds configured; always nil ground = nil end logger.debug " line: >#{line}<" ### todo: cache team lookups in hash? team1 = Team.find_by_key!( team1_key ) team2 = Team.find_by_key!( team2_key ) @last_team1 = team1 # store for later use for goals etc. @last_team2 = team2 if @round.nil? ## no round header found; calculate round from date ### ## todo/fix: add some unit tests for round look up # fix: use date_v2 if present!! (old/original date; otherwise use date) # # fix: check - what to do with hours e.g. start_at use 00:00 and for end_at use 23.59 ?? # -- for now - remove hours (e.g. use end_of_day and beginnig_of_day) ## # note: start_at and end_at are dates ONLY (note datetime) # - do NOT pass in hours etc. in query # again use --> date.end_of_day, date.beginning_of_day # new: not working: date.to_date, date.to_date # will not find round if start_at same as date !! (in theory hours do not matter) ### # hack: # special case for sqlite3 (date compare not working reliable; use casts) # fix: move to adapter_name to activerecord_utils as sqlite? or similar? 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 # all other dbs (postgresql, mysql, etc.) 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 ################################### # -- try auto-adding matchday round = Round.new round_attribs = { event_id: @event.id, title: "Matchday #{date.to_date}", pos: 999001+@patch_round_ids_pos.length, # e.g. 999<count> - 999001,999002,etc. 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 # todo/check - add just id or "full" record as now - why? why not? end # store pos for auto-number next round if missing # - note: only if greater/bigger than last; use max # - note: last_round_pos might be nil - thus set to 0 if round.pos > 999000 # note: do NOT update last_round_pos for to-be-patched rounds else @last_round_pos = [round.pos,@last_round_pos||0].max end ## note: will crash (round.pos) if round is nil logger.debug( " using round #{round.pos} >#{round.title}< start_at: #{round.start_at}, end_at: #{round.end_at}" ) else ## use round from last round header round = @round end ### check if games exists ## with this teams in this round if yes only update game = Game.find_by_round_id_and_team1_id_and_team2_id( round.id, team1.id, team2.id ) game_attribs = { score1i: scores[0], score2i: scores[1], score1: scores[2], score2: scores[3], score1et: scores[4], score2et: scores[5], score1p: scores[6], score2p: scores[7], play_at: date, play_at_v2: date_v2, postponed: postponed, knockout: round.knockout, ## note: for now always use knockout flag from round - why? why not?? ground_id: ground.present? ? ground.id : nil, group_id: @group.present? ? @group.id : nil } game_attribs[ :pos ] = pos if pos.present? #### # note: only update if any changes (or create if new record) 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 } ## NB: use round.games.count for pos ## lets us add games out of order if later needed 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 @last_game = game # store for later reference (e.g. used for goals etc.) return true # game match found end |
#parse_goals(line) ⇒ Object
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'lib/sportdb/readers/game.rb', line 690 def parse_goals( line ) logger.debug "parsing goals (fixture) line: >#{line}<" goals = GoalsFinder.new.find!( line ) ## check if squads/rosters present for player mappings # squad1_count = Roster.where( event_id: @event.id, team_id: @last_team1 ).count if squad1_count > 0 squad1 = Roster.where( event_id: @event.id, team_id: @last_team1 ) else squad1 = [] end squad2_count = Roster.where( event_id: @event.id, team_id: @last_team2 ).count if squad2_count > 0 squad2 = Roster.where( event_id: @event.id, team_id: @last_team2 ) else squad2 = [] end ##### # todo/fix: try lookup by squads first!!! # issue warning if player not included in squad!! ########## # try mapping player names to player ## note: first delete all goals for match (and recreate new ones # no need to figure out update/merge strategy) @last_game.goals.delete_all goals.each do |goal| player_name = goal.name player = Person.where( name: player_name ).first if player logger.info " player match (name eq) - using player key #{player.key}" else # try like match (player name might only include part of name e.g. Messi) # try three variants # try %Messi # try Messi% # try %Messi% -- check if there's an easier way w/ "one" where clause? player = Person.where( 'name LIKE ? OR name LIKE ? OR name LIKE ?', "%#{player_name}", "#{player_name}%", "%#{player_name}%" ).first if player logger.info " player match (name like) - using player key #{player.key}" else # try synonyms player = Person.where( 'synonyms LIKE ? OR synonyms LIKE ? OR synonyms LIKE ?', "%#{player_name}", "#{player_name}%", "%#{player_name}%" ).first if player logger.info " player match (synonyms like) - using player key #{player.key}" else # auto-create player (player not found) logger.info " player NOT found >#{player_name}< - auto-create" ## fix: add auto flag (for auto-created persons/players) ## fix: move title_to_key logic to person model etc. player_key = TextUtils.title_to_key( player_name ) player_attribs = { key: player_key, title: player_name } logger.info " using attribs: #{player_attribs.inspect}" player = Person.create!( player_attribs ) end end end goal_attribs = { game_id: @last_game.id, team_id: goal.team == 1 ? @last_team1.id : @last_team2.id, person_id: player.id, minute: goal.minute, offset: goal.offset, penalty: goal.penalty, owngoal: goal.owngoal, score1: goal.score1, score2: goal.score2 } logger.info " adding goal using attribs: #{goal_attribs.inspect}" Goal.create!( goal_attribs ) end # each goals end |
#parse_group_def(line) ⇒ Object
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 276 |
# File 'lib/sportdb/readers/game.rb', line 239 def parse_group_def( line ) logger.debug "parsing group def line: >#{line}<" @mapper_teams.map_teams!( line ) team_keys = @mapper_teams.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 # remove old teams ## add new teams 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 |
#parse_group_header(line) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/sportdb/readers/game.rb', line 217 def parse_group_header( line ) logger.debug "parsing group header line: >#{line}<" # note: group header resets (last) round (allows, for example): # e.g. # Group Playoffs/Replays -- round header # team1 team2 -- match # Group B: -- group header # team1 team2 - match (will get new auto-matchday! not last round) @round = nil ## fix: change/rename to @last_round !!! title, pos = find_group_title_and_pos!( line ) logger.debug " title: >#{title}<" logger.debug " pos: >#{pos}<" logger.debug " line: >#{line}<" # set group for games @group = Group.find_by_event_id_and_pos!( @event.id, pos ) end |
#parse_round_def(line) ⇒ Object
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 343 |
# File 'lib/sportdb/readers/game.rb', line 279 def parse_round_def( line ) logger.debug "parsing round def line: >#{line}<" ### todo/fix/check: move cut off optional comment in reader for all lines? why? why not? cut_off_end_of_line_comment!( line ) # cut off optional comment starting w/ # start_at = find_date!( line, start_at: @event.start_at ) end_at = find_date!( line, start_at: @event.start_at ) # note: if end_at missing -- assume start_at is (==) end_at end_at = start_at if end_at.nil? # note: - NOT needed; start_at and end_at are saved as date only (NOT datetime) # set hours,minutes,secs to beginning and end of day (do NOT use default 12.00) # e.g. use 00.00 and 23.59 # start_at = start_at.beginning_of_day # end_at = end_at.end_of_day # note: make sure start_at/end_at is date only (e.g. use start_at.to_date) # sqlite3 saves datetime in date field as datetime, for example (will break date compares later!) start_at = start_at.to_date end_at = end_at.to_date pos = find_round_pos!( line ) title = find_round_def_title!( line ) # NB: use extracted round title for knockout check 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}<" ####################################### # fix: add auto flag is false !!!! 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 |
#parse_round_header(line) ⇒ Object
346 347 348 349 350 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 |
# File 'lib/sportdb/readers/game.rb', line 346 def parse_round_header( line ) logger.debug "parsing round header line: >#{line}<" ### todo/fix/check: move cut off optional comment in reader for all lines? why? why not? cut_off_end_of_line_comment!( line ) # cut off optional comment starting w/ # # NB: cut off optional title2 starting w/ // first title2 = find_round_header_title2!( line ) # todo/fix: check if it is possible title2 w/ group? # add an example here group_title, group_pos = find_group_title_and_pos!( line ) ## todo/check/fix: # make sure Round of 16 will not return pos 16 -- how? possible? # add unit test too to verify pos = find_round_pos!( line ) ## check if pos available; if not auto-number/calculate 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 # note: if any rounds w/o pos already seen (add for auto-numbering at the end) # will get auto-numbered sorted by start_at date pos = 999001+@patch_round_ids_pos.length # e.g. 999<count> - 999001,999002,etc. logger.debug( " no round pos found; auto-number round w/ patch (backtrack) at the end" ) end end # store pos for auto-number next round if missing # - note: only if greater/bigger than last; use max # - note: last_round_pos might be nil - thus set to 0 if pos > 999000 # note: do NOT update last_round_pos for to-be-patched rounds else @last_round_pos = [pos,@last_round_pos||0].max end title = find_round_header_title!( line ) ## NB: use extracted round title for knockout check 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 # reset group to no group end logger.debug " line: >#{line}<" ## NB: dummy/placeholder start_at, end_at date ## replace/patch after adding all games for round round_attribs = { title: title, title2: title2, knockout: knockout_flag } if pos > 999000 # no pos (e.g. will get autonumbered later) - try match by title for now # e.g. lets us use title 'Group Replays', for example, multiple times @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 ### store list of round ids for patching start_at/end_at at the end @patch_round_ids_dates << @round.id # todo/fix/check: check if round has definition (do NOT patch if definition (not auto-added) present) end |
#read ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sportdb/readers/game.rb', line 152 def read if @text_or_text_ary.is_a?( String ) text_ary = [@text_or_text_ary] else text_ary = @text_or_text_ary end ## reset cached values ## for auto-number rounds etc. @last_round_pos = nil text_ary.each do |text| ## assume en for now? why? why not? ## fix (cache) store lang in event table (e.g. auto-add and auto-update)!!! SportDb.lang.lang = SportDb.lang.classify( text ) reader = LineReader.from_string( text ) read_fixtures_worker( @event.key, reader ) end ## fix add prop ?? ### Prop.create!( key: "db.#{fixture_name_to_prop_key(name)}.version", value: "file.txt.#{File.mtime(path).strftime('%Y.%m.%d')}" ) end |
#read_fixtures_worker(event_key, reader) ⇒ Object
178 179 180 181 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 |
# File 'lib/sportdb/readers/game.rb', line 178 def read_fixtures_worker( event_key, reader ) ## NB: assume active activerecord connection ## reset cached values @patch_round_ids_dates = [] @patch_round_ids_pos = [] @round = nil ## fix: change/rename to @last_round !!! @group = nil ## fix: change/rename to @last_group !!! @last_date = nil @last_team1 = nil # used for goals (to match players via squads) @last_team2 = nil @last_game = nil ##### # fix: move to read and share event/known_teams # for all 1-n fixture files (no need to configure every time!!) @event = Event.find_by_key!( event_key ) logger.debug "Event #{@event.key} >#{@event.title}<" ### fix: use build_title_table_for ??? why? why not?? ## @known_teams = @event.known_teams_table @mapper_teams = TeamMapper.new( @event.teams ) @known_grounds = TextUtils.build_title_table_for( @event.grounds ) parse_fixtures( reader ) end |
#try_parse_date_header(line) ⇒ Object
656 657 658 659 660 |
# File 'lib/sportdb/readers/game.rb', line 656 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
442 443 444 445 446 |
# File 'lib/sportdb/readers/game.rb', line 442 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 |