Make match position randomizable
This commit is contained in:
parent
88cbba440d
commit
bc0c7fddc3
|
|
@ -6,7 +6,8 @@ class AddPlayoffsToTournament
|
||||||
def call
|
def call
|
||||||
tournament = context.tournament
|
tournament = context.tournament
|
||||||
context.fail! if tournament.stages.size > 1
|
context.fail! if tournament.stages.size > 1
|
||||||
if (playoff_stages = PlayoffStageService.generate_playoff_stages_from_tournament(tournament))
|
if (playoff_stages = PlayoffStageService.generate_playoff_stages_from_tournament(tournament,
|
||||||
|
context.randomize_matches))
|
||||||
if tournament.stages.empty?
|
if tournament.stages.empty?
|
||||||
tournament.stages = playoff_stages
|
tournament.stages = playoff_stages
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,12 @@ class PlayoffStageService
|
||||||
#
|
#
|
||||||
# @param teams [Array] The teams to generate the playoff stages with
|
# @param teams [Array] The teams to generate the playoff stages with
|
||||||
# @return [Array] the generated playoff stages
|
# @return [Array] the generated playoff stages
|
||||||
def generate_playoff_stages(teams)
|
def generate_playoff_stages(teams, randomize_matches)
|
||||||
playoffs = []
|
playoffs = []
|
||||||
stage_count = calculate_required_stage_count(teams.size)
|
stage_count = calculate_required_stage_count(teams.size)
|
||||||
# initial_matches are the matches in the first stage; this is the only stage filled with teams from the start on
|
# 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_matches = MatchService.generate_matches(teams)
|
||||||
|
initial_matches = initial_matches.shuffle.each_with_index { |m, i| m.position = i } if randomize_matches
|
||||||
initial_stage = Stage.new level: stage_count - 1, matches: initial_matches
|
initial_stage = Stage.new level: stage_count - 1, matches: initial_matches
|
||||||
initial_stage.state = :intermediate_stage unless initial_stage.matches.find(&:single_team?).nil?
|
initial_stage.state = :intermediate_stage unless initial_stage.matches.find(&:single_team?).nil?
|
||||||
playoffs << initial_stage
|
playoffs << initial_stage
|
||||||
|
|
@ -24,8 +25,8 @@ class PlayoffStageService
|
||||||
#
|
#
|
||||||
# @param tournament [Tournament] The tournament to generate the playoff stages from
|
# @param tournament [Tournament] The tournament to generate the playoff stages from
|
||||||
# @return [Array] the generated playoff stages
|
# @return [Array] the generated playoff stages
|
||||||
def generate_playoff_stages_from_tournament(tournament)
|
def generate_playoff_stages_from_tournament(tournament, randomize_matches)
|
||||||
generate_playoff_stages tournament.teams
|
generate_playoff_stages(tournament.teams, randomize_matches)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generates given number of empty stages
|
# Generates given number of empty stages
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ RSpec.describe PlayoffStageService do
|
||||||
amount_of_teams = parameters[:team_size]
|
amount_of_teams = parameters[:team_size]
|
||||||
expected_amount_of_playoff_stages = parameters[:expected_amount_of_playoff_stages]
|
expected_amount_of_playoff_stages = parameters[:expected_amount_of_playoff_stages]
|
||||||
teams = build_list(:team, amount_of_teams)
|
teams = build_list(:team, amount_of_teams)
|
||||||
stages = PlayoffStageService.generate_playoff_stages(teams)
|
stages = PlayoffStageService.generate_playoff_stages(teams, false)
|
||||||
expect(stages.size).to eq(expected_amount_of_playoff_stages)
|
expect(stages.size).to eq(expected_amount_of_playoff_stages)
|
||||||
stages.each_index do |i|
|
stages.each_index do |i|
|
||||||
stage = stages[i]
|
stage = stages[i]
|
||||||
|
|
@ -82,7 +82,7 @@ RSpec.describe PlayoffStageService do
|
||||||
|
|
||||||
describe 'number of teams isn\'t a power of two' do
|
describe 'number of teams isn\'t a power of two' do
|
||||||
let(:generated_stages) do
|
let(:generated_stages) do
|
||||||
PlayoffStageService.generate_playoff_stages(create_list(:team, 12))
|
PlayoffStageService.generate_playoff_stages(create_list(:team, 12), false)
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:intermediate_stage) do
|
let(:intermediate_stage) do
|
||||||
|
|
@ -102,7 +102,7 @@ RSpec.describe PlayoffStageService do
|
||||||
|
|
||||||
describe 'number of teams is a power of two' do
|
describe 'number of teams is a power of two' do
|
||||||
let(:generated_stages) do
|
let(:generated_stages) do
|
||||||
PlayoffStageService.generate_playoff_stages(create_list(:team, 16))
|
PlayoffStageService.generate_playoff_stages(create_list(:team, 16), false)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'generates only normal playoff_stage state stages' do
|
it 'generates only normal playoff_stage state stages' do
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue