Skip to content

Commit

Permalink
Change swiss settings for tournament fixes (#382)
Browse files Browse the repository at this point in the history
* When changing swiss format, update first stage if unpaired or error.
  • Loading branch information
plural authored Jan 19, 2025
1 parent ef7d89b commit 45d8a5e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
43 changes: 34 additions & 9 deletions app/controllers/tournaments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,43 @@ def update
authorize @tournament

params = tournament_params
if params[:swiss_deck_visibility]
unless params[:cut_deck_visibility]
params[:cut_deck_visibility] = Tournament.max_visibility_cut_or_swiss(
@tournament.cut_deck_visibility, params[:swiss_deck_visibility]

error_found = false

if params[:swiss_format] != @tournament.swiss_format
first_stage = @tournament.stages.first
if !first_stage.nil? &&
((params[:swiss_format] == 'single_sided' && first_stage.swiss?) ||
(params[:swiss_format] == 'double_sided' && first_stage.single_sided_swiss?))
if !@tournament.rounds.empty?
flash[:alert] = "Can't change Swiss format when rounds exist."
error_found = true
else
case params[:swiss_format] # rubocop:disable Metrics/BlockNesting
when 'single_sided'
first_stage.single_sided_swiss!
when 'double_sided'
first_stage.swiss!
end
first_stage.save
end
end
end

unless error_found
if params[:swiss_deck_visibility]
unless params[:cut_deck_visibility]
params[:cut_deck_visibility] = Tournament.max_visibility_cut_or_swiss(
@tournament.cut_deck_visibility, params[:swiss_deck_visibility]
)
end
elsif params[:cut_deck_visibility]
params[:swiss_deck_visibility] = Tournament.min_visibility_swiss_or_cut(
@tournament.swiss_deck_visibility, params[:cut_deck_visibility]
)
end
elsif params[:cut_deck_visibility]
params[:swiss_deck_visibility] = Tournament.min_visibility_swiss_or_cut(
@tournament.swiss_deck_visibility, params[:cut_deck_visibility]
)
@tournament.update(params)
end
@tournament.update(params)

redirect_back_or_to edit_tournament_path(@tournament)
end
Expand Down
33 changes: 33 additions & 0 deletions spec/requests/tournaments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

RSpec.describe TournamentsController do
let(:tournament) { create(:tournament, name: 'My Tournament') }
let(:player1) { create(:player, tournament:) }
let(:player2) { create(:player, tournament:) }
let(:player3) { create(:player, tournament:) }
let(:player4) { create(:player, tournament:) }

describe '#save_json' do
before do
Expand Down Expand Up @@ -37,4 +41,33 @@
expect(tournament).to have_received(:cut_to!).with(:double_elim, 8)
end
end

describe '#change_swiss_format' do
before do
allow(Tournament).to receive(:find)
.with(tournament.to_param)
.and_return(tournament)
end

it 'changes swiss format when stage has no pairings' do
sign_in tournament.user

patch tournament_path(tournament), params: { tournament: { swiss_format: 'single_sided' } }

expect(tournament.single_sided?).to be(true)
end

it 'will not change swiss format when stage has pairings' do
expect(tournament.swiss?).to be(true)
tournament.pair_new_round!
sign_in tournament.user

patch tournament_path(tournament), params: { tournament: { swiss_format: 'single_sided' } }

expect(flash[:alert]).to eq("Can't change Swiss format when rounds exist.")

# Format is unchanged
expect(tournament.swiss?).to be(true)
end
end
end

0 comments on commit 45d8a5e

Please sign in to comment.