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
|
end
|
||||||
|
|
||||||
def <=>(other)
|
def <=>(other)
|
||||||
point_comparison = [-group_points, -difference_in_points, -scored_points] <=> [-other.group_points, -other.difference_in_points, -other.scored_points]
|
point_comparison = [-position, -group_points, -difference_in_points, -scored_points] <=> [-other.position, -other.group_points, -other.difference_in_points, -other.scored_points]
|
||||||
p point_comparison, team.name, other.team.name
|
|
||||||
if point_comparison.zero?
|
if point_comparison.zero?
|
||||||
comparison_match = group.matches.find do |match|
|
comparison_match = group.matches.find do |match|
|
||||||
match.match_scores.any? { |match_score| match_score.team == team }
|
match.match_scores.any? { |match_score| match_score.team == team }
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class GroupScoreSerializer < ApplicationSerializer
|
class GroupScoreSerializer < ApplicationSerializer
|
||||||
attributes :group_points, :received_points, :scored_points
|
attributes :group_points, :received_points, :scored_points, :position, :difference_in_points
|
||||||
|
|
||||||
belongs_to :team
|
belongs_to :team
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,7 @@
|
||||||
|
|
||||||
class GroupSerializer < ApplicationSerializer
|
class GroupSerializer < ApplicationSerializer
|
||||||
attributes :number
|
attributes :number
|
||||||
attributes :group_scores
|
|
||||||
|
|
||||||
has_many :matches
|
has_many :matches
|
||||||
|
has_many :group_scores
|
||||||
def group_scores
|
|
||||||
object.group_scores.sort.map { |group_score| GroupScoreSerializer.new(group_score) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ class GroupStageService
|
||||||
end
|
end
|
||||||
changed_group_scores << group_score
|
changed_group_scores << group_score
|
||||||
end
|
end
|
||||||
changed_group_scores
|
recalculate_position_of_group_scores!(changed_group_scores)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a list of the teams in the group sorted by their group_points, difference_in_points, scored_points
|
# 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
|
end
|
||||||
advancing_teams
|
advancing_teams
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue