Assign :intermediate_stage to first stage if single_team matches present

This commit is contained in:
Daniel Schädler 2019-06-12 20:41:06 +02:00
parent 62f3ccba31
commit 98319e9625
3 changed files with 34 additions and 1 deletions

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Stage < ApplicationRecord
enum state: %i[playoff_stage in_progress finished]
enum state: %i[playoff_stage intermediate_stage in_progress finished]
belongs_to :tournament
has_many :matches, dependent: :destroy

View File

@ -12,6 +12,7 @@ class PlayoffStageService
# initial_matches are the matches in the first stage; this is the only stage filled with teams from the start on
initial_matches = MatchService.generate_matches(teams)
initial_stage = Stage.new level: stage_count - 1, matches: initial_matches
initial_stage.state = :intermediate_stage unless initial_stage.matches.find(&:single_team?).nil?
playoffs << initial_stage
# empty stages are the stages, the tournament is filled with to have the matches ready for later
empty_stages = generate_stages_with_empty_matches(stage_count - 1)

View File

@ -79,6 +79,38 @@ RSpec.describe PlayoffStageService do
end
end
end
describe 'number of teams isn\'t a power of two' do
let(:generated_stages) do
PlayoffStageService.generate_playoff_stages(create_list(:team, 12))
end
let(:intermediate_stage) do
generated_stages.max_by(&:level)
end
it 'generates an intermediate stage at the top level' do
expect(intermediate_stage.state).to eq('intermediate_stage')
end
it 'generates normal playoff_stage state stages elsewhere' do
(generated_stages - [intermediate_stage]).each do |stage|
expect(stage.state).to eq('playoff_stage')
end
end
end
describe 'number of teams is a power of two' do
let(:generated_stages) do
PlayoffStageService.generate_playoff_stages(create_list(:team, 16))
end
it 'generates only normal playoff_stage state stages' do
generated_stages.each do |stage|
expect(stage.state).to eq('playoff_stage')
end
end
end
end
describe '#populate_match_below' do