summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornfoy <nfoy@purdue.edu>2014-03-06 20:40:18 -0500
committernfoy <nfoy@purdue.edu>2014-03-06 20:40:18 -0500
commita892016443d4845c1f738bcc57db82174c53701c (patch)
treef05cfe552d4b7c6793b172aa6b92906a0b2241fa
parente10bf58ec059ec263c1d1a9dcac608475377868a (diff)
parent7bcd854443e368806cf1f4ece562c157db723d1a (diff)
Merge branch 'master' of https://github.com/LukeShu/leaguer
Conflicts: config/routes.rb
-rw-r--r--app/controllers/tournaments_controller.rb44
-rw-r--r--app/models/tournament.rb22
-rw-r--r--app/models/user.rb2
-rw-r--r--app/models/user_tournament_pair.rb4
-rw-r--r--app/views/tournaments/new.html.erb3
-rw-r--r--app/views/tournaments/show.html.erb7
-rw-r--r--config/routes.rb4
-rw-r--r--db/migrate/20140307003703_create_teams.rb (renamed from db/migrate/20140306222325_create_teams.rb)0
-rw-r--r--db/migrate/20140307003709_create_alerts.rb (renamed from db/migrate/20140306222330_create_alerts.rb)0
-rw-r--r--db/migrate/20140307003714_create_pms.rb (renamed from db/migrate/20140306222336_create_pms.rb)0
-rw-r--r--db/migrate/20140307003719_create_tournaments.rb (renamed from db/migrate/20140306222314_create_tournaments.rb)2
-rw-r--r--db/migrate/20140307003926_create_servers.rb (renamed from db/migrate/20140306222309_create_servers.rb)0
-rw-r--r--db/migrate/20140307003931_create_matches.rb (renamed from db/migrate/20140306222320_create_matches.rb)0
-rw-r--r--db/migrate/20140307003956_create_games.rb (renamed from db/migrate/20140306222341_create_games.rb)0
-rw-r--r--db/migrate/20140307004001_create_users.rb (renamed from db/migrate/20140306222347_create_users.rb)0
-rw-r--r--db/migrate/20140307004006_create_sessions.rb (renamed from db/migrate/20140306222352_create_sessions.rb)0
-rw-r--r--db/migrate/20140307004011_create_server_settings.rb (renamed from db/migrate/20140306222357_create_server_settings.rb)0
-rw-r--r--db/migrate/20140307004016_create_tournament_options.rb (renamed from db/migrate/20140306222402_create_tournament_options.rb)0
-rw-r--r--db/migrate/20140307004021_create_game_options.rb (renamed from db/migrate/20140306222407_create_game_options.rb)0
-rw-r--r--db/migrate/20140307004027_create_scores.rb (renamed from db/migrate/20140306222412_create_scores.rb)0
-rw-r--r--db/migrate/20140307004031_create_user_team_pairs.rb (renamed from db/migrate/20140306222422_create_user_team_pairs.rb)0
-rw-r--r--db/migrate/20140307004036_create_user_tournament_pairs.rb (renamed from db/migrate/20140306222417_create_tournament_user_pairs.rb)6
-rw-r--r--db/migrate/20140307004041_create_team_match_pairs.rb (renamed from db/migrate/20140306222427_create_team_match_pairs.rb)0
-rw-r--r--db/migrate/20140307004100_add_hidden_attrs_to_user.rb (renamed from db/migrate/20140306222447_add_hidden_attrs_to_user.rb)0
-rw-r--r--db/schema.rb24
-rwxr-xr-xgenerate.sh16
-rw-r--r--stop.sh3
-rw-r--r--test/fixtures/user_tournament_pairs.yml9
-rw-r--r--test/models/user_tournament_pair_test.rb7
29 files changed, 116 insertions, 37 deletions
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb
index 4f79d44..6fc3ad6 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -1,6 +1,6 @@
class TournamentsController < ApplicationController
- before_action :set_tournament, only: [:show, :edit, :update, :destroy]
- before_action :check_perms, only: [:new, :create, :edit, :update, :destroy]
+ before_action :set_tournament, only: [:show, :edit, :update, :destroy, :join]
+ before_action :check_perms, only: [:new, :create, :edit, :destroy]
# GET /tournaments
# GET /tournaments.json
@@ -42,13 +42,39 @@ 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 }
+ require 'pp'
+ pp params
+ if params[:update_action].nil?
+ check_perms
+ 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' }
+ format.json { render json: @tournament.errors, status: :unprocessable_entity }
+ end
+ end
+ else
+ case params[:update_action]
+ when "join"
+ respond_to do |format|
+ if @tournament.join(current_user)
+ format.html { render action: 'show', 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
+ #when "open"
+ # TODO
+ #when "close"
+ # TODO
else
- format.html { render action: 'edit' }
- format.json { render json: @tournament.errors, status: :unprocessable_entity }
+ respond_to do |format|
+ format.html { render action: 'show', notice: "Invalid action", status: :unprocessable_entity }
+ format.json { render json: @tournament.errors, status: :unprocessable_entity }
+ end
end
end
end
@@ -69,7 +95,7 @@ class TournamentsController < ApplicationController
@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..26dec72 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -1,4 +1,22 @@
class Tournament < ActiveRecord::Base
- belongs_to :game
- has_many :matches
+ belongs_to :game
+ has_many :matches
+ has_many :user_tournament_pairs
+ has_many :users, :through => :user_tournament_pairs
+
+ 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_by?(user)
+ return false
+ end
+ pair = UserTournamentPair.new(tournament: self, user: user)
+ return pair.save
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 9288ef6..bad7f7b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ has_many :user_tournament_pairs
+ has_many :tournaments, :through => :user_tournament_pairs
before_save { self.email = email.downcase }
before_save { self.user_name = user_name }
diff --git a/app/models/user_tournament_pair.rb b/app/models/user_tournament_pair.rb
new file mode 100644
index 0000000..b2676e5
--- /dev/null
+++ b/app/models/user_tournament_pair.rb
@@ -0,0 +1,4 @@
+class UserTournamentPair < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :tournament
+end
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..91c1961 100644
--- a/app/views/tournaments/show.html.erb
+++ b/app/views/tournaments/show.html.erb
@@ -1,3 +1,10 @@
+<% if @tournament.joinable_by?(current_user) %>
+ <%= form_tag(tournament_path(@tournament), method: "put") do %>
+ <input type="hidden" name="update_action" value="join">
+ <%= submit_tag("Join") %>
+ <% end %>
+<% end %>
+
<p>
<strong>Game:</strong>
<%= @tournament.game %>
diff --git a/config/routes.rb b/config/routes.rb
index 72445c8..a14c11a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -16,8 +16,8 @@ Leaguer::Application.routes.draw do
resources :tournaments do
resources :matches
end
-
- resources :servers
+
+ resource :server, only: [:show, :edit, :update]
root to: 'static#homepage'
diff --git a/db/migrate/20140306222325_create_teams.rb b/db/migrate/20140307003703_create_teams.rb
index fdf9a68..fdf9a68 100644
--- a/db/migrate/20140306222325_create_teams.rb
+++ b/db/migrate/20140307003703_create_teams.rb
diff --git a/db/migrate/20140306222330_create_alerts.rb b/db/migrate/20140307003709_create_alerts.rb
index 68a8e10..68a8e10 100644
--- a/db/migrate/20140306222330_create_alerts.rb
+++ b/db/migrate/20140307003709_create_alerts.rb
diff --git a/db/migrate/20140306222336_create_pms.rb b/db/migrate/20140307003714_create_pms.rb
index 93bb5c6..93bb5c6 100644
--- a/db/migrate/20140306222336_create_pms.rb
+++ b/db/migrate/20140307003714_create_pms.rb
diff --git a/db/migrate/20140306222314_create_tournaments.rb b/db/migrate/20140307003719_create_tournaments.rb
index c7ec765..545e603 100644
--- a/db/migrate/20140306222314_create_tournaments.rb
+++ b/db/migrate/20140307003719_create_tournaments.rb
@@ -2,13 +2,13 @@ class CreateTournaments < ActiveRecord::Migration
def change
create_table :tournaments do |t|
t.references :game, index: true
+ t.integer :status
t.integer :min_players_per_team
t.integer :max_players_per_team
t.integer :min_teams_per_match
t.integer :max_teams_per_match
t.integer :set_rounds
t.boolean :randomized_teams
- t.integer :status
t.timestamps
end
diff --git a/db/migrate/20140306222309_create_servers.rb b/db/migrate/20140307003926_create_servers.rb
index f33241a..f33241a 100644
--- a/db/migrate/20140306222309_create_servers.rb
+++ b/db/migrate/20140307003926_create_servers.rb
diff --git a/db/migrate/20140306222320_create_matches.rb b/db/migrate/20140307003931_create_matches.rb
index 325863d..325863d 100644
--- a/db/migrate/20140306222320_create_matches.rb
+++ b/db/migrate/20140307003931_create_matches.rb
diff --git a/db/migrate/20140306222341_create_games.rb b/db/migrate/20140307003956_create_games.rb
index 5e4f56f..5e4f56f 100644
--- a/db/migrate/20140306222341_create_games.rb
+++ b/db/migrate/20140307003956_create_games.rb
diff --git a/db/migrate/20140306222347_create_users.rb b/db/migrate/20140307004001_create_users.rb
index 8032870..8032870 100644
--- a/db/migrate/20140306222347_create_users.rb
+++ b/db/migrate/20140307004001_create_users.rb
diff --git a/db/migrate/20140306222352_create_sessions.rb b/db/migrate/20140307004006_create_sessions.rb
index fe25bf2..fe25bf2 100644
--- a/db/migrate/20140306222352_create_sessions.rb
+++ b/db/migrate/20140307004006_create_sessions.rb
diff --git a/db/migrate/20140306222357_create_server_settings.rb b/db/migrate/20140307004011_create_server_settings.rb
index dfdd91b..dfdd91b 100644
--- a/db/migrate/20140306222357_create_server_settings.rb
+++ b/db/migrate/20140307004011_create_server_settings.rb
diff --git a/db/migrate/20140306222402_create_tournament_options.rb b/db/migrate/20140307004016_create_tournament_options.rb
index 53b601c..53b601c 100644
--- a/db/migrate/20140306222402_create_tournament_options.rb
+++ b/db/migrate/20140307004016_create_tournament_options.rb
diff --git a/db/migrate/20140306222407_create_game_options.rb b/db/migrate/20140307004021_create_game_options.rb
index 2f62ef7..2f62ef7 100644
--- a/db/migrate/20140306222407_create_game_options.rb
+++ b/db/migrate/20140307004021_create_game_options.rb
diff --git a/db/migrate/20140306222412_create_scores.rb b/db/migrate/20140307004027_create_scores.rb
index 4ca0b0b..4ca0b0b 100644
--- a/db/migrate/20140306222412_create_scores.rb
+++ b/db/migrate/20140307004027_create_scores.rb
diff --git a/db/migrate/20140306222422_create_user_team_pairs.rb b/db/migrate/20140307004031_create_user_team_pairs.rb
index 2c492ac..2c492ac 100644
--- a/db/migrate/20140306222422_create_user_team_pairs.rb
+++ b/db/migrate/20140307004031_create_user_team_pairs.rb
diff --git a/db/migrate/20140306222417_create_tournament_user_pairs.rb b/db/migrate/20140307004036_create_user_tournament_pairs.rb
index ab72aad..9885b20 100644
--- a/db/migrate/20140306222417_create_tournament_user_pairs.rb
+++ b/db/migrate/20140307004036_create_user_tournament_pairs.rb
@@ -1,8 +1,8 @@
-class CreateTournamentUserPairs < ActiveRecord::Migration
+class CreateUserTournamentPairs < ActiveRecord::Migration
def change
- create_table :tournament_user_pairs do |t|
- t.references :tournament, index: true
+ create_table :user_tournament_pairs do |t|
t.references :user, index: true
+ t.references :tournament, index: true
t.timestamps
end
diff --git a/db/migrate/20140306222427_create_team_match_pairs.rb b/db/migrate/20140307004041_create_team_match_pairs.rb
index 8fac07e..8fac07e 100644
--- a/db/migrate/20140306222427_create_team_match_pairs.rb
+++ b/db/migrate/20140307004041_create_team_match_pairs.rb
diff --git a/db/migrate/20140306222447_add_hidden_attrs_to_user.rb b/db/migrate/20140307004100_add_hidden_attrs_to_user.rb
index 2f1b0b2..2f1b0b2 100644
--- a/db/migrate/20140306222447_add_hidden_attrs_to_user.rb
+++ b/db/migrate/20140307004100_add_hidden_attrs_to_user.rb
diff --git a/db/schema.rb b/db/schema.rb
index eadf24e..cae1815 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20140306222447) do
+ActiveRecord::Schema.define(version: 20140307004100) do
create_table "alerts", force: true do |t|
t.integer "author_id"
@@ -120,25 +120,15 @@ ActiveRecord::Schema.define(version: 20140306222447) do
add_index "tournament_options", ["tournament_id"], name: "index_tournament_options_on_tournament_id"
- create_table "tournament_user_pairs", force: true do |t|
- t.integer "tournament_id"
- t.integer "user_id"
- t.datetime "created_at"
- t.datetime "updated_at"
- end
-
- add_index "tournament_user_pairs", ["tournament_id"], name: "index_tournament_user_pairs_on_tournament_id"
- add_index "tournament_user_pairs", ["user_id"], name: "index_tournament_user_pairs_on_user_id"
-
create_table "tournaments", force: true do |t|
t.integer "game_id"
+ t.integer "status"
t.integer "min_players_per_team"
t.integer "max_players_per_team"
t.integer "min_teams_per_match"
t.integer "max_teams_per_match"
t.integer "set_rounds"
t.boolean "randomized_teams"
- t.integer "status"
t.datetime "created_at"
t.datetime "updated_at"
end
@@ -155,6 +145,16 @@ ActiveRecord::Schema.define(version: 20140306222447) do
add_index "user_team_pairs", ["team_id"], name: "index_user_team_pairs_on_team_id"
add_index "user_team_pairs", ["user_id"], name: "index_user_team_pairs_on_user_id"
+ create_table "user_tournament_pairs", force: true do |t|
+ t.integer "user_id"
+ t.integer "tournament_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "user_tournament_pairs", ["tournament_id"], name: "index_user_tournament_pairs_on_tournament_id"
+ add_index "user_tournament_pairs", ["user_id"], name: "index_user_tournament_pairs_on_user_id"
+
create_table "users", force: true do |t|
t.string "name"
t.string "email"
diff --git a/generate.sh b/generate.sh
index c6e63dd..db9e84b 100755
--- a/generate.sh
+++ b/generate.sh
@@ -18,24 +18,30 @@ git checkout clean-start -- app test config/routes.rb
# The whole shebang, models, views, and controllers
bundle exec rails generate scaffold server --force $NOTEST
-bundle exec rails generate scaffold tournament game:references min_players_per_team:integer max_players_per_team:integer min_teams_per_match:integer max_teams_per_match:integer set_rounds:integer randomized_teams:boolean status:integer $NOTEST
bundle exec rails generate scaffold match tournament:references name:string --force $NOTEST
bundle exec rails generate scaffold team match:references $NOTEST
bundle exec rails generate scaffold alert author:references message:text $NOTEST
bundle exec rails generate scaffold pm author:references recipient:references message:text $NOTEST
-bundle exec rails generate scaffold game name:text min_players_per_team:integer max_players_per_team:integer min_teams_per_match:integer max_teams_per_match:integer set_rounds:integer randomized_teams:boolean --force $NOTEST
+bundle exec rails generate scaffold tournament game:references status:integer \
+ min_players_per_team:integer max_players_per_team:integer \
+ min_teams_per_match:integer max_teams_per_match:integer \
+ set_rounds:integer randomized_teams:boolean
+bundle exec rails generate scaffold game \
+ name:text \
+ min_players_per_team:integer max_players_per_team:integer \
+ min_teams_per_match:integer max_teams_per_match:integer \
+ set_rounds:integer randomized_teams:boolean
bundle exec rails generate scaffold user name:string email:string:uniq user_name:string:uniq
bundle exec rails generate scaffold session user:references
-
# Just models
bundle exec rails generate model server_settings $NOTEST
bundle exec rails generate model tournament_option tournament:references vartype:integer name:string value:text $NOTEST
bundle exec rails generate model game_option vartype:integer name:string default:text $NOTEST
bundle exec rails generate model score user:references match:references value:integer $NOTEST
-# Bridge Models
-bundle exec rails generate model tournament_user_pair tournament:references user:references $NOTEST
+# Bridge Models
bundle exec rails generate model user_team_pair user:references team:references $NOTEST
+bundle exec rails generate model user_tournament_pair user:references tournament:references
bundle exec rails generate model team_match_pair team:references match:references $NOTEST
# Just controllers
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)
diff --git a/test/fixtures/user_tournament_pairs.yml b/test/fixtures/user_tournament_pairs.yml
new file mode 100644
index 0000000..974ee71
--- /dev/null
+++ b/test/fixtures/user_tournament_pairs.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ user_id:
+ tournament_id:
+
+two:
+ user_id:
+ tournament_id:
diff --git a/test/models/user_tournament_pair_test.rb b/test/models/user_tournament_pair_test.rb
new file mode 100644
index 0000000..6167ff6
--- /dev/null
+++ b/test/models/user_tournament_pair_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class UserTournamentPairTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end