From 2f9c54df2e7dcc4f82bf8a9a5c9bbff3dc510f87 Mon Sep 17 00:00:00 2001 From: jcwimer Date: Sat, 14 May 2016 13:28:28 +0000 Subject: [PATCH] Refactored match generation so I can eventually support multiple types of tournaments --- app/controllers/tournaments_controller.rb | 2 +- app/models/generates_tournament_matches.rb | 60 ------------------ app/models/tournament.rb | 19 ------ app/models/weight.rb | 49 --------------- .../generate_tournament_matches.rb | 61 +++++++++++++++++++ .../pool_bracket_generation.rb} | 2 +- .../tournament_services/pool_generation.rb} | 2 +- .../pool_to_bracket_generate_loser_names.rb} | 19 +++--- .../pool_to_bracket_match_generation.rb | 46 ++++++++++++++ .../tournament_services/tournament_seeding.rb | 44 +++++++++++++ .../wipe_tournament_matches.rb | 19 ++++++ config/application.rb | 2 + test/integration/pool_advancement_test.rb | 4 -- test/integration/poolbracket_matchups_test.rb | 4 +- 14 files changed, 189 insertions(+), 144 deletions(-) delete mode 100644 app/models/generates_tournament_matches.rb create mode 100644 app/services/tournament_services/generate_tournament_matches.rb rename app/{models/pool_bracket.rb => services/tournament_services/pool_bracket_generation.rb} (98%) rename app/{models/pool.rb => services/tournament_services/pool_generation.rb} (96%) rename app/{models/generates_loser_names.rb => services/tournament_services/pool_to_bracket_generate_loser_names.rb} (92%) create mode 100644 app/services/tournament_services/pool_to_bracket_match_generation.rb create mode 100644 app/services/tournament_services/tournament_seeding.rb create mode 100644 app/services/tournament_services/wipe_tournament_matches.rb diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index b06c577..b89d609 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -163,7 +163,7 @@ class TournamentsController < ApplicationController end def generate_matches - @tournament.generateMatchups + GenerateTournamentMatches.new(@tournament).generate end def brackets diff --git a/app/models/generates_tournament_matches.rb b/app/models/generates_tournament_matches.rb deleted file mode 100644 index 7661a64..0000000 --- a/app/models/generates_tournament_matches.rb +++ /dev/null @@ -1,60 +0,0 @@ -module GeneratesTournamentMatches - - def generateMatchups - self.curently_generating_matches = 1 - self.save - resetSchoolScores - setSeedsAndRandomSeedingWrestlersWithoutSeeds - poolToBracket() if tournament_type == "Pool to bracket" - self.curently_generating_matches = nil - self.save - end - if Rails.env.production? - handle_asynchronously :generateMatchups - end - - def poolToBracket - destroyAllMatches - generatePoolToBracketMatches - poolToBracketPostMatchCreation - end - - def generatePoolToBracketMatches - weights.order(:max).each do |weight| - Pool.new(weight).generatePools() - last_match = matches.where(weight: weight).order(round: :desc).limit(1).first - highest_round = last_match.round - PoolBracket.new(weight, highest_round).generateBracketMatches() - end - end - - def poolToBracketPostMatchCreation - moveFinalsMatchesToLastRound - assignBouts - assignLoserNames - assignFirstMatchesToMats - movePoolSeedsToFinalPoolRound - end - - def moveFinalsMatchesToLastRound - finalsRound = self.totalRounds - finalsMatches = self.matches.select{|m| m.bracket_position == "1/2" || m.bracket_position == "3/4" || m.bracket_position == "5/6" || m.bracket_position == "7/8"} - finalsMatches. each do |m| - m.round = finalsRound - m.save - end - end - - def setSeedsAndRandomSeedingWrestlersWithoutSeeds - weights.each do |w| - w.setSeeds - end - end - - def movePoolSeedsToFinalPoolRound - self.weights.each do |w| - w.setOriginalSeedsToWrestleLastPoolRound - end - end - -end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index e1224f3..19dff17 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,7 +1,5 @@ class Tournament < ActiveRecord::Base - include GeneratesLoserNames - include GeneratesTournamentMatches belongs_to :user has_many :schools, dependent: :destroy has_many :weights, dependent: :destroy @@ -24,10 +22,6 @@ class Tournament < ActiveRecord::Base time end - def resetSchoolScores - schools.update_all("score = 0.0") - end - def tournament_types ["Pool to bracket"] end @@ -50,19 +44,6 @@ class Tournament < ActiveRecord::Base def matchesByRound(round) matches.joins(:weight).where(round: round).order("weights.max") end - - def assignBouts - bout_counts = Hash.new(0) - matches.each do |m| - m.bout_number = m.round * 1000 + bout_counts[m.round] - bout_counts[m.round] += 1 - m.save! - end - end - - def assignFirstMatchesToMats - assignMats(mats) - end def totalRounds self.matches.sort_by{|m| m.round}.last.round diff --git a/app/models/weight.rb b/app/models/weight.rb index 824a103..1573b19 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -46,22 +46,6 @@ class Weight < ActiveRecord::Base wrestlersForPool(pool).sort_by{|w| [w.original_seed ? 0 : 1, w.original_seed || 0]} end - def setOriginalSeedsToWrestleLastPoolRound - pool = 1 - until pool > self.pools - wrestler1 = poolSeedOrder(pool).first - wrestler2 = poolSeedOrder(pool).second - match = wrestler1.poolMatches.sort_by{|m| m.round}.last - if match.w1 != wrestler2.id or match.w2 != wrestler2.id - if match.w1 == wrestler1.id - swapWrestlers(match.w2,wrestler2.id) - elsif match.w2 == wrestler1.id - swapWrestlers(match.w1,wrestler2.id) - end - end - pool += 1 - end - end def swapWrestlers(wrestler1_id,wrestler2_id) SwapWrestlers.new.swapWrestlers(wrestler1_id,wrestler2_id) @@ -180,38 +164,5 @@ class Weight < ActiveRecord::Base def poolOrder(pool) PoolOrder.new(wrestlersForPool(pool)).getPoolOrder end - - def randomSeeding - wrestlerWithSeeds = self.wrestlers.select{|w| w.original_seed != nil }.sort_by{|w| w.original_seed} - if wrestlerWithSeeds.size > 0 - highestSeed = wrestlerWithSeeds.last.seed - seed = highestSeed + 1 - else - seed = 1 - end - wrestlersWithoutSeed = self.wrestlers.select{|w| w.original_seed == nil } - wrestlersWithoutSeed.shuffle.each do |w| - w.seed = seed - w.save - seed += 1 - end - end - - def setSeeds - resetAllSeeds - wrestlerWithSeeds = self.wrestlers.select{|w| w.original_seed != nil } - wrestlerWithSeeds.each do |w| - w.seed = w.original_seed - w.save - end - randomSeeding - end - - def resetAllSeeds - self.wrestlers.each do |w| - w.seed = nil - w.save - end - end end diff --git a/app/services/tournament_services/generate_tournament_matches.rb b/app/services/tournament_services/generate_tournament_matches.rb new file mode 100644 index 0000000..4ffdb34 --- /dev/null +++ b/app/services/tournament_services/generate_tournament_matches.rb @@ -0,0 +1,61 @@ +class GenerateTournamentMatches + def initialize( tournament ) + @tournament = tournament + end + + def generate + standardStartingActions + PoolToBracketMatchGeneration.new(@tournament).generatePoolToBracketMatches if @tournament.tournament_type == "Pool to bracket" + postMatchCreationActions + PoolToBracketMatchGeneration.new(@tournament).assignLoserNames if @tournament.tournament_type == "Pool to bracket" + end + + def standardStartingActions + @tournament.curently_generating_matches = 1 + @tournament.save + WipeTournamentMatches.new(@tournament).setUpMatchGeneration + TournamentSeeding.new(@tournament).setSeeds + end + + def postMatchCreationActions + moveFinalsMatchesToLastRound + assignBouts + assignFirstMatchesToMats + @tournament.curently_generating_matches = nil + @tournament.save + end + + def assignBouts + bout_counts = Hash.new(0) + @tournament.matches.each do |m| + m.bout_number = m.round * 1000 + bout_counts[m.round] + bout_counts[m.round] += 1 + m.save! + end + end + + def moveFinalsMatchesToLastRound + finalsRound = @tournament.totalRounds + finalsMatches = @tournament.matches.select{|m| m.bracket_position == "1/2" || m.bracket_position == "3/4" || m.bracket_position == "5/6" || m.bracket_position == "7/8"} + finalsMatches. each do |m| + m.round = finalsRound + m.save + end + end + + def assignFirstMatchesToMats + matsToAssign = @tournament.mats + if matsToAssign.count > 0 + until matsToAssign.sort_by{|m| m.id}.last.matches.count == 4 + matsToAssign.sort_by{|m| m.id}.each do |m| + m.assignNextMatch + end + end + end + end + + + + + +end \ No newline at end of file diff --git a/app/models/pool_bracket.rb b/app/services/tournament_services/pool_bracket_generation.rb similarity index 98% rename from app/models/pool_bracket.rb rename to app/services/tournament_services/pool_bracket_generation.rb index 26be9f4..4b42763 100644 --- a/app/models/pool_bracket.rb +++ b/app/services/tournament_services/pool_bracket_generation.rb @@ -1,4 +1,4 @@ -class PoolBracket +class PoolBracketGeneration def initialize(weight, highest_round) @weight = weight diff --git a/app/models/pool.rb b/app/services/tournament_services/pool_generation.rb similarity index 96% rename from app/models/pool.rb rename to app/services/tournament_services/pool_generation.rb index 8e7a3af..12a3e5c 100644 --- a/app/models/pool.rb +++ b/app/services/tournament_services/pool_generation.rb @@ -1,4 +1,4 @@ -class Pool +class PoolGeneration def initialize(weight) @weight = weight @tournament = @weight.tournament diff --git a/app/models/generates_loser_names.rb b/app/services/tournament_services/pool_to_bracket_generate_loser_names.rb similarity index 92% rename from app/models/generates_loser_names.rb rename to app/services/tournament_services/pool_to_bracket_generate_loser_names.rb index 023eb12..d315c04 100644 --- a/app/models/generates_loser_names.rb +++ b/app/services/tournament_services/pool_to_bracket_generate_loser_names.rb @@ -1,8 +1,12 @@ -module GeneratesLoserNames - def assignLoserNames +class PoolToBracketGenerateLoserNames + def initialize( tournament ) + @tournament = tournament + end + + def assignLoserNames matches_by_weight = nil - weights.each do |w| - matches_by_weight = matches.where(weight_id: w.id) + @tournament.weights.each do |w| + matches_by_weight = @tournament.matches.where(weight_id: w.id) if w.pool_bracket_type == "twoPoolsToSemi" twoPoolsToSemiLoser(matches_by_weight) elsif w.pool_bracket_type == "fourPoolsToQuarter" @@ -12,7 +16,7 @@ module GeneratesLoserNames end saveMatches(matches_by_weight) end - end + end def twoPoolsToSemiLoser(matches_by_weight) match1 = matches_by_weight.select{|m| m.loser1_name == "Winner Pool 1"}.first @@ -59,5 +63,6 @@ module GeneratesLoserNames matches.each do |m| m.save! end - end -end + end + +end \ No newline at end of file diff --git a/app/services/tournament_services/pool_to_bracket_match_generation.rb b/app/services/tournament_services/pool_to_bracket_match_generation.rb new file mode 100644 index 0000000..31e9388 --- /dev/null +++ b/app/services/tournament_services/pool_to_bracket_match_generation.rb @@ -0,0 +1,46 @@ +class PoolToBracketMatchGeneration + def initialize( tournament ) + @tournament = tournament + end + + + + def generatePoolToBracketMatches + @tournament.weights.order(:max).each do |weight| + PoolGeneration.new(weight).generatePools() + last_match = @tournament.matches.where(weight: weight).order(round: :desc).limit(1).first + highest_round = last_match.round + PoolBracketGeneration.new(weight, highest_round).generateBracketMatches() + end + movePoolSeedsToFinalPoolRound + end + + def movePoolSeedsToFinalPoolRound + @tournament.weights.each do |w| + setOriginalSeedsToWrestleLastPoolRound(w) + end + end + + def setOriginalSeedsToWrestleLastPoolRound(weight) + pool = 1 + until pool > weight.pools + wrestler1 = weight.poolSeedOrder(pool).first + wrestler2 = weight.poolSeedOrder(pool).second + match = wrestler1.poolMatches.sort_by{|m| m.round}.last + if match.w1 != wrestler2.id or match.w2 != wrestler2.id + if match.w1 == wrestler1.id + SwapWrestlers.new.swapWrestlers(match.w2,wrestler2.id) + elsif match.w2 == wrestler1.id + SwapWrestlers.new.swapWrestlers(match.w1,wrestler2.id) + end + end + pool += 1 + end + end + + + def assignLoserNames + PoolToBracketGenerateLoserNames.new(@tournament).assignLoserNames + end + +end \ No newline at end of file diff --git a/app/services/tournament_services/tournament_seeding.rb b/app/services/tournament_services/tournament_seeding.rb new file mode 100644 index 0000000..4ffb9aa --- /dev/null +++ b/app/services/tournament_services/tournament_seeding.rb @@ -0,0 +1,44 @@ +class TournamentSeeding + def initialize( tournament ) + @tournament = tournament + end + + def setSeeds + @tournament.weights.each do |w| + resetAllSeeds(w) + setOriginalSeeds(w) + randomSeeding(w) + end + end + + def randomSeeding(weight) + wrestlerWithSeeds = weight.wrestlers.select{|w| w.original_seed != nil }.sort_by{|w| w.original_seed} + if wrestlerWithSeeds.size > 0 + highestSeed = wrestlerWithSeeds.last.seed + seed = highestSeed + 1 + else + seed = 1 + end + wrestlersWithoutSeed = weight.wrestlers.select{|w| w.original_seed == nil } + wrestlersWithoutSeed.shuffle.each do |w| + w.seed = seed + w.save + seed += 1 + end + end + + def setOriginalSeeds(weight) + wrestlerWithSeeds = weight.wrestlers.select{|w| w.original_seed != nil } + wrestlerWithSeeds.each do |w| + w.seed = w.original_seed + w.save + end + end + + def resetAllSeeds(weight) + weight.wrestlers.each do |w| + w.seed = nil + w.save + end + end +end \ No newline at end of file diff --git a/app/services/tournament_services/wipe_tournament_matches.rb b/app/services/tournament_services/wipe_tournament_matches.rb new file mode 100644 index 0000000..e77c0cb --- /dev/null +++ b/app/services/tournament_services/wipe_tournament_matches.rb @@ -0,0 +1,19 @@ +class WipeTournamentMatches + + def initialize( tournament ) + @tournament = tournament + end + + def setUpMatchGeneration + wipeMatches + resetSchoolScores + end + + def wipeMatches + @tournament.matches.destroy_all + end + + def resetSchoolScores + @tournament.schools.update_all("score = 0.0") + end +end \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 786352b..a05c52e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -34,6 +34,8 @@ module Wrestling config.to_prepare do DeviseController.respond_to :html, :json end + + config.autoload_paths += %W(#{config.root}/app/services/tournament_services) end diff --git a/test/integration/pool_advancement_test.rb b/test/integration/pool_advancement_test.rb index 502e27c..cb5382c 100644 --- a/test/integration/pool_advancement_test.rb +++ b/test/integration/pool_advancement_test.rb @@ -4,10 +4,6 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest def setup tournament = Tournament.find(1) - - # WHY DOES THIS NOT WORK WITHOUT GENERATING MATCHUPS BEFORE EVERY TEST? - # FIXTURES FOR MATCHES ARE FILLED OUT AND WORK FOR OTHER TESTS - # tournament.generateMatchups end def singlePoolNotFinished diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index 0279a91..5946be3 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -3,7 +3,7 @@ require 'test_helper' class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest def setup @tournament = Tournament.find(1) - @tournament.generateMatchups + GenerateTournamentMatches.new(@tournament).generate end def createTournament(numberOfWrestlers) @@ -69,7 +69,7 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest end def checkForByeInPool(tournament) - tournament.generateMatchups + GenerateTournamentMatches.new(tournament).generate matchups = tournament.matches tournament.weights.each do |w| w.wrestlers.each do |wr|