Skip to content

Commit

Permalink
Stats comparison page
Browse files Browse the repository at this point in the history
  • Loading branch information
jsomers committed Apr 26, 2011
1 parent ecd1e31 commit 072d72a
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 0 deletions.
104 changes: 104 additions & 0 deletions app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
class StatsController < ApplicationController

def pairwise
@no_script = true
@slick_input = true
end

def pairwise_results
p1 = Player.find_by_email(params[:player_1].strip)
p2 = Player.find_by_email(params[:player_2].strip)
if p1 and p2
showdowns = (p1.episodes & p2.episodes)
@results = parsed(showdowns, p1.email, p2.email)
else
if p1
flash[:alert] = "Couldn't find player with e-mail <strong>#{params[:player_2]}</strong>"
elsif p2
flash[:alert] = "Couldn't find player with e-mail <strong>#{params[:player_1]}</strong>"
else
flash[:alert] = "Couldn't find players with either of those e-mail addresses"
end
redirect_to :action => :pairwise
end
end

private

def parsed(showdowns, p1, p2)
episodes = []
wins = {p1 => 0, p2 => 0}
showdowns.each do |episode|
sode = {:game_id => episode.game_id}
if episode.players.length == 2
pos = {episode.players[0].email => 0, episode.players[1].email => 2}
elsif episode.players.length == 3
pos = {episode.players[0].email => 0, episode.players[1].email => 1, episode.players[2].email => 2}
end
# Basic outcome stuff.
scores = {p1 => episode.points[pos[p1]].to_i, p2 => episode.points[pos[p2]].to_i}
if scores[p1] > scores[p2]
sode[:winner] = p1
sode[:loser] = p2
wins[p1] += 1
elsif scores[p2] > scores[p1]
sode[:winner] = p2
sode[:loser] = p1
wins[p2] += 1
else
sode[:winner] = p1
sode[:loser] = p1
end
sode[:pts_final] = scores
# Number of questions each player got right and wrong
# Negative points and positive points for each player
# Scores after single jeopardy, double jeopardy, and final
n_correct = {0 => 0, 1 => 0, 2 => 0}
n_wrong = {0 => 0, 1 => 0, 2 => 0}
pts_single = {0 => 0, 1 => 0, 2 => 0}
pts_double = {0 => 0, 1 => 0, 2 => 0}
for i in (1..6)
for j in (1..5)
begin single = episode.single_table[i][j] rescue next end
begin double = episode.double_table[i][j] rescue next end
begin single_question = Question.find(single[4]) rescue next end
if single_question.value != "DD"
(0..2).each do |i|
if single[i] == 0
n_wrong[i] += 1
pts_single[i] -= single_question.value.to_i
elsif single[i] == 1
n_correct[i] += 1
pts_single[i] += single_question.value.to_i
end
end
end

begin double_question = Question.find(double[4]) rescue next end
if double_question.value != "DD"
(0..2).each do |i|
if double[i] == 0
n_wrong[i] += 1
pts_double[i] -= double_question.value.to_i
elsif double[i] == 1
n_correct[i] += 1
pts_double[i] += double_question.value.to_i
end
end
end
end
end

sode[:pts_single] = {p1 => pts_single[pos[p1]], p2 => pts_single[pos[p2]]}
sode[:pts_double] = {p1 => pts_double[pos[p1]], p2 => pts_double[pos[p2]]}
sode[:n_correct] = {p1 => n_correct[pos[p1]], p2 => n_correct[pos[p2]]}
sode[:n_wrong] = {p1 => n_wrong[pos[p1]], p2 => n_wrong[pos[p2]]}

wagers = Game.find_by_game_id(episode.game_id).questions.select {|q| q.value == "DD"}.collect {|q| q.wagers}.flatten
dds = wagers.collect {|w| begin [w.player.email, w.my_score, w.amount, Guess.find_by_question_id_and_player_id(w.question_id, w.player.id).correct?] rescue nil end}.compact
sode[:dds] = dds
episodes << sode
end
return {:wins => wins.to_a, :episodes => episodes}
end
end
2 changes: 2 additions & 0 deletions app/helpers/stats_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module StatsHelper
end
46 changes: 46 additions & 0 deletions app/views/stats/pairwise.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<%= javascript_include_tag "jquery-1.3.2.min.js" -%>

<style type="text/css">
input::-webkit-input-placeholder {
text-align: center;
}

input:-moz-placeholder {
text-align: center;
}
</style>

<% if (a = flash[:alert]) %>
<div id="flash_alert">
<%= a %>
</div>
<% end %>

<div id="landing" style="margin:0 auto; width: 800px; text-align: center; margin-top: 300px; font-size: 32px;">
<form action="/stats/pairwise_results" method="post" id="go">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<input id="player_1" name="player_1" type="text" class="inactive" onkeypress="keypress(this)" onfocus="enterTextbox(this)" onblur="leaveTextbox(this, 'Player 1')" value="Player 1" style="font-size: 28px; width: 350px; padding: 5px; text-align: center;"/>
<br/>
<span style="color: #777">vs.</span><br/>
<input id="player_2" name="player_2" type="text" class="inactive" onkeypress="keypress(this)" onfocus="enterTextbox(this)" onblur="leaveTextbox(this, 'Player 2')" value="Player 2" style="font-size: 28px; width: 350px; padding: 5px; text-align: center;"/><br/><br/>
<div id="play_now_button" style="float: none; margin: 0 auto;">
<a href="#" id="fight">Fight!</a>
</div>
</form>
</div>

<script type="text/javascript">
$(document).ready(function() {

$("#fight").click(function() {
$("#go").submit();
return false;
})

$("input[type=text]").keypress(function(e) {
if (e.keyCode == "13") {
$("#go").submit();
}
})
});
</script>
66 changes: 66 additions & 0 deletions app/views/stats/pairwise_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<style type="text/css">
body {
font-family: Verdana;
font-size: 12.3px;
}

#container {
margin:0 auto;
width: 800px;
}
</style>

<% sorted = @results[:wins].sort {|a, b| b[1] <=> a[1]} %>
<% winner = sorted.first %>
<% loser = sorted.last %>

<div id="container">

<div style="font-size: 32px; text-align: center;">
<strong><%= winner[0].split("@").first %></strong> vs. <strong><%= loser[0].split("@").first %></strong>
</div>
<div style="font-size: 20px; text-align: center;">
<% if winner[1] != loser[1] %>
<strong><%= winner[0].split("@").first %></strong> is ahead with <%= winner[1] %> games to <%= loser[0].split("@").first %>'s <%= loser[1] %>.
<% else %>
<%= winner[0].split("@").first %> and <%= loser[0].split("@").first %> are tied with <%= winner[1] %> games apiece.
<% end %>
</div>

<div style="font-size: 16px; text-align: center;">
<a href="/stats/pairwise">(compare other players)</a>
</div>

<table style="margin-top: 20px;">
<% @results[:episodes].each_with_index do |sode, i| %>
<% if i % 2 == 0 %>
<tr>
<% end %>
<td valign="top" style="padding: 10px; border: 1px solid #ccc;">
<h3><a href="/play/board/<%= sode[:game_id] %>">Episode #<%= sode[:game_id] %></a></h3>
<% if (sode[:winner] == sode[:loser]) %>
This was a tie game. The scores were:
<% else %>
<strong><%= sode[:winner].split("@").first %></strong> won this episode <%= number_with_delimiter(sode[:pts_final][sode[:winner]]) %> to <%= number_with_delimiter(sode[:pts_final][sode[:loser]]) %>. The scores were:
<% end %>
<ul>
<li><%= number_with_delimiter(sode[:pts_single][sode[:winner]]) %> to <%= number_with_delimiter(sode[:pts_single][sode[:loser]]) %> after Single Jeopardy!</li>
<li><%= number_with_delimiter(sode[:pts_double][sode[:winner]]) %> to <%= number_with_delimiter(sode[:pts_double][sode[:loser]]) %> after Double Jeopardy!</li>
<li><%= number_with_delimiter(sode[:pts_final][sode[:winner]]) %> to <%= number_with_delimiter(sode[:pts_final][sode[:loser]]) %> after Final Jeopardy!</li>
</ul>

<%= sode[:winner].split("@").first %> got <%= pluralize(sode[:n_correct][sode[:winner]], "question") %> and missed <%= pluralize(sode[:n_wrong][sode[:winner]], "question") %>;<br/>
<%= sode[:loser].split("@").first %> got <%= pluralize(sode[:n_correct][sode[:loser]], "question") %> and missed <%= pluralize(sode[:n_wrong][sode[:loser]], "question") %>.<br/><br/>

There were <%= sode[:dds].length %> Daily Double! situations:
<ul>
<% sode[:dds].each do |dd| %>
<li>With <%= number_with_delimiter(dd[1]) %>, <%= dd[0].split("@").first %> wagered <%= number_with_delimiter(dd[2]) %> <% if dd[3] %> and got it right. <% else %> but missed it.<% end %></li>
<% end %>
</ul>
</td>
<% if i % 2 == 1 %>
</tr>
<% end %>
<% end %>
</div>
8 changes: 8 additions & 0 deletions test/functional/stats_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class StatsControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/stats_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class StatsHelperTest < ActionView::TestCase
end

0 comments on commit 072d72a

Please sign in to comment.