Defer decider checks until groups finish

This commit is contained in:
Daniel Schädler 2026-04-15 10:43:21 +02:00
parent d7f362e2bb
commit 7da30df291
2 changed files with 45 additions and 1 deletions

View File

@ -271,6 +271,7 @@ class GroupStageService
sorted_group_scores = group_scores_for(group).sort sorted_group_scores = group_scores_for(group).sort
advancing_slots = advancing_slots_for_group(group) advancing_slots = advancing_slots_for_group(group)
group_ready_for_decider_check = regular_group_matches_finished?(group)
cache = sorted_group_scores.each_with_index.each_with_object({}) do |(group_score, index), decisions| cache = sorted_group_scores.each_with_index.each_with_object({}) do |(group_score, index), decisions|
next if sorted_group_scores.size <= 1 next if sorted_group_scores.size <= 1
@ -290,7 +291,8 @@ class GroupStageService
hidden_points_result: comparison_reason[:hidden_points_result].to_s, hidden_points_result: comparison_reason[:hidden_points_result].to_s,
decider_match_id: comparison_reason[:decider_match_id], decider_match_id: comparison_reason[:decider_match_id],
tied: comparison_reason[:tied], tied: comparison_reason[:tied],
needs_decider_match: comparison_reason[:tied] && needs_decider_match: group_ready_for_decider_check &&
comparison_reason[:tied] &&
unresolved_tie_crosses_advancing_cutoff?(sorted_group_scores, group_score, advancing_slots) unresolved_tie_crosses_advancing_cutoff?(sorted_group_scores, group_score, advancing_slots)
} }
end end
@ -309,5 +311,16 @@ class GroupStageService
def group_score_cache_key(group_score) def group_score_cache_key(group_score)
group_score.id || group_score.object_id group_score.id || group_score.object_id
end end
def regular_group_matches_finished?(group)
matches = if group.association(:matches).loaded? && group.matches.any?
group.matches
else
group.matches.reload
end
regular_matches = matches.reject(&:decider?)
regular_matches.any? && regular_matches.all?(&:finished?)
end
end end
end end

View File

@ -300,6 +300,37 @@ RSpec.describe GroupStageService do
) )
end end
it 'does not request a decider while regular matches in the group are still unfinished' do
tournament.update!(playoff_teams_amount: 1, instant_finalists_amount: 1, intermediate_round_participants_amount: 0)
create(:group_match, group: group, state: :in_progress).tap do |match|
create(:match_score, match: match, team: team_a, points: 2)
create(:match_score, match: match, team: team_b, points: 2)
end
score_a = create(:group_score, group: group, team: team_a, group_points: 1, scored_points: 2, received_points: 2)
score_b = create(:group_score, group: group, team: team_b, group_points: 1, scored_points: 2, received_points: 2)
score_c = create(:group_score, group: group, team: team_c, group_points: 0, scored_points: 0, received_points: 0)
score_d = create(:group_score, group: group, team: team_d, group_points: 0, scored_points: 0, received_points: 0)
GroupStageService.send(:recalculate_position_of_group_scores!, [score_a, score_b, score_c, score_d]).each(&:save!)
expect(GroupStageService.ranking_decision_for(score_a)).to include(
resolved_by: 'tie',
direct_comparison_result: 'draw',
hidden_points_result: 'not_played',
tied: true,
needs_decider_match: false
)
expect(GroupStageService.ranking_decision_for(score_b)).to include(
resolved_by: 'tie',
direct_comparison_result: 'draw',
hidden_points_result: 'not_played',
tied: true,
needs_decider_match: false
)
end
it 'marks unresolved ties outside the advancing cutoff without requesting a decider' do it 'marks unresolved ties outside the advancing cutoff without requesting a decider' do
tournament.update!(playoff_teams_amount: 2, instant_finalists_amount: 2, intermediate_round_participants_amount: 0) tournament.update!(playoff_teams_amount: 2, instant_finalists_amount: 2, intermediate_round_participants_amount: 0)