Skip to content

Commit

Permalink
new stats page
Browse files Browse the repository at this point in the history
  • Loading branch information
jpslav committed Apr 14, 2011
1 parent 8a1cd14 commit 2744461
Show file tree
Hide file tree
Showing 9 changed files with 15,236 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/league_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,17 @@ def mini_chase_points(start_datetime, end_datetime)

points
end

def last_stable
RaceStable.find_by_league_membership_id_and_race_id(id, Race.lastRace.id)
end

def personal_best_week_points
race_stables.collect{|s| s.points}.max
end

def personal_worst_week_points
race_stables.collect{|s| s.points}.min
end

end
83 changes: 83 additions & 0 deletions app/models/league_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class LeagueStats

attr_reader :mini_chase_points
attr_reader :ranked_members
attr_reader :last_week_points
attr_reader :previous_standings
attr_reader :standings
attr_reader :standings_change
attr_reader :park_and_stop_awards
attr_reader :id_with_highest_personal_best
attr_reader :id_with_lowest_personal_best
attr_reader :num_races

def initialize(league)
members = league.league_memberships
member_ids = members.collect{|m| m.id}

# Put in logic for before first race of each chase, etc

@mini_chase_points = members.make_hash{|m| [m.id, m.current_mini_chase_points]}
@ranked_members = members.sort{|a,b| b.current_mini_chase_points <=> a.current_mini_chase_points}
@last_week_points = members.make_hash{|m| [m.id, m.last_stable.nil? ? 0 : m.last_stable.points]}

previous_mini_chase_points = {}
@mini_chase_points.each_pair{|id, points| previous_mini_chase_points[id] = points-@last_week_points[id]}

previous_ordered_member_ids = previous_mini_chase_points.sort{|a,b| b[1]<=>a[1]}.collect{|x| x[0]}

@previous_standings = {}
standing = 0
previous_ordered_member_ids.each do |id|
@previous_standings[id] = standing += 1
end

@standings = {}
standing = 0
@ranked_members.each do |rm|
@standings[rm.id] = standing += 1
end

@standings_change = {}
@standings.each_pair{|id, standing| @standings_change[id] = -standing + @previous_standings[id]}

@personal_bests = {}
@personal_worsts = {}
members.each do |member|
@personal_bests[member.id] = member.personal_best_week_points
@personal_worsts[member.id] = member.personal_worst_week_points
end

@id_with_highest_personal_best = @personal_bests.max_by{|a| a[1]}[0]
@id_with_lowest_personal_best = @personal_worsts.min_by{|a| a[1]}[0]

@park_and_stop_awards = {}
member_ids.each{|id| @park_and_stop_awards[id] = 0}

races = Race.racesUpToToday

@num_races = races.length

races.each do |race|
stables = RaceStable.all(:conditions => ["league_membership_id IN (?) AND race_id = ?",
member_ids, race.id])
stable_points = stables.make_hash{|s| [s.points, s.league_membership.id]}
sorted_stable_points = stable_points.sort

loser_ids = (sorted_stable_points.select{|s| s[0] == sorted_stable_points[0][0]}).collect{|s| s[1]}

if loser_ids.length == 1
loser_id = loser_ids[0]
else
loser_stables = stables.select{|s| loser_ids.include?(s.league_membership.id)}
reduced = loser_stables.make_hash{|ls| [ls.worst_car_points, ls.league_membership.id]}
loser_id = reduced.sort[0][1]
end

@park_and_stop_awards[loser_id] += 1
end


end

end
6 changes: 6 additions & 0 deletions app/models/race_stable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ def points
#points = results.sum {|result| (orderedCarIds.include?(result.car_id) ? result.points_delta * orderedCarIds.count(result.car_id) : 0)}
points = results.sum {|result| result.points_delta * orderedCarIds.count(result.car_id)}
end

def worst_car_points
results = RaceResult.find_all_by_race_id(race_id).to_a
points = results.collect{|result| result.points_delta * orderedCarIds.count(result.car_id)}
points.min
end
end
38 changes: 38 additions & 0 deletions app/views/league_details/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<div id="pageHeading">League Details: <%= league.name %></div>




<div id="sectionHeading_no_under">Standings</div>

Expand All @@ -25,6 +27,42 @@
<% end %>
</table>

<div id="sectionHeading_no_under">Stats</div>

<% stats = LeagueStats.new(league) %>

<table id="list" width="100%">
<tr>
<th width="" colspan=2>Standings</th>
<th width="">Owner</th>
<th width="">Points<br/>(Chase)</th>
<th width="">Points<br/>(Week)</th>
<th width="">Avg/Week</th>
<th width="">Avg/Driver</th>
<th width="">Best Single<br/>Race Points</th>
<th width="">Worst Single<br/>Race Points</th>
<th width="">Start &amp; Park<br/>Awards</th>
</tr>
<% standing = 0 %>
<% members.each do |member| %>
<tr class="<%= cycle("evenrow", "oddrow") %>">
<td><%= stats.standings[member.id] %>.</td>
<% change = stats.standings_change[member.id] %>
<% change_str = "%+d" % change %>
<td><%= change != 0 ? "(#{change_str})" : ""%></td>
<td><%= member.owner.user.first_name %></td>
<td><%= stats.mini_chase_points[member.id] %></td>
<td><%= stats.last_week_points[member.id] %></td>
<td><%= stats.mini_chase_points[member.id]/stats.num_races %></td>
<td><%=stats.mini_chase_points[member.id]/stats.num_races/4 %></td>
<td <% if stats.id_with_highest_personal_best == member.id %>class="highlight_cell"<%end%>>
<%= member.personal_best_week_points %></td>
<td <% if stats.id_with_lowest_personal_best == member.id %>class="highlight_cell"<%end%>><%= member.personal_worst_week_points %></td>
<td><%= stats.park_and_stop_awards[member.id] %></td>
</tr>
<% end %>
</table>

<div id="sectionHeading_no_under">Owned Stables</div>

<table id="list" width="100%">
Expand Down
2 changes: 2 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

require 'ruby_extensions'

ActionMailer::Base.default :from => "GFTL Bot <[email protected]>"
ActionMailer::Base.default :sender => "GFTL Bot <[email protected]>"
ActionMailer::Base.delivery_method = :smtp
Expand Down
Loading

0 comments on commit 2744461

Please sign in to comment.