Class: RacePerformance

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/race_performance.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



200
201
202
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
# File 'app/models/race_performance.rb', line 200

def as_json(options={})
  json = {
    :id => self.id,
    :pos => self.position,
    :name => self.name,
  }
  splits = []
  baseline = case options[:vs]
  when 'median'
    race_instance.median_checkpoint_times
  when 'leader'
    race_instance.winning_performance.split_seconds
  when RacePerformance
    options[:vs].split_seconds.dup
  else
    nil
  end
  if baseline
    splits = self.split_seconds.map{ |cpt| 
      base = baseline.shift # we need to step through the baseline even if this cp is missing
      base - cpt if cpt && base
    }
  else
    splits = self.split_seconds
  end
  intervals = race_instance.fastest_checkpoint_legs
  carry = 0
  json[:splits] = splits.map{|t| [carry += intervals.shift, t]}
  json
end

#calculate_positionObject



109
110
111
112
# File 'app/models/race_performance.rb', line 109

def calculate_position
  update_attribute(:position, race_instance.performances.quicker_than(time_in_seconds).count + 1)
  position
end

#calculate_prizesObject



118
119
120
121
122
123
124
125
# File 'app/models/race_performance.rb', line 118

def calculate_prizes
  prizelist = []
  positions.each do |cat,pos| 
    prizelist << "#{pos.ordinalize} #{cat}" if pos <= 3
  end
  update_attribute(:prizes, prizelist.join(', '))
  prizes
end

#club_nameObject



196
197
198
# File 'app/models/race_performance.rb', line 196

def club_name
  self.club ? self.club.name : 'unattached'
end

#elapsed_timeObject



151
152
153
154
155
156
157
# File 'app/models/race_performance.rb', line 151

def elapsed_time
  if s = time_in_seconds
    s.to_timecode
  else
    ""
  end
end

#elapsed_time=(time) ⇒ Object



159
160
161
# File 'app/models/race_performance.rb', line 159

def elapsed_time=(time)
  write_attribute(:elapsed_time, time.seconds) if time    # numbers will pass through unchanged. strings will be timecode-parsed
end

#finishObject



101
102
103
# File 'app/models/race_performance.rb', line 101

def finish
  finished_at || start + elapsed_time
end

#finished?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'app/models/race_performance.rb', line 188

def finished?
   status == RacePerformanceStatus["Finished"]
end

#neighbourhood(spread = 3) ⇒ Object



139
140
141
# File 'app/models/race_performance.rb', line 139

def neighbourhood(spread=3)
  race_instance.performances.finishing_between(self.position.to_i - spread, self.position.to_i + spread)
end

#neighbours(spread = 3) ⇒ Object



143
144
145
# File 'app/models/race_performance.rb', line 143

def neighbours(spread=3)
  neighbourhood(spread) - [self]
end

#positionObject



105
106
107
# File 'app/models/race_performance.rb', line 105

def position
  read_attribute(:position) || calculate_position
end

#position_in(cat) ⇒ Object



131
132
133
# File 'app/models/race_performance.rb', line 131

def position_in(cat)
  race_instance.performances.eligible_for_category(cat).quicker_than(time_in_seconds).count + 1
end

#positionsObject



127
128
129
# File 'app/models/race_performance.rb', line 127

def positions
  race_instance.categories.containing(category).inject({}) { |h, cat| h.merge( { cat.name => position_in(cat) } ) }
end

#prized?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'app/models/race_performance.rb', line 192

def prized?
  true if prizes.any?
end

#prizesObject



114
115
116
# File 'app/models/race_performance.rb', line 114

def prizes
  read_attribute(:prizes) || calculate_prizes
end

#sparkline_positionsObject



135
136
137
# File 'app/models/race_performance.rb', line 135

def sparkline_positions
  checkpoint_times.map(&:inverted_leg_position)
end

#split_secondsObject



178
179
180
# File 'app/models/race_performance.rb', line 178

def split_seconds
  @split_seconds ||= split_times.map{ |st| st.time_in_seconds if st }
end

#split_timesObject



174
175
176
# File 'app/models/race_performance.rb', line 174

def split_times
  @split_times ||= race_instance.checkpoints.map{ |cp| self.time_at(cp) }
end

#splitsObject



168
169
170
171
172
# File 'app/models/race_performance.rb', line 168

def splits
  self.checkpoint_times.each_with_object({}) do |cpt, hsh| 
    hsh[cpt.race_checkpoint_id] = cpt.elapsed_time
  end
end

#startObject



97
98
99
# File 'app/models/race_performance.rb', line 97

def start
  started_at || race_instance.started_at || checkpoint_times.first.elapsed_time
end

#statusObject



182
183
184
# File 'app/models/race_performance.rb', line 182

def status
  RacePerformanceStatus.find(self.status_id)
end

#status=(value) ⇒ Object



185
186
187
# File 'app/models/race_performance.rb', line 185

def status=(value)
  self.status_id = value.id
end

#time_at(checkpoint) ⇒ Object



163
164
165
166
# File 'app/models/race_performance.rb', line 163

def time_at(checkpoint)
  cpt = checkpoint_times.at_checkpoint(checkpoint)
  cpt.first if cpt.any?
end

#time_in_secondsObject



147
148
149
# File 'app/models/race_performance.rb', line 147

def time_in_seconds
  read_attribute(:elapsed_time)
end