lib/sportdb/reader.rb in sportdb-1.8.1 vs lib/sportdb/reader.rb in sportdb-1.8.2

- old
+ new

@@ -20,10 +20,14 @@ def match_skiers_for_country( name, &blk ) match_xxx_for_country( name, 'skiers', &blk ) end + def match_stadiums_for_country( name, &blk ) + match_xxx_for_country( name, 'stadiums', &blk ) + end + end # module Matcher class Reader @@ -90,10 +94,14 @@ # NB: assume @event is set from previous load race = Race.find_by_event_id_and_pos( @event.id, race_pos ) load_records( name, race_id: race.id ) # e.g. 2013/04-gp-monaco.txt in formula1.db elsif name =~ /(?:^|\/)seasons/ # NB: ^seasons or also possible at-austria!/seasons load_seasons( name ) + elsif match_stadiums_for_country( name ) do |country_key| + country = Country.find_by_key!( country_key ) + load_stadiums( name, country_id: country.id ) + end elsif match_leagues_for_country( name ) do |country_key| # name =~ /^([a-z]{2})\/leagues/ # auto-add country code (from folder structure) for country-specific leagues # e.g. at/leagues country = Country.find_by_key!( country_key ) load_leagues( name, club: true, country_id: country.id ) @@ -135,11 +143,20 @@ logger.error "unknown sportdb fixture type >#{name}<" # todo/fix: exit w/ error end end # method load + + def load_stadiums( name, more_attribs={} ) + reader = ValuesReaderV2.new( name, include_path, more_attribs ) + reader.each_line do |new_attributes, values| + Ground.create_or_update_from_values( new_attributes, values ) + end # each lines + end + + def load_leagues( name, more_attribs={} ) reader = ValuesReaderV2.new( name, include_path, more_attribs ) reader.each_line do |new_attributes, values| @@ -349,12 +366,22 @@ end_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' ) end event_attribs['end_at'] = end_at - elsif key == 'teams' + elsif key == 'grounds' || key == 'stadiums' || key == 'venues' + ## assume grounds value is an array + ground_ids = [] + value.each do |item| + ground_key = item.to_s.strip + ground = Ground.find_by_key!( ground_key ) + ground_ids << ground.id + end + + event_attribs['ground_ids'] = ground_ids + elsif key == 'teams' ## assume teams value is an array team_ids = [] value.each do |item| team_key = item.to_s.strip @@ -682,13 +709,17 @@ @event = Event.find_by_key!( event_key ) logger.debug "Event #{@event.key} >#{@event.title}<" - - @known_teams = @event.known_teams_table - + + ### fix: use build_title_table_for ??? why? why not?? + @known_teams = @event.known_teams_table + + @known_grounds = TextUtils.build_title_table_for( @event.grounds ) + + parse_fixtures( reader ) end # method load_fixtures @@ -810,18 +841,25 @@ date = find_date!( line, start_at: @event.start_at ) end scores = find_scores!( line ) + + map_ground!( line ) + ground_key = find_ground!( line ) + ground = ground_key.nil? ? nil : Ground.find_by_key!( ground_key ) + + logger.debug " line: >#{line}<" ### todo: cache team lookups in hash? team1 = Team.find_by_key!( team1_key ) team2 = Team.find_by_key!( team2_key ) + ### 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 ) @@ -835,9 +873,10 @@ score2p: scores[5], play_at: date, play_at_v2: date_v2, postponed: postponed, knockout: @knockout_flag, + ground_id: ground.present? ? ground.id : nil, group_id: @group.present? ? @group.id : nil } game_attribs[ :pos ] = pos if pos.present?