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
This commit is contained in:
Daniel Schädler 2019-05-27 21:10:22 +02:00
parent e222669321
commit a8744c6987
2 changed files with 95 additions and 2 deletions

View File

@ -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

View File

@ -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