Add group stage service

This service is responsible for all actions concerning the group stage
It returns false if no groups are given to generate_group_stage method
This prevents dividing by zero in the next line
This commit is contained in:
Daniel Schädler 2019-04-24 12:29:46 +02:00
parent f7919ec0c6
commit a842e0db3c
1 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,37 @@
# frozen_string_literal: true
class GroupStageService
def self.generate_group_stage(groups)
return false if groups.length.zero?
average_group_size = (groups.flatten.length.to_f / groups.length.to_f)
if (average_group_size % 1).zero?
groups = groups.map { |group| get_group_object_from(group) }
group_stage = Stage.new level: -1, groups: groups
group_stage
else
false
end
end
def self.get_group_object_from(team_array)
matches = generate_all_matches_between team_array
group = Group.new matches: matches
group
end
def self.generate_all_matches_between(teams)
matches = []
matchups = teams.combination(2).to_a
matchups.each_with_index do |matchup, i|
match = Match.new state: :not_started,
position: i,
match_scores: [
MatchScore.create(team: matchup.first),
MatchScore.create(team: matchup.second)
]
matches << match
end
matches
end
end