Move self methods into self block
This commit is contained in:
parent
3ace826180
commit
e222669321
|
|
@ -1,32 +1,34 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GroupStageService
|
||||
def self.generate_group_stage(groups)
|
||||
raise 'Cannot generate group stage without groups' if groups.length.zero?
|
||||
class << self
|
||||
def generate_group_stage(groups)
|
||||
raise 'Cannot generate group stage without groups' if groups.length.zero?
|
||||
|
||||
# raise an error if the average group size is not a whole number
|
||||
raise 'Groups need to be equal size' unless (groups.flatten.length.to_f / groups.length.to_f % 1).zero?
|
||||
# raise an error if the average group size is not a whole number
|
||||
raise 'Groups need to be equal size' unless (groups.flatten.length.to_f / groups.length.to_f % 1).zero?
|
||||
|
||||
groups = groups.map(&method(:get_group_object_from))
|
||||
Stage.new level: -1, groups: groups
|
||||
end
|
||||
|
||||
def self.get_group_object_from(team_array)
|
||||
Group.new matches: generate_all_matches_between(team_array)
|
||||
end
|
||||
|
||||
def self.generate_all_matches_between(teams)
|
||||
matches = []
|
||||
teams.combination(2).to_a # = matchups
|
||||
.each_with_index do |matchup, i|
|
||||
match = Match.new state: :not_started,
|
||||
position: i,
|
||||
match_scores: [
|
||||
MatchScore.new(team: matchup.first),
|
||||
MatchScore.new(team: matchup.second)
|
||||
]
|
||||
matches << match
|
||||
groups = groups.map(&method(:get_group_object_from))
|
||||
Stage.new level: -1, groups: groups
|
||||
end
|
||||
|
||||
def get_group_object_from(team_array)
|
||||
Group.new matches: generate_all_matches_between(team_array)
|
||||
end
|
||||
|
||||
def generate_all_matches_between(teams)
|
||||
matches = []
|
||||
teams.combination(2).to_a # = matchups
|
||||
.each_with_index do |matchup, i|
|
||||
match = Match.new state: :not_started,
|
||||
position: i,
|
||||
match_scores: [
|
||||
MatchScore.new(team: matchup.first),
|
||||
MatchScore.new(team: matchup.second)
|
||||
]
|
||||
matches << match
|
||||
end
|
||||
matches
|
||||
end
|
||||
matches
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue