From c9f2abad7b9c967e23ed2b2867d426eb27f61387 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 6 Mar 2014 21:29:24 -0500 Subject: make the join tables not have models --- app/controllers/tournaments_controller.rb | 2 +- app/models/team_match_pair.rb | 4 ---- app/models/tournament_user_pair.rb | 4 ---- app/models/user_team_pair.rb | 4 ---- app/models/user_tournament_pair.rb | 4 ---- app/views/tournaments/_form.html.erb | 8 ++++---- app/views/tournaments/index.html.erb | 4 ++-- app/views/tournaments/index.json.jbuilder | 2 +- app/views/tournaments/show.html.erb | 10 +++++----- app/views/tournaments/show.json.jbuilder | 2 +- 10 files changed, 14 insertions(+), 30 deletions(-) delete mode 100644 app/models/team_match_pair.rb delete mode 100644 app/models/tournament_user_pair.rb delete mode 100644 app/models/user_team_pair.rb delete mode 100644 app/models/user_tournament_pair.rb (limited to 'app') diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 346e16b..571203c 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -69,6 +69,6 @@ class TournamentsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def tournament_params - params.require(:tournament).permit(:game_id, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams, :status) + params.require(:tournament).permit(:game_id, :status, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams) end end diff --git a/app/models/team_match_pair.rb b/app/models/team_match_pair.rb deleted file mode 100644 index 85f8eaa..0000000 --- a/app/models/team_match_pair.rb +++ /dev/null @@ -1,4 +0,0 @@ -class TeamMatchPair < ActiveRecord::Base - belongs_to :team - belongs_to :match -end diff --git a/app/models/tournament_user_pair.rb b/app/models/tournament_user_pair.rb deleted file mode 100644 index eb4e9c5..0000000 --- a/app/models/tournament_user_pair.rb +++ /dev/null @@ -1,4 +0,0 @@ -class TournamentUserPair < ActiveRecord::Base - belongs_to :tournament - belongs_to :user -end diff --git a/app/models/user_team_pair.rb b/app/models/user_team_pair.rb deleted file mode 100644 index c55dc2e..0000000 --- a/app/models/user_team_pair.rb +++ /dev/null @@ -1,4 +0,0 @@ -class UserTeamPair < ActiveRecord::Base - belongs_to :user - belongs_to :team -end diff --git a/app/models/user_tournament_pair.rb b/app/models/user_tournament_pair.rb deleted file mode 100644 index b2676e5..0000000 --- a/app/models/user_tournament_pair.rb +++ /dev/null @@ -1,4 +0,0 @@ -class UserTournamentPair < ActiveRecord::Base - belongs_to :user - belongs_to :tournament -end diff --git a/app/views/tournaments/_form.html.erb b/app/views/tournaments/_form.html.erb index c2c0ba7..d098cbb 100644 --- a/app/views/tournaments/_form.html.erb +++ b/app/views/tournaments/_form.html.erb @@ -15,6 +15,10 @@ <%= f.label :game_id %>
<%= f.text_field :game_id %> +
+ <%= f.label :status %>
+ <%= f.number_field :status %> +
<%= f.label :min_players_per_team %>
<%= f.number_field :min_players_per_team %> @@ -39,10 +43,6 @@ <%= f.label :randomized_teams %>
<%= f.check_box :randomized_teams %>
-
- <%= f.label :status %>
- <%= f.number_field :status %> -
<%= f.submit %>
diff --git a/app/views/tournaments/index.html.erb b/app/views/tournaments/index.html.erb index dec16d1..fb8b381 100644 --- a/app/views/tournaments/index.html.erb +++ b/app/views/tournaments/index.html.erb @@ -4,13 +4,13 @@ Game + Status Min players per team Max players per team Min teams per match Max teams per match Set rounds Randomized teams - Status @@ -21,13 +21,13 @@ <% @tournaments.each do |tournament| %> <%= tournament.game %> + <%= tournament.status %> <%= tournament.min_players_per_team %> <%= tournament.max_players_per_team %> <%= tournament.min_teams_per_match %> <%= tournament.max_teams_per_match %> <%= tournament.set_rounds %> <%= tournament.randomized_teams %> - <%= tournament.status %> <%= link_to 'Show', tournament %> <%= link_to 'Edit', edit_tournament_path(tournament) %> <%= link_to 'Destroy', tournament, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/tournaments/index.json.jbuilder b/app/views/tournaments/index.json.jbuilder index df8eee4..e95c6a7 100644 --- a/app/views/tournaments/index.json.jbuilder +++ b/app/views/tournaments/index.json.jbuilder @@ -1,4 +1,4 @@ json.array!(@tournaments) do |tournament| - json.extract! tournament, :id, :game_id, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams, :status + json.extract! tournament, :id, :game_id, :status, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams json.url tournament_url(tournament, format: :json) end diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index 355bc90..208c6f4 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -5,6 +5,11 @@ <%= @tournament.game %>

+

+ Status: + <%= @tournament.status %> +

+

Min players per team: <%= @tournament.min_players_per_team %> @@ -35,10 +40,5 @@ <%= @tournament.randomized_teams %>

-

- Status: - <%= @tournament.status %> -

- <%= link_to 'Edit', edit_tournament_path(@tournament) %> | <%= link_to 'Back', tournaments_path %> diff --git a/app/views/tournaments/show.json.jbuilder b/app/views/tournaments/show.json.jbuilder index 7bedd4b..de2fbe0 100644 --- a/app/views/tournaments/show.json.jbuilder +++ b/app/views/tournaments/show.json.jbuilder @@ -1 +1 @@ -json.extract! @tournament, :id, :game_id, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams, :status, :created_at, :updated_at +json.extract! @tournament, :id, :game_id, :status, :min_players_per_team, :max_players_per_team, :min_teams_per_match, :max_teams_per_match, :set_rounds, :randomized_teams, :created_at, :updated_at -- cgit v1.2.3-2-g168b From 64d4bd0d7bff0bf0e1c405bfa154f22775ca724e Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 6 Mar 2014 21:31:18 -0500 Subject: use had_and_belongs_to_many --- app/models/tournament.rb | 4 ++-- app/models/user.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 26dec72..0510864 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,8 +1,8 @@ class Tournament < ActiveRecord::Base belongs_to :game has_many :matches - has_many :user_tournament_pairs - has_many :users, :through => :user_tournament_pairs + has_and_belongs_to_many :players, class_name: "User", join_table: "tournaments_players" + has_and_belongs_to_many :hosts, class_name: "User", join_table: "tournaments_hosts" def open? return true diff --git a/app/models/user.rb b/app/models/user.rb index bad7f7b..0a226e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,6 @@ class User < ActiveRecord::Base - has_many :user_tournament_pairs - has_many :tournaments, :through => :user_tournament_pairs + has_and_belongs_to_many :tournaments_played, class_name: "Tournament", join_table: "tournaments_players" + has_and_belongs_to_many :tournaments_hosted, class_name: "Tournament", join_table: "tournaments_hosts" before_save { self.email = email.downcase } before_save { self.user_name = user_name } -- cgit v1.2.3-2-g168b From 2bfc17c39f9b8b5ea71d36d2d5f041295f24192f Mon Sep 17 00:00:00 2001 From: Tomer Kimia Date: Thu, 6 Mar 2014 22:06:32 -0500 Subject: tournaments setup started. NOT FINISHED --- app/controllers/tournaments_controller.rb | 14 ++++++++++---- app/models/tournament.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 3f6de26..750a4e7 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -46,8 +46,7 @@ class TournamentsController < ApplicationController # PATCH/PUT /tournaments/1 # PATCH/PUT /tournaments/1.json def update - require 'pp' - pp params + if params[:update_action].nil? check_perms respond_to do |format| @@ -70,8 +69,15 @@ class TournamentsController < ApplicationController format.html { render action: 'permission_denied', status: :forbidden } format.json { render json: "Permission denied", status: :forbidden } end - #when "open" - # TODO + when "open" + respond_to do |format| + if @tournament.setup + 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 "close" # TODO else diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 26dec72..c359b1c 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -19,4 +19,17 @@ class Tournament < ActiveRecord::Base pair = UserTournamentPair.new(tournament: self, user: user) return pair.save end + + def setup + num_teams = (self.users.count/self.players_per_team).floor + num_matches = num_teams - 1 + for i in 0..num_matches + self.matches.create(name: "Match #{i}") + end + #self.players.each_slice(num_teams) do |team_players| + # Team.new(users: team_players) + #end + end + + end -- cgit v1.2.3-2-g168b From 4a91128f6fe20d95b6692abaa5e6c4dc92418b46 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 6 Mar 2014 22:14:58 -0500 Subject: fix the has_and_belongs_to_many relations --- app/models/tournament.rb | 7 +++---- app/models/user.rb | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'app') diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 0510864..aa22afa 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,8 +1,8 @@ class Tournament < ActiveRecord::Base belongs_to :game has_many :matches - has_and_belongs_to_many :players, class_name: "User", join_table: "tournaments_players" - has_and_belongs_to_many :hosts, class_name: "User", join_table: "tournaments_hosts" + has_and_belongs_to_many :players, class_name: "User", association_foreign_key: "player_id", join_table: "players_tournaments" + has_and_belongs_to_many :hosts, class_name: "User", association_foreign_key: "host_id", join_table: "hosts_tournaments" def open? return true @@ -16,7 +16,6 @@ class Tournament < ActiveRecord::Base unless joinable_by?(user) return false end - pair = UserTournamentPair.new(tournament: self, user: user) - return pair.save + players< Date: Thu, 6 Mar 2014 22:26:37 -0500 Subject: add self as a host to a tourney upon creation --- app/controllers/tournaments_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 74a1f56..1e26584 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -31,9 +31,9 @@ class TournamentsController < ApplicationController # POST /tournaments.json def create @tournament = Tournament.new(tournament_params) - respond_to do |format| if @tournament.save + @tournament.hosts.push(current_user) format.html { redirect_to @tournament, notice: 'Tournament was successfully created.' } format.json { render action: 'show', status: :created, location: @tournament } else -- cgit v1.2.3-2-g168b