1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-25 01:14:43 +00:00

Dynamic double elim match generation and loser name generation

This commit is contained in:
2025-09-02 22:10:55 -04:00
parent 782baedcfe
commit 9a4e6f6597
3 changed files with 274 additions and 297 deletions

View File

@@ -207,3 +207,6 @@ The application has been migrated from using vanilla JavaScript to Hotwired Stim
- `app/assets/javascripts/application.js` - Registers and loads all controllers - `app/assets/javascripts/application.js` - Registers and loads all controllers
The importmap configuration in `config/importmap.rb` handles the loading of all JavaScript dependencies including Stimulus controllers. The importmap configuration in `config/importmap.rb` handles the loading of all JavaScript dependencies including Stimulus controllers.
# Using Repomix with LLMs
`npx repomix app test`

View File

@@ -3,138 +3,203 @@ class DoubleEliminationGenerateLoserNames
@tournament = tournament @tournament = tournament
end end
# Entry point: assign loser placeholders and advance any byes
def assign_loser_names def assign_loser_names
@tournament.weights.each do |weight| @tournament.weights.each do |weight|
assign_loser_names_for_weight(weight) assign_loser_names_for_weight(weight)
advance_bye_matches_championship(weight.matches.reload) advance_bye_matches_championship(weight)
advance_bye_matches_consolation(weight)
end end
end end
def define_losername_championship_mappings(bracket_size) private
# Use hashes instead of arrays for mappings
case bracket_size
when 4
[
{ conso_bracket_position: "3/4", championship_bracket_position: "Semis", cross_bracket: false, both_wrestlers: true }
]
when 8
[
{ conso_bracket_position: "Conso Quarter", championship_bracket_position: "Quarter", cross_bracket: false, both_wrestlers: true },
{ conso_bracket_position: "Conso Semis", championship_bracket_position: "Semis", cross_bracket: true, both_wrestlers: false }
]
when 16
[
{ conso_bracket_position: "Conso Round of 8.1", championship_bracket_position: "Bracket Round of 16", cross_bracket: false, both_wrestlers: true },
{ conso_bracket_position: "Conso Round of 8.2", championship_bracket_position: "Quarter", cross_bracket: true, both_wrestlers: false },
{ conso_bracket_position: "Conso Semis", championship_bracket_position: "Semis", cross_bracket: false, both_wrestlers: false }
]
when 32
[
{ conso_bracket_position: "Conso Round of 16.1", championship_bracket_position: "Bracket Round of 32", cross_bracket: false, both_wrestlers: true },
{ conso_bracket_position: "Conso Round of 16.2", championship_bracket_position: "Bracket Round of 16", cross_bracket: true, both_wrestlers: false },
{ conso_bracket_position: "Conso Round of 8.2", championship_bracket_position: "Quarter", cross_bracket: false, both_wrestlers: false },
{ conso_bracket_position: "Conso Semis", championship_bracket_position: "Semis", cross_bracket: true, both_wrestlers: false },
]
when 64
[
{ conso_bracket_position: "Conso Round of 32.1", championship_bracket_position: "Bracket Round of 64", cross_bracket: false, both_wrestlers: true },
{ conso_bracket_position: "Conso Round of 32.2", championship_bracket_position: "Bracket Round of 32", cross_bracket: true, both_wrestlers: false },
{ conso_bracket_position: "Conso Round of 16.2", championship_bracket_position: "Bracket Round of 16", cross_bracket: false, both_wrestlers: false },
{ conso_bracket_position: "Conso Round of 8.2", championship_bracket_position: "Quarter", cross_bracket: true, both_wrestlers: false },
{ conso_bracket_position: "Conso Semis", championship_bracket_position: "Semis", cross_bracket: false, both_wrestlers: false }
]
else
nil
end
end
# Assign loser names for a single weight bracket
def assign_loser_names_for_weight(weight) def assign_loser_names_for_weight(weight)
number_of_placers = @tournament.number_of_placers
bracket_size = weight.calculate_bracket_size bracket_size = weight.calculate_bracket_size
matches_by_weight = weight.matches.reload matches = weight.matches.reload
num_placers = @tournament.number_of_placers
loser_name_championship_mappings = define_losername_championship_mappings(bracket_size) # Build dynamic round definitions
champ_rounds = dynamic_championship_rounds(bracket_size)
conso_rounds = dynamic_consolation_rounds(bracket_size)
first_round = { bracket_position: first_round_label(bracket_size) }
champ_full = [first_round] + champ_rounds
loser_name_championship_mappings.each do |mapping| # Map championship losers into consolation slots
conso_bracket_position = mapping[:conso_bracket_position] mappings = []
championship_bracket_position = mapping[:championship_bracket_position] champ_full[0...-1].each_with_index do |champ_info, i|
cross_bracket = mapping[:cross_bracket] map_idx = i.zero? ? 0 : (2 * i - 1)
both_wrestlers = mapping[:both_wrestlers] next if map_idx < 0 || map_idx >= conso_rounds.size
conso_matches = matches_by_weight.select do |match| mappings << {
match.bracket_position == conso_bracket_position && match.bracket_position == conso_bracket_position championship_bracket_position: champ_info[:bracket_position],
end.sort_by(&:bracket_position_number) consolation_bracket_position: conso_rounds[map_idx][:bracket_position],
both_wrestlers: i.zero?,
championship_matches = matches_by_weight.select do |match| champ_round_index: i
match.bracket_position == championship_bracket_position && match.bracket_position == championship_bracket_position }
end.sort_by(&:bracket_position_number)
conso_matches.reverse! if cross_bracket
championship_bracket_position_number = 1
conso_matches.each do |match|
bout_number1 = championship_matches.find do |bout_match|
bout_match.bracket_position_number == championship_bracket_position_number
end.bout_number
match.loser1_name = "Loser of #{bout_number1}"
if both_wrestlers
championship_bracket_position_number += 1
bout_number2 = championship_matches.find do |bout_match|
bout_match.bracket_position_number == championship_bracket_position_number
end.bout_number
match.loser2_name = "Loser of #{bout_number2}"
end end
championship_bracket_position_number += 1
# Apply loser-name mappings
mappings.each do |map|
champ = matches.select { |m| m.bracket_position == map[:championship_bracket_position] }
.sort_by(&:bracket_position_number)
conso = matches.select { |m| m.bracket_position == map[:consolation_bracket_position] }
.sort_by(&:bracket_position_number)
current_champ_round_index = map[:champ_round_index]
if current_champ_round_index.odd?
conso.reverse!
end
idx = 0
# Determine if this mapping is for losers from the first championship round
is_first_champ_round_feed = map[:champ_round_index].zero?
conso.each do |cm|
champ_match1 = champ[idx]
if champ_match1
if is_first_champ_round_feed && ((champ_match1.w1 && champ_match1.w2.nil?) || (champ_match1.w1.nil? && champ_match1.w2))
cm.loser1_name = "BYE"
else
cm.loser1_name = "Loser of #{champ_match1.bout_number}"
end
else
cm.loser1_name = nil # Should not happen if bracket generation is correct
end
if map[:both_wrestlers] # This is true only if is_first_champ_round_feed
idx += 1 # Increment for the second championship match
champ_match2 = champ[idx]
if champ_match2
# BYE check is only relevant for the first championship round feed
if is_first_champ_round_feed && ((champ_match2.w1 && champ_match2.w2.nil?) || (champ_match2.w1.nil? && champ_match2.w2))
cm.loser2_name = "BYE"
else
cm.loser2_name = "Loser of #{champ_match2.bout_number}"
end
else
cm.loser2_name = nil # Should not happen
end
end
idx += 1 # Increment for the next consolation match or next pair from championship
end end
end end
conso_semi_matches = matches_by_weight.select { |match| match.bracket_position == "Conso Semis" } # 5th/6th place
conso_quarter_matches = matches_by_weight.select { |match| match.bracket_position == "Conso Quarter" } if bracket_size >= 5 && num_placers >= 6
conso_semis = matches.select { |m| m.bracket_position == "Conso Semis" }
if number_of_placers >= 6 && weight.wrestlers.size >= 5 .sort_by(&:bracket_position_number)
five_six_match = matches_by_weight.find { |match| match.bracket_position == "5/6" } if conso_semis.size >= 2
bout_number1 = conso_semi_matches.find { |match| match.bracket_position_number == 1 }.bout_number m56 = matches.find { |m| m.bracket_position == "5/6" }
bout_number2 = conso_semi_matches.find { |match| match.bracket_position_number == 2 }.bout_number m56.loser1_name = "Loser of #{conso_semis[0].bout_number}"
five_six_match.loser1_name = "Loser of #{bout_number1}" m56.loser2_name = "Loser of #{conso_semis[1].bout_number}" if m56
five_six_match.loser2_name = "Loser of #{bout_number2}" end
end end
if number_of_placers >= 8 && weight.wrestlers.size >= 7 # 7th/8th place
seven_eight_match = matches_by_weight.find { |match| match.bracket_position == "7/8" } if bracket_size >= 7 && num_placers >= 8
bout_number1 = conso_quarter_matches.find { |match| match.bracket_position_number == 1 }.bout_number conso_quarters = matches.select { |m| m.bracket_position == "Conso Quarter" }
bout_number2 = conso_quarter_matches.find { |match| match.bracket_position_number == 2 }.bout_number .sort_by(&:bracket_position_number)
seven_eight_match.loser1_name = "Loser of #{bout_number1}" if conso_quarters.size >= 2
seven_eight_match.loser2_name = "Loser of #{bout_number2}" m78 = matches.find { |m| m.bracket_position == "7/8" }
m78.loser1_name = "Loser of #{conso_quarters[0].bout_number}"
m78.loser2_name = "Loser of #{conso_quarters[1].bout_number}" if m78
end
end end
save_matches(matches_by_weight)
end
def save_matches(matches)
matches.each(&:save!) matches.each(&:save!)
end end
def advance_bye_matches_championship(matches) # Advance first-round byes in championship bracket
first_round = matches.sort_by{|m| m.round}.first.round def advance_bye_matches_championship(weight)
matches.select do |m| matches = weight.matches.reload
m.round == first_round first_round = matches.map(&:round).min
end.sort_by(&:bracket_position_number).each do |match| matches.select { |m| m.round == first_round }
next unless match.w1.nil? || match.w2.nil? .sort_by(&:bracket_position_number)
.each { |m| handle_bye(m) }
end
# Advance first-round byes in consolation bracket
def advance_bye_matches_consolation(weight)
matches = weight.matches.reload
bracket_size = weight.calculate_bracket_size
first_conso = dynamic_consolation_rounds(bracket_size).first
matches.select { |m| m.round == first_conso[:round] && m.bracket_position == first_conso[:bracket_position] }
.sort_by(&:bracket_position_number)
.each { |m| handle_bye(m) }
end
# Mark bye match, set finished, and advance
def handle_bye(match)
if [match.w1, match.w2].compact.size == 1
match.finished = 1 match.finished = 1
match.win_type = "BYE" match.win_type = 'BYE'
if match.w1 if match.w1
match.winner_id = match.w1 match.winner_id = match.w1
match.loser2_name = "BYE" match.loser2_name = 'BYE'
elsif match.w2 else
match.winner_id = match.w2 match.winner_id = match.w2
match.loser1_name = "BYE" match.loser1_name = 'BYE'
end end
match.score = "" match.score = ''
match.save match.save!
match.advance_wrestlers match.advance_wrestlers
end end
end end
# Helpers for dynamic bracket labels
def first_round_label(size)
case size
when 2 then 'Final'
when 4 then 'Semis'
when 8 then 'Quarter'
else "Bracket Round of #{size}"
end
end
def dynamic_championship_rounds(size)
total = Math.log2(size).to_i
(1...total).map do |i|
participants = size / (2**i)
{ bracket_position: bracket_label(participants), round: i + 1 }
end
end
def dynamic_consolation_rounds(size)
total_log2 = Math.log2(size).to_i
return [] if total_log2 <= 1
max_j_val = (2 * (total_log2 - 1) - 1)
(1..max_j_val).map do |j|
current_participants = size / (2**((j.to_f / 2).ceil))
{
bracket_position: consolation_label(current_participants, j, size),
round: j
}
end
end
def bracket_label(participants)
case participants
when 2 then '1/2'
when 4 then 'Semis'
when 8 then 'Quarter'
else "Bracket Round of #{participants}"
end
end
def consolation_label(participants, j, bracket_size)
max_j_for_bracket = (2 * (Math.log2(bracket_size).to_i - 1) - 1)
if participants == 2 && j == max_j_for_bracket
return '3/4'
elsif participants == 4
return j.odd? ? 'Conso Quarter' : 'Conso Semis'
else
suffix = j.odd? ? ".1" : ".2"
return "Conso Round of #{participants}#{suffix}"
end
end
end end

View File

@@ -30,7 +30,7 @@ class DoubleEliminationMatchGeneration
seed1, seed2 = matchup[:seeds] seed1, seed2 = matchup[:seeds]
bracket_position = matchup[:bracket_position] bracket_position = matchup[:bracket_position]
bracket_pos_number = idx + 1 bracket_pos_number = idx + 1
round_number = matchup[:round] # Use the round from our definition round_number = matchup[:round]
create_matchup_from_seed( create_matchup_from_seed(
seed1, seed1,
@@ -77,222 +77,112 @@ class DoubleEliminationMatchGeneration
) )
end end
#
# 5/6, 7/8 placing logic # 5/6, 7/8 placing logic
# if weight.wrestlers.size >= 5 && @tournament.number_of_placers >= 6 && matches_this_round == 1
if weight.wrestlers.size >= 5
if @tournament.number_of_placers >= 6 && matches_this_round == 1
create_matchup(nil, nil, "5/6", 1, round_number, weight) create_matchup(nil, nil, "5/6", 1, round_number, weight)
end end
end if weight.wrestlers.size >= 7 && @tournament.number_of_placers >= 8 && matches_this_round == 1
if weight.wrestlers.size >= 7
if @tournament.number_of_placers >= 8 && matches_this_round == 1
create_matchup(nil, nil, "7/8", 1, round_number, weight) create_matchup(nil, nil, "7/8", 1, round_number, weight)
end end
end end
end end
end
# # Single bracket definition dynamically generated for any power-of-two bracket size.
# Single bracket definition that includes both bracket_position and round. # Returns a hash with :round_one_matchups, :championship_rounds, and :consolation_rounds.
# If you later decide to tweak round numbering, you do it in ONE place.
#
def define_bracket_matches(bracket_size) def define_bracket_matches(bracket_size)
case bracket_size # Only support brackets that are powers of two
when 4 return nil unless (bracket_size & (bracket_size - 1)).zero?
{
round_one_matchups: [
# First round is Semis => round=1
{ seeds: [1, 4], bracket_position: "Semis", round: 1 },
{ seeds: [2, 3], bracket_position: "Semis", round: 1 }
],
championship_rounds: [
# Final => round=2
{ bracket_position: "1/2", number_of_matches: 1, round: 2 }
],
consolation_rounds: [
# 3rd place => round=2
{ bracket_position: "3/4", number_of_matches: 1, round: 2 }
]
}
when 8 # 1) Generate the seed sequence (e.g., [1,8,5,4,...] for size=8)
{ seeds = generate_seed_sequence(bracket_size)
round_one_matchups: [
# Quarter => round=1
{ seeds: [1, 8], bracket_position: "Quarter", round: 1 },
{ seeds: [4, 5], bracket_position: "Quarter", round: 1 },
{ seeds: [3, 6], bracket_position: "Quarter", round: 1 },
{ seeds: [2, 7], bracket_position: "Quarter", round: 1 }
],
championship_rounds: [
# Semis => round=2, Final => round=4
{ bracket_position: "Semis", number_of_matches: 2, round: 2 },
{ bracket_position: "1/2", number_of_matches: 1, round: 4 }
],
consolation_rounds: [
# Conso Quarter => round=2, Conso Semis => round=3, 3/4 => round=4
{ bracket_position: "Conso Quarter", number_of_matches: 2, round: 2 },
{ bracket_position: "Conso Semis", number_of_matches: 2, round: 3 },
{ bracket_position: "3/4", number_of_matches: 1, round: 4 }
]
}
when 16 # 2) Pair seeds into first-round matchups, sorting so lower seed is w1
round_one = seeds.each_slice(2).map.with_index do |(s1, s2), idx|
a, b = [s1, s2].sort
{ {
round_one_matchups: [ seeds: [a, b],
{ seeds: [1,16], bracket_position: "Bracket Round of 16", round: 1 }, bracket_position: first_round_label(bracket_size),
{ seeds: [8,9], bracket_position: "Bracket Round of 16", round: 1 }, round: 1
{ seeds: [5,12], bracket_position: "Bracket Round of 16", round: 1 },
{ seeds: [4,13], bracket_position: "Bracket Round of 16", round: 1 },
{ seeds: [3,14], bracket_position: "Bracket Round of 16", round: 1 },
{ seeds: [6,11], bracket_position: "Bracket Round of 16", round: 1 },
{ seeds: [7,10], bracket_position: "Bracket Round of 16", round: 1 },
{ seeds: [2,15], bracket_position: "Bracket Round of 16", round: 1 }
],
championship_rounds: [
# Quarter => round=2, Semis => round=4, Final => round=6
{ bracket_position: "Quarter", number_of_matches: 4, round: 2 },
{ bracket_position: "Semis", number_of_matches: 2, round: 4 },
{ bracket_position: "1/2", number_of_matches: 1, round: 6 }
],
consolation_rounds: [
# Just carry over your standard numbering
{ bracket_position: "Conso Round of 8.1", number_of_matches: 4, round: 2 },
{ bracket_position: "Conso Round of 8.2", number_of_matches: 4, round: 3 },
{ bracket_position: "Conso Quarter", number_of_matches: 2, round: 4 },
{ bracket_position: "Conso Semis", number_of_matches: 2, round: 5 },
{ bracket_position: "3/4", number_of_matches: 1, round: 6 }
]
} }
when 32
{
round_one_matchups: [
{ seeds: [1,32], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [16,17], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [9,24], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [8,25], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [5,28], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [12,21], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [13,20], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [4,29], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [3,30], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [14,19], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [11,22], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [6,27], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [7,26], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [10,23], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [15,18], bracket_position: "Bracket Round of 32", round: 1 },
{ seeds: [2,31], bracket_position: "Bracket Round of 32", round: 1 }
],
championship_rounds: [
{ bracket_position: "Bracket Round of 16", number_of_matches: 8, round: 2 },
{ bracket_position: "Quarter", number_of_matches: 4, round: 4 },
{ bracket_position: "Semis", number_of_matches: 2, round: 6 },
{ bracket_position: "1/2", number_of_matches: 1, round: 8 }
],
consolation_rounds: [
{ bracket_position: "Conso Round of 16.1", number_of_matches: 8, round: 2 },
{ bracket_position: "Conso Round of 16.2", number_of_matches: 8, round: 3 },
{ bracket_position: "Conso Round of 8.1", number_of_matches: 4, round: 4 },
{ bracket_position: "Conso Round of 8.2", number_of_matches: 4, round: 5 },
{ bracket_position: "Conso Quarter", number_of_matches: 2, round: 6 },
{ bracket_position: "Conso Semis", number_of_matches: 2, round: 7 },
{ bracket_position: "3/4", number_of_matches: 1, round: 8 }
]
}
when 64
{
round_one_matchups: [
{ seeds: [1, 64], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [32, 33], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [17, 48], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [16, 49], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [9, 56], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [24, 41], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [25, 40], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [8, 57], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [5, 60], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [28, 37], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [21, 44], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [12, 53], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [13, 52], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [20, 45], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [29, 36], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [4, 61], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [3, 62], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [30, 35], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [19, 46], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [14, 51], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [11, 54], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [22, 43], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [27, 38], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [6, 59], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [7, 58], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [26, 39], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [23, 42], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [10, 55], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [15, 50], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [18, 47], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [31, 34], bracket_position: "Bracket Round of 64", round: 1 },
{ seeds: [2, 63], bracket_position: "Bracket Round of 64", round: 1 }
],
championship_rounds: [
{ bracket_position: "Bracket Round of 32", number_of_matches: 16, round: 2 },
{ bracket_position: "Bracket Round of 16", number_of_matches: 8, round: 3 },
{ bracket_position: "Quarter", number_of_matches: 4, round: 4 },
{ bracket_position: "Semis", number_of_matches: 2, round: 5 },
{ bracket_position: "1/2", number_of_matches: 1, round: 10 }
],
consolation_rounds: [
{ bracket_position: "Conso Round of 32.1", number_of_matches: 16, round: 2 },
{ bracket_position: "Conso Round of 32.2", number_of_matches: 16, round: 3 },
{ bracket_position: "Conso Round of 16.1", number_of_matches: 8, round: 4 },
{ bracket_position: "Conso Round of 16.2", number_of_matches: 8, round: 5 },
{ bracket_position: "Conso Round of 8.1", number_of_matches: 4, round: 6 },
{ bracket_position: "Conso Round of 8.2", number_of_matches: 4, round: 7 },
{ bracket_position: "Conso Quarter", number_of_matches: 2, round: 8 },
{ bracket_position: "Conso Semis", number_of_matches: 2, round: 9 },
{ bracket_position: "3/4", number_of_matches: 1, round: 10 }
]
}
else
nil
end end
# 3) Build full structure, including dynamic championship & consolation rounds
{
round_one_matchups: round_one,
championship_rounds: dynamic_championship_rounds(bracket_size),
consolation_rounds: dynamic_consolation_rounds(bracket_size)
}
end
# Returns a human-readable label for the first round based on bracket size.
def first_round_label(bracket_size)
case bracket_size
when 2 then "1/2"
when 4 then "Semis"
when 8 then "Quarter"
else "Bracket Round of #{bracket_size}"
end
end
# Dynamically generate championship rounds for any power-of-two bracket size.
def dynamic_championship_rounds(bracket_size)
rounds = []
num_rounds = Math.log2(bracket_size).to_i
# i: 1 -> first post-initial round, up to num_rounds-1 (final)
(1...num_rounds).each do |i|
participants = bracket_size / (2**i)
number_of_matches = participants / 2
bracket_position = case participants
when 2 then "1/2"
when 4 then "Semis"
when 8 then "Quarter"
else "Bracket Round of #{participants}"
end
round_number = i * 2
rounds << { bracket_position: bracket_position,
number_of_matches: number_of_matches,
round: round_number }
end
rounds
end
# Dynamically generate consolation rounds for any power-of-two bracket size.
def dynamic_consolation_rounds(bracket_size)
rounds = []
num_rounds = Math.log2(bracket_size).to_i
total_conso = 2 * (num_rounds - 1) - 1
(1..total_conso).each do |j|
participants = bracket_size / (2**((j.to_f / 2).ceil))
number_of_matches = participants / 2
bracket_position = case participants
when 2 then "3/4"
when 4
j.odd? ? "Conso Quarter" : "Conso Semis"
else
suffix = j.odd? ? ".1" : ".2"
"Conso Round of #{participants}#{suffix}"
end
round_number = j + 1
rounds << { bracket_position: bracket_position,
number_of_matches: number_of_matches,
round: round_number }
end
rounds
end end
########################################################################### ###########################################################################
# PHASE 2: Overwrite rounds in all smaller brackets to match the largest one. # PHASE 2: Overwrite rounds in all smaller brackets to match the largest one.
########################################################################### ###########################################################################
def align_all_rounds_to_largest_bracket def align_all_rounds_to_largest_bracket
#
# 1) Find the bracket size that is largest
#
largest_weight = @tournament.weights.max_by { |w| w.calculate_bracket_size } largest_weight = @tournament.weights.max_by { |w| w.calculate_bracket_size }
return unless largest_weight return unless largest_weight
#
# 2) Gather all matches for that bracket. Build a map from bracket_position => round
#
# We assume "largest bracket" is the single weight with the largest bracket_size.
#
largest_bracket_size = largest_weight.calculate_bracket_size
largest_matches = largest_weight.tournament.matches.where(weight_id: largest_weight.id)
position_to_round = {} position_to_round = {}
largest_matches.each do |m| largest_weight.tournament.matches.where(weight_id: largest_weight.id).each do |m|
# In case multiple matches have the same bracket_position but different rounds
# (like "3/4" might appear more than once), you can pick the first or max.
position_to_round[m.bracket_position] ||= m.round position_to_round[m.bracket_position] ||= m.round
end end
#
# 3) For every other match in the entire tournament (including possibly the largest bracket, if you want),
# overwrite the round to match this map.
#
@tournament.matches.find_each do |match| @tournament.matches.find_each do |match|
# If there's a known round for this bracket_position, use it
if position_to_round.key?(match.bracket_position) if position_to_round.key?(match.bracket_position)
match.update(round: position_to_round[match.bracket_position]) match.update(round: position_to_round[match.bracket_position])
end end
@@ -327,4 +217,23 @@ class DoubleEliminationMatchGeneration
bracket_position_number: bracket_position_number bracket_position_number: bracket_position_number
) )
end end
# Calculates the sequence of seeds for the first round of a power-of-two bracket.
def generate_seed_sequence(n)
raise ArgumentError, "Bracket size must be a power of two" unless (n & (n - 1)).zero?
return [1, 2] if n == 2
half = n / 2
prev = generate_seed_sequence(half)
comp = prev.map { |s| n + 1 - s }
result = []
(0...prev.size).step(2) do |k|
result << prev[k]
result << comp[k]
result << comp[k + 1]
result << prev[k + 1]
end
result
end
end end