From a8744c698765612867ca774593dabd73b507d796 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Mon, 27 May 2019 21:10:22 +0200 Subject: [PATCH] Implement Methods returning points of teams per match These methods return group_points scored_points and received_points respectively when given a team that is present in the match They return 0 when given a team that isn't contesting in that match --- app/models/match.rb | 34 +++++++++++++++++++-- spec/models/match_spec.rb | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/app/models/match.rb b/app/models/match.rb index 2779fa7..dee7f66 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -19,17 +19,47 @@ class Match < ApplicationRecord stage ? stage.owner : group.owner end - def winner - return nil unless finished? + def current_leading_team return nil if match_scores.first.points == match_scores.second.points match_scores.max_by(&:points).team end + def winner + return nil unless finished? + + current_leading_team + end + def group_match? group.present? end + def scored_points_of(team) + return 0 unless teams.include?(team) + + match_scores.find_by(team: team).points + end + + def received_points_of(team) + return 0 unless teams.include?(team) + + match_scores.find { |ms| ms.team != team }.points + end + + def group_points_of(team) + return 0 unless teams.include?(team) + + case current_leading_team + when team + 3 + when nil + 1 + else + 0 + end + end + private def stage_xor_group diff --git a/spec/models/match_spec.rb b/spec/models/match_spec.rb index 0d4656c..05322b2 100644 --- a/spec/models/match_spec.rb +++ b/spec/models/match_spec.rb @@ -90,4 +90,67 @@ RSpec.describe Match, type: :model do end end end + + context '#points_of' do + before do + @match = create(:running_group_match) + teams = @match.teams + @team1 = teams.first + @team2 = teams.second + @uninvolved_team = create(:team) + end + context 'even match' do + before do + @match.match_scores.each do |ms| + ms.points = 34 + ms.save! + end + end + + it 'returns correct group_points' do + expect(@match.group_points_of(@team1)).to be(1) + expect(@match.group_points_of(@team2)).to be(1) + expect(@match.group_points_of(@uninvolved_team)).to be(0) + end + + it 'returns correct scored_points' do + expect(@match.scored_points_of(@team1)).to be(34) + expect(@match.scored_points_of(@team2)).to be(34) + expect(@match.scored_points_of(@uninvolved_team)).to be(0) + end + + it 'returns correct received_points' do + expect(@match.received_points_of(@team1)).to be(34) + expect(@match.received_points_of(@team2)).to be(34) + expect(@match.received_points_of(@uninvolved_team)).to be(0) + end + end + + context 'uneven match' do + before do + @match.match_scores.each do |ms| + ms.points = ms.team == @team1 ? 42 : 17 + ms.save! + end + end + + it 'returns correct group_points' do + expect(@match.group_points_of(@team1)).to be(3) + expect(@match.group_points_of(@team2)).to be(0) + expect(@match.group_points_of(@uninvolved_team)).to be(0) + end + + it 'returns correct scored_points' do + expect(@match.scored_points_of(@team1)).to be(42) + expect(@match.scored_points_of(@team2)).to be(17) + expect(@match.scored_points_of(@uninvolved_team)).to be(0) + end + + it 'returns correct received_points' do + expect(@match.received_points_of(@team1)).to be(17) + expect(@match.received_points_of(@team2)).to be(42) + expect(@match.received_points_of(@uninvolved_team)).to be(0) + end + end + end end