summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/tournaments_controller.rb17
-rw-r--r--app/models/tournament.rb21
-rw-r--r--app/models/user.rb1
-rw-r--r--app/views/tournaments/new.html.erb3
-rw-r--r--app/views/tournaments/show.html.erb6
-rw-r--r--config/routes.rb8
-rw-r--r--stop.sh3
7 files changed, 50 insertions, 9 deletions
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb
index 4f79d44..8ed8cc9 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -1,5 +1,5 @@
class TournamentsController < ApplicationController
- before_action :set_tournament, only: [:show, :edit, :update, :destroy]
+ before_action :set_tournament, only: [:show, :edit, :update, :destroy, :join]
before_action :check_perms, only: [:new, :create, :edit, :update, :destroy]
# GET /tournaments
@@ -63,13 +63,26 @@ class TournamentsController < ApplicationController
end
end
+ # POST /tournaments/1/join
+ # POST /tournaments/1/join.json
+ def join
+ 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 }
+ end
+ format.html { render action: 'permission_denied', status: :forbidden }
+ format.json { render json: "Permission denied", status: :forbidden }
+ end
+ end
+
private
# Use callbacks to share common setup or constraints between actions.
def set_tournament
@tournament = Tournament.find(params[:id])
end
- def check_perms
+ def check_perms
unless (signed_in? and current_user.in_group?(:host))
respond_to do |format|
format.html { render action: 'permission_denied', status: :forbidden }
diff --git a/app/models/tournament.rb b/app/models/tournament.rb
index 5e8ddfe..ca7fade 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -1,4 +1,21 @@
class Tournament < ActiveRecord::Base
- belongs_to :game
- has_many :matches
+ belongs_to :game
+ has_many :matches
+ has_many :users, :through => :user_tournament_pair
+
+ def open?
+ return true
+ end
+
+ def joinable_by?(user)
+ return ((not user.nil?) and user.in_group?(:player) and open?)
+ end
+
+ def join(user)
+ unless joinable?(user)
+ return false
+ end
+ pair = new_user_tournament_pair(user: user)
+ return pair.save
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 9288ef6..6405c8e 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,4 +1,5 @@
class User < ActiveRecord::Base
+ has_many :tournaments, :through => :user_tournament_pair
before_save { self.email = email.downcase }
before_save { self.user_name = user_name }
diff --git a/app/views/tournaments/new.html.erb b/app/views/tournaments/new.html.erb
index 1e80147..8c74068 100644
--- a/app/views/tournaments/new.html.erb
+++ b/app/views/tournaments/new.html.erb
@@ -1,8 +1,5 @@
-<%= javascript_include_tag :defaults %>
-
<h1>New Tournament</h1>
-
<%= form_tag(new_tournament_path, method: "get") do %>
<%= select_tag('game',
options_from_collection_for_select(@games, 'id', 'name', @tournament.game.nil? || @tournament.game.id),
diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb
index 4f97c7c..85ed026 100644
--- a/app/views/tournaments/show.html.erb
+++ b/app/views/tournaments/show.html.erb
@@ -1,3 +1,9 @@
+<% if @tournament.joinable_by?(current_user) %>
+ <%= form_tag(tournament_page(@tournament)+"/join", method: "get") do %>
+ <%= submit_tag("Join") %>
+ <% end %>
+<% end %>
+
<p>
<strong>Game:</strong>
<%= @tournament.game %>
diff --git a/config/routes.rb b/config/routes.rb
index 0adafbe..4563a9a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -9,9 +9,13 @@ Leaguer::Application.routes.draw do
resources :matches
- resources :tournaments
+ resources :tournaments do
+ collection do
+ post 'join'
+ end
+ end
- resources :servers
+ resource :server, only: [:show, :edit, :update]
root to: 'static#homepage'
diff --git a/stop.sh b/stop.sh
new file mode 100644
index 0000000..719ed94
--- /dev/null
+++ b/stop.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+kill $(<tmp/pids/server.pid)