Add and calculate position to sort group_scores by
This commit is contained in:
parent
1084d836c1
commit
4a8cb71fc4
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue