diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/brackets_controller.rb | 21 | ||||
-rw-r--r-- | app/controllers/matches_controller.rb | 1 | ||||
-rw-r--r-- | app/models/bracket.rb | 23 | ||||
-rw-r--r-- | app/models/bracket_match.rb | 4 | ||||
-rw-r--r-- | app/models/match.rb | 6 | ||||
-rw-r--r-- | app/models/statistic.rb | 2 | ||||
-rw-r--r-- | app/views/brackets/show.html.erb | 48 | ||||
-rw-r--r-- | app/views/common/_show_tournament.html.erb | 8 |
8 files changed, 82 insertions, 31 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> |