From 88cbba440d05c293d75167c41add9d633785517d Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 14 Jun 2019 18:03:40 +0200 Subject: [PATCH] Implement methods to get teams sorted by their group scores --- app/services/group_stage_service.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app/services/group_stage_service.rb b/app/services/group_stage_service.rb index 5e92a1d..c8f7181 100644 --- a/app/services/group_stage_service.rb +++ b/app/services/group_stage_service.rb @@ -55,5 +55,32 @@ class GroupStageService end changed_group_scores end + + # Returns a list of the teams in the group sorted by their group_points, difference_in_points, scored_points + # + # @param group Group the group to get the teams from + # @return [Array] of teams + def teams_sorted_by_group_scores(group) + group.teams.sort_by do |t| + [group.group_scores.find_by(team: t).group_points, + group.group_scores.find_by(team: t).difference_in_points, + group.group_scores.find_by(team: t).scored_points] + end + end + + # Returns all teams advancing to playoff stage from given group stage + # They are ordered in such a way, that PlayoffStageService will correctly match the teams + # + # @param group_stage GroupStage the group stage to get all advancing teams from + # @return [Array] the teams advancing from that group stage + def get_advancing_teams(group_stage) + advancing_teams = [] + group_winners = group_stage.groups.map(&method(:teams_sorted_by_group_scores)) + (group_stage.tournament.instant_finalists_amount + group_stage.tournament.intermediate_round_participants_amount) + .times do |i| + advancing_teams << group_winners[i % group_stage.groups.size].shift + end + advancing_teams + end end end