Class: SportDb::Models::Tip

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_user_and_pool_and_game(user_arg, pool_arg, game_arg) ⇒ Object

todo: rename to find_by_play_and_game ????



149
150
151
152
153
154
# File 'lib/sportdb/play/models/tip.rb', line 149

def self.find_by_user_and_pool_and_game( user_arg, pool_arg, game_arg )
  recs = self.where( user_id: user_arg.id,
                     pool_id: pool_arg.id,
                     game_id: game_arg.id )
  recs.first
end

Instance Method Details

#bingo_style_classObject

todo: use tip-fail, tip-bingo, etc.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/sportdb/play/models/tip.rb', line 235

def bingo_style_class
  if incomplete?
    # show missing label for upcoming games only
    if game.over?
      ''
    elsif score1.blank? || score2.blank?
      'missing'  # missing tip scores
    else
      ''  # tip scores filled in; game scores not yet available
    end
  else
    pts = calc_points()
    if pts == 0
      'fail'
    elsif pts == 1
      'bingo'
    elsif pts == 2
      'bingoo'
    elsif pts == 3
      'bingooo'
    else
      ''  # unknown state; return empty (css) class
    end
  end
end

#bingo_textObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/sportdb/play/models/tip.rb', line 288

def bingo_text
  if incomplete?
    # show missing label for upcoming games only
    if game.over?
      ''
    elsif score1.blank? || score2.blank?
      '?'  # missing tip scores
    else
      ''  # tip scores filled in; game scores not yet available
    end
  else
    pts = calc_points()
    if pts == 0
### FIX: - make extendable how?
###        if game.calc? && (game.team1_id != calc_team1_id || game.team2_id != calc_team2_id )
##          ## sorry, wrong teams - show team1 n team2 tags
##          "× Leider, nein. Richtige Spielpaarung (#{game.team1.tag}) - (#{game.team2.tag})."
##        else
        "× Leider, nein. Richtiger Tipp #{game.toto12x}."  # return 1,2,X from game
##        end
    elsif pts == 1
      '♣ 1 Pkt - Ja!'
    elsif pts == 2
      '♣♣ 2 Pkte - Jaaa!'
    elsif pts == 3
      '♣♣♣ 3 Pkte - Jaaaaa!'
    else
      ''  # unknown state; return empty (css) class
    end
  end
end

#bingo_win_style_classObject

like bingo_style_class but only for pts>0 (that is not for fail)



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/sportdb/play/models/tip.rb', line 262

def bingo_win_style_class
  if incomplete?
    # show missing label for upcoming games only
    if game.over?
      ''
    elsif score1.blank? || score2.blank?
      'missing'  # missing tip scores
    else
      ''  # tip scores filled in; game scores not yet available
    end
  else
    pts = calc_points()
    if pts == 0
      ''
    elsif pts == 1
      'bingo'
    elsif pts == 2
      'bingoo'
    elsif pts == 3
      'bingooo'
    else
      ''  # unknown state; return empty (css) class
    end
  end
end

#calc_pointsObject



220
221
222
223
224
# File 'lib/sportdb/play/models/tip.rb', line 220

def calc_points
  pts = 0
  pts = calc_points_worker()  if complete?
  pts
end

#calc_points_strObject



226
227
228
229
230
# File 'lib/sportdb/play/models/tip.rb', line 226

def calc_points_str
  buf = ''
  calc_points.times { buf << '' }
  buf
end

#calc_points_workerObject



167
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sportdb/play/models/tip.rb', line 167

def calc_points_worker
  pts = 0

    if(((game.score1 == game.score2) && (score1 == score2)) ||
       ((game.score1 >  game.score2) && (score1 >  score2)) ||
       ((game.score1 <  game.score2) && (score1 <  score2)))
        pts += 1
    end

    # tordifferenz richtig? todo: auch fuer unentschieden???
    if((game.score1-game.score2) == (score1-score2))
      ## nb: for now now points for tordifferenz
      ### pts +=1
    end

    # ergebnis richtig?
    if game.score1 == score1 && game.score2 == score2
      pts += 2
    else
      # 2nd chance!
      # -- check 4+ rule for result
      if( [game.score1,4].min == [score1,4].min &&
          [game.score2,4].min == [score2,4].min )
        pts += 2
      end
    end


    ## check n.V. - after extra time/a.e.t

    if( game.score1et.present? && game.score2et.present? && score1et.present? && score2et.present?)
      
       if(((game.score1et == game.score2et) && (score1et == score2et)) ||
          ((game.score1et >  game.score2et) && (score1et >  score2et)) ||
          ((game.score1et <  game.score2et) && (score1et <  score2et)))
              pts += 1
       end
    end

    ## check i.E.

    if( game.score1p.present? && game.score2p.present? && score1p.present? && score2p.present?)
          
       if(((game.score1p > game.score2p) && (score1p > score2p)) ||
          ((game.score1p < game.score2p) && (score1p < score2p)))
              pts += 1
       end
    end
  
  pts
end

#calc_winnerObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sportdb/play/models/tip.rb', line 18

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

#complete?Boolean

Returns:

  • (Boolean)


321
322
323
# File 'lib/sportdb/play/models/tip.rb', line 321

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

#export?Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
# File 'lib/sportdb/play/models/tip.rb', line 157

def export?
  # check if user entered some data
  # - do NOT export nil records (all scores blank)
  
  (score1.blank?   && score2.blank?   &&
   score1et.blank? && score2et.blank? &&
   score1p.blank?  && score2p.blank?) == false
end

#incomplete?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/sportdb/play/models/tip.rb', line 325

def incomplete?
  complete? == false
end

#locked?Boolean

Returns:

  • (Boolean)


329
330
331
332
333
334
# File 'lib/sportdb/play/models/tip.rb', line 329

def locked?
###  FIX: make extendable  (pool.fix? only in addon??)
###     return true if pool.fix? && pool.locked?  # if fix pool is locked all games are (automatically) locked too
  return true if pool.locked?
  game.locked?
end

#public?Boolean

Returns:

  • (Boolean)


336
337
338
339
340
341
342
# File 'lib/sportdb/play/models/tip.rb', line 336

def public?
  return true if pool.public? 
  return true if locked?       # if fix pool is locked or game (make tip public)
  
  ## todo: use builtin utc.past? method ???
  Time.now.utc > game.play_at.utc
end

#score1_strObject



373
# File 'lib/sportdb/play/models/tip.rb', line 373

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

#score1otObject



106
107
108
# File 'lib/sportdb/play/models/tip.rb', line 106

def score1ot
  score1et
end

#score1ot=(value) ⇒ Object



130
131
132
# File 'lib/sportdb/play/models/tip.rb', line 130

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

#score2_strObject



374
# File 'lib/sportdb/play/models/tip.rb', line 374

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

#score2otObject



110
111
112
# File 'lib/sportdb/play/models/tip.rb', line 110

def score2ot
  score2et
end

#score2ot=(value) ⇒ Object



134
135
136
# File 'lib/sportdb/play/models/tip.rb', line 134

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

#score3Object

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



98
99
100
# File 'lib/sportdb/play/models/tip.rb', line 98

def score3
  score1et
end

#score3=(value) ⇒ Object



122
123
124
# File 'lib/sportdb/play/models/tip.rb', line 122

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

#score4Object



102
103
104
# File 'lib/sportdb/play/models/tip.rb', line 102

def score4
  score2et
end

#score4=(value) ⇒ Object



126
127
128
# File 'lib/sportdb/play/models/tip.rb', line 126

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

#score5Object



114
115
116
# File 'lib/sportdb/play/models/tip.rb', line 114

def score5
  score1p
end

#score5=(value) ⇒ Object



138
139
140
# File 'lib/sportdb/play/models/tip.rb', line 138

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

#score6Object



118
119
120
# File 'lib/sportdb/play/models/tip.rb', line 118

def score6
  score2p
end

#score6=(value) ⇒ Object



142
143
144
# File 'lib/sportdb/play/models/tip.rb', line 142

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

#score_strObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/sportdb/play/models/tip.rb', line 344

def score_str

  ## fix: check game
  #  use buf and allow result90 plus penalty only too

  ## fix: use new game.toto12x instead of game.over ??? (doesn't depend on time) 
  if score1.blank? && score2.blank? && game.over?
    # return no data marker (e.g. middot) if not touched by user
    '·'
  else
    str = ''
    if score1p.present? && score2p.present?    # im Elfmeterschiessen i.E.?
      str = "#{score1_str} : #{score2_str} / #{score1et} : #{score2et} n.V. / #{score1p} : #{score2p} i.E."
    elsif score1et.present? && score2et.present?  # nach Verlaengerung n.V.?
      str = "#{score1_str} : #{score2_str} / #{score1et} : #{score2et} n.V."
    else
      str = "#{score1_str} : #{score2_str}"
    end
 
##### FIX: make extendable!!
#      if calc
#        str_calc_team1 = calc_team1_id.blank? ? '' : calc_team1.tag
#        str_calc_team2 = calc_team2_id.blank? ? '' : calc_team2.tag
#        str = "(#{str_calc_team1}) #{str} (#{str_calc_team2})"
#      end
    str
  end
end

#toto12xObject

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



76
# File 'lib/sportdb/play/models/tip.rb', line 76

def toto12x() toto1x2; end

#toto1x2Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sportdb/play/models/tip.rb', line 77

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