summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/alerts_controller.rb32
-rw-r--r--app/controllers/application_controller.rb67
-rw-r--r--app/controllers/brackets_controller.rb74
-rw-r--r--app/controllers/games_controller.rb24
-rw-r--r--app/controllers/main_controller.rb2
-rw-r--r--app/controllers/matches_controller.rb135
-rw-r--r--app/controllers/pms_controller.rb33
-rw-r--r--app/controllers/search_controller.rb61
-rw-r--r--app/controllers/servers_controller.rb78
-rw-r--r--app/controllers/sessions_controller.rb94
-rw-r--r--app/controllers/teams_controller.rb23
-rw-r--r--app/controllers/tournaments_controller.rb194
-rw-r--r--app/controllers/users_controller.rb62
13 files changed, 702 insertions, 177 deletions
diff --git a/app/controllers/alerts_controller.rb b/app/controllers/alerts_controller.rb
index a3cb8f9..1da2c9b 100644
--- a/app/controllers/alerts_controller.rb
+++ b/app/controllers/alerts_controller.rb
@@ -1,6 +1,26 @@
-class AlertsController < ApplicationController
- before_action :set_alert, only: [:show, :edit, :update, :destroy]
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+class AlertsController < ApplicationController
# GET /alerts
# GET /alerts.json
def index
@@ -25,6 +45,13 @@ class AlertsController < ApplicationController
# POST /alerts.json
def create
@alert = Alert.new(alert_params)
+ @alert.author = current_user
+ users = {}
+ users = User.all
+
+ for i in 0..users.length
+ current_user.send_message(users[i], @alert.message, "Pay Attention!")
+ end
respond_to do |format|
if @alert.save
@@ -62,6 +89,7 @@ class AlertsController < ApplicationController
end
private
+
# Use callbacks to share common setup or constraints between actions.
def set_alert
@alert = Alert.find(params[:id])
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 27ef6a7..f45eb49 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,5 +1,72 @@
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+
class ApplicationController < ActionController::Base
+ before_action :set_object, only: [:show]
+ before_action :check_create, only: [:new, :create]
+ before_action :check_edit, only: [:edit, :update]
+ before_action :check_delete, only: [:destroy]
+
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
+
+ #include sessionhelper for the session controller and view
+ include SessionsHelper
+
+ include SimpleCaptcha::ControllerHelpers
+
+ def check_permission(verb, object=nil)
+ unless current_user.can?("#{verb.to_s}_#{noun}".to_sym) or object.try(:check_permission, current_user, verb)
+ respond_to do |format|
+ format.html do
+ if object.nil?
+ redirect_to send(noun.pluralize+"_url"), notice: "You don't have permission to #{verb} #{noun.pluralize}."
+ else
+ redirect_to object, notice: "You don't have permission to #{verb} this #{noun}."
+ end
+ end
+ format.json { render json: "Permission denied", status: :forbidden }
+ end
+ end
+ end
+
+ def noun
+ @noun ||= self.class.name.underscore.sub(/_controller$/, '').singularize
+ end
+
+ def set_object
+ object = send("set_"+noun)
+ end
+
+ def check_create
+ check_permission(:create)
+ end
+ def check_edit
+ object = send("set_"+noun)
+ check_permission(:edit, object)
+ end
+ def check_delete
+ object = send("set_"+noun)
+ check_permission(:edit, object)
+ end
end
diff --git a/app/controllers/brackets_controller.rb b/app/controllers/brackets_controller.rb
index fe43ca9..014687f 100644
--- a/app/controllers/brackets_controller.rb
+++ b/app/controllers/brackets_controller.rb
@@ -1,20 +1,52 @@
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+
class BracketsController < ApplicationController
- before_action :set_bracket, only: [:show, :edit, :update, :destroy]
+ before_action :set_tournament, only: [:index, :create]
# GET /brackets
# GET /brackets.json
def index
- @brackets = Bracket.all
+ @tournament = Tournament.find(params[:tournament_id])
+ @brackets = @tournament.brackets
end
# GET /brackets/1
# GET /brackets/1.json
def show
- end
+ @results = (@tournament.status == 4)? @bracket.calcResult : nil;
+ @matches = @tournament.stages.order(:id).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;
- # GET /brackets/new
- def new
- @bracket = Bracket.new
+ @base = 1
+ @pBase = 1
end
# GET /brackets/1/edit
@@ -24,14 +56,17 @@ class BracketsController < ApplicationController
# POST /brackets
# POST /brackets.json
def create
- @bracket = Bracket.new(bracket_params)
+ @bracket = @tournament.brackets.build(user: current_user)
+ @bracket.name = current_user.user_name + "'s Prediction for " + @tournament.name
respond_to do |format|
- if @bracket.save
+ if @tournament.status == 1 && @tournament.stages.first.scheduling_method == "elimination" && @tournament.stages.first.matches.first.status < 2
+ @bracket.save
+ @bracket.create_matches
format.html { redirect_to @bracket, notice: 'Bracket was successfully created.' }
- format.json { render action: 'show', status: :created, location: @bracket }
+ 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
@@ -41,11 +76,11 @@ class BracketsController < ApplicationController
# PATCH/PUT /brackets/1.json
def update
respond_to do |format|
- if @bracket.update(bracket_params)
- format.html { redirect_to @bracket, 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
@@ -64,11 +99,24 @@ class BracketsController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_bracket
+ @tournament = Tournament.find(params[:tournament_id])
@bracket = Bracket.find(params[:id])
end
+ def set_tournament
+ @tournament = Tournament.find(params[:tournament_id])
+ end
+
# 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
+ params.require(:bracket).require(:matches)
+ end
end
diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb
index 27df771..b724d3a 100644
--- a/app/controllers/games_controller.rb
+++ b/app/controllers/games_controller.rb
@@ -1,6 +1,26 @@
-class GamesController < ApplicationController
- before_action :set_game, only: [:show, :edit, :update, :destroy]
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+class GamesController < ApplicationController
# GET /games
# GET /games.json
def index
diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb
index 6519d7b..0ba4d94 100644
--- a/app/controllers/main_controller.rb
+++ b/app/controllers/main_controller.rb
@@ -1,2 +1,4 @@
class MainController < ApplicationController
+ def homepage
+ end
end
diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb
index 4042d3c..70d7359 100644
--- a/app/controllers/matches_controller.rb
+++ b/app/controllers/matches_controller.rb
@@ -1,62 +1,92 @@
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+
class MatchesController < ApplicationController
- before_action :set_match, only: [:show, :edit, :update, :destroy]
+ require 'httparty'
+ require 'json'
+ require 'delayed_job'
+
+ before_action :set_tournament, only: [:index]
- # GET /matches
- # GET /matches.json
+ # GET /tournaments/1/matches
+ # GET /tournaments/1/matches.json
def index
- @matches = Match.all
end
- # GET /matches/1
- # GET /matches/1.json
+ # GET /tournaments/1/matches/1
+ # GET /tournaments/1/matches/1.json
def show
end
- # GET /matches/new
- def new
- @match = Match.new
- end
-
- # GET /matches/1/edit
- def edit
- end
-
- # POST /matches
- # POST /matches.json
- def create
- @match = Match.new(match_params)
-
- respond_to do |format|
- if @match.save
- format.html { redirect_to @match, notice: 'Match was successfully created.' }
- format.json { render action: 'show', status: :created, location: @match }
- else
- format.html { render action: 'new' }
- format.json { render json: @match.errors, status: :unprocessable_entity }
- end
- end
- end
-
- # PATCH/PUT /matches/1
- # PATCH/PUT /matches/1.json
+ # PATCH/PUT /tournaments/1/matches/1
+ # PATCH/PUT /tournaments/1/matches/1.json
def update
- respond_to do |format|
- if @match.update(match_params)
- format.html { redirect_to @match, notice: 'Match was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
- format.json { render json: @match.errors, status: :unprocessable_entity }
+ notice = nil
+ case @match.status
+ when 0
+ # Created, waiting to be scheduled
+ when 1
+ # Scheduled, waiting to start
+ if (@tournament.hosts.include? current_user) and (params[:update_action] == "start")
+ @match.status = 2
+ @match.start_sampling
+ if @match.save
+ notice = 'Match has started.'
+ else
+ respond_to do |format|
+ format.html { render action: 'show' }
+ format.json { render json: @match.errors, status: :unprocessable_entity }
+ end
+ return
+ end
+ end
+ when 2
+ # Started, waiting to finish
+ @match.handle_sampling(current_user, params)
+ # The @match.status will be updated by Statistic's after_save hook
+ if @match.status == 3
+ notice = 'Match has finished'
+ end
+ when 3
+ if (@tournament.hosts.include? current_user) and (params[:update_action] == "start")
+ ok = true
+ ActiveRecord::Base.transaction do
+ ok &= @match.statistics.destroy_all
+ ok &- @match.status = 1
+ ok &= @match.save
+ end
+ if ok
+ notice = "Match has been reset"
+ else
+ respond_to do |format|
+ format.html { render action: 'show' }
+ format.json { render json: @match.errors, status: :unprocessable_entity }
+ end
+ return
+ end
end
end
- end
-
- # DELETE /matches/1
- # DELETE /matches/1.json
- def destroy
- @match.destroy
respond_to do |format|
- format.html { redirect_to matches_url }
+ format.html { redirect_to match_path(@match), notice: notice }
format.json { head :no_content }
end
end
@@ -65,10 +95,21 @@ class MatchesController < ApplicationController
# Use callbacks to share common setup or constraints between actions.
def set_match
@match = Match.find(params[:id])
+ @tournament = @match.tournament_stage.tournament
+ end
+
+ def set_tournament
+ @tournament = Tournament.find(params[:tournament_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def match_params
params.require(:match).permit(:status, :tournament_stage_id, :winner_id)
+ params.require(:match).permit(:status, :tournament_stage_id, :winner_id)
+ end
+
+ # Turn of check_edit, since our #update is flexible
+ def check_edit
+ set_match
end
end
diff --git a/app/controllers/pms_controller.rb b/app/controllers/pms_controller.rb
index 11f51c8..faaa38f 100644
--- a/app/controllers/pms_controller.rb
+++ b/app/controllers/pms_controller.rb
@@ -1,6 +1,26 @@
-class PmsController < ApplicationController
- before_action :set_pm, only: [:show, :edit, :update, :destroy]
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+class PmsController < ApplicationController
# GET /pms
# GET /pms.json
def index
@@ -25,6 +45,10 @@ class PmsController < ApplicationController
# POST /pms.json
def create
@pm = Pm.new(pm_params)
+ @pm.author = current_user
+ @pm.recipient = User.find_by_user_name(pm_params['recipient_id'])
+
+ @pm.conversation = @pm.author.send_message(@pm.recipient, @pm.message, @pm.subject).conversation
respond_to do |format|
if @pm.save
@@ -37,6 +61,10 @@ class PmsController < ApplicationController
end
end
+ #def reply
+ # current_user.reply_to_conversation(conversation, message)
+ #end
+
# PATCH/PUT /pms/1
# PATCH/PUT /pms/1.json
def update
@@ -49,6 +77,7 @@ class PmsController < ApplicationController
format.json { render json: @pm.errors, status: :unprocessable_entity }
end
end
+ current_user.reply_to_conversation(@pm.conversation, @pm.message)
end
# DELETE /pms/1
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index ee61487..a66fb40 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -1,2 +1,63 @@
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+
class SearchController < ApplicationController
+
+ def go
+ @games = Game.all
+ @query = params[:query]
+ @gametype = params[:game_type]
+
+ if ( @gametype.nil? and (@query.nil? or @query.empty?)) then
+ return
+ end
+
+ tour_filters = []
+ user_filters = []
+ unless @query.empty?
+ tour_filters.push(["name LIKE ?", "%#{@query}%"])
+ user_filters.push(["name LIKE ?", "%#{@query}%"])
+ end
+ unless @gametype.nil? or @gametype.empty?
+ tour_filters.push(["game_id = ?", @gametype])
+ end
+
+ if tour_filters.empty?
+ @tournamets = []
+ else
+ @tournaments = Tournament
+ tour_filters.each do |filter|
+ @tournaments = @tournaments.where(*filter)
+ end
+ end
+
+ if user_filters.empty?
+ @players = []
+ else
+ @players = User
+ user_filters.each do |filter|
+ @players = @players.where(*filter)
+ end
+ end
+ end
+
end
diff --git a/app/controllers/servers_controller.rb b/app/controllers/servers_controller.rb
index 4c12c7e..2a2ce5f 100644
--- a/app/controllers/servers_controller.rb
+++ b/app/controllers/servers_controller.rb
@@ -1,48 +1,41 @@
-class ServersController < ApplicationController
- before_action :set_server, only: [:show, :edit, :update, :destroy]
-
- # GET /servers
- # GET /servers.json
- def index
- @servers = Server.all
- end
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
- # GET /servers/1
- # GET /servers/1.json
+class ServersController < ApplicationController
+ # 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)
- format.html { redirect_to @server, notice: 'Server was successfully updated.' }
+ format.html { redirect_to edit_server_url, notice: 'Server was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
@@ -51,24 +44,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.require(:server).permit(:default_user_permissions)
+ params.require(:server).permit(:default_user_permissions, :default_user_abilities => User.permission_bits.keys)
end
end
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
index b035ea0..ead53f0 100644
--- a/app/controllers/sessions_controller.rb
+++ b/app/controllers/sessions_controller.rb
@@ -1,52 +1,47 @@
-class SessionsController < ApplicationController
- before_action :set_session, only: [:show, :edit, :update, :destroy]
-
- # GET /sessions
- # GET /sessions.json
- def index
- @sessions = Session.all
- end
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
- # GET /sessions/1
- # GET /sessions/1.json
- def show
- end
+class SessionsController < ApplicationController
# GET /sessions/new
def new
- @session = Session.new
- end
-
- # GET /sessions/1/edit
- def edit
end
# POST /sessions
# POST /sessions.json
def create
- @session = Session.new(session_params)
+ # find the user...
+ user = User.find_by_email(params[:username_or_email].to_s) || User.find_by_user_name(params[:username_or_email].to_s)
+ #@session = Session.new(@user)
+ # ... and create a new session
respond_to do |format|
- if @session.save
- format.html { redirect_to @session, notice: 'Session was successfully created.' }
- format.json { render action: 'show', status: :created, location: @session }
+ if user && user.authenticate(params[:password].to_s)
+ sign_in user
+ format.html { redirect_to root_path, notice: "Welcome, #{user.name}" } # TODO; previous URL
+ #format.json { # TODO }
else
format.html { render action: 'new' }
- format.json { render json: @session.errors, status: :unprocessable_entity }
- end
- end
- end
-
- # PATCH/PUT /sessions/1
- # PATCH/PUT /sessions/1.json
- def update
- respond_to do |format|
- if @session.update(session_params)
- format.html { redirect_to @session, notice: 'Session was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
- format.json { render json: @session.errors, status: :unprocessable_entity }
+ format.json { render json: user.errors, status: :unprocessable_entity }
end
end
end
@@ -54,21 +49,32 @@ class SessionsController < ApplicationController
# DELETE /sessions/1
# DELETE /sessions/1.json
def destroy
- @session.destroy
+ #@session.destroy
+ sign_out
respond_to do |format|
- format.html { redirect_to sessions_url }
+ format.html { redirect_to root_path }
format.json { head :no_content }
end
end
private
- # Use callbacks to share common setup or constraints between actions.
- def set_session
- @session = Session.find(params[:id])
+
+ # Only allow creating a session if not logged in.
+ def check_create
+ unless current_user.nil?
+ respond_to do |format|
+ format.html { redirect_to root_path, notice: "You are already logged in" } # TODO: previous URL
+ format.json { render json: {"errors" => ["already logged in"]}, status: :forbidden }
+ end
+ end
end
- # Never trust parameters from the scary internet, only allow the white list through.
- def session_params
- params.require(:session).permit(:user_id, :token)
+ def check_delete
+ unless signed_in?
+ respond_to do |format|
+ format.html { redirect_to root_path, notice: "You are not logged in" } # TODO: previous URL
+ format.json { render json: {"errors" => ["not logged in"]}, status: :forbidden }
+ end
+ end
end
end
diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb
index 57b3d91..9dbdd82 100644
--- a/app/controllers/teams_controller.rb
+++ b/app/controllers/teams_controller.rb
@@ -1,5 +1,26 @@
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+
class TeamsController < ApplicationController
- before_action :set_team, only: [:show, :edit, :update, :destroy]
# GET /teams
# GET /teams.json
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb
index 51229cb..8458ba1 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -1,5 +1,26 @@
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+
class TournamentsController < ApplicationController
- before_action :set_tournament, only: [:show, :edit, :update, :destroy]
# GET /tournaments
# GET /tournaments.json
@@ -10,24 +31,73 @@ class TournamentsController < ApplicationController
# GET /tournaments/1
# GET /tournaments/1.json
def show
+ respond_to do |format|
+ format.html {
+ case @tournament.status
+ when 0
+ render action: 'show'
+ when 1
+ redirect_to tournament_matches_path(@tournament)
+ when 2
+ redirect_to tournaments_page
+ end
+ }
+ format.json {
+ data = JSON.parse(@tournament.to_json)
+ data["players"] = @tournament.players;
+ render :json => data.to_json
+ }
+ end
end
# GET /tournaments/new
def new
- @tournament = Tournament.new
+ @tournament = Tournament.new(tournament_attribute_params)
+ if @tournament.game
+ @tournament.game.settings.each do |game_setting|
+ @tournament.tournament_settings.build(
+ name: game_setting.name,
+ value: game_setting.value,
+ vartype: game_setting.vartype,
+ type_opt: game_setting.type_opt,
+ description: game_setting.description,
+ display_order: game_setting.display_order)
+ end
+ end
end
# GET /tournaments/1/edit
def edit
+ check_permission(:edit, @tournament)
end
# POST /tournaments
# POST /tournaments.json
def create
- @tournament = Tournament.new(tournament_params)
-
+ ok = true
+ begin
+ ActiveRecord::Base.transaction do
+ ok &= @tournament = Tournament.new(tournament_attribute_params.merge({hosts: [current_user]}))
+ ok &= @tournament.update(tournament_setting_params)
+ for i in 1..(params[:num_stages].to_i) do
+ begin
+ ok &= @tournament.stages.build(tournament_stage_params(i))
+ rescue ActionController::ParameterMissing => e
+ ok = false
+ @tournament.errors.add("stages[#{i}]", "needs to be set")
+ end
+ end
+ ok &= @tournament.save
+ end
+ rescue ActiveRecord::RecordNotUnique => e
+ ok = false
+ @tournament.errors.add(:name, "must be unique")
+ rescue => e
+ ok = false
+ @tournament.errors.add(:exception, "Unknown error: ``#{e.class.name}'' -- #{e.inspect} -- #{e.methods - Object.new.methods}")
+ end
respond_to do |format|
- if @tournament.save
+ if ok
format.html { redirect_to @tournament, notice: 'Tournament was successfully created.' }
format.json { render action: 'show', status: :created, location: @tournament }
else
@@ -40,12 +110,71 @@ class TournamentsController < ApplicationController
# PATCH/PUT /tournaments/1
# PATCH/PUT /tournaments/1.json
def update
- respond_to do |format|
- if @tournament.update(tournament_params)
- format.html { redirect_to @tournament, notice: 'Tournament was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
+ case params[:update_action]
+ when nil
+ check_permission(:edit, @tournament)
+ ok = true
+ ActiveRecord::Base.transaction do
+ ok &= @tournament.update(tournament_attribute_params)
+ ok &= @tournament.update(tournament_setting_params)
+ end
+ respond_to do |format|
+ if ok
+ format.html { redirect_to @tournament, notice: 'Tournament was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: 'edit' }
+ format.json { render json: @tournament.errors, status: :unprocessable_entity }
+ end
+ end
+ when "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
+ 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
+ end
+ when "start"
+ check_permission(:edit, @tournament)
+ respond_to do |format|
+ if @tournament.status == 0
+ @tournament.status = 1
+ @tournament.save
+ success = true
+ ActiveRecord::Base.transaction do
+ # sched = tournament_attribute_params[:type_opt]
+ # success &= @tournament.stages.create(scheduling_method: sched)
+ success &= @tournament.stages.first.create_matches
+ end
+ if success
+ format.html { redirect_to @tournament, notice: 'You have started 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
+ else
+ format.html { redirect_to @tournament, notice: "This tournament is not in a state that it can be started." }
+ 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
@@ -64,11 +193,48 @@ class TournamentsController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_tournament
- @tournament = Tournament.find(params[:id])
+ begin
+ @tournament = Tournament.find(params[:id])
+ rescue
+ redirect_to tournaments_url, notice: 'That tournament no longer exists.'
+ end
end
# Never trust parameters from the scary internet, only allow the white list through.
- def tournament_params
- params.require(:tournament).permit(:game_id, :status, :name, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :scoring_method)
+ def tournament_attribute_params
+ params[:num_stages] ||= 1
+ if params[:tournament]
+ p = params.require(:tournament).permit(:game_id, :status, :name, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :scoring_method)
+ if p[:game_id]
+ game = Game.find(p[:game_id])
+ p[:min_players_per_team] ||= game.min_players_per_team
+ p[:max_players_per_team] ||= game.max_players_per_team
+ p[:min_teams_per_match] ||= game.min_teams_per_match
+ p[:max_teams_per_match] ||= game.max_teams_per_match
+ p[:scoring_method] ||= game.scoring_method
+ end
+ return p
+ else
+ return {}
+ end
end
+
+ def tournament_setting_params
+ if tournament_attribute_params[:game_id]
+ game = Game.find(params[:tournament][:game_id])
+ params.require(:tournament).permit({:settings => game.settings.collect{|s| s.name}})
+ else
+ return {}
+ end
+ end
+
+ def tournament_stage_params(i)
+ params.require(:tournament).require(:stages).require(i.to_s).permit(:scheduling_method, :seeding_method)
+ end
+
+ # Turn of check_edit, since our #update is flexible
+ def check_edit
+ set_tournament
+ end
+
end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 58bf4c6..37c84ae 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1,7 +1,32 @@
+# Copyright (C) 2014 Andrew Murrell
+# Copyright (C) 2014 Davis Webb
+# Copyright (C) 2014 Guntas Grewal
+# Copyright (C) 2014 Luke Shumaker
+# Copyright (C) 2014 Nathaniel Foy
+# Copyright (C) 2014 Tomer Kimia
+#
+# This file is part of Leaguer.
+#
+# Leaguer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Leaguer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the Affero GNU General Public License
+# along with Leaguer. If not, see <http://www.gnu.org/licenses/>.
+
class UsersController < ApplicationController
- before_action :set_user, only: [:show, :edit, :update, :destroy]
+
+ require 'httparty'
+ require 'json'
# GET /users
+
# GET /users.json
def index
@users = User.all
@@ -25,13 +50,26 @@ class UsersController < ApplicationController
# POST /users.json
def create
@user = User.new(user_params)
+ unless (true) # simple_captcha_valid?)
+ respond_to do |format|
+ format.html { render action: 'new', status: :unprocessable_entity }
+ format.json { render json: @user.errors, status: :unprocessable_entity }
+ end
+ return
+ end
respond_to do |format|
if @user.save
- format.html { redirect_to @user, notice: 'User was successfully created.' }
+ sign_in @user
+ if @user.id == 1
+ # This is the first user, so give them all the power
+ @user.permissions = 0x7FFFFFFF
+ @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' }
+ format.html { render action: 'new', status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
@@ -40,8 +78,17 @@ class UsersController < ApplicationController
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
+ ok = true
+ if params[:user][:remote_usernames].nil?
+ ok &= @user.update(user_params)
+ else
+ params[:user][:remote_usernames].each do |game_name,user_name|
+ game = Game.find_by_name(game_name)
+ Sampling::RiotApi::set_remote_name(@user, game, user_name)
+ end
+ end
respond_to do |format|
- if @user.update(user_params)
+ if ok
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
@@ -61,6 +108,7 @@ class UsersController < ApplicationController
end
end
+
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@@ -69,6 +117,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)
+ 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