class Sports::Matchlist::StatLine

matchlist helpers

Attributes

end_date[R]
goals[R]
matches[R]
rounds[R]
start_date[R]
team_usage[R]

Public Class Methods

new() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 148
def initialize
   @matches    = 0
   @goals      = 0

   @start_date = nil
   @end_date   = nil

   @team_usage = Hash.new(0)

   @match_counts = nil
end

Public Instance Methods

build_match_counts() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 119
def build_match_counts   ## use/rename to matches_played - why? why not?
  counts = Hash.new(0)
  team_usage.values.each do |count|
    counts[count] += 1
  end

  ## sort (descending) highest usage value first (in returned array)
  ##  e.g. [[32,8],[31,2]]  ## 32 matches by 8 teams, 31 matches by 2 teams etc.
  counts.sort_by {|count, usage| -count }
end
dates_str() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 84
def dates_str
  ## note: start_date/end_date might be optional/missing
  if has_dates?
    "#{start_date.strftime( '%a %d %b %Y' )} - #{end_date.strftime( '%a %d %b %Y' )}"
  else
    "??? - ???"
  end
end
days() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 93
def days() end_date.jd - start_date.jd; end
end_date?() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 81
def end_date?()   @end_date.nil? == false; end
has_dates?() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 83
def has_dates?()  @start_date && @end_date; end
match_counts() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 130
def match_counts
  # match counts / nos played per team
  @match_counts ||= build_match_counts
  @match_counts
end
match_counts_str() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 136
def match_counts_str
  ## pretty print / formatted match_counts
  buf = String.new('')
  match_counts.each_with_index do |rec,i|
    buf << ' '  if i > 0   ## add (space) separator
    buf << "#{rec[0]}×#{rec[1]}"
  end
  buf
end
rounds?() click to toggle source

todo: add has_rounds? alias for rounds? too

# File lib/sportdb/structs/structs/matchlist.rb, line 102
def rounds?
   ## return true if all match_played in team_usage are the same
   ##  e.g. assumes league with matchday rounds
   if @has_rounds.nil?    ## check/todo: if undefined attribute is nil by default??
      ## check/calc rounds
      ##  note: values => matches_played by team
      if match_counts.size == 1
        @rounds = match_counts[0][0]
      else
        @rounds = nil
      end
      @has_rounds = @rounds ? true : false
   end
   @has_rounds
end
start_date?() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 80
def start_date?() @start_date.nil? == false; end
teams() click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 78
def teams() @team_usage.keys.sort; end
update( match ) click to toggle source
# File lib/sportdb/structs/structs/matchlist.rb, line 161
def update( match )
   @matches += 1    ## match counter

   if match.score1 && match.score2
     @goals += match.score1
     @goals += match.score2

     ## todo: add after extra time? if knock out (k.o.) - why? why not?
     ##   make it a flag/opt?
   end

   @team_usage[ match.team1 ] += 1
   @team_usage[ match.team2 ] += 1

   if match.date
     ## return / store date as string as is - why? why not?
     date = Date.strptime( match.date, '%Y-%m-%d' )

     @start_date = date  if @start_date.nil? || date < @start_date
     @end_date   = date  if @end_date.nil?   || date > @end_date
   end
end