Class: SportDb::Model::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 ????



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

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/sportdb/play/models/tip.rb', line 222

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 || pts == 2
      'bingo'
    elsif pts == 3
      'bingoo'
    elsif pts >= 4     # note; use greater 4 for now - add more bingooo states? how to deal w/ custom points - normalize - how ??
      'bingooo'
    else
      ''  # unknown state; return empty (css) class
    end
  end
end

#bingo_textObject



275
276
277
278
279
280
281
282
283
284
285
286
287
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
# File 'lib/sportdb/play/models/tip.rb', line 275

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!'
    elsif pts == 4
      '♣♣♣ 4 Pkte - Jaaaaaa!'
    elsif pts == 5
      '♣♣♣ 5 Pkte - Jaaaaaaa!'
    elsif pts == 6
      '♣♣♣ 6 Pkte - Jaaaaaaaa!'
    elsif pts == 7
      '♣♣♣ 7 Pkte - Jaaaaaaaaa!'
    elsif pts == 8
      '♣♣♣ 8 Pkte - Wahnsinnn!'   # max points?  2pt+2pt + (1pt+1pt) + (1pt+1pt)
    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)



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/sportdb/play/models/tip.rb', line 249

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 || pts == 2
      'bingo'
    elsif pts == 3
      'bingoo'
    elsif pts >= 4
      'bingooo'
    else
      ''  # unknown state; return empty (css) class
    end
  end
end

#calc_pointsObject



207
208
209
210
211
# File 'lib/sportdb/play/models/tip.rb', line 207

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

#calc_points_strObject



213
214
215
216
217
# File 'lib/sportdb/play/models/tip.rb', line 213

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

#calc_points_workerObject



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
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
# File 'lib/sportdb/play/models/tip.rb', line 134

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 += 2
    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)))

             ### note: disqualify (no pts) if tip for regular time is NOT draw/tie
             ###
             if score1 == score2
               pts += 1
              
               # ergebnis richtig? extra point
               if game.score1et == score1et && game.score2et == score2et
                 pts += 1
               end
             end
       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)))

             ### note: disqualify (no pts) if tip for regular and extra time is NOT draw/tie
             ###
             if score1 == score2 && score1et == score2et
               pts += 1

               # ergebnis richtig? extra point
               if game.score1p == score1p && game.score2p == score2p
                 pts += 1
               end
             end
       end
    end

  pts
end

#calc_winnerObject



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
61
# File 'lib/sportdb/play/models/tip.rb', line 19

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)


318
319
320
# File 'lib/sportdb/play/models/tip.rb', line 318

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

#export?Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
# File 'lib/sportdb/play/models/tip.rb', line 124

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)


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

def incomplete?
  complete? == false
end

#locked?Boolean

Returns:

  • (Boolean)


326
327
328
329
330
331
# File 'lib/sportdb/play/models/tip.rb', line 326

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)


333
334
335
336
337
338
339
# File 'lib/sportdb/play/models/tip.rb', line 333

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



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

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

#score1otObject



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

def score1ot()  score1et;  end

#score1ot=(value) ⇒ Object



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

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

#score2_strObject



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

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

#score2otObject



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

def score2ot()  score2et;  end

#score2ot=(value) ⇒ Object



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

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

#score3Object

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



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

def score3()    score1et;  end

#score3=(value) ⇒ Object



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

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

#score4Object



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

def score4()    score2et;  end

#score4=(value) ⇒ Object



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

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

#score5Object



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

def score5()    score1p;   end

#score5=(value) ⇒ Object



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

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

#score6Object



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

def score6()    score2p;   end

#score6=(value) ⇒ Object



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

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

#score_strObject



341
342
343
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
# File 'lib/sportdb/play/models/tip.rb', line 341

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



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

def toto12x() toto1x2; end

#toto1x2Object



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

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