Class: SportDb::Model::Game

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_ary!(games, round, knockout = false) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sportdb/models/game.rb', line 115

def self.create_from_ary!( games, round, knockout=false )

### fix:
#  replace knockout=false with more attribs
#    see create teams and than merge attribs

  games.each_with_index do |values,index|
    
    value_pos      = index+1
    value_scores   = []
    value_teams    = []
    value_knockout = knockout
    value_play_at  = round.start_at  # if no date present use it from round
    value_group    = nil
    
    ### lets you use arguments in any order
    ##   makes pos optional (if not present counting from 1 to n)
    
    values.each do |value|
      if value.kind_of? Numeric
        value_pos = value
      elsif value.kind_of?( TrueClass ) || value.kind_of?( FalseClass )
        value_knockout = value
      elsif value.kind_of? Array
        value_scores = value
      elsif value.kind_of? Team
        value_teams << value
      elsif value.kind_of? Group
        value_group = value
      elsif value.kind_of?( Date ) || value.kind_of?( Time ) || value.kind_of?( DateTime )
        value_play_at = value
      else
        # issue an error/warning here
      end
    end

    Game.create!(
      :round     => round,
      :pos       => value_pos,
      :team1     => value_teams[0],
      :score1    => value_scores[0],
      :score2    => value_scores[1],
      :score1et  => value_scores[2],
      :score2et  => value_scores[3],
      :score1p   => value_scores[4],
      :score2p   => value_scores[5],
      :team2     => value_teams[1],
      :play_at   => value_play_at,
      :group     => value_group,     # Note: group is optional (may be null/nil)
      :knockout  => value_knockout )
  end # each games
end

.create_knockout_pairs_from_ary!(pairs, round1, round2) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/sportdb/models/game.rb', line 203

def self.create_knockout_pairs_from_ary!( pairs, round1, round2 )
  
  pairs.each do |pair|
    game1_attribs = {
      :round     =>round1,
      :pos       =>pair[0][0],
      :team1     =>pair[0][1],
      :score1    =>pair[0][2][0],
      :score2    =>pair[0][2][1],
      :team2     =>pair[0][3],
      :play_at   =>pair[0][4] }

    game2_attribs = {
      :round     =>round2,
      :pos       =>pair[1][0],
      :team1     =>pair[1][1],
      :score1    =>pair[1][2][0],
      :score2    =>pair[1][2][1],
      :score1et  =>pair[1][2][2],
      :score2et  =>pair[1][2][3],
      :score1p   =>pair[1][2][4],
      :score1p   =>pair[1][2][5],
      :team2     =>pair[1][3],
      :play_at   =>pair[1][4],
      :knockout  =>true }

    game1 = Game.create!( game1_attribs )
    game2 = Game.create!( game2_attribs )

    # linkup games
    game1.next_game_id = game2.id
    game1.save!

    game2.prev_game_id = game1.id
    game2.save!
  end # each pair
end

.create_knockouts_from_ary!(games, round) ⇒ Object



111
112
113
# File 'lib/sportdb/models/game.rb', line 111

def self.create_knockouts_from_ary!( games, round )
  Game.create_from_ary!( games, round, true )
end

.create_pairs_from_ary_for_group!(pairs, group) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/sportdb/models/game.rb', line 168

def self.create_pairs_from_ary_for_group!( pairs, group )
  
  pairs.each do |pair|
    game1_attribs = {
      :round     =>pair[0][5],
      :pos       =>pair[0][0],
      :team1     =>pair[0][1],
      :score1    =>pair[0][2][0],
      :score2    =>pair[0][2][1],
      :team2     =>pair[0][3],
      :play_at   =>pair[0][4],
      :group     =>group }

    game2_attribs = {
      :round     =>pair[1][5],
      :pos       =>pair[1][0],
      :team1     =>pair[1][1],
      :score1    =>pair[1][2][0],
      :score2    =>pair[1][2][1],
      :team2     =>pair[1][3],
      :play_at   =>pair[1][4],
      :group     =>group }

    game1 = Game.create!( game1_attribs )
    game2 = Game.create!( game2_attribs )

    # linkup games
    game1.next_game_id = game2.id
    game1.save!

    game2.prev_game_id = game1.id
    game2.save!
  end # each pair
end

Instance Method Details

#calc_winnerObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sportdb/models/game.rb', line 48

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?


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/sportdb/models/game.rb', line 345

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)


251
252
253
# File 'lib/sportdb/models/game.rb', line 251

def complete?
  score1.present? && score2.present?
end

#draw?Boolean

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

Returns:

  • (Boolean)


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

def draw?   () winner == 0; end

#knockout?Boolean

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

Returns:

  • (Boolean)


247
248
249
# File 'lib/sportdb/models/game.rb', line 247

def knockout?
  knockout == true
end

#over?Boolean

game over?

Returns:

  • (Boolean)


242
243
244
# File 'lib/sportdb/models/game.rb', line 242

def over?   # game over?
  play_at <= Time.now
end

#play_at_str(format = nil) ⇒ Object



304
305
306
307
308
309
310
311
312
313
# File 'lib/sportdb/models/game.rb', line 304

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



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

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

#score1et_strObject



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

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

#score1otObject



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

def score1ot() score1et  end

#score1ot=(value) ⇒ Object



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

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

#score1p_strObject



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

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

#score2_strObject



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

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

#score2et_strObject



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

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

#score2otObject



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

def score2ot() score2et  end

#score2ot=(value) ⇒ Object



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

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

#score2p_strObject



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

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

#score3Object

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



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

def score3()   score1et  end

#score3=(value) ⇒ Object



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

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

#score4Object



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

def score4()   score2et  end

#score4=(value) ⇒ Object



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

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

#score5Object



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

def score5()   score1p   end

#score5=(value) ⇒ Object



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

def score5=(value)   self.score1p = value   end

#score6Object



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

def score6()   score2p   end

#score6=(value) ⇒ Object



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

def score6=(value)   self.score2p = value   end

#score_strObject



316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/sportdb/models/game.rb', line 316

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_style_classObject

convenience helpers for styling



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/sportdb/models/game.rb', line 259

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_style_classObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/sportdb/models/game.rb', line 281

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



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

def toto12x() toto1x2; end

#toto1x2Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sportdb/models/game.rb', line 24

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)


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

def winner1?() winner == 1; end

#winner2?Boolean

Returns:

  • (Boolean)


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

def winner2?() winner == 2; end