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