From b0b2a235f02b95058aa7bfbf817d5dd1d85b6730 Mon Sep 17 00:00:00 2001 From: tkimia Date: Mon, 28 Apr 2014 01:30:32 -0400 Subject: first draft selection. Not done... most of my time was spent reqriting the SVG in ERB --- app/controllers/brackets_controller.rb | 22 ++++++-- app/models/bracket.rb | 2 +- app/views/brackets/show.html.erb | 88 +++++++++++++++++++++++++----- app/views/common/_show_tournament.html.erb | 6 +- 4 files changed, 97 insertions(+), 21 deletions(-) diff --git a/app/controllers/brackets_controller.rb b/app/controllers/brackets_controller.rb index ed335d6..50ff5fe 100644 --- a/app/controllers/brackets_controller.rb +++ b/app/controllers/brackets_controller.rb @@ -11,6 +11,19 @@ class BracketsController < ApplicationController # GET /brackets/1 # GET /brackets/1.json def show + @matches = @tournament.stages.first.matches_ordered + @numTeams = @tournament.min_teams_per_match + @logBase = @numTeams + + # depth of SVG tree + @depth = Math.log(@matches.count*(@logBase-1),@logBase).floor+1; + + # height of SVG + @matchHeight = 50*@logBase; + @height = [(@matchHeight+50) * @logBase**(@depth-1) + 100, 500].max; + + @base = 1 + @pBase = 1 end # GET /brackets/1/edit @@ -20,16 +33,17 @@ class BracketsController < ApplicationController # POST /brackets # POST /brackets.json def create - @bracket = @tournament.brackets.create(user: current_user) + @bracket = @tournament.brackets.build(user: current_user) @bracket.name = current_user.user_name + "'s Prediction for " + @tournament.name - @bracket.create_matches respond_to do |format| - if @bracket.save + if @tournament.status == 1 && @tournament.stages.first.scheduling_method == "elimination" && @tournament.stages.first.matches.first.status == 0 + @bracket.save + @bracket.create_matches format.html { redirect_to @bracket, notice: 'Bracket was successfully created.' } format.json { render action: 'edit', status: :created, location: @bracket } else - format.html { render action: 'new' } + format.html { redirect_to tournaments_path action: 'You can\'t make a bracket for this tournament' } format.json { render json: @bracket.errors, status: :unprocessable_entity } end end diff --git a/app/models/bracket.rb b/app/models/bracket.rb index acd33ca..7e22f0d 100644 --- a/app/models/bracket.rb +++ b/app/models/bracket.rb @@ -4,7 +4,7 @@ class Bracket < ActiveRecord::Base has_many :bracket_matches def create_matches - tournament.stages.first.matches.each do |m| + tournament.stages.first.matches_ordered.each do |m| bracket_matches.create(match: m) end end diff --git a/app/views/brackets/show.html.erb b/app/views/brackets/show.html.erb index 2e92bfb..75446f5 100644 --- a/app/views/brackets/show.html.erb +++ b/app/views/brackets/show.html.erb @@ -1,21 +1,83 @@ -

- User: - <%= @bracket.user.user_name %> -

+

<%= @bracket.name %>

-

- Tournament: - <%= @bracket.tournament.name %> -

- -

- Name: - <%= @bracket.name %> -

+

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

<% @bracket.bracket_matches.each do |m| %>

<%= m.match.id %>

<% end %> +> + + + + + + + + + <% (1..@matches.count).each do |i| %> + <% matchDepth = Math.log(i*(@logBase-1), @logBase).floor+1 %> + <% if matchDepth > Math.log(@base*(@logBase-1), @logBase).floor+1 + @pBase = @base + @base = i + end %> + + + <% when 0 %> + <% if @matches[i].teams.count < @tournament.min_teams_per_match %> + stroke="red" + fill-opacity="0.6" + <% else %> + stroke="green" + <% end %> + <% when 1 %> + stroke="orange" + <% when 2 %> + stroke="yellow" + <% when 3 %> + stroke="grey" + <% end %> + /> + + <% t = 1 + while t <= @numTeams %> + " onclick="chooseWinner(<%= @matches[i].id, %>)" /> + + <% if @matches[i].teams[t-1] %> + Team <%= @matches[i].teams[t-1].id %> + <% end %> + + <% if (t < @numTeams) %> + VS + <% end %> + <% t = t + 1 %> + <% end %> + + <% if i > 1 %> + <% parent = (i+@logBase-2)/@logBase + pDepth = Math.log(parent*(@logBase-1), @logBase).floor+1 + lastrx = 50/(@depth+1) + 100/(@depth+1)*(@depth-pDepth) + lastry = 100/(@logBase**(pDepth-1)+1) * (parent-@pBase+1) - rh/2 %> + + <% end %> + + + <% 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 12a7fea..5659947 100644 --- a/app/views/common/_show_tournament.html.erb +++ b/app/views/common/_show_tournament.html.erb @@ -23,17 +23,17 @@
<% if signed_in? %> - <% if !target.players.include?(current_user) %> + <% if !target.players.include?(current_user) && target.status == 0 %> <%= form_tag(tournament_path(target), method: "put") do %>

<%= submit_tag("Join")%> <% end %>

- <% else %> + <% elsif target.players.include?(current_user)%>

You've signed up for this tournament!

<% end %> - <% if target.status == 1 %> + <% if target.status == 1 && target.stages.first.scheduling_method == "elimination" && target.stages.first.matches.first.status == 0 %> <%= form_tag(tournament_brackets_path(target), method: "post") do %> <%= submit_tag("Make Bracket") %> <% end %> -- cgit v1.2.3