diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/brackets_controller.rb | 22 | ||||
-rw-r--r-- | app/models/bracket.rb | 2 | ||||
-rw-r--r-- | app/views/brackets/show.html.erb | 88 | ||||
-rw-r--r-- | 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 @@ -<p> - <strong>User:</strong> - <%= @bracket.user.user_name %> -</p> +<h2><%= @bracket.name %></h2> -<p> - <strong>Tournament:</strong> - <%= @bracket.tournament.name %> -</p> - -<p> - <strong>Name:</strong> - <%= @bracket.name %> -</p> +<h4> Make your prediction for the tournament by clicking on the teams you think will win </h4> <% @bracket.bracket_matches.each do |m| %> <p><b><%= m.match.id %></b></p> <% end %> +<svg version="1.1" baseProfile="full" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink" + width="100%" height=<%= "#{@height}"%>> + <defs> + <radialGradient id="gradMatch" cx="50%" cy="50%" r="80%" fx="50%" fy="50%"> + <stop offset="0%" style="stop-color:#fff; stop-opacity:1" /> + <stop offset="100%" style="stop-color:#ccc;stop-opacity:0" /> + </radialGradient> + </defs> + <script type="text/ecmascript"><![CDATA[ + function redirect(i){ + window.location.replace("<%= request.original_url %>"+"/"+i); + } + ]]> + </script> + + <% (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 %> + <g id="svg-match-<%= i %>"> + <rect height="<%= rh = 100 / (@logBase**(@depth-1)+1) - 100/@height %>%" + width="<%= rw = 100/(@depth+1) - 5 %>%" + x="<%= rx = 50/(@depth+1) + 100/(@depth+1)*(@depth-matchDepth) %>%" + y="<%= ry = 100/(@logBase**(matchDepth-1)+1) * (i-@base+1) - rh/2 %>%" + fill="url(#gradMatch)" + rx="5px" + stroke-width="2" + <% case @matches[i].status %> + <% 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 %> + <rect width="<%= rw-5 %>%" height="<%= rh*Float(30)/(@matchHeight) %>%" x="<%= rx + 2.5 %>%" y="<%= ry + (Float(t-1)/@numTeams)*rh + 1 %>%" fill="<%= @matches[i].teams[t-1] and @matches[i].teams[t-1].include?(current_user) ? "#5BC0DE" : "white" %>" onclick="chooseWinner(<%= @matches[i].id, %>)" /> + <text x="<%= rx + rw/4 %>%" y="<%= ry + (Float(t-1)/@numTeams + Float(33)/(@matchHeight))*rh %>%" font-size="200%"> + <% if @matches[i].teams[t-1] %> + Team <%= @matches[i].teams[t-1].id %> + <% end %> + </text> + <% if (t < @numTeams) %> + <text x="<%= rx + 1.3*rw/3 %>%" y="<%= ry + (Float(t)/@numTeams)*rh + 1%>%" font-size="200%"> VS </text> + <% 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 %> + <line x1="<%= rx+rw %>%" y1="<%= ry+rh/2 %>%" x2="<%= lastrx%>%" y2="<%= lastry+rh/2%>%" stroke="white" stroke-width="2" > + <% end %> + </g> + + <% end %> +</SVG> <%= 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 @@ </div> <div class="col-md-2 col-sm-2 col-xs-2"> <% 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 %> <p> <input type="hidden" name="update_action" value="join"> <%= submit_tag("Join")%> <% end %> </p> - <% else %> + <% elsif target.players.include?(current_user)%> <p style="margin-top:10px;"> You've signed up for this tournament! </p> <% 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 %> |