Class: SportDb::Model::Game

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

Instance Method Summary collapse

Instance Method Details

#calc_winnerObject



71
72
73
74
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
# File 'lib/sportdb/models/game.rb', line 71

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-game/pre-game !!!
    #    use 1st leg and 2nd leg - use for winner too
    #  or add new winner_total or winner_aggregated method ???

    ## check for penalty  - note: some games 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?


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/sportdb/models/game.rb', line 222

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)


130
# File 'lib/sportdb/models/game.rb', line 130

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

#draw?Boolean

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

Returns:

  • (Boolean)


49
# File 'lib/sportdb/models/game.rb', line 49

def draw?   () winner == 0; end

#knockout?Boolean

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

Returns:

  • (Boolean)


129
# File 'lib/sportdb/models/game.rb', line 129

def knockout?() knockout == true;  end

#over?Boolean

game over?

Returns:

  • (Boolean)


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

def over?()     play_at <= Time.now;  end

#play_at_str(format = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/sportdb/models/game.rb', line 181

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



208
# File 'lib/sportdb/models/game.rb', line 208

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

#score1et_strObject



211
# File 'lib/sportdb/models/game.rb', line 211

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

#score1otObject

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



118
# File 'lib/sportdb/models/game.rb', line 118

def score1ot() score1et  end

#score1ot=(value) ⇒ Object



121
# File 'lib/sportdb/models/game.rb', line 121

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

#score1p_strObject



214
# File 'lib/sportdb/models/game.rb', line 214

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

#score2_strObject



209
# File 'lib/sportdb/models/game.rb', line 209

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

#score2et_strObject



212
# File 'lib/sportdb/models/game.rb', line 212

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

#score2otObject



119
# File 'lib/sportdb/models/game.rb', line 119

def score2ot() score2et  end

#score2ot=(value) ⇒ Object



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

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

#score2p_strObject



215
# File 'lib/sportdb/models/game.rb', line 215

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

#score_strObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/sportdb/models/game.rb', line 193

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



23
# File 'lib/sportdb/models/game.rb', line 23

def team1_name()  team1.name; end

#team1_style_classObject

convenience helpers for styling



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/sportdb/models/game.rb', line 136

def team1_style_class
  buf = ''
  ## NB: remove if calc?

  ### fix: loser
  ## - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals) ??
  ## use new winner_total method ??
 
  if complete?
    if winner1?
      buf << 'game-team-winner '
    elsif winner2?
      buf << 'game-team-loser '
    else # assume draw
      buf << 'game-team-draw '
    end
  end
  
  buf << 'game-knockout '     if knockout?
  buf
end

#team2_nameObject



24
# File 'lib/sportdb/models/game.rb', line 24

def team2_name()  team2.name; end

#team2_style_classObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sportdb/models/game.rb', line 158

def team2_style_class
  buf = ''
  ## NB: remove if calc?

  ### fix: loser
  ## - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals) ??
  ## use new winner_total method ??

  if complete?
    if winner1?
      buf << 'game-team-loser '
    elsif winner2?
      buf << 'game-team-winner '
    else # assume draw
      buf << 'game-team-draw '
    end
  end

  buf << 'game-knockout '     if knockout?
  buf
end

#toto12xObject

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



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

def toto12x() toto1x2; end

#toto1x2Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sportdb/models/game.rb', line 28

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)


47
# File 'lib/sportdb/models/game.rb', line 47

def winner1?() winner == 1; end

#winner2?Boolean

Returns:

  • (Boolean)


48
# File 'lib/sportdb/models/game.rb', line 48

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?



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sportdb/models/game.rb', line 55

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