From 819fd70c1285bf5670b68842adad8c77c36d1076 Mon Sep 17 00:00:00 2001 From: nfoy Date: Mon, 28 Apr 2014 21:32:04 -0400 Subject: All the changes. Luke should know what's up. --- app/controllers/matches_controller.rb | 1 + app/models/match.rb | 6 ++++++ db/seeds.rb | 2 +- lib/sampling/riot_api.rb | 14 ++++++++------ lib/throttled_api_request.rb | 5 +++-- 5 files changed, 19 insertions(+), 9 deletions(-) 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/match.rb b/app/models/match.rb index 219e662..57b7d36 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/db/seeds.rb b/db/seeds.rb index 7e13f1e..8327e30 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..18a5ee7 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 @@ -110,7 +110,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 +158,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 @@ -176,20 +176,22 @@ module Sampling super("v1.3/game/by-summoner/%{summonerId}/recent", { :summonerId => summoner["id"] }) end def handle(data) + puts("handling...") 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]) end end end + puts("done handling") end end diff --git a/lib/throttled_api_request.rb b/lib/throttled_api_request.rb index 1020071..b632d1e 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) @@ -23,6 +23,7 @@ class ThrottledApiRequest < Struct.new(:api_name, :limits) end end if sleep_for != -1 + puts "sleeping for #{sleeping_for}" sleep(sleep_for) else break -- cgit v1.2.3 From bc8be5e217279012cd4dc62e3fc2fe2bd9308b1c Mon Sep 17 00:00:00 2001 From: tkimia Date: Mon, 28 Apr 2014 22:01:00 -0400 Subject: bracket update works --- app/controllers/brackets_controller.rb | 20 +++++++++++++++---- app/models/bracket.rb | 14 ++++++++++++- app/models/bracket_match.rb | 4 +++- app/views/brackets/show.html.erb | 32 +++++++++++++++--------------- app/views/common/_show_tournament.html.erb | 3 ++- 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/app/controllers/brackets_controller.rb b/app/controllers/brackets_controller.rb index ac34bfe..b91a2c4 100644 --- a/app/controllers/brackets_controller.rb +++ b/app/controllers/brackets_controller.rb @@ -11,7 +11,7 @@ class BracketsController < ApplicationController # GET /brackets/1 # GET /brackets/1.json def show - @matches = @tournament.stages.first.matches_ordered + @matches = @tournament.stages.order(:id).first.matches_ordered @numTeams = @tournament.min_teams_per_match @logBase = @numTeams @@ -53,11 +53,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 +86,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 ""*80 + params.require(:bracket).require(:matches) + end + def is_owner?(bracket) bracket.user == current_user end diff --git a/app/models/bracket.rb b/app/models/bracket.rb index acd33ca..fc3e10e 100644 --- a/app/models/bracket.rb +++ b/app/models/bracket.rb @@ -4,8 +4,20 @@ 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 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/views/brackets/show.html.erb b/app/views/brackets/show.html.erb index 64e6e6a..a3eb000 100644 --- a/app/views/brackets/show.html.erb +++ b/app/views/brackets/show.html.erb @@ -21,30 +21,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); } } @@ -100,9 +100,9 @@ <%= form_tag(tournament_bracket_path(@tournament, @bracket), method: 'put') do %> <% for i in 1..@matches.length %> - <%= hidden_field_tag('match-'+@matches[i].id.to_s+'-pred', value = nil) %> + <%= hidden_field_tag("bracket[matches][#{@matches[i].id.to_s}]", value = nil) %> <% end %> - <%= submit_tag("Sumit Prediction", disabled: true, id: "bracket-submit") %> + <%= submit_tag("Submit 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..f4d02ec 100644 --- a/app/views/common/_show_tournament.html.erb +++ b/app/views/common/_show_tournament.html.erb @@ -33,7 +33,8 @@ <% elsif target.players.include?(current_user)%>

You've signed up for this tournament!

<% 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 %> -- cgit v1.2.3 From 40c112d42922c14063759d381f834724f22a340d Mon Sep 17 00:00:00 2001 From: tkimia Date: Mon, 28 Apr 2014 22:54:35 -0400 Subject: made svg smaller --- app/controllers/brackets_controller.rb | 1 + app/models/bracket.rb | 9 +++++++++ app/views/brackets/show.html.erb | 20 +++++++++++--------- app/views/common/_show_tournament.html.erb | 5 +++++ lib/scheduling/elimination.rb | 5 +++-- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/controllers/brackets_controller.rb b/app/controllers/brackets_controller.rb index b91a2c4..e202c96 100644 --- a/app/controllers/brackets_controller.rb +++ b/app/controllers/brackets_controller.rb @@ -11,6 +11,7 @@ class BracketsController < ApplicationController # GET /brackets/1 # GET /brackets/1.json def show + @results = (@tournament.status == 4)? @bracket.calcResult : nil; @matches = @tournament.stages.order(:id).first.matches_ordered @numTeams = @tournament.min_teams_per_match @logBase = @numTeams diff --git a/app/models/bracket.rb b/app/models/bracket.rb index fc3e10e..48414c3 100644 --- a/app/models/bracket.rb +++ b/app/models/bracket.rb @@ -20,4 +20,13 @@ class Bracket < ActiveRecord::Base 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/views/brackets/show.html.erb b/app/views/brackets/show.html.erb index a3eb000..24b19fe 100644 --- a/app/views/brackets/show.html.erb +++ b/app/views/brackets/show.html.erb @@ -1,7 +1,8 @@

<%= @bracket.name %>

-

Make your prediction for the tournament by clicking on the teams you think will win

- +<% if !@results %> +

Make your prediction for the tournament by clicking on the teams you think will win

+<% end %> + <% if @matches[i].teams[t-1] && !@results %> onclick="chooseWinner(<%= @matches[i].id%>, <%= @matches[i].teams[t-1].id %>)" <% end %> > @@ -97,12 +98,13 @@ <% end %> -<%= form_tag(tournament_bracket_path(@tournament, @bracket), method: 'put') do %> - - <% for i in 1..@matches.length %> - <%= hidden_field_tag("bracket[matches][#{@matches[i].id.to_s}]", value = nil) %> +<% if !@results %> + <%= form_tag(tournament_bracket_path(@tournament, @bracket), method: 'put') do %> + + <% 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("Submit 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 f4d02ec..c0237b1 100644 --- a/app/views/common/_show_tournament.html.erb +++ b/app/views/common/_show_tournament.html.erb @@ -38,6 +38,11 @@ <%= 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 %> + + <%= submit_tag("Bracket Results") %> + <% end %> <% end %> <% end %> 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 \n" if matches[i].teams[t-1] - str += "\t\tTeam #{matches[i].teams[t-1].id}\n" + str += "\t\tTeam #{matches[i].teams[t-1].id}\n" end if (t < numTeams) - str += "\t\t VS \n" + str += "\t\t VS \n" end t = t + 1 end -- cgit v1.2.3 From 7297cff5b674a61fd57ccc46eb75173011bd205c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 28 Apr 2014 23:57:48 -0400 Subject: fix riot_api --- app/models/statistic.rb | 2 +- lib/sampling/riot_api.rb | 5 ++--- lib/throttled_api_request.rb | 1 - 3 files changed, 3 insertions(+), 5 deletions(-) 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/lib/sampling/riot_api.rb b/lib/sampling/riot_api.rb index 18a5ee7..bbe9cea 100644 --- a/lib/sampling/riot_api.rb +++ b/lib/sampling/riot_api.rb @@ -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 @@ -176,7 +177,6 @@ module Sampling super("v1.3/game/by-summoner/%{summonerId}/recent", { :summonerId => summoner["id"] }) end def handle(data) - puts("handling...") user = User.find(@user_id) match = Match.find(@match_id) if @last_game_id.nil? @@ -191,7 +191,6 @@ module Sampling end end end - puts("done handling") end end diff --git a/lib/throttled_api_request.rb b/lib/throttled_api_request.rb index b632d1e..c48a66d 100644 --- a/lib/throttled_api_request.rb +++ b/lib/throttled_api_request.rb @@ -23,7 +23,6 @@ class ThrottledApiRequest < Struct.new(:api_name, :limits) end end if sleep_for != -1 - puts "sleeping for #{sleeping_for}" sleep(sleep_for) else break -- cgit v1.2.3