From 63a5a9832e515f813471e158bfc7a1334f09ae1c Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Mon, 27 May 2019 23:24:20 +0200 Subject: [PATCH] Implement update_group_score method This method calculates all the group scores of the group given to it --- app/services/group_stage_service.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/services/group_stage_service.rb b/app/services/group_stage_service.rb index bdd18d9..ea2336a 100644 --- a/app/services/group_stage_service.rb +++ b/app/services/group_stage_service.rb @@ -30,5 +30,29 @@ class GroupStageService end matches 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