diff options
Diffstat (limited to 'app')
22 files changed, 269 insertions, 170 deletions
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index d6925fa..cb6edeb 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -14,3 +14,6 @@ //= require jquery_ujs //= require turbolinks //= require_tree . +//= require 'drag.js' +//= require 'dragsort.js' +//= require 'coordinates.js' diff --git a/app/assets/stylesheets/custom.css.scss b/app/assets/stylesheets/custom.css.scss index 1a9b09a..febcbb8 100644 --- a/app/assets/stylesheets/custom.css.scss +++ b/app/assets/stylesheets/custom.css.scss @@ -24,6 +24,7 @@ a, input[type="submit"] { &.signup { @extend .btn-success; } &.signin { @extend .btn-info; } &.signout { @extend .btn-danger; } + &.server { @extend .btn-danger; } } p.errors { diff --git a/app/assets/stylesheets/matches.css.scss b/app/assets/stylesheets/matches.css.scss index 4c396e3..3ef9170 100644 --- a/app/assets/stylesheets/matches.css.scss +++ b/app/assets/stylesheets/matches.css.scss @@ -1,3 +1,27 @@ // Place all the styles related to the matches controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ +#boxes li { + cursor: move; + position: relative; + float: left; + margin: 5px; + width: 180px; + height: 240px; + border: 1px solid rgb(0, 0, 0); + text-align: center; + padding-top: 10px; + background-color: rgb(238, 238, 255); +} +#numeric li { + cursor: move; + position: relative; + float: left; + margin: 5px; + width: 180px; + height: 240px; + border: 1px solid rgb(0, 0, 0); + text-align: center; + padding-top: 10px; + background-color: rgb(238, 238, 255); +}
\ No newline at end of file diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index 31fc9ad..ee68e11 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -1,15 +1,22 @@ class MatchesController < ApplicationController - before_action :set_tournament, only: [:index] + before_action :set_tournament, only: [:index, :update] # GET /matches # GET /matches.json require 'httparty' require 'json' + require 'delayed_job' def index @matches = @tournament.matches + # width of SVG + @width = 300 * (Math.log2(@matches.count).floor + 1); + # height of SVG + @height = 200 * 2**Math.log2(@matches.count).floor + 100; end + + def get_riot_info if signed_in? @@ -114,14 +121,64 @@ class MatchesController < ApplicationController end #end def # GET /matches/1 # GET /matches/1.json + + def is_match_over + response = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/summoner/by-name/#{@first}?api_key=ad539f86-22fd-474d-9279-79a7a296ac38") + riot_id = response["#{@first}"]['id'] + #recent game information + game_info = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/game/by-summoner/#{riot_id}/recent?api_key=ad539f86-22fd-474d-9279-79a7a296ac38") + first_id = game_info["games"][0]["gameId"] + + while true do + sleep(240) #wait four minutes + + recent = HTTParty.get("https://prod.api.pvp.net/api/lol/na/v1.3/game/by-summoner/#{riot_id}/recent?api_key=ad539f86-22fd-474d-9279-79a7a296ac38") + current_id = recent["games"][0]["gameId"] + + if current_id != first_id + @match.status = 2 + end + end #while + end + handle_asynchronously :is_match_over + def show + if @match.id == 1 + is_match_over + end + + + end + + def update + case params[:update_action] + when "start" + check_permission(:edit, @tournament) + status = 1 + respond_to do |format| + if @match + format.html { redirect_to tournament_match_path(@tournament, self), notice: 'Match has started.' } + format.json { head :no_content } + else + format.html { redirect_to @tournament, notice: "You don't have permission to start this match." } + format.json { render json: "Permission denied", status: :forbidden } + end + end + else + respond_to do |format| + format.html { redirect_to @tournament, notice: "Invalid action", status: :unprocessable_entity } + format.json { render json: @tournament.errors, status: :unprocessable_entity } + end + end + end private # Use callbacks to share common setup or constraints between actions. def set_match set_tournament - @match = @tournament.matches.find(params[:id]); + @match = @tournament.matches.find(params[:id]) + @first = @match.teams.first.users.first.user_name.downcase end def set_tournament @tournament = Tournament.find(params[:tournament_id]) diff --git a/app/controllers/servers_controller.rb b/app/controllers/servers_controller.rb index 6596dc6..83a9f31 100644 --- a/app/controllers/servers_controller.rb +++ b/app/controllers/servers_controller.rb @@ -1,43 +1,15 @@ class ServersController < ApplicationController - - # GET /servers - # GET /servers.json - def index - @servers = Server.all - end - - # GET /servers/1 - # GET /servers/1.json + # GET /server + # GET /server.json def show end - # GET /servers/new - def new - @server = Server.new - end - - # GET /servers/1/edit + # GET /server/edit def edit end - # POST /servers - # POST /servers.json - def create - @server = Server.new(server_params) - - respond_to do |format| - if @server.save - format.html { redirect_to @server, notice: 'Server was successfully created.' } - format.json { render action: 'show', status: :created, location: @server } - else - format.html { render action: 'new' } - format.json { render json: @server.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /servers/1 - # PATCH/PUT /servers/1.json + # PATCH/PUT /server + # PATCH/PUT /server.json def update respond_to do |format| if @server.update(server_params) @@ -50,24 +22,15 @@ class ServersController < ApplicationController end end - # DELETE /servers/1 - # DELETE /servers/1.json - def destroy - @server.destroy - respond_to do |format| - format.html { redirect_to servers_url } - format.json { head :no_content } - end - end - private + # Use callbacks to share common setup or constraints between actions. def set_server - @server = Server.find(params[:id]) + @server = Server.first end # Never trust parameters from the scary internet, only allow the white list through. def server_params - params[:server] + params.require(:server).permit(:default_user_permissions, :default_user_abilities => User.permission_bits.keys) end end diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index a9e91b0..2fc82ed 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -99,7 +99,7 @@ class TournamentsController < ApplicationController format.html { redirect_to @tournament, notice: 'You have joined this tournament.' } format.json { head :no_content } else - format.html { render action: 'permission_denied', status: :forbidden } + format.html { redirect_to @tournament, notice: "You don't have permission to start this tournament." } format.json { render json: "Permission denied", status: :forbidden } end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dd66c18..637480f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -33,7 +33,7 @@ class UsersController < ApplicationController return end - @user.permissions = 0 + @user.permissions = Server.first.default_user_permissions respond_to do |format| if @user.save sign_in @user diff --git a/app/models/server.rb b/app/models/server.rb index 120f0fa..5ba7524 100644 --- a/app/models/server.rb +++ b/app/models/server.rb @@ -1,2 +1,38 @@ class Server < ActiveRecord::Base + def default_user_abilities + @abilities ||= User::Abilities.new(DefaultUser.new(self)) + end + def default_user_abilities=(new) + new.each do |k,v| + if v == "0" + v = false + end + default_user_abilities[k] = v + end + end + class DefaultUser + def initialize(server) + @server = server + end + def can?(action) + bit = User.permission_bits[action] + if bit.nil? + return false + else + return (@server.default_user_permissions & bit != 0) + end + end + def add_ability(action) + bit = User.permission_bits[action.to_sym] + unless bit.nil? + @server.default_user_permissions |= bit + end + end + def remove_ability(action) + bit = User.permission_bits[action.to_sym] + unless bit.nil? + @server.default_user_permissions &= ~ bit + end + end + end end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 0b55cb6..fdcdba2 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -78,19 +78,20 @@ class Tournament < ActiveRecord::Base for i in 1..num_matches self.matches.create(name: "Match #{i}", status: 0) end - match_num = 0 + match_num = num_matches-1 team_num = 0 #for each grouping of min_players_per_team - self.players.each_slice(min_players_per_team) do |players| - #create a new team in the current match - self.matches[match_num].teams.push(Team.create(users: players)) + players.each_slice(min_players_per_team) do |players| + #if the match is full, move to the next match, otherwise move to the next team - if (team_num != 0 and team_num % max_teams_per_match == 0) - match_num += 1 + if (team_num == min_teams_per_match) + match_num -= 1 team_num = 0 else team_num += 1 end + #create a new team in the current match + self.matches[match_num].teams.push(Team.create(users: players)) end end end diff --git a/app/models/user.rb b/app/models/user.rb index 626e4bf..0b77ab1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,31 +11,32 @@ class User < ActiveRecord::Base def self.permission_bits return { - :create_tournament => 1, - :edit_tournament => 2, - :join_tournament => 3, - :delete_tournament => 4, + :create_tournament => (2**1), + :edit_tournament => (2**2), + :join_tournament => (2**3), + :delete_tournament => (2**4), - :create_game => 5, - :edit_game => 6, - :delete_game => 7, + :create_game => (2**5), + :edit_game => (2**6), + :delete_game => (2**7), - :create_user => 8, - :edit_user => 9, - :delete_user => 10, + :create_user => (2**8), + :edit_user => (2**9), + :delete_user => (2**10), - :create_alert => 11, - :edit_alert => 12, - :delete_alert => 13, + :create_alert => (2**11), + :edit_alert => (2**12), + :delete_alert => (2**13), - :create_pm => 14, - :edit_pm => 15, - :delete_pm => 16, + :create_pm => (2**14), + :edit_pm => (2**15), + :delete_pm => (2**16), - :create_session => 17, - :delete_session => 18, + :create_session => (2**17), + :delete_session => (2**18), - :edit_permissions => 19, + :edit_permissions => (2**19), + :edit_server => (2**20), } end @@ -44,21 +45,21 @@ class User < ActiveRecord::Base if bit.nil? return false else - return (self.permissions & (2**bit) != 0) + return (self.permissions & bit != 0) end end def add_ability(action) bit = User.permission_bits[action.to_sym] unless bit.nil? - self.permissions |= 2**bit + self.permissions |= bit end end def remove_ability(action) bit = User.permission_bits[action.to_sym] unless bit.nil? - self.permissions &= ~ (2**bit) + self.permissions &= ~ bit end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 976ee85..de9f3b8 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,6 +8,11 @@ <%= yield :head %> </head> <body> +<script> + window.onload = function() { + BetterDragSort.makeListSortable(document.getElementById("boxes")); + }; +</script> <header><nav> <div class="navbar-brand"><%= link_to('Leaguer', root_path) %></div> <div> @@ -21,6 +26,9 @@ <% if signed_in? %> <%= link_to current_user.user_name, current_user, :class => "user" %> <%= link_to "Sign out", session_path("current"), method: "delete", :class => "signout" %> + <% if current_user.can? :edit_server %> + <%= link_to "Server settings", edit_server_path, :class => "server" %> + <% end %> <% else %> <%= link_to "Log in", new_session_path, :class => "signin" %> <%= link_to "Sign up", new_user_path, :class => "signup" %> diff --git a/app/views/matches/index.html.erb b/app/views/matches/index.html.erb index 219507d..052d176 100644 --- a/app/views/matches/index.html.erb +++ b/app/views/matches/index.html.erb @@ -18,7 +18,15 @@ <td><%= match.id%></td> <td><%= match.name %></td> <td><%= link_to "Show", tournament_match_path(@tournament, match) %> - <td><%= submit_tag("Start Match") %> + <td> <%# If user is the host, let them start the tournment %> + <% if @tournament.hosts.include?(current_user) %> + + <%= form_tag(tournament_match_path(@tournament, match), method: "put") do %> + <input type="hidden" name="update_action" value="start"> + <%= submit_tag("Start Match") %> + <% end %> + <% end %> +</div></td> </tr> <% end %> </tbody> @@ -26,17 +34,51 @@ <br> - -<SVG version="1.1" - baseProfile="full" - width="<%= 300 * @matches.count / 2 + 50 %>" height="<%= 200 * @matches.count + 50 %>" - xmlns="http://www.w3.org/2000/svg"> - - <% (1..@matches.count).each do |i| %> - <g class="svg-match"> - <rect rx="10" - - </g> - <% end %> - +<div id="match-tree"> + <SVG version="1.1" + baseProfile="full" + width="<%= @width %>" height="<%= @height = [@height, 500].max %>" + xmlns="http://www.w3.org/2000/svg"> + <script type="text/ecmascript"><![CDATA[ + function redirect(i){ + window.location.replace("<%= request.original_url %>"+"/"+i); + } + ]]> + </script> + <% (1..@matches.count).each do |i| %> + <g id="svg-match-<%= i %>" onmouseover="dispStats(<%= i %>)" onclick="redirect(<%= @matches[i-1].id %>)" cursor="pointer"> + <rect height="120px" width="213px" + x="<%= @width - (i-1)*50 - 250*(Math.log2(i).floor+1) %>" + y="<%= (@height/(Math.log2(i).floor+2)) - 60 + 250*(i - 2**(Math.log2(i).floor)) %>" + fill="#ffd281" + rx="20px" + stroke-width="2" + <% case @matches[i-1].status %> + <% when 0 %> + <% if @matches[i-1].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 %> + /> + </g> + <% if i > 1 %> + <line + stroke="black" + x1="<%= @width - (i-2)*50 - 250*(Math.log2(i-1).floor+1) %>" + y1="<%= (@height/(Math.log2(i-1).floor+2)) - 60 + 250*((i-1) - 2**(Math.log2(i-1).floor)) + 60 %>" + x2="<%= @width - (i-1)*50 - 250*(Math.log2(i).floor+1) + 213 %>" + y2="<%= (@height/(Math.log2(i).floor+2)) - 60 + 250*(i - 2**(Math.log2(i).floor)) + 60 %>" + /> + <% end %> + <% end %> </SVG> +</div> diff --git a/app/views/matches/show.html.erb b/app/views/matches/show.html.erb index 6fb4042..25be7b8 100644 --- a/app/views/matches/show.html.erb +++ b/app/views/matches/show.html.erb @@ -2,7 +2,6 @@ <strong>Status:</strong> <%= @match.status %> </p> - <p> <strong>Tournament:</strong> <%= @match.tournament.id %> @@ -27,49 +26,39 @@ Note:- The change of status from 1 to 2 is coming from League Data Pull (RIOT API) --> -<!-- - This is what the HOST will see when the Match Status is NOT 3 ---> -<% if (@tournament.hosts.include?(current_user) and @match.winner.nil?) %> - <%= form_for([@tournament, @match], method: "put") do |f| %> - <ul> - <% @match.teams.each do |team| %> - <li><label><%= f.radio_button(:winner, team.id) %> - <%= team.users.collect{|u| u.user_name}.join(", ") %></label></li> - <% end %> - </ul> - <%= f.submit("Select Winner") %> - <% end %> -<% end %> <!-- This is what the Players and the Hosts of the tournament will view when the Match Status is 0 --> -<% if (@match.status==0) %> +<% if (@match.status==1) %> <% if (@tournament.players.include?(current_user) || @tournament.hosts.include?(current_user)) %> <% @match.teams.each do |team| %> - <ul> + <ol> <% team.users.collect{|u| u.user_name}.each do |k| %> - <li><label><%= k %></label></li> + <li><%= k %></li> <% end %> - </ul> + </ol> <% end %> <% end %> <% end %> <!-- + When Match Status is 2 Players see the Peer Review Page Host see the Game Status --> <% if (@match.status==0) %> - <% if (@tournament.players.include?(current_user) %> + <% if (@tournament.players.include?(current_user)) %> <% @match.teams.each do |team| %> - <ul> + <ol id="boxes" class="sortable"> <% team.users.collect{|u| u.user_name}.each do |k| %> - <li><label><%= k %></label></li> + <li><%= k %></li> <% end %> - </ul> - <% end %> + </ol> + + <% end %> + <% elsif (@tournament.hosts.include?(current_user)) %> + <label>Game Status Page Goes here! Because you are a Host that is not a player!</label> <% end %> <% end %> diff --git a/app/views/servers/_form.html.erb b/app/views/servers/_form.html.erb index b08654b..1afde11 100644 --- a/app/views/servers/_form.html.erb +++ b/app/views/servers/_form.html.erb @@ -1,15 +1,16 @@ <%= form_for(@server) do |f| %> - <% if @server.errors.any? %> - <div id="error_explanation"> - <h2><%= pluralize(@server.errors.count, "error") %> prohibited this server from being saved:</h2> + <%= render "common/error_messages", :target => @server %> - <ul> - <% @server.errors.full_messages.each do |msg| %> - <li><%= msg %></li> + <fieldset> + <legend>Default permissions for new users</legend> + <ul> + <%= fields_for "server[default_user_abilities]", @server.default_user_abilities do |a| %> + <% @server.default_user_abilities.keys.each do |ability| %> + <li><label><%= a.check_box(ability) %> <%= ability.to_s.humanize %></label></li> <% end %> - </ul> - </div> - <% end %> + <% end %> + </ul> + </fieldset> <div class="actions"> <%= f.submit %> diff --git a/app/views/servers/edit.html.erb b/app/views/servers/edit.html.erb index a92cdb5..d37864f 100644 --- a/app/views/servers/edit.html.erb +++ b/app/views/servers/edit.html.erb @@ -2,5 +2,4 @@ <%= render 'form' %> -<%= link_to 'Show', @server %> | -<%= link_to 'Back', servers_path %> +<%= link_to server_path %> diff --git a/app/views/servers/index.html.erb b/app/views/servers/index.html.erb deleted file mode 100644 index f45d393..0000000 --- a/app/views/servers/index.html.erb +++ /dev/null @@ -1,25 +0,0 @@ -<h1>Listing servers</h1> - -<table> - <thead> - <tr> - <th></th> - <th></th> - <th></th> - </tr> - </thead> - - <tbody> - <% @servers.each do |server| %> - <tr> - <td><%= link_to 'Show', server %></td> - <td><%= link_to 'Edit', edit_server_path(server) %></td> - <td><%= link_to 'Destroy', server, method: :delete, data: { confirm: 'Are you sure?' } %></td> - </tr> - <% end %> - </tbody> -</table> - -<br> - -<%= link_to 'New Server', new_server_path %> diff --git a/app/views/servers/index.json.jbuilder b/app/views/servers/index.json.jbuilder deleted file mode 100644 index 2776abc..0000000 --- a/app/views/servers/index.json.jbuilder +++ /dev/null @@ -1,4 +0,0 @@ -json.array!(@servers) do |server| - json.extract! server, :id - json.url server_url(server, format: :json) -end diff --git a/app/views/servers/new.html.erb b/app/views/servers/new.html.erb deleted file mode 100644 index 0422009..0000000 --- a/app/views/servers/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<h1>New server</h1> - -<%= render 'form' %> - -<%= link_to 'Back', servers_path %> diff --git a/app/views/servers/show.html.erb b/app/views/servers/show.html.erb index 67f7647..54aaf66 100644 --- a/app/views/servers/show.html.erb +++ b/app/views/servers/show.html.erb @@ -1,2 +1,6 @@ -<%= link_to 'Edit', edit_server_path(@server) %> | -<%= link_to 'Back', servers_path %> +<p> + <strong>Default user permissions:</strong> + <%= @server.default_user_permissions %> +</p> + +<%= link_to 'Edit', edit_server_path %> diff --git a/app/views/servers/show.json.jbuilder b/app/views/servers/show.json.jbuilder index 972b1c0..c566f76 100644 --- a/app/views/servers/show.json.jbuilder +++ b/app/views/servers/show.json.jbuilder @@ -1 +1 @@ -json.extract! @server, :id, :created_at, :updated_at +json.extract! @server, :id, :default_user_permissions, :created_at, :updated_at diff --git a/app/views/tournaments/index.html.erb b/app/views/tournaments/index.html.erb index e174de7..72eacba 100644 --- a/app/views/tournaments/index.html.erb +++ b/app/views/tournaments/index.html.erb @@ -15,13 +15,17 @@ <h3><%= t.name %></h3> <% end %> - <div class="row"> + <div class="row" style="margin-left:2%;"> <div class="col-md-4 host"> Hosted by: <%= t.hosts.first.name %> </div> - <div class="col-md-8 things"> - <p> Players per team </p> - <p> two </p> + <div class="col-md-4 things"> + <p> Players per team: <%= t.min_players_per_team %></p> + <p> Players signed up: <%= t.players.count %> </p> + </div> + <div class="col-md-4 things"> + <p> <%= (t.randomized_teams)? "Teams are Random" : "Teams are Chosen" %></p> + <p> Players signed up: <%= t.players.count %> </p> </div> </div> @@ -36,7 +40,7 @@ <%= submit_tag("Join") %> <% end %> <% else %> - <p> You've signed up for this tournament! </p> + <p style="margin-top:10px;"> You've signed up for this tournament! </p> <% end %> </div> diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index b654804..e80e0e8 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -66,13 +66,13 @@ <% if @tournament.joinable_by?(current_user) && !@tournament.players.include?(current_user) %> <%= form_tag(tournament_path(@tournament), method: "put") do %> <input type="hidden" name="update_action" value="join"> - <%= submit_tag("Join Tournamnet") %> + <%= submit_tag("Join Tournament") %> <% end %> <% elsif @tournament.players.include?(current_user) %> <%= form_tag(tournament_path(@tournament), method: "put") do %> <input type="hidden" name="update_action" value="leave"> - <%= submit_tag("Leave Tournamnet") %> + <%= submit_tag("Leave Tournament") %> <% end %> <% end %> |