diff --git a/app/models/group_score.rb b/app/models/group_score.rb index 0882d87..8fc6dad 100644 --- a/app/models/group_score.rb +++ b/app/models/group_score.rb @@ -9,8 +9,7 @@ class GroupScore < ApplicationRecord end def <=>(other) - point_comparison = [-group_points, -difference_in_points, -scored_points] <=> [-other.group_points, -other.difference_in_points, -other.scored_points] - p point_comparison, team.name, other.team.name + point_comparison = [-position, -group_points, -difference_in_points, -scored_points] <=> [-other.position, -other.group_points, -other.difference_in_points, -other.scored_points] if point_comparison.zero? comparison_match = group.matches.find do |match| match.match_scores.any? { |match_score| match_score.team == team } diff --git a/app/serializers/group_score_serializer.rb b/app/serializers/group_score_serializer.rb index aa4d5a6..42f5493 100644 --- a/app/serializers/group_score_serializer.rb +++ b/app/serializers/group_score_serializer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class GroupScoreSerializer < ApplicationSerializer - attributes :group_points, :received_points, :scored_points + attributes :group_points, :received_points, :scored_points, :position, :difference_in_points belongs_to :team end diff --git a/app/serializers/group_serializer.rb b/app/serializers/group_serializer.rb index b533d39..1700307 100644 --- a/app/serializers/group_serializer.rb +++ b/app/serializers/group_serializer.rb @@ -2,11 +2,7 @@ class GroupSerializer < ApplicationSerializer attributes :number - attributes :group_scores has_many :matches - - def group_scores - object.group_scores.sort.map { |group_score| GroupScoreSerializer.new(group_score) } - end + has_many :group_scores end diff --git a/app/services/group_stage_service.rb b/app/services/group_stage_service.rb index 36e7732..8cd0efb 100644 --- a/app/services/group_stage_service.rb +++ b/app/services/group_stage_service.rb @@ -85,7 +85,7 @@ class GroupStageService end changed_group_scores << group_score end - changed_group_scores + recalculate_position_of_group_scores!(changed_group_scores) end # Returns a list of the teams in the group sorted by their group_points, difference_in_points, scored_points @@ -110,5 +110,42 @@ class GroupStageService end advancing_teams end + + # private + + def recalculate_position_of_group_scores!(group_scores) + group_scores.each do |group_score| + group_score.position = 0 + end + group_scores = group_scores.sort + + ranks = [] + rank = 1 + previous = nil + group_scores.each_with_index do |group_score, i| + comparison = if i.zero? + 1 + else + group_score <=> previous + end + case comparison + when 1 + ranks.append i + rank = i + when 0 + ranks.append rank + else + raise # should not happen, list is sorted + end + previous = group_score + end + + # assigning position is split into a second loop to not mess up the ranks by adding the position + # (which is used for sorting) too early + group_scores.each_with_index do |group_score, i| + group_score.position = ranks[i] + 1 + end + group_scores + end end end