summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorguntasgrewal <guntasgrewal@gmail.com>2014-04-06 20:09:54 -0400
committerguntasgrewal <guntasgrewal@gmail.com>2014-04-06 20:09:54 -0400
commitf3c395c56a644d976aa9a5608300557600bc683e (patch)
tree83cb961a721fa8c50903c70bb08ae14dd15ca58c /app
parent7575d8cc70a28b323db0486ed06ab8af33b1f21a (diff)
parentf85943114dba527a1f87abb03229553472f57c0c (diff)
solved merge conflicts
Diffstat (limited to 'app')
-rw-r--r--app/assets/stylesheets/custom.css.scss1
-rw-r--r--app/controllers/matches_controller.rb4
-rw-r--r--app/controllers/servers_controller.rb53
-rw-r--r--app/controllers/tournaments_controller.rb17
-rw-r--r--app/controllers/users_controller.rb36
-rw-r--r--app/helpers/sessions_helper.rb4
-rw-r--r--app/models/server.rb36
-rw-r--r--app/models/tournament.rb4
-rw-r--r--app/models/user.rb158
-rw-r--r--app/views/layouts/application.html.erb3
-rw-r--r--app/views/matches/index.html.erb32
-rw-r--r--app/views/servers/_form.html.erb19
-rw-r--r--app/views/servers/edit.html.erb3
-rw-r--r--app/views/servers/index.html.erb25
-rw-r--r--app/views/servers/index.json.jbuilder4
-rw-r--r--app/views/servers/new.html.erb5
-rw-r--r--app/views/servers/show.html.erb8
-rw-r--r--app/views/servers/show.json.jbuilder2
-rw-r--r--app/views/users/_form.html.erb34
19 files changed, 263 insertions, 185 deletions
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/controllers/matches_controller.rb b/app/controllers/matches_controller.rb
index 31fc9ad..e773667 100644
--- a/app/controllers/matches_controller.rb
+++ b/app/controllers/matches_controller.rb
@@ -8,6 +8,10 @@ class MatchesController < ApplicationController
def index
@matches = @tournament.matches
+ # width of SVG
+ @width = 300 * (Math.log2(@matches.count).floor + 1) + 300;
+ # height of SVG
+ @height = 200 * 2**Math.log2(@matches.count).floor + 100;
end
def get_riot_info
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 010f279..2fc82ed 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -70,23 +70,25 @@ class TournamentsController < ApplicationController
end
end
when "join"
- check_permission(:join)
+ # permission checking for join is done in the Tournament model
respond_to do |format|
if @tournament.join(current_user)
format.html { redirect_to @tournament, notice: 'You have joined this tournament.' }
format.json { head :no_content }
+ else
+ format.html { redirect_to @tournament, notice: "You can't join this tournament." }
+ format.json { render json: "Permission denied", status: :forbidden }
end
- format.html { render action: 'permission_denied', status: :forbidden }
- format.json { render json: "Permission denied", status: :forbidden }
end
when "leave"
respond_to do |format|
if @tournament.leave(current_user)
format.html { redirect_to tournaments_url, notice: 'You have left the tournament.' }
format.json { head :no_content }
+ else
+ format.html { redirect_to @tournament, notice: 'You were\'t a part of this tournament.' }
+ format.json { render json: "Permission denied", status: :forbidden }
end
- format.html {redirect_to @tournament, notice: 'You were\'t a part of this tournament.' }
- format.json { render json: "Permission denied", status: :forbidden }
end
when "start"
check_permission(:edit, @tournament)
@@ -96,9 +98,10 @@ class TournamentsController < ApplicationController
if @tournament.setup
format.html { redirect_to @tournament, notice: 'You have joined this tournament.' }
format.json { head :no_content }
+ else
+ format.html { redirect_to @tournament, notice: "You don't have permission to start this tournament." }
+ format.json { render json: "Permission denied", status: :forbidden }
end
- format.html { render action: 'permission_denied', status: :forbidden }
- format.json { render json: "Permission denied", status: :forbidden }
end
else
respond_to do |format|
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index bcb45aa..637480f 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -24,17 +24,29 @@ class UsersController < ApplicationController
# POST /users
# POST /users.json
def create
- if simple_captcha_valid?
- @user = User.new(user_params)
+ @user = User.new(user_params)
+ unless (simple_captcha_valid?)
respond_to do |format|
- if @user.save
- sign_in @user
- format.html { redirect_to root_path, notice: 'User was successfully created.' }
- format.json { render action: 'show', status: :created, location: @user }
- else
- format.html { render action: 'new', status: :unprocessable_entity }
- format.json { render json: @user.errors, status: :unprocessable_entity }
+ format.html { render action: 'new', status: :unprocessable_entity }
+ format.json { render json: @user.errors, status: :unprocessable_entity }
+ end
+ return
+ end
+
+ @user.permissions = Server.first.default_user_permissions
+ respond_to do |format|
+ if @user.save
+ sign_in @user
+ if @user.id == 1
+ # This is the first user, so give them all the power
+ @user.permissions = 0xFFFFFFFF
+ @user.save
end
+ format.html { redirect_to root_path, notice: 'User was successfully created.' }
+ format.json { render action: 'show', status: :created, location: @user }
+ else
+ format.html { render action: 'new', status: :unprocessable_entity }
+ format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
@@ -75,6 +87,10 @@ class UsersController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
- params.require(:user).permit(:name, :email, :user_name, :password, :password_confirmation)
+ permitted = [ :name, :email, :user_name, :password, :password_confirmation ]
+ if current_user.can? :edit_permissions
+ permitted.push(:abilities => User.permission_bits.keys)
+ end
+ params.require(:user).permit(permitted)
end
end
diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb
index ac62cdc..499e988 100644
--- a/app/helpers/sessions_helper.rb
+++ b/app/helpers/sessions_helper.rb
@@ -20,7 +20,7 @@ module SessionsHelper
def current_user
@token ||= Session.hash_token(cookies[:remember_token])
@session ||= Session.find_by(token: @token)
- @current_user ||= (@session.nil? ? NilUser.new : @session.user)
+ @current_user ||= (@session.nil? ? User::NilUser.new : @session.user)
end
# checks if someone is currently signed in
@@ -32,7 +32,7 @@ module SessionsHelper
if signed_in?
@session.destroy
end
- @current_user = NilUser.new
+ @current_user = User::NilUser.new
cookies.delete(:remember_token)
end
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 e408cfe..0029de7 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -31,7 +31,7 @@ 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|
@@ -39,7 +39,7 @@ class Tournament < ActiveRecord::Base
self.matches[match_num].teams.push(Team.create(users: 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
+ match_num -= 1
team_num = 0
else
team_num += 1
diff --git a/app/models/user.rb b/app/models/user.rb
index 64dd7ed..0b77ab1 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -9,57 +9,109 @@ class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_save { self.user_name = user_name }
- def after_initialize
- self.permissions = 0
- end
+ def self.permission_bits
+ return {
+ :create_tournament => (2**1),
+ :edit_tournament => (2**2),
+ :join_tournament => (2**3),
+ :delete_tournament => (2**4),
- def can?(action)
- return true
- case action
- when :create_tournament
- return true
- when :edit_tournament
- return true
- when :join_tournament
- return true
- when :delete_tournament
+ :create_game => (2**5),
+ :edit_game => (2**6),
+ :delete_game => (2**7),
- when :create_game
- when :edit_game
- when :delete_game
+ :create_user => (2**8),
+ :edit_user => (2**9),
+ :delete_user => (2**10),
- when :create_user
- return false
- when :edit_user
- when :delete_user
+ :create_alert => (2**11),
+ :edit_alert => (2**12),
+ :delete_alert => (2**13),
- when :create_alert
- when :edit_alert
- when :delete_alert
+ :create_pm => (2**14),
+ :edit_pm => (2**15),
+ :delete_pm => (2**16),
- when :create_pm
- when :edit_pm
- when :delete_pm
+ :create_session => (2**17),
+ :delete_session => (2**18),
- when :create_session
- return false
- when :delete_session
+ :edit_permissions => (2**19),
+ :edit_server => (2**20),
+ }
+ end
- else
+ def can?(action)
+ bit = User.permission_bits[action]
+ if bit.nil?
return false
+ else
+ return (self.permissions & bit != 0)
+ end
+ end
+
+ def add_ability(action)
+ bit = User.permission_bits[action.to_sym]
+ unless bit.nil?
+ self.permissions |= bit
+ end
+ end
+
+ def remove_ability(action)
+ bit = User.permission_bits[action.to_sym]
+ unless bit.nil?
+ self.permissions &= ~ bit
+ end
+ end
+
+
+ # A representation of the permission bits as a mock-array.
+ def abilities
+ @abilities ||= Abilities.new(self)
+ end
+ def abilities=(new)
+ new.each do |k,v|
+ if v == "0"
+ v = false
+ end
+ abilities[k] = v
+ end
+ end
+
+ # A thin array-like wrapper around the permission bits to make it
+ # easy to modify them using a form.
+ class Abilities
+ def initialize(user)
+ @user = user
+ end
+ def [](ability)
+ return @user.can?(ability)
+ end
+ def []=(ability, val)
+ if val
+ @user.add_ability(ability)
+ else
+ @user.remove_ability(ability)
+ end
+ end
+ def keys
+ User.permission_bits.keys
+ end
+ def method_missing(name, *args)
+ if name.to_s.ends_with?('=')
+ self[name.to_s.sub(/=$/, '').to_sym] = args.first
+ else
+ return self[name.to_sym]
+ end
end
end
- ##
# VAILD_EMAIL is the regex used to validate a user given email.
VALID_EMAIL_REG = /\A\S+@\S+\.\S+\z/i
- ##
# VALID_USER_NAME checks to make sure a user's user_name
# is in the proper format.
VALID_USER_NAME_REG = /\A[a-zA-Z0-9\-]+\z/
- ##
# The following lines put a user account through a series of
# validations in order to make sure all of their information
# is in the proper format.
@@ -78,7 +130,6 @@ class User < ActiveRecord::Base
format: {with: VALID_USER_NAME_REG },
uniqueness: {case_sensitive: false })
- ##
# Instead of adding password and password_confirmation
# attributes, requiring the presence of a password,
# requiring that pw and pw_com match, and add an authenticate
@@ -88,26 +139,27 @@ class User < ActiveRecord::Base
has_secure_password
validates :password, length: { minimum: 6 }
-end
-class NilUser
- def nil?
- return true
- end
- def can?(action)
- case action
- when :create_user
- return true
- when :create_session
+
+ class NilUser
+ def nil?
return true
- else
- return false
end
- end
- def method_missing(name, *args)
- # Throw an error if User doesn't have this method
- super unless User.new.respond_to?(name)
- # User has this method -- return a blank value
- # 'false' if the method ends with '?'; 'nil' otherwise.
- name.ends_with?('?') ? false : nil
+ def can?(action)
+ case action
+ when :create_user
+ return true
+ when :create_session
+ return true
+ else
+ return false
+ end
+ end
+ def method_missing(name, *args)
+ # Throw an error if User doesn't have this method
+ super unless User.new.respond_to?(name)
+ # User has this method -- return a blank value
+ # 'false' if the method ends with '?'; 'nil' otherwise.
+ name.to_s.ends_with?('?') ? false : nil
+ end
end
end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 8a5f985..de9f3b8 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -26,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..031b2a9 100644
--- a/app/views/matches/index.html.erb
+++ b/app/views/matches/index.html.erb
@@ -26,17 +26,39 @@
<br>
-
+<div id="match-tree">
<SVG version="1.1"
baseProfile="full"
- width="<%= 300 * @matches.count / 2 + 50 %>" height="<%= 200 * @matches.count + 50 %>"
+ width="<%= @width %>" height="<%= @height = [@height, 500].max %>"
xmlns="http://www.w3.org/2000/svg">
-
+ <line x1="300" y1="0" x2="300" y2="<%= @height %>" stroke="black" />
<% (1..@matches.count).each do |i| %>
<g class="svg-match">
- <rect rx="10"
-
+ <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>
<% end %>
+
</SVG>
+</div> \ No newline at end of file
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/users/_form.html.erb b/app/views/users/_form.html.erb
index ae63f06..40f8f09 100644
--- a/app/views/users/_form.html.erb
+++ b/app/views/users/_form.html.erb
@@ -1,37 +1,45 @@
<%= form_for(@user) do |f| %>
- <% if @user.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
-
- <ul>
- <% @user.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
+ <%= render "common/error_messages", :target => @user %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
+
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
+
<div class="field">
<%= f.label :user_name %><br>
<%= f.text_field :user_name %>
</div>
- <p>
+
+ <div>
<%= f.label(:password, "New Password (or use old)") %><br>
<%= f.password_field :password %>
- </p>
+ </div>
<div>
<%= f.label(:password_confirmation, "Confirm Password") %><br>
<%= f.password_field :password_confirmation %>
</div>
+
+ <% if current_user.can? :edit_permissions %>
+ <fieldset>
+ <legend>User permissions</legend>
+ <ul>
+ <%= fields_for "user[abilities]", @user.abilities do |abilities_fields| %>
+ <% @user.abilities.keys.each do |ability| %>
+ <li><label><%= abilities_fields.check_box(ability) %> <%= ability.to_s.humanize %></label></li>
+ <% end %>
+ <% end %>
+ </ul>
+ </fieldset>
+ <% end %>
+
<div class="actions">
<%= f.submit %>
</div>
+
<% end %>