From 3f72a912b62154b6910d03974240b4c8ad288229 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Thu, 2 Mar 2017 18:11:21 +0000 Subject: [PATCH] Wrestler pool number is now saved in db. Pool number generation moved to it's own class. --- app/models/match.rb | 2 +- app/models/weight.rb | 85 ++---------------- app/models/wrestler.rb | 8 +- .../bracket_advancement/pool_advance.rb | 4 +- .../tournament_services/pool_generation.rb | 3 +- .../weight_services/generate_pool_numbers.rb | 77 ++++++++++++++++ app/views/tournaments/_pool.html.erb | 4 +- config/application.rb | 1 + db/migrate/20170301174920_add_pool_column.rb | 5 ++ db/schema.rb | 3 +- test/fixtures/wrestlers.yml | 88 +++++++++++++++---- test/integration/single_test_test.rb | 3 +- 12 files changed, 177 insertions(+), 106 deletions(-) create mode 100644 app/services/weight_services/generate_pool_numbers.rb create mode 100644 db/migrate/20170301174920_add_pool_column.rb diff --git a/app/models/match.rb b/app/models/match.rb index af655d4..ebcd1ef 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -130,7 +130,7 @@ class Match < ActiveRecord::Base end def poolNumber if self.w1? - wrestler1.generatePoolNumber + wrestler1.pool end end diff --git a/app/models/weight.rb b/app/models/weight.rb index 1573b19..bc86549 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -17,8 +17,13 @@ class Weight < ActiveRecord::Base # self.tournament.destroyAllMatches end - def wrestlersForPool(pool) - self.wrestlers.select{|w| w.generatePoolNumber == pool} + def wrestlersForPool(poolNumber) + #For some reason this does not work + # wrestlers.select{|w| w.pool == poolNumber} + + #This does... + weightWrestlers = Wrestler.where(:weight_id => self.id) + weightWrestlers.select{|w| w.pool == poolNumber} end def allPoolMatchesFinished(pool) @@ -43,7 +48,8 @@ class Weight < ActiveRecord::Base end def poolSeedOrder(pool) - wrestlersForPool(pool).sort_by{|w| [w.original_seed ? 0 : 1, w.original_seed || 0]} + # wrestlersForPool(pool).sort_by{|w| [w.original_seed ? 0 : 1, w.original_seed || 0]} + return wrestlersForPool(pool).sort_by{|w|w.seed} end @@ -51,79 +57,6 @@ class Weight < ActiveRecord::Base SwapWrestlers.new.swapWrestlers(wrestler1_id,wrestler2_id) end - def returnPoolNumber(wrestler) - if self.pools == 4 - @wrestlers = fourPoolNumbers(self.wrestlers) - elsif self.pools == 2 - @wrestlers = twoPoolNumbers(self.wrestlers) - elsif self.pools == 1 - @wrestlers = onePoolNumbers(self.wrestlers) - end - @wrestler = @wrestlers.select{|w| w.id == wrestler.id}.first - return @wrestler.poolNumber - end - - def onePoolNumbers(poolWrestlers) - poolWrestlers.sort_by{|x| x.seed }.each do |w| - w.poolNumber = 1 - end - return poolWrestlers - end - - - def twoPoolNumbers(poolWrestlers) - pool = 1 - poolWrestlers.sort_by{|x| x.seed }.reverse.each do |w| - if w.seed == 1 - w.poolNumber = 1 - elsif w.seed == 2 - w.poolNumber = 2 - elsif w.seed == 3 - w.poolNumber = 2 - elsif w.seed == 4 - w.poolNumber = 1 - else - w.poolNumber = pool - end - if pool < 2 - pool = pool + 1 - else - pool = 1 - end - end - return poolWrestlers - end - - def fourPoolNumbers(poolWrestlers) - pool = 1 - poolWrestlers.sort_by{|x| x.seed }.reverse.each do |w| - if w.seed == 1 - w.poolNumber = 1 - elsif w.seed == 2 - w.poolNumber = 2 - elsif w.seed == 3 - w.poolNumber = 3 - elsif w.seed == 4 - w.poolNumber = 4 - elsif w.seed == 8 - w.poolNumber = 1 - elsif w.seed == 7 - w.poolNumber = 2 - elsif w.seed == 6 - w.poolNumber = 3 - elsif w.seed == 5 - w.poolNumber = 4 - else - w.poolNumber = pool - end - if pool < 4 - pool = pool + 1 - else - pool = 1 - end - end - return poolWrestlers - end def bracket_size wrestlers.size diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index 328f2f8..c20358e 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -4,7 +4,7 @@ class Wrestler < ActiveRecord::Base has_one :tournament, through: :weight has_many :matches, through: :weight has_many :deductedPoints, class_name: "Teampointadjust" - attr_accessor :poolNumber, :poolAdvancePoints, :originalId, :swapId + attr_accessor :poolAdvancePoints, :originalId, :swapId validates :name, :weight_id, :school_id, presence: true @@ -103,10 +103,6 @@ class Wrestler < ActiveRecord::Base end end - def generatePoolNumber - self.weight.returnPoolNumber(self) - end - def boutByRound(round) round_match = allMatches.select{|m| m.round == round}.first if round_match.blank? @@ -122,7 +118,7 @@ class Wrestler < ActiveRecord::Base def poolMatches pool_matches = allMatches.select{|m| m.bracket_position == "Pool"} - pool_matches.select{|m| m.poolNumber == self.generatePoolNumber} + pool_matches.select{|m| m.poolNumber == self.pool} end def hasAPoolBye diff --git a/app/services/bracket_advancement/pool_advance.rb b/app/services/bracket_advancement/pool_advance.rb index 177c292..b70855f 100644 --- a/app/services/bracket_advancement/pool_advance.rb +++ b/app/services/bracket_advancement/pool_advance.rb @@ -6,7 +6,7 @@ class PoolAdvance end def advanceWrestler - if @wrestler.weight.allPoolMatchesFinished(@wrestler.generatePoolNumber) && @wrestler.finishedBracketMatches.size == 0 + if @wrestler.weight.allPoolMatchesFinished(@wrestler.pool) && @wrestler.finishedBracketMatches.size == 0 poolToBracketAdvancment end if @wrestler.finishedBracketMatches.size > 0 @@ -15,7 +15,7 @@ class PoolAdvance end def poolToBracketAdvancment - pool = @wrestler.generatePoolNumber + pool = @wrestler.pool if @wrestler.weight.wrestlers.size > 6 poolOrder = @wrestler.weight.poolOrder(pool) #Take pool order and move winner and runner up to correct match based on w1_name and w2_name diff --git a/app/services/tournament_services/pool_generation.rb b/app/services/tournament_services/pool_generation.rb index 12a3e5c..12e3948 100644 --- a/app/services/tournament_services/pool_generation.rb +++ b/app/services/tournament_services/pool_generation.rb @@ -6,12 +6,13 @@ class PoolGeneration end def generatePools + GeneratePoolNumbers.new(@weight).savePoolNumbers pools = @weight.pools while @pool <= pools roundRobin @pool += 1 end - end + end def roundRobin wrestlers = @weight.wrestlersForPool(@pool) diff --git a/app/services/weight_services/generate_pool_numbers.rb b/app/services/weight_services/generate_pool_numbers.rb new file mode 100644 index 0000000..c8fb179 --- /dev/null +++ b/app/services/weight_services/generate_pool_numbers.rb @@ -0,0 +1,77 @@ +class GeneratePoolNumbers + def initialize( weight ) + @weight = weight + end + + def savePoolNumbers + if @weight.pools == 4 + saveFourPoolNumbers(@weight.wrestlers) + elsif @weight.pools == 2 + saveTwoPoolNumbers(@weight.wrestlers) + elsif @weight.pools == 1 + saveOnePoolNumbers(@weight.wrestlers) + end + end + + def saveOnePoolNumbers(poolWrestlers) + poolWrestlers.sort_by{|x| x.seed }.each do |w| + w.pool = 1 + w.save + end + end + + + def saveTwoPoolNumbers(poolWrestlers) + pool = 1 + poolWrestlers.sort_by{|x| x.seed }.reverse.each do |w| + if w.seed == 1 + w.pool = 1 + elsif w.seed == 2 + w.pool = 2 + elsif w.seed == 3 + w.pool = 2 + elsif w.seed == 4 + w.pool = 1 + else + w.pool = pool + end + if pool < 2 + pool = pool + 1 + else + pool = 1 + end + w.save + end + end + + def saveFourPoolNumbers(poolWrestlers) + pool = 1 + poolWrestlers.sort_by{|x| x.seed }.reverse.each do |w| + if w.seed == 1 + w.pool = 1 + elsif w.seed == 2 + w.pool = 2 + elsif w.seed == 3 + w.pool = 3 + elsif w.seed == 4 + w.pool = 4 + elsif w.seed == 8 + w.pool = 1 + elsif w.seed == 7 + w.pool = 2 + elsif w.seed == 6 + w.pool = 3 + elsif w.seed == 5 + w.pool = 4 + else + w.pool = pool + end + if pool < 4 + pool = pool + 1 + else + pool = 1 + end + w.save + end + end +end \ No newline at end of file diff --git a/app/views/tournaments/_pool.html.erb b/app/views/tournaments/_pool.html.erb index cd651df..fe1625c 100644 --- a/app/views/tournaments/_pool.html.erb +++ b/app/views/tournaments/_pool.html.erb @@ -1,6 +1,6 @@ <% @round = 1 %> <% @pool = 1 %> -<% until @wrestlers.select{|w| w.generatePoolNumber == @pool}.blank? %> +<% until @wrestlers.select{|w| w.pool == @pool}.blank? %>
Pool <%= @pool %>
@@ -15,7 +15,7 @@ - <% @wrestlers.select{|w| w.generatePoolNumber == @pool}.sort_by{|w| w.seed}.each do |w| %> + <% @wrestlers.select{|w| w.pool == @pool}.sort_by{|w| w.seed}.each do |w| %> <% @round = 1 %> diff --git a/config/application.rb b/config/application.rb index 83f26e4..183d62a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -37,6 +37,7 @@ module Wrestling config.autoload_paths += %W(#{config.root}/app/services/tournament_services) config.autoload_paths += %W(#{config.root}/app/services/wrestler_services) + config.autoload_paths += %W(#{config.root}/app/services/weight_services) config.autoload_paths += %W(#{config.root}/app/services/bracket_advancement) end diff --git a/db/migrate/20170301174920_add_pool_column.rb b/db/migrate/20170301174920_add_pool_column.rb new file mode 100644 index 0000000..6acb531 --- /dev/null +++ b/db/migrate/20170301174920_add_pool_column.rb @@ -0,0 +1,5 @@ +class AddPoolColumn < ActiveRecord::Migration + def change + add_column :wrestlers, :pool, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index e320d6e..06009f2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160126173424) do +ActiveRecord::Schema.define(version: 20170301174920) do create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false @@ -154,6 +154,7 @@ ActiveRecord::Schema.define(version: 20160126173424) do t.string "criteria" t.boolean "extra" t.decimal "offical_weight" + t.integer "pool" end add_index "wrestlers", ["weight_id"], name: "index_wrestlers_on_weight_id" diff --git a/test/fixtures/wrestlers.yml b/test/fixtures/wrestlers.yml index 56dfaff..2c2aeed 100644 --- a/test/fixtures/wrestlers.yml +++ b/test/fixtures/wrestlers.yml @@ -1,4 +1,4 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + tournament_1_wrestler_1: id: 1 @@ -11,6 +11,7 @@ tournament_1_wrestler_1: season_win: 30 criteria: SQ extra: + pool: 1 tournament_1_wrestler_2: id: 2 @@ -23,6 +24,7 @@ tournament_1_wrestler_2: season_win: 30 criteria: DP 6th extra: + pool: 1 tournament_1_wrestler_3: id: 3 @@ -35,6 +37,7 @@ tournament_1_wrestler_3: season_win: 49 criteria: SP 2nd extra: + pool: 1 tournament_1_wrestler_4: id: 4 @@ -47,6 +50,7 @@ tournament_1_wrestler_4: season_win: 30 criteria: DP 5th extra: + pool: 1 tournament_1_wrestler_5: id: 5 @@ -59,6 +63,7 @@ tournament_1_wrestler_5: season_win: 49 criteria: SP 5th extra: + pool: 1 tournament_1_wrestler_6: id: 6 @@ -71,6 +76,7 @@ tournament_1_wrestler_6: season_win: 16 criteria: extra: + pool: 2 tournament_1_wrestler_7: id: 7 @@ -83,6 +89,7 @@ tournament_1_wrestler_7: season_win: 16 criteria: DQ extra: + pool: 1 tournament_1_wrestler_8: id: 8 @@ -95,6 +102,7 @@ tournament_1_wrestler_8: season_win: 50 criteria: SQ extra: + pool: 2 tournament_1_wrestler_9: id: 9 @@ -107,6 +115,7 @@ tournament_1_wrestler_9: season_win: 40 criteria: SQ extra: + pool: 2 tournament_1_wrestler_10: id: 10 @@ -119,6 +128,7 @@ tournament_1_wrestler_10: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_11: id: 11 @@ -131,6 +141,7 @@ tournament_1_wrestler_11: season_win: 20 criteria: DP 6th extra: + pool: 1 tournament_1_wrestler_12: id: 12 @@ -143,6 +154,7 @@ tournament_1_wrestler_12: season_win: 30 criteria: SP 7th extra: + pool: 1 tournament_1_wrestler_13: id: 13 @@ -155,6 +167,7 @@ tournament_1_wrestler_13: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_14: id: 14 @@ -167,6 +180,7 @@ tournament_1_wrestler_14: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_15: id: 15 @@ -179,6 +193,7 @@ tournament_1_wrestler_15: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_16: id: 16 @@ -191,6 +206,7 @@ tournament_1_wrestler_16: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_17: id: 17 @@ -203,6 +219,7 @@ tournament_1_wrestler_17: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_18: id: 18 @@ -215,6 +232,7 @@ tournament_1_wrestler_18: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_19: id: 19 @@ -227,6 +245,7 @@ tournament_1_wrestler_19: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_20: id: 20 @@ -239,6 +258,7 @@ tournament_1_wrestler_20: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_21: id: 21 @@ -251,6 +271,7 @@ tournament_1_wrestler_21: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_22: id: 22 @@ -263,6 +284,7 @@ tournament_1_wrestler_22: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_23: id: 23 @@ -275,6 +297,7 @@ tournament_1_wrestler_23: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_24: id: 24 @@ -287,6 +310,7 @@ tournament_1_wrestler_24: season_win: 50 criteria: extra: + pool: 3 tournament_1_wrestler_25: id: 25 @@ -299,6 +323,7 @@ tournament_1_wrestler_25: season_win: 50 criteria: extra: + pool: 3 tournament_1_wrestler_26: id: 26 @@ -311,6 +336,7 @@ tournament_1_wrestler_26: season_win: 50 criteria: extra: + pool: 4 tournament_1_wrestler_27: id: 27 @@ -323,6 +349,7 @@ tournament_1_wrestler_27: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_28: id: 28 @@ -335,6 +362,7 @@ tournament_1_wrestler_28: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_29: id: 29 @@ -347,6 +375,7 @@ tournament_1_wrestler_29: season_win: 50 criteria: extra: + pool: 4 tournament_1_wrestler_30: id: 30 @@ -359,6 +388,7 @@ tournament_1_wrestler_30: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_31: id: 31 @@ -371,6 +401,7 @@ tournament_1_wrestler_31: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_32: id: 32 @@ -383,6 +414,7 @@ tournament_1_wrestler_32: season_win: 50 criteria: extra: + pool: 3 tournament_1_wrestler_33: id: 33 @@ -395,6 +427,7 @@ tournament_1_wrestler_33: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_34: id: 34 @@ -407,6 +440,7 @@ tournament_1_wrestler_34: season_win: 50 criteria: extra: + pool: 3 tournament_1_wrestler_35: id: 35 @@ -419,6 +453,7 @@ tournament_1_wrestler_35: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_36: id: 36 @@ -431,6 +466,7 @@ tournament_1_wrestler_36: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_37: id: 37 @@ -443,6 +479,7 @@ tournament_1_wrestler_37: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_38: id: 38 @@ -455,6 +492,7 @@ tournament_1_wrestler_38: season_win: 50 criteria: extra: + pool: 4 tournament_1_wrestler_39: id: 39 @@ -467,6 +505,7 @@ tournament_1_wrestler_39: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_40: id: 40 @@ -479,6 +518,7 @@ tournament_1_wrestler_40: season_win: 50 criteria: extra: + pool: 4 tournament_1_wrestler_41: id: 41 @@ -491,6 +531,7 @@ tournament_1_wrestler_41: season_win: 50 criteria: extra: + pool: 3 tournament_1_wrestler_42: id: 42 @@ -503,6 +544,7 @@ tournament_1_wrestler_42: season_win: 50 criteria: extra: + pool: 4 tournament_1_wrestler_43: id: 43 @@ -515,6 +557,7 @@ tournament_1_wrestler_43: season_win: 50 criteria: extra: + pool: 3 tournament_1_wrestler_44: id: 44 @@ -527,6 +570,7 @@ tournament_1_wrestler_44: season_win: 50 criteria: extra: + pool: 2 tournament_1_wrestler_45: id: 45 @@ -539,6 +583,7 @@ tournament_1_wrestler_45: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_46: id: 46 @@ -551,6 +596,7 @@ tournament_1_wrestler_46: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_47: id: 47 @@ -563,6 +609,7 @@ tournament_1_wrestler_47: season_win: 50 criteria: extra: + pool: 3 tournament_1_wrestler_48: id: 48 @@ -575,6 +622,7 @@ tournament_1_wrestler_48: season_win: 50 criteria: extra: + pool: 4 tournament_1_wrestler_49: id: 49 @@ -582,11 +630,12 @@ tournament_1_wrestler_49: school_id: 1 weight_id: 6 original_seed: - seed: 4 + seed: 6 season_loss: 2 season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_50: id: 50 @@ -599,6 +648,7 @@ tournament_1_wrestler_50: season_win: 50 criteria: extra: + pool: 1 tournament_1_wrestler_51: id: 51 @@ -606,23 +656,25 @@ tournament_1_wrestler_51: school_id: 1 weight_id: 6 original_seed: - seed: 3 - season_loss: 2 - season_win: 50 - criteria: - extra: - -tournament_1_wrestler_52: - id: 52 - name: Guy43 - school_id: 1 - weight_id: 6 - original_seed: seed: 5 season_loss: 2 season_win: 50 criteria: extra: + pool: 1 + +tournament_1_wrestler_52: + id: 52 + name: Guy43 + school_id: 1 + weight_id: 6 + original_seed: + seed: 3 + season_loss: 2 + season_win: 50 + criteria: + extra: + pool: 1 tournament_1_wrestler_53: id: 53 @@ -630,8 +682,12 @@ tournament_1_wrestler_53: school_id: 1 weight_id: 6 original_seed: - seed: 6 + seed: 4 season_loss: 2 season_win: 50 criteria: - extra: \ No newline at end of file + extra: + pool: 1 + + + diff --git a/test/integration/single_test_test.rb b/test/integration/single_test_test.rb index 919fda3..a20d1a0 100644 --- a/test/integration/single_test_test.rb +++ b/test/integration/single_test_test.rb @@ -3,7 +3,7 @@ require 'test_helper' class SingleTestTest < ActionDispatch::IntegrationTest def setup @tournament = Tournament.find(1) - # @tournament.generateMatchups + # GenerateTournamentMatches.new(@tournament).generate end #rake test test/integration/single_test_test.rb > matches.txt @@ -47,6 +47,7 @@ class SingleTestTest < ActionDispatch::IntegrationTest # puts " season_win: #{w.season_win}" # puts " criteria: #{w.criteria}" # puts " extra: #{w.extra}" + # puts " pool: #{w.pool}" # puts "" # count += 1 # end
<%= w.original_seed %> <%= w.name %> <%= w.season_win %>-<%= w.season_loss %> <%= w.school.name %>