| Class | MbraTeamBar |
| In: |
app/models/competitions/mbra_team_bar.rb
|
| Parent: | Competition |
# File app/models/competitions/mbra_team_bar.rb, line 3
3: def MbraTeamBar.calculate!(year = Date.today.year)
4: benchmark(name, Logger::INFO, false) {
5: transaction do
6: year = year.to_i if year.is_a?(String)
7: date = Date.new(year, 1, 1)
8:
9: # Maybe I will exclude MTB and CX if people are disturbed by it...but calc for now for fun.
10: Discipline.find_all_bar.reject {|discipline| discipline == Discipline[:age_graded] || discipline == Discipline[:overall]}.each do |discipline|
11: bar = MbraTeamBar.find(:first, :conditions => { :date => date, :discipline => discipline.name })
12: logger.debug("In MbraTeamBar.calculate!: discipline #{discipline}") if logger.debug?
13: unless bar
14: bar = MbraTeamBar.create!(
15: :name => "#{year} #{discipline.name} BAT",
16: :date => date,
17: :discipline => discipline.name
18: )
19: end
20: end
21:
22: MbraTeamBar.find(:all, :conditions => { :date => date }).each do |bar|
23: logger.debug("In MbraTeamBar.calculate!: processing bar #{bar.name}") if logger.debug?
24: bar.destroy_races
25: bar.create_races
26: bar.calculate!
27: end
28: end
29: }
30: # Don't return the entire populated instance!
31: true
32: end
Riders obtain points inversely proportional to the starting field size, plus bonuses for first, second and third place (6, 3, and 1 points respectively). DNF: 1/2 point
# File app/models/competitions/mbra_team_bar.rb, line 41
41: def calculate_point_schedule(field_size)
42: @point_schedule = [0]
43: field_size.downto(1) { |i| @point_schedule << i }
44: @point_schedule[1] += 6 if @point_schedule.length > 1
45: @point_schedule[2] += 3 if @point_schedule.length > 2
46: @point_schedule[3] += 1 if @point_schedule.length > 3
47: end
create a competition_result for each team appearing in this set of results, which is per race
# File app/models/competitions/mbra_team_bar.rb, line 74
74: def create_competition_results_for(results, race)
75: competition_result = nil
76: for source_result in results
77: logger.debug("#{self.class.name} scoring result: #{source_result.event.name} | #{source_result.race.name} | #{source_result.place} | #{source_result.name} | #{source_result.team_name}") if logger.debug?
78: # e.g., MbraTeamBar scoring result: Belt Creek Omnium - Highwood TT | Master A Men | 4 | Steve Zellmer | Northern Rockies Cycling
79:
80: # I don't need this now: no allowance for TTT or tandem teams with members of multiple teams
81: # teams = extract_teams_from(source_result)
82: # logger.debug("#{self.class.name} teams for result: #{teams}") if logger.debug?
83: # for team in teams
84:
85: if member?(source_result.team, source_result.date)
86:
87: if first_result_for_team(source_result, competition_result)
88: # Bit of a hack here, because we split tandem team results into two results,
89: # we can't guarantee that results are in team-order.
90: # So 'first result' really means 'not the same as last result'
91: # race here is the category in the competition
92: competition_result = race.results.detect {|result| result.team == source_result.team}
93: competition_result = race.results.create(:team => source_result.team) if competition_result.nil?
94: end
95:
96: # limit to top two results for team for each source race by
97: # removing the lowest score for the event after every result.
98: # I do it this way because results do not arrive in postion order.
99: # SQL sorting does not work as position is a varchar field.
100: score = competition_result.scores.create(
101: :source_result => source_result,
102: :competition_result => competition_result,
103: :points => points_for(source_result).to_f #/ teams.size
104: )
105: this_points = score.points
106: logger.debug("#{self.class.name} competition result: #{competition_result.event.name} | #{competition_result.race.name} | #{competition_result.place} | #{competition_result.team_name}") if logger.debug?
107: # e.g., MbraTeamBar competition result: 2009 MBRA BAT | Master A Men | | Gallatin Alpine Sports/Intrinsik Architecture
108: scores_for_event = competition_result.scores.select{ |s| s.source_result.event.name == source_result.event.name}
109: if scores_for_event.size > 2
110: competition_result.scores.delete(scores_for_event.min { |a,b| a.points <=> b.points })
111: end
112: end
113: end
114: end
# File app/models/competitions/mbra_team_bar.rb, line 140
140: def create_races
141: logger.debug("In create_races: discipline #{discipline}") if logger.debug?
142: Discipline[discipline].bar_categories.each do |category|
143: logger.debug("In create_race: category #{category}") if logger.debug?
144: races.create!(:category => category)
145: end
146: end
# File app/models/competitions/mbra_team_bar.rb, line 116
116: def first_result_for_team(source_result, competition_result)
117: competition_result.nil? || source_result.team != competition_result.team
118: end
# File app/models/competitions/mbra_team_bar.rb, line 148
148: def friendly_name
149: 'MBRA BAT'
150: end
# File app/models/competitions/mbra_team_bar.rb, line 34
34: def point_schedule
35: @point_schedule = @point_schedule || []
36: end
Apply points from point_schedule Points are awarded using the same criteria as for individuals except that there are no double points awarded for State Championships.
# File app/models/competitions/mbra_team_bar.rb, line 123
123: def points_for(source_result, team_size = nil)
124: calculate_point_schedule(source_result.race.field_size)
125: points = 0
126: MbraTeamBar.benchmark('points_for') {
127: if source_result.place.strip.downcase == "dnf"
128: points = 0.5
129: else
130: # if multiple riders got the same place (must be a TTT or tandem team or... ?), then they split the points...
131: #this screws up the scoring of match sprints where riders eliminatined in qualifying heats all earn the same place
132: #team_size = team_size || Result.count(:conditions => ["race_id =? and place = ?", source_result.race.id, source_result.place])
133: points = point_schedule[source_result.place.to_i] #/ team_size.to_f
134: end
135: }
136: logger.debug("#{self.class.name} points: #{points} for #{source_result.event.name} | #{source_result.race.name} | #{source_result.place} | #{source_result.name} | #{source_result.team_name}") if logger.debug?
137: points
138: end
Find the source results from discipline BAR‘s competition results. this does not work for mbra due to the 70%-of-events rule for bar that does not apply to bat
# File app/models/competitions/mbra_team_bar.rb, line 51
51: def source_results(race)
52: race_disciplines = "'#{race.discipline}'"
53: category_ids = category_ids_for(race)
54: Result.find(:all,
55: :include => [:race, {:person => :team}, :team, {:race => [{:event => { :parent => :parent }}, :category]}],
56: :conditions => [%Q{
57: (events.type in ('Event', 'SingleDayEvent', 'MultiDayEvent') or events.type is NULL)
58: and bar = true
59: and categories.id in (#{category_ids})
60: and (events.discipline in (#{race_disciplines})
61: or (events.discipline is null and parents_events.discipline in (#{race_disciplines}))
62: or (events.discipline is null and parents_events.discipline is null and parents_events_2.discipline in (#{race_disciplines})))
63: and (races.bar_points > 0
64: or (races.bar_points is null and events.bar_points > 0)
65: or (races.bar_points is null and events.bar_points is null and parents_events.bar_points > 0)
66: or (races.bar_points is null and events.bar_points is null and parents_events.bar_points is null and parents_events_2.bar_points > 0))
67: and events.date between '#{date.year}-01-01' and '#{date.year}-12-31'
68: }],
69: :order => 'results.team_id, race_id'
70: )
71: end