Move all methods in playoff_stage_service into self block
This commit is contained in:
parent
9f92ca7e5b
commit
8bdcd51e66
|
|
@ -1,11 +1,12 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class PlayoffStageService
|
class PlayoffStageService
|
||||||
|
class << self
|
||||||
# Generates the playoff stage given the tournament
|
# Generates the playoff stage given the tournament
|
||||||
#
|
#
|
||||||
# @param teams [Array] The teams to generate the playoff stages with
|
# @param teams [Array] The teams to generate the playoff stages with
|
||||||
# @return [Array] the generated playoff stages
|
# @return [Array] the generated playoff stages
|
||||||
def self.generate_playoff_stages(teams)
|
def generate_playoff_stages(teams)
|
||||||
playoffs = []
|
playoffs = []
|
||||||
stage_count = calculate_required_stage_count(teams.size)
|
stage_count = calculate_required_stage_count(teams.size)
|
||||||
# initial_matches are the matches in the first stage; this is the only stage filled with teams from the start on
|
# initial_matches are the matches in the first stage; this is the only stage filled with teams from the start on
|
||||||
|
|
@ -24,7 +25,7 @@ class PlayoffStageService
|
||||||
#
|
#
|
||||||
# @param tournament [Tournament] The tournament to generate the playoff stages from
|
# @param tournament [Tournament] The tournament to generate the playoff stages from
|
||||||
# @return [Array] the generated playoff stages
|
# @return [Array] the generated playoff stages
|
||||||
def self.generate_playoff_stages_from_tournament(tournament)
|
def generate_playoff_stages_from_tournament(tournament)
|
||||||
generate_playoff_stages tournament.teams
|
generate_playoff_stages tournament.teams
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -32,7 +33,7 @@ class PlayoffStageService
|
||||||
#
|
#
|
||||||
# @param stage_count [Integer] number of stages to generate
|
# @param stage_count [Integer] number of stages to generate
|
||||||
# @return [Array] the generated stages
|
# @return [Array] the generated stages
|
||||||
def self.generate_stages_with_empty_matches(stage_count)
|
def generate_stages_with_empty_matches(stage_count)
|
||||||
empty_stages = []
|
empty_stages = []
|
||||||
stage_count.times do |i|
|
stage_count.times do |i|
|
||||||
stage = Stage.new level: i, matches: generate_empty_matches(2**i)
|
stage = Stage.new level: i, matches: generate_empty_matches(2**i)
|
||||||
|
|
@ -47,7 +48,7 @@ class PlayoffStageService
|
||||||
#
|
#
|
||||||
# @param amount [Integer] the amount of matches to generate
|
# @param amount [Integer] the amount of matches to generate
|
||||||
# @return [Array] the generated matches
|
# @return [Array] the generated matches
|
||||||
def self.generate_empty_matches(amount)
|
def generate_empty_matches(amount)
|
||||||
matches = []
|
matches = []
|
||||||
amount.times do |i|
|
amount.times do |i|
|
||||||
match = Match.new state: :not_ready, position: i
|
match = Match.new state: :not_ready, position: i
|
||||||
|
|
@ -60,7 +61,7 @@ class PlayoffStageService
|
||||||
#
|
#
|
||||||
# @param number_of_teams [Integer] the teams number of teams to calculate amount of stages
|
# @param number_of_teams [Integer] the teams number of teams to calculate amount of stages
|
||||||
# @return [Integer] amount of required stages
|
# @return [Integer] amount of required stages
|
||||||
def self.calculate_required_stage_count(number_of_teams)
|
def calculate_required_stage_count(number_of_teams)
|
||||||
if number_of_teams == 1
|
if number_of_teams == 1
|
||||||
1
|
1
|
||||||
else
|
else
|
||||||
|
|
@ -75,7 +76,7 @@ class PlayoffStageService
|
||||||
#
|
#
|
||||||
# @param current_match [Match] The Match which finished, the match below it gets populated
|
# @param current_match [Match] The Match which finished, the match below it gets populated
|
||||||
# @return [Array] the objects that changed and need to be saved
|
# @return [Array] the objects that changed and need to be saved
|
||||||
def self.populate_match_below(current_match)
|
def populate_match_below(current_match)
|
||||||
current_stage = current_match.stage
|
current_stage = current_match.stage
|
||||||
next_stage = current_stage.tournament.stages.find { |s| s.level == current_stage.level - 1 }
|
next_stage = current_stage.tournament.stages.find { |s| s.level == current_stage.level - 1 }
|
||||||
# return if next stage does not exist (there are no matches after the finale)
|
# return if next stage does not exist (there are no matches after the finale)
|
||||||
|
|
@ -83,8 +84,8 @@ class PlayoffStageService
|
||||||
|
|
||||||
current_position = current_match.position
|
current_position = current_match.position
|
||||||
|
|
||||||
# a "companion" match is the one that with the selected match makes up the two matches of which the winners advance
|
# a "companion" match is the one that with the selected match makes up the two matches
|
||||||
# into the match below
|
# of which the winners advance into the match below
|
||||||
# depending on the position of the match, the companion match is either on the left or right of it
|
# depending on the position of the match, the companion match is either on the left or right of it
|
||||||
companion_match = find_companion_match(current_position, current_stage)
|
companion_match = find_companion_match(current_position, current_stage)
|
||||||
|
|
||||||
|
|
@ -103,7 +104,6 @@ class PlayoffStageService
|
||||||
[match_below, match_scores].flatten
|
[match_below, match_scores].flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_companion_match(current_position, current_stage)
|
def find_companion_match(current_position, current_stage)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue