summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/brackets_controller.rb21
-rw-r--r--app/controllers/matches_controller.rb1
-rw-r--r--app/models/bracket.rb23
-rw-r--r--app/models/bracket_match.rb4
-rw-r--r--app/models/match.rb6
-rw-r--r--app/models/statistic.rb2
-rw-r--r--app/views/brackets/show.html.erb48
-rw-r--r--app/views/common/_show_tournament.html.erb8
-rw-r--r--db/seeds.rb2
-rw-r--r--lib/sampling/riot_api.rb15
-rw-r--r--lib/scheduling/elimination.rb5
-rw-r--r--lib/throttled_api_request.rb4
12 files changed, 96 insertions, 43 deletions
diff --git a/app/controllers/brackets_controller.rb b/app/controllers/brackets_controller.rb
index ac34bfe..e202c96 100644
--- a/app/controllers/brackets_controller.rb
+++ b/app/controllers/brackets_controller.rb
@@ -11,7 +11,8 @@ class BracketsController < ApplicationController
# GET /brackets/1
# GET /brackets/1.json
def show
- @matches = @tournament.stages.first.matches_ordered
+ @results = (@tournament.status == 4)? @bracket.calcResult : nil;
+ @matches = @tournament.stages.order(:id).first.matches_ordered
@numTeams = @tournament.min_teams_per_match
@logBase = @numTeams
@@ -53,11 +54,11 @@ class BracketsController < ApplicationController
# PATCH/PUT /brackets/1.json
def update
respond_to do |format|
- if @bracket.update(bracket_params)
- format.html { redirect_to @tournament, notice: 'Bracket was successfully updated.' }
+ if @bracket.predict_winners(prediction_params)
+ format.html { redirect_to @tournament, notice: 'Your bracket was made! Check back when this stage finishes to see how you did!' }
format.json { head :no_content }
else
- format.html { render action: 'edit' }
+ format.html { redirect_to @tournament, notice: 'bracket was not made... :('}
format.json { render json: @bracket.errors, status: :unprocessable_entity }
end
end
@@ -86,9 +87,21 @@ class BracketsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def bracket_params
+ # bracket[user_id]
+ # bracket[tournament_id]
+ # bracket[name]
+ # bracket[matches][#{i}]
params.require(:bracket).permit(:user_id, :tournament_id, :name)
end
+ def prediction_params
+ require 'pp'
+ puts "<params"+"<"*80
+ pp params
+ puts ">"*80
+ params.require(:bracket).require(:matches)
+ end
+
def is_owner?(bracket)
bracket.user == current_user
end
diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb
index 81ffcd8..d2dc918 100644
--- a/app/controllers/matches_controller.rb
+++ b/app/controllers/matches_controller.rb
@@ -31,6 +31,7 @@ class MatchesController < ApplicationController
# Scheduled, waiting to start
if (@tournament.hosts.include? current_user) and (params[:update_action] == "start")
@match.status = 2
+ @match.start_sampling
respond_to do |format|
if @match.save
format.html { redirect_to tournament_match_path(@tournament, @match), notice: 'Match has started.' }
diff --git a/app/models/bracket.rb b/app/models/bracket.rb
index acd33ca..48414c3 100644
--- a/app/models/bracket.rb
+++ b/app/models/bracket.rb
@@ -4,8 +4,29 @@ class Bracket < ActiveRecord::Base
has_many :bracket_matches
def create_matches
- tournament.stages.first.matches.each do |m|
+ tournament.stages.order(:id).first.matches.order(:id).each do |m|
bracket_matches.create(match: m)
end
end
+
+
+ def predict_winners(predictions)
+ require 'pp'
+ puts("<"*80)
+ pp predictions
+ puts(">"*80)
+ (0..bracket_matches.count-1).each do |i|
+ bracket_matches.order(:match_id)[i].update(predicted_winner: Team.find(predictions[(i+1).to_s]));
+ end
+ return true
+ end
+
+
+ def calcResults
+ results = Array.new
+ (0..bracket_matches.count-1).each do |i|
+ results.push(bracket_matches.order(:match_id)[i].predicted_winner == tournament.stages.order(:id).first.matches.order(:id).winner)
+ end
+ return results
+ end
end
diff --git a/app/models/bracket_match.rb b/app/models/bracket_match.rb
index 823bc40..f9a11f0 100644
--- a/app/models/bracket_match.rb
+++ b/app/models/bracket_match.rb
@@ -1,5 +1,7 @@
class BracketMatch < ActiveRecord::Base
belongs_to :bracket
belongs_to :match
- belongs_to :predicted_winner
+ belongs_to :predicted_winner, class_name: "Team"
+
+
end
diff --git a/app/models/match.rb b/app/models/match.rb
index 5c3a022..85084f5 100644
--- a/app/models/match.rb
+++ b/app/models/match.rb
@@ -46,6 +46,12 @@ class Match < ActiveRecord::Base
return html.html_safe
end
+ def start_sampling
+ method_classes.each do |klass|
+ klass.new(self).start
+ end
+ end
+
private
def figure_sampling_methods
if @sampling_methods.nil?
diff --git a/app/models/statistic.rb b/app/models/statistic.rb
index b4608b8..fefa3a8 100644
--- a/app/models/statistic.rb
+++ b/app/models/statistic.rb
@@ -16,7 +16,7 @@ class Statistic < ActiveRecord::Base
after_save :update_match
def update_match
- if (self.name == "win") and (self.value > 0)
+ if (self.name == "win") and (self.value)
self.match.winner = self.match.teams.find{|t| t.users.include? self.user}
end
if (self.match.status == 2) and (self.match.finished?)
diff --git a/app/views/brackets/show.html.erb b/app/views/brackets/show.html.erb
index 64e6e6a..24b19fe 100644
--- a/app/views/brackets/show.html.erb
+++ b/app/views/brackets/show.html.erb
@@ -1,7 +1,8 @@
<h2><%= @bracket.name %></h2>
-<h4> Make your prediction for the tournament by clicking on the teams you think will win </h4>
-
+<% if !@results %>
+ <h4> Make your prediction for the tournament by clicking on the teams you think will win </h4>
+<% end %>
<svg id="prediction-svg" version="1.1" baseProfile="full"
xmlns="http://www.w3.org/2000/svg"
@@ -21,30 +22,30 @@
%>
function chooseWinner(matchNum, teamNum, currentBox){
console.log(matchNum, teamNum);
- $id = '#match-'+matchNum+'-pred';
- $($id).val(teamNum);
+ var id = '#bracket_matches_'+matchNum;
+ $(id).val(teamNum);
if (matchNum != 1) {
- $parent = parseFloat(matchNum+<%= @logBase%> -2)/<%=@logBase%>;
- $textBox = ($parent - Math.floor($parent)) * <%= @logBase %>;
- $parent = Math.floor($parent);
- $textBox = Math.round($textBox);
- $id = "#svg-match-"+$parent+"-team-"+$textBox;
+ var parent = parseFloat(matchNum+<%= @logBase%> -2)/<%=@logBase%>;
+ var textBox = (parent - Math.floor(parent)) * <%= @logBase %>;
+ var parent = Math.floor(parent);
+ var textBox = Math.round(textBox);
+ var id = "#svg-match-"+parent+"-team-"+textBox;
- console.log($id);
+ console.log(id);
- $($id).text("Team "+teamNum);
- $($id).attr("onclick", "chooseWinner("+$parent+", "+teamNum+", "+$textBox+")");
+ $(id).text("Team "+teamNum);
+ $(id).attr("onclick", "chooseWinner("+parent+", "+teamNum+", "+textBox+")");
}
else
{
console.log("final countdown");
for(var i = 0; i < 3; i++){
- $id = "#svg-match-"+matchNum+"-team-"+i;
- $($id).attr("fill", "black");
+ id = "#svg-match-"+matchNum+"-team-"+i;
+ $(id).attr("fill", "black");
}
- $id = "#svg-match-"+matchNum+"-team-"+currentBox;
- $($id).attr("fill", "green");
+ id = "#svg-match-"+matchNum+"-team-"+currentBox;
+ $(id).attr("fill", "green");
$("#bracket-submit").prop('disabled', false);
}
}
@@ -71,7 +72,7 @@
while t <= @numTeams %>
<rect width="<%= rw-5 %>%" height="<%= rh*Float(30)/(@matchHeight) %>%" x="<%= rx + 2.5 %>%" y="<%= ry + (Float(t-1)/@numTeams)*rh + 1 %>%" fill="white" />
<text id="svg-match-<%= i %>-team-<%= t-1 %>" x="<%= rx + rw/4 %>%" y="<%= ry + (Float(t-1)/@numTeams + Float(33)/(@matchHeight))*rh %>%" font-size="150%"
- <% if @matches[i].teams[t-1] %>
+ <% if @matches[i].teams[t-1] && !@results %>
onclick="chooseWinner(<%= @matches[i].id%>, <%= @matches[i].teams[t-1].id %>)"
<% end %>
>
@@ -97,12 +98,13 @@
<% end %>
</SVG>
-<%= form_tag(tournament_bracket_path(@tournament, @bracket), method: 'put') do %>
- <input type="hidden" name="update_action" value="predict">
- <% for i in 1..@matches.length %>
- <%= hidden_field_tag('match-'+@matches[i].id.to_s+'-pred', value = nil) %>
+<% if !@results %>
+ <%= form_tag(tournament_bracket_path(@tournament, @bracket), method: 'put') do %>
+ <input type="hidden" name="update_action" value="predict">
+ <% for i in 1..@matches.length %>
+ <%= hidden_field_tag("bracket[matches][#{@matches[i].id.to_s}]", value = nil) %>
+ <% end %>
+ <%= submit_tag("Submit Prediction", disabled: true, id: "bracket-submit") %>
<% end %>
- <%= submit_tag("Sumit Prediction", disabled: true, id: "bracket-submit") %>
<% end %>
-
<%= link_to 'Back', tournaments_path %>
diff --git a/app/views/common/_show_tournament.html.erb b/app/views/common/_show_tournament.html.erb
index 02852cf..c0237b1 100644
--- a/app/views/common/_show_tournament.html.erb
+++ b/app/views/common/_show_tournament.html.erb
@@ -33,10 +33,16 @@
<% elsif target.players.include?(current_user)%>
<p class="message">You've signed up for this tournament!</p>
<% end %>
- <% if target.status == 1 && target.stages.order(:id).first.scheduling_method == "elimination" && target.stages.order(:id).first.matches.order(:id).first.status < 2 %>
+ <% @user_bracket = target.brackets.find_by(user: current_user) %>
+ <% if target.status == 1 && target.stages.order(:id).first.scheduling_method == "elimination" && target.stages.order(:id).first.matches.order(:id).first.status < 2 && !@user_bracket %>
<%= form_tag(tournament_brackets_path(target), method: "post") do %>
<%= submit_tag("Make Bracket") %>
<% end %>
+ <% elsif @user_bracket && target.status == 4 %>
+ <%= form_tag(tournament_bracket_path(@tournament, @bracket), method: 'put') do %>
+ <input type="hidden" name="update_action" value="results">
+ <%= submit_tag("Bracket Results") %>
+ <% end %>
<% end %>
<% end %>
</div>
diff --git a/db/seeds.rb b/db/seeds.rb
index 16becf3..910727e 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -177,7 +177,7 @@ if Rails.env.development?
custom.hosts.push(User.find(1))
- custom.stages.create(scheduling_method: "round_robin" , seeding_method: "early_bird")
+ custom.stages.create(scheduling_method: "round_robin" , seeding_method: "early_bird_seeding")
custom.hosts.push(User.find(1))
diff --git a/lib/sampling/riot_api.rb b/lib/sampling/riot_api.rb
index 7d75475..bbe9cea 100644
--- a/lib/sampling/riot_api.rb
+++ b/lib/sampling/riot_api.rb
@@ -17,7 +17,7 @@ module Sampling
protected
def self.url(request, args={})
- "https://prod.api.pvp.net/api/lol/#{region}/#{request % args.merge(args){|k,v|url_escape(v)}}?#{api_key}"
+ "https://prod.api.pvp.net/api/lol/#{region}/#{request % args.merge(args){|k,v|url_escape(v)}}?api_key=#{api_key}"
end
protected
@@ -43,7 +43,7 @@ module Sampling
{:unit_time => 10.seconds, :requests_per => 10},
{:unit_time => 10.minutes, :requests_per => 500},
]
- super(api_name, limits)
+ super(RiotApi::api_name, limits)
end
def perform
@@ -62,10 +62,11 @@ module Sampling
if status[0] != "200"
raise "GET #{@url} => #{status.join(" ")}"
end
- self.handle(data)
+ return self.handle(data)
end
def handle(data)
+ return true
end
end
@@ -110,7 +111,7 @@ module Sampling
# When given a summoner name for a user, figure out the summoner ID.
public
def self.set_remote_name(user, game, summoner_name)
- Delayed::Job.enqueue(UsernameJob.new(user, game, summoner_name), :queue => api_name)
+ Delayed::Job.enqueue(UsernameJob.new(user, game, summoner_name), :queue => RiotApi::api_name)
end
protected
class UsernameJob < Job
@@ -158,7 +159,7 @@ module Sampling
@match.teams.each do |team|
team.users.each do |user|
#For demo purposes, we are hard coding in a league of legends game id.
- Delayed::Job.enqueue(MatchJob.new(user, @match, @match.stats_from(self.class), 1362730546), :queue => api_name)
+ Delayed::Job.enqueue(FetchStatisticsJob.new(user, @match, @match.stats_from(self.class), 10546), :queue => RiotApi::api_name)
end
end
end
@@ -179,11 +180,11 @@ module Sampling
user = User.find(@user_id)
match = Match.find(@match_id)
if @last_game_id.nil?
- Delayed::Job.enqueue(MatchJob.new(user, match, data["games"][0]["gameId"]), :queue => api_name)
+ Delayed::Job.enqueue(FetchStatisticsJob.new(user, match, data["games"][0]["gameId"]), :queue => RiotApi::api_name)
else
if @last_game_id == data["games"][0]["gameId"]
sleep(4.minutes)
- Delayed::Job.enqueue(MatchJob.new(user, match, @last_game_id), :queue => api_name)
+ Delayed::Job.enqueue(FetchStatisticsJob.new(user, match, @last_game_id), :queue => RiotApi::api_name)
else
@stats.each do |stat|
Statistic.create(user: user, match: match, name: stat, value: data["games"][0]["stats"][stat])
diff --git a/lib/scheduling/elimination.rb b/lib/scheduling/elimination.rb
index 4518cff..73aefb4 100644
--- a/lib/scheduling/elimination.rb
+++ b/lib/scheduling/elimination.rb
@@ -52,6 +52,7 @@ module Scheduling
# height of SVG
matchHeight = 50*logBase;
height = [(matchHeight+50) * logBase**(depth-1) + 100, 500].max;
+ height = height/2;
str = <<-STRING
<svg version="1.1" baseProfile="full"
@@ -109,10 +110,10 @@ module Scheduling
color = (matches[i].teams[t-1] and matches[i].teams[t-1].users.include?(current_user)) ? "#5BC0DE" : "white"
str += "\t\t<rect width=\"#{rw-5}%\" height=\"#{rh*Float(30)/(matchHeight)}%\" x=\"#{rx + 2.5}%\" y=\"#{ry + (Float(t-1)/numTeams)*rh + 1 }%\" fill=\"#{color}\" />\n"
if matches[i].teams[t-1]
- str += "\t\t<text x=\"#{rx + rw/4}%\" y=\"#{ry + (Float(t-1)/numTeams + Float(33)/(matchHeight))*rh}%\" font-size=\"200%\">Team #{matches[i].teams[t-1].id}</text>\n"
+ str += "\t\t<text x=\"#{rx + rw/4}%\" y=\"#{ry + (Float(t-1)/numTeams + Float(33)/(matchHeight))*rh}%\" font-size=\"120%\">Team #{matches[i].teams[t-1].id}</text>\n"
end
if (t < numTeams)
- str += "\t\t<text x=\"#{rx + 1.3*rw/3}%\" y=\"#{ry + (Float(t)/numTeams)*rh + 1}%\" font-size=\"200%\"> VS </text>\n"
+ str += "\t\t<text x=\"#{rx + 1.3*rw/3}%\" y=\"#{ry + (Float(t)/numTeams)*rh + 1}%\" font-size=\"120%\"> VS </text>\n"
end
t = t + 1
end
diff --git a/lib/throttled_api_request.rb b/lib/throttled_api_request.rb
index 1020071..c48a66d 100644
--- a/lib/throttled_api_request.rb
+++ b/lib/throttled_api_request.rb
@@ -8,9 +8,9 @@ class ThrottledApiRequest < Struct.new(:api_name, :limits)
loop do
sleep_for = -1
ActiveRecord::Base.transaction do
- ApiRequests.create(:api_name => self.api_name)
+ ApiRequest.create(:api_name => self.api_name)
self.limits.each do |limit|
- recent_requests = ApiRequets.
+ recent_requests = ApiRequest.
where(:api_name => self.api_name).
where("updated_at > ?", Time.now.utc - limit[:unit_time]).
order(:updated_at)