Class: SportDb::Model::Match

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sportdb/models/models/match.rb

Instance Method Summary collapse

Instance Method Details

#calc_winnerObject



75
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
# File 'lib/sportdb/models/models/match.rb', line 75

def calc_winner
  if score1.nil? || score2.nil?
    self.winner90 = nil
    self.winner   = nil
  else
    if score1 > score2
      self.winner90 = 1
    elsif score1 < score2
      self.winner90 = 2
    else # assume score1 == score2 - draw
      self.winner90 = 0
    end

    ## todo/fix:
    #  check for next-match/pre-match !!!
    #    use 1st leg and 2nd leg - use for winner too
    #  or add new winner_total or winner_aggregated method ???

    ## check for penalty  - note: some matches might only have penalty and no extra time (e.g. copa liberatadores)
    if score1p.present? && score2p.present?
      if score1p > score2p
        self.winner = 1
      elsif score1p < score2p
        self.winner = 2
      else
        # issue warning! - should not happen; penalty goes on until winner found!
        puts "*** warn: should not happen; penalty goes on until winner found"
      end
    ## check for extra time
    elsif score1et.present? && score2et.present?
      if score1et > score2et
        self.winner = 1
      elsif score1et < score2et
        self.winner = 2
      else # assume score1et == score2et - draw
        self.winner = 0
      end
    else
      # assume no penalty and no extra time; same as 90min result
      self.winner = self.winner90
    end
  end
end

#check_for_changes(new_attributes) ⇒ Object

todo/fix: find a better name?

todo: move to utils for reuse?


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/sportdb/models/models/match.rb', line 180

def check_for_changes( new_attributes )
  changes_counter = 0
  new_attributes.each do |key,new_value|
    old_value = attributes[ key.to_s ]
    ## todo/fix: also check for class/type matching ????
    if new_value == old_value
      # do nothing
    else
      changes_counter +=1
      puts "change #{changes_counter} for #{key} old:>#{old_value}< : #{old_value.class.name} new:>#{new_value}< : #{new_value.class.name}"
    end
  end

  # no changes found for counter==0;
  # -- otherwise x changes found; return true
  changes_counter == 0 ? false : true
end

#complete?Boolean

Returns:

  • (Boolean)


135
# File 'lib/sportdb/models/models/match.rb', line 135

def complete?() score1.present? && score2.present?;  end

#draw?Boolean

use different name; use an alias (any better names more speaking???)

Returns:

  • (Boolean)


53
# File 'lib/sportdb/models/models/match.rb', line 53

def draw?   () winner == 0; end

#knockout?Boolean

fix/todo: already added by ar magic ??? remove code

Returns:

  • (Boolean)


134
# File 'lib/sportdb/models/models/match.rb', line 134

def knockout?() knockout == true;  end

#over?Boolean

match over?

todo/fix: add back time (hours/minutes) to date if present!!!!

Returns:

  • (Boolean)


131
# File 'lib/sportdb/models/models/match.rb', line 131

def over?()     date <= Date.today;  end

#play_at_str(format = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/sportdb/models/models/match.rb', line 139

def play_at_str( format = nil )
  ## e.g. use like
  #  play_at_str  or
  #  play_at_str( :db ) etc.
  if format == :db
    play_at.strftime( '%Y-%m-%d %H:%M %z' )  # NB: removed seconds (:%S)
  else
    play_at.strftime( "%a. %d. %b. / %H:%M" )
  end
end

#score1_strObject



166
# File 'lib/sportdb/models/models/match.rb', line 166

def score1_str()  score1.nil? ? '-' : score1.to_s;  end

#score1et_strObject



169
# File 'lib/sportdb/models/models/match.rb', line 169

def score1et_str()  score1et.nil? ? '-' : score1et.to_s;  end

#score1otObject

getter/setters for deprecated attribs (score3,4,5,6) n national



122
# File 'lib/sportdb/models/models/match.rb', line 122

def score1ot() score1et  end

#score1ot=(value) ⇒ Object



125
# File 'lib/sportdb/models/models/match.rb', line 125

def score1ot=(value) self.score1et = value  end

#score1p_strObject



172
# File 'lib/sportdb/models/models/match.rb', line 172

def score1p_str()  score1p.nil? ? '-' : score1p.to_s;  end

#score2_strObject



167
# File 'lib/sportdb/models/models/match.rb', line 167

def score2_str()  score2.nil? ? '-' : score2.to_s;  end

#score2et_strObject



170
# File 'lib/sportdb/models/models/match.rb', line 170

def score2et_str()  score2et.nil? ? '-' : score2et.to_s;  end

#score2otObject



123
# File 'lib/sportdb/models/models/match.rb', line 123

def score2ot() score2et  end

#score2ot=(value) ⇒ Object



126
# File 'lib/sportdb/models/models/match.rb', line 126

def score2ot=(value) self.score2et = value  end

#score2p_strObject



173
# File 'lib/sportdb/models/models/match.rb', line 173

def score2p_str()  score2p.nil? ? '-' : score2p.to_s;  end

#score_strObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sportdb/models/models/match.rb', line 151

def score_str
  ## return ' - ' if score1.nil? && score2.nil?

  # note: make after extra time optional;
  # e.g. copa liberatadores only has regular time plus penalty, for example

  buf = ""

  buf << "#{score1_str} : #{score2_str}"
  buf << " / #{score1et_str} : #{score2et_str} n.V."  if score1et.present? || score2et.present?
  buf << " / #{score1p_str} : #{score2p_str} i.E."    if score1p.present?  || score2p.present?

  buf
end

#team1_nameObject



27
# File 'lib/sportdb/models/models/match.rb', line 27

def team1_name()  team1.name; end

#team2_nameObject



28
# File 'lib/sportdb/models/models/match.rb', line 28

def team2_name()  team2.name; end

#toto12xObject

alias for toto12x - todo/fix: use ruby alias helper



31
# File 'lib/sportdb/models/models/match.rb', line 31

def toto12x() toto1x2; end

#toto1x2Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sportdb/models/models/match.rb', line 32

def toto1x2
  ## note: will return string e.g. 1-X-2 (winner will return int e.g. 1-0-2)

  ## fix: use switch/when expr/stmt instead of ifs
  value = winner90   # 1 0 2  1 => team 1 0 => draw 2 => team
  if value == 0
    'X'
  elsif value == 1
    '1'
  elsif value == 2
    '2'
  elsif value == -1
    nil  # ??? - unknown -- include --??? why? why not??
  else
    nil
  end
end

#winner1?Boolean

Returns:

  • (Boolean)


51
# File 'lib/sportdb/models/models/match.rb', line 51

def winner1?() winner == 1; end

#winner2?Boolean

Returns:

  • (Boolean)


52
# File 'lib/sportdb/models/models/match.rb', line 52

def winner2?() winner == 2; end

#winneretObject

winner after extra time (will ignore possible penalty shootout; used for alltime standings in world cup calc, for example)

  • also add winnerp nil,1,2 => nil -> no penalty shoutout (or no scores) – needed for what?



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sportdb/models/models/match.rb', line 59

def winneret
    ## check for extra time
    if score1et.present? && score2et.present?
      if score1et > score2et
        1
      elsif score1et < score2et
        2
      else # assume score1et == score2et - draw
        0
      end
    else
      nil  # no extra time; note: return nil  use  winneret || winner90 to get result for both extra time or if not present regular time
    end
end