class SportDb::TxtMatchWriter

Constants

DATES
DE_WEEKDAY
EN_WEEKDAY
ES_WEEKDAY

en.wikipedia.org/wiki/Date_and_time_notation_in_Spain

FR_MONTH
FR_WEEKDAY
IT_WEEKDAY

en.wikipedia.org/wiki/Date_and_time_notation_in_Italy

LANGS
PT_MONTH
PT_WEEKDAY
SCORES

Public Class Methods

build( matches, lang: 'en', rounds: true ) click to toggle source

note: build returns buf - an (in-memory) string buf(fer)

# File lib/sportdb/writers/txt_writer.rb, line 193
def self.build( matches, lang: 'en', rounds: true )
  ## note: make sure rounds is a bool, that is, true or false  (do NOT pass in strings etc.)
  raise ArgumentError, "rounds flag - bool expected; got: #{rounds.inspect}"    unless rounds.is_a?( TrueClass ) || rounds.is_a?( FalseClass )


  defaults = LANGS[ lang ] || LANGS[ 'en' ]   ## note: fallback for now to english if no defaults defined for lang

  round              = defaults[ :round ]
  format_date        = defaults[ :date ]
  format_score       = defaults[ :score ]
  round_translations = defaults[ :translations ]


  buf = String.new('')

  last_round = nil
  last_date  = nil
  last_time  = nil


  matches.each do |match|

     ## note: make rounds optional (set rounds flag to false to turn off)
     if rounds
       if match.round != last_round
         buf << "\n\n"
         if match.round.is_a?( Integer ) ||
           match.round =~ /^[0-9]+$/   ## all numbers/digits
           if round.is_a?( Proc )
             buf << round.call( match.round )
           else
             ## default "class format
             ##   e.g. Runde 1, Spieltag 1, Matchday 1, Week 1
             buf << "#{round} #{match.round}"
           end
        else ## use as is from match
          ## note: for now assume english names
          if round_translations
            buf << "#{round_translations[match.round] || match.round}"
          else
            buf << "#{match.round}"
          end
        end
        buf << "\n"
       end
     end


     date = if match.date.is_a?( String )
               Date.strptime( match.date, '%Y-%m-%d' )
            else  ## assume it's already a date (object)
               match.date
            end

     time = if match.time.is_a?( String )
              Time.strptime( match.time, '%H:%M')
            else ## assume it's already a time (object) or nil
              match.time
            end


     date_yyyymmdd = date.strftime( '%Y-%m-%d' )

     ## note: time is OPTIONAL for now
     ## note: use 17.00 and NOT 17:00 for now
     time_hhmm     = time ? time.strftime( '%H.%M' ) : nil


     if rounds
       if match.round != last_round || date_yyyymmdd != last_date
          buf << "[#{format_date.call( date )}]\n"
          last_time = nil  ## note: reset time for new date
       end
     else
       if date_yyyymmdd != last_date
          buf << "\n"    ## note: add an extra leading blank line (if no round headings printed)
          buf << "[#{format_date.call( date )}]\n"
          last_time = nil
       end
    end


     ## allow strings and structs for team names
     team1 = match.team1.is_a?( String ) ? match.team1 : match.team1.name
     team2 = match.team2.is_a?( String ) ? match.team2 : match.team2.name


     line = String.new('')
     line << '  '

     if time
        if last_time.nil? || last_time != time_hhmm
          line << "%5s" % time_hhmm
        else
          line << '     '
        end
        line << '  '
     else
      ## do nothing for now
     end


     line << "%-23s" % team1    ## note: use %-s for left-align

     line << "  #{format_score.call( match )}  "  ## note: separate by at least two spaces for now

     line << "%-23s" % team2


     if match.status
      line << '  '
      case match.status
      when Status::CANCELLED
        line << '[cancelled]'
      when Status::AWARDED
        line << '[awarded]'
      when Status::ABANDONED
        line << '[abandoned]'
      when Status::REPLAY
        line << '[replay]'
      when Status::POSTPONED
        ## note: add NOTHING for postponed for now
      else
        puts "!! WARN - unknown match status >#{match.status}<:"
        pp match
        line << "[#{match.status.downcase}]"  ## print "literal" downcased for now
      end
     end

     ## add match line
     buf << line.rstrip   ## remove possible right trailing spaces before adding
     buf << "\n"

     if match.goals
       buf << '    '               # 4 space indent
       buf << '       '  if time   # 7 (5+2) space indent (for hour e.g. 17.30)
       buf << "[#{build_goals(match.goals, lang: lang )}]"
       buf << "\n"
     end


     last_round = match.round
     last_date  = date_yyyymmdd
     last_time  = time_hhmm
  end
  buf
end
build_goals( goals, lang: ) click to toggle source
# File lib/sportdb/writers/txt_writer.rb, line 356
def self.build_goals( goals, lang: )
  ## todo/fix: for now assumes always minutes (without offset) - add offset support

  ## note: "fold" multiple goals by players
  team1_goals = {}
  team2_goals = {}
  goals.each do |goal|
    team_goals = goal.team == 1 ? team1_goals : team2_goals
    player_goals = team_goals[ goal.player ] ||= []
    player_goals << goal
  end

  buf = String.new('')
  if team1_goals.size > 0
    buf << build_goals_for_team( team1_goals, lang: lang )
  end

  ## note: only add a separator (;) if BOTH teams have goal scores
  if team1_goals.size > 0 && team2_goals.size > 0
    buf << '; '
  end

  if team2_goals.size > 0
    buf << build_goals_for_team( team2_goals, lang: lang )
  end
  buf
end
build_goals_for_team( team_goals, lang: ) click to toggle source
# File lib/sportdb/writers/txt_writer.rb, line 385
def self.build_goals_for_team( team_goals, lang: )
  buf = String.new('')
  team_goals.each_with_index do |(player_name, goals),i|
    buf << ' ' if i > 0
    buf << "#{player_name} "
    buf << goals.map do |goal|
                        str = "#{goal.minute}'"
                        if ['de', 'de_AT', 'de_DE', 'de_CH'].include?( lang )
                          str << " (Eigentor)"  if goal.owngoal?
                          str << " (Elf.)"      if goal.penalty?
                        else  ## fallback to english (by default)
                          str << " (o.g.)"      if goal.owngoal?
                          str << " (pen.)"      if goal.penalty?
                        end
                        str
                     end.join( ', ' )
  end
  buf
end
write( path, matches, name:, lang: 'en', rounds: true) click to toggle source
# File lib/sportdb/writers/txt_writer.rb, line 342
def self.write( path, matches, name:, lang: 'en', rounds: true)

  buf = build( matches, lang: lang, rounds: rounds )

  ## for convenience - make sure parent folders/directories exist
  FileUtils.mkdir_p( File.dirname( path) )  unless Dir.exists?( File.dirname( path ))

  File.open( path, 'w:utf-8' ) do |f|
    f.write( "= #{name}\n" )
    f.write( buf )
  end
end