Implement update_group_score method

This method calculates all the group scores of the group given to it
This commit is contained in:
Daniel Schädler 2019-05-27 23:24:20 +02:00
parent 752beefca6
commit d556e3e833
1 changed files with 24 additions and 0 deletions

View File

@ -30,5 +30,29 @@ class GroupStageService
end end
matches matches
end end
# Updates all group_scores of the given group
#
# @param group Group the group to update the group_scores in
# @return [Array] the changed group_scores that need to be saved
def update_group_scores(group)
changed_group_scores = []
group.teams.each do |team|
group_score = group.group_scores.find_by(team: team)
matches = group.matches.select { |match| match.teams.include? team }
# reset previous values
group_score.group_points = 0
group_score.scored_points = 0
group_score.received_points = 0
matches.each do |match|
# calculate points for every match
group_score.group_points += match.group_points_of team
group_score.scored_points += match.scored_points_of team
group_score.received_points += match.received_points_of team
end
changed_group_scores << group_score
end
changed_group_scores
end
end end
end end