Make match position randomizable

This commit is contained in:
Daniel Schädler 2019-06-14 18:04:31 +02:00
parent 88cbba440d
commit bc0c7fddc3
3 changed files with 9 additions and 7 deletions

View File

@ -6,7 +6,8 @@ class AddPlayoffsToTournament
def call
tournament = context.tournament
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?
tournament.stages = playoff_stages
else

View File

@ -6,11 +6,12 @@ class PlayoffStageService
#
# @param teams [Array] The teams to generate the playoff stages with
# @return [Array] the generated playoff stages
def generate_playoff_stages(teams)
def generate_playoff_stages(teams, randomize_matches)
playoffs = []
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 = 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.state = :intermediate_stage unless initial_stage.matches.find(&:single_team?).nil?
playoffs << initial_stage
@ -24,8 +25,8 @@ class PlayoffStageService
#
# @param tournament [Tournament] The tournament to generate the playoff stages from
# @return [Array] the generated playoff stages
def generate_playoff_stages_from_tournament(tournament)
generate_playoff_stages tournament.teams
def generate_playoff_stages_from_tournament(tournament, randomize_matches)
generate_playoff_stages(tournament.teams, randomize_matches)
end
# Generates given number of empty stages

View File

@ -70,7 +70,7 @@ RSpec.describe PlayoffStageService do
amount_of_teams = parameters[:team_size]
expected_amount_of_playoff_stages = parameters[:expected_amount_of_playoff_stages]
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)
stages.each_index do |i|
stage = stages[i]
@ -82,7 +82,7 @@ RSpec.describe PlayoffStageService do
describe 'number of teams isn\'t a power of two' do
let(:generated_stages) do
PlayoffStageService.generate_playoff_stages(create_list(:team, 12))
PlayoffStageService.generate_playoff_stages(create_list(:team, 12), false)
end
let(:intermediate_stage) do
@ -102,7 +102,7 @@ RSpec.describe PlayoffStageService do
describe 'number of teams is a power of two' do
let(:generated_stages) do
PlayoffStageService.generate_playoff_stages(create_list(:team, 16))
PlayoffStageService.generate_playoff_stages(create_list(:team, 16), false)
end
it 'generates only normal playoff_stage state stages' do