Class: SportDb::Models::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



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
114
115
116
117
118
119
120
121
122
123
# File 'lib/sportdb/models/game.rb', line 72

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



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

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



68
69
70
# File 'lib/sportdb/models/game.rb', line 68

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



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

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_toto12xObject



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/sportdb/models/game.rb', line 200

def calc_toto12x
  if score1.nil? || score2.nil?
    self.toto12x = nil
  elsif score1 == score2
    self.toto12x = 'X'
  elsif score1 > score2
    self.toto12x = '1'
  elsif score1 < score2
    self.toto12x = '2'
  end
end

#complete?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/sportdb/models/game.rb', line 222

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

#knockout?Boolean

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

Returns:

  • (Boolean)


218
219
220
# File 'lib/sportdb/models/game.rb', line 218

def knockout?
  knockout == true
end

#over?Boolean

game over?

Returns:

  • (Boolean)


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

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

#play_at_str(format = nil) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/sportdb/models/game.rb', line 252

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



276
277
278
# File 'lib/sportdb/models/game.rb', line 276

def score1_str
  if score1.blank? then '-' else score1.to_s end
end

#score1otObject



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

def score1ot
  score1et
end

#score1ot=(value) ⇒ Object



50
51
52
# File 'lib/sportdb/models/game.rb', line 50

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

#score2_strObject



280
281
282
# File 'lib/sportdb/models/game.rb', line 280

def score2_str
  if score2.blank? then '-' else score2.to_s end
end

#score2otObject



30
31
32
# File 'lib/sportdb/models/game.rb', line 30

def score2ot
  score2et
end

#score2ot=(value) ⇒ Object



54
55
56
# File 'lib/sportdb/models/game.rb', line 54

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

#score3Object

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



18
19
20
# File 'lib/sportdb/models/game.rb', line 18

def score3
  score1et
end

#score3=(value) ⇒ Object



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

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

#score4Object



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

def score4
  score2et
end

#score4=(value) ⇒ Object



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

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

#score5Object



34
35
36
# File 'lib/sportdb/models/game.rb', line 34

def score5
  score1p
end

#score5=(value) ⇒ Object



58
59
60
# File 'lib/sportdb/models/game.rb', line 58

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

#score6Object



38
39
40
# File 'lib/sportdb/models/game.rb', line 38

def score6
  score2p
end

#score6=(value) ⇒ Object



62
63
64
# File 'lib/sportdb/models/game.rb', line 62

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

#score_strObject



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/sportdb/models/game.rb', line 264

def score_str
  return ' - ' if score1.blank? && score2.blank?
  
  if score1p.present? && score2p.present?    # im Elfmeterschiessen i.E.?
    "#{score1_str} : #{score2_str} / #{score1et} : #{score2et} n.V. / #{score1p} : #{score2p} i.E."
  elsif score1et.present? && score2et.present?  # nach Verlaengerung n.V.?
    "#{score1_str} : #{score2_str} / #{score1et} : #{score2et} n.V."
  else
    "#{score1_str} : #{score2_str}"
  end
end

#team1_style_classObject

convenience helpers for styling



229
230
231
232
233
234
235
236
237
238
# File 'lib/sportdb/models/game.rb', line 229

def team1_style_class
  buf = ''
  ## NB: remove if calc?
  buf << 'game-team-winner '  if complete? && (score1 >  score2)
  buf << 'game-team-draw '    if complete? && (score1 == score2)
  buf << 'game-knockout '     if knockout?
  ### fix: loser - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals)
  buf << 'game-team-loser '   if complete? && (score1 < score2)
  buf
end

#team2_style_classObject



240
241
242
243
244
245
246
247
248
249
# File 'lib/sportdb/models/game.rb', line 240

def team2_style_class
  buf = ''
  ## NB: remove if calc?
  buf << 'game-team-winner '  if complete? && (score2 >  score1)
  buf << 'game-team-draw '    if complete? && (score2 == score1)
  buf << 'game-knockout '     if knockout?
  ### fix: loser - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals)
  buf << 'game-team-loser '   if complete? && (score2 < score1)
  buf
end