Permalink
Browse files

Calculate the expected points margin for each player.

  • Loading branch information...
1 parent 19be643 commit 3b49a7a7fad6201091379c2b23c7becf4f5480fa @dougalcorn dougalcorn committed Jan 31, 2012
Showing with 30 additions and 4 deletions.
  1. +5 −1 app/controllers/players_controller.rb
  2. +12 −0 app/models/player.rb
  3. +13 −3 app/views/players/show.html.erb
@@ -1,7 +1,7 @@
class PlayersController < InheritedResources::Base
respond_to :html, :json
- helper_method :recent_games, :scores_over_time
+ helper_method :recent_games, :scores_over_time, :other_players
def create
super do |format|
@@ -28,4 +28,8 @@ def collection
def recent_games
(Game.all + DoublesGame.all).sort_by(&:created_at).reverse
end
+
+ def other_players
+ Player.all(:order => "rank DESC") - [@player]
+ end
end
View
@@ -61,6 +61,18 @@ def win_expectancy(diff)
1 / ( 10**(-diff.to_f/F_RATING_INTERVAL_SCALE_WEIGHT.to_f) + 1)
end
+ def margin(player)
+ # percentage of points scored needs to exceed We for your rank to go up.
+ we = win_expectancy(rank - player.rank)
+ if we > 0.50
+ # greater than even odds to win
+ 10 - ((10.0 - 10.0*we) / we).round
+ else
+ # less than even odds to win
+ ((10.0*we)/(1.0 - we)).round - 10
+ end
+ end
+
def wins!(opponent, loser_score = 0)
update_attributes!(:rank => new_rank(opponent.rank, percentage_of_points_winning(loser_score)))
end
@@ -38,12 +38,22 @@
<%= image_tag(gravatar_url_for(@player.email, :size => 100)) %>
<div id="rank_graph">
</div>
+
+<h3>Margins</h4>
+<p>This is how many points the rankings say you should win (or lose)
+ by. If you don't beat the margin, your score will go down</p>
+<ul>
+<% other_players.each do |player| -%>
+<li><%= link_to player.display_name, player %>(<%= player.rank %>): <%= @player.margin(player) %></li>
+<% end -%>
+</ul>
+
<% content_for(:sidebar) do %>
<% unless @player.games.count == 0 %>
<p>
- <b>Singles: </b><%= @player.singles_wins %>-<%= @player.singles_losses %>
- <br/>
- <b>Doubles: </b><%= @player.doubles_wins %>-<%= @player.doubles_losses %>
+ <b>Singles: </b><%= @player.singles_wins %>-<%= @player.singles_losses %> <br/>
+ <b>Doubles: </b><%= @player.doubles_wins %>-<%= @player.doubles_losses %> <br />
+ <b>Rank: </b><%= @player.rank %>/<%= @player.doubles_rank %>
</p>
<% end %>

0 comments on commit 3b49a7a

Please sign in to comment.