Assign default values on Group Stage creation

default values are assigned to instant_finalists_amount and
intermediate_round_participants_amount depending on playoff_teams_amount and group amount
This commit is contained in:
Daniel Schädler 2019-06-10 19:17:40 +02:00
parent 4d5d7bc812
commit f04c11427a
3 changed files with 35 additions and 2 deletions

View File

@ -10,6 +10,9 @@ class AddGroupStageToTournament
begin
group_stage = GroupStageService.generate_group_stage(groups)
tournament.stages = [group_stage]
tournament.instant_finalists_amount, tournament.intermediate_round_participants_amount =
TournamentService.calculate_default_amount_of_teams_advancing(tournament.playoff_teams_amount,
group_stage.groups.size)
context.object_to_save = tournament
rescue StandardError
context.fail!

View File

@ -0,0 +1,14 @@
# frozen_string_literal: true
class TournamentService
class << self
def calculate_default_amount_of_teams_advancing(playoff_teams_amount, amount_of_groups)
# the amount of whole places that advance in a group (e. g. all 1rst places of every group instantly go through)
instant_finalists_amount = (playoff_teams_amount.floor / amount_of_groups.floor) * amount_of_groups.floor
# the amount of teams that still need to play an intermediate round before advancing to playoffs
intermediate_round_participants_amount = (playoff_teams_amount - instant_finalists_amount) * 2
[instant_finalists_amount, intermediate_round_participants_amount]
end
end
end

View File

@ -14,6 +14,7 @@ RSpec.describe AddGroupStageToTournament, type: :interactor do
@group_stage_tournament = create(:group_stage_tournament, stage_count: 0, group_count: 0)
@group_stage = create(:group_stage)
@groups = Hash[1 => create_list(:team, 4), 2 => create_list(:team, 4)].values
@tournament_service_defaults = [78_345, 2_387]
end
context 'GroupStageService mocked' do
@ -24,13 +25,28 @@ RSpec.describe AddGroupStageToTournament, type: :interactor do
end
context 'empty tournament' do
before do
allow(class_double('TournamentService').as_stubbed_const(transfer_nested_constants: true))
.to receive(:calculate_default_amount_of_teams_advancing)
.with(@empty_tournament.playoff_teams_amount, @group_stage.groups.size)
.and_return(@tournament_service_defaults)
end
it 'succeeds' do
expect(empty_tournament_context).to be_a_success
end
it 'adds group stage to the tournament' do
test = empty_tournament_context.tournament.stages.first
expect(test).to eq(@group_stage)
expect(empty_tournament_context.tournament.stages.first).to eq(@group_stage)
end
it 'sets default for instant_finalists_amount' do
expect(empty_tournament_context.tournament.instant_finalists_amount).to eq(@tournament_service_defaults.first)
end
it 'sets default for intermediate_round_participants_amount' do
expect(empty_tournament_context.tournament.intermediate_round_participants_amount)
.to eq(@tournament_service_defaults.second)
end
end
end