From c86f678fd1a2866e289d5ac42880d24c4968deb9 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Tue, 17 Mar 2015 09:06:21 -0400 Subject: [PATCH] Moved poolNumber generation to weight. --- app/controllers/static_pages_controller.rb | 8 +- app/models/pool.rb | 67 ++--------------- app/models/weight.rb | 74 ++++++++++++++++++- app/models/wrestler.rb | 7 +- db/migrate/20150317122709_drop_pool_number.rb | 5 ++ db/schema.rb | 3 +- db/seeds.rb | 5 ++ 7 files changed, 98 insertions(+), 71 deletions(-) create mode 100644 db/migrate/20150317122709_drop_pool_number.rb diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index f8a4ea7..9f2b06e 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -39,10 +39,10 @@ class StaticPagesController < ApplicationController @tournament = Tournament.find(@weight.tournament_id) @matches = @tournament.upcomingMatches @wrestlers = Wrestler.where(weight_id: @weight.id) - @poolOneWrestlers = @wrestlers.select{|w| w.poolNumber == 1} - @poolTwoWrestlers = @wrestlers.select{|w| w.poolNumber == 2} - @poolThreeWrestlers = @wrestlers.select{|w| w.poolNumber == 3} - @poolFourWrestlers = @wrestlers.select{|w| w.poolNumber == 4} + @poolOneWrestlers = @wrestlers.select{|w| w.generatePoolNumber == 1} + @poolTwoWrestlers = @wrestlers.select{|w| w.generatePoolNumber == 2} + @poolThreeWrestlers = @wrestlers.select{|w| w.generatePoolNumber == 3} + @poolFourWrestlers = @wrestlers.select{|w| w.generatePoolNumber == 4} end diff --git a/app/models/pool.rb b/app/models/pool.rb index bbad467..85c5a5a 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -1,69 +1,16 @@ class Pool - def onePool(wrestlers,weight,tournament,matches) - wrestlers.sort_by{|x|[x.original_seed]}.each do |w| - w.poolNumber = 1 - end - matches = roundRobin(1,tournament,weight,matches,wrestlers) - return matches - - end - - - def twoPools(wrestlers,weight,tournament,matches) - pool = 1 - wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| - if w.original_seed == 1 - w.poolNumber = 1 - elsif w.original_seed == 2 - w.poolNumber = 2 - elsif w.original_seed == 3 - w.poolNumber = 2 - elsif w.original_seed == 4 - w.poolNumber = 1 - else - w.poolNumber = pool - end - if pool < 2 - pool = pool + 1 - else - pool =1 - end - end - matches = roundRobin(1,tournament,weight,matches,wrestlers) - matches = roundRobin(2,tournament,weight,matches,wrestlers) - return matches - end - - - def fourPools(wrestlers,weight,tournament,matches) + def generatePools(pools,wrestlers,weight,tournament,matches) + @pools = pools @pool = 1 - wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| - if w.original_seed == 3 - w.poolNumber = 3 - elsif w.original_seed == 4 - w.poolNumber = 4 - elsif w.original_seed == 1 - w.poolNumber = 1 - elsif w.original_seed == 2 - w.poolNumber = 2 - else - w.poolNumber = @pool - end - if @pool < 4 - @pool = @pool + 1 - else - @pool =1 - end + while @pool <= @pools + matches = roundRobin(@pool,wrestlers,weight,tournament,matches) + @pool = @pool +1 end - matches = roundRobin(1,tournament,weight,matches,wrestlers) - matches = roundRobin(2,tournament,weight,matches,wrestlers) - matches = roundRobin(3,tournament,weight,matches,wrestlers) - matches = roundRobin(4,tournament,weight,matches,wrestlers) return matches end - def roundRobin(pool,tournament_id,weight,matches,wrestlers) - @wrestlers = wrestlers.select{|w| w.poolNumber == pool}.to_a + def roundRobin(pool,wrestlers,weight,tournament,matches) + @wrestlers = wrestlers.select{|w| w.generatePoolNumber == pool}.to_a @poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse @poolMatches.each_with_index do |b,index| @bout = b.map diff --git a/app/models/weight.rb b/app/models/weight.rb index c0f22f3..d3bae91 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -15,6 +15,73 @@ class Weight < ActiveRecord::Base end 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 fourPoolNumbers(wrestlers) + @pool = 1 + wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| + if w.original_seed == 3 + w.poolNumber = 3 + elsif w.original_seed == 4 + w.poolNumber = 4 + elsif w.original_seed == 1 + w.poolNumber = 1 + elsif w.original_seed == 2 + w.poolNumber = 2 + else + w.poolNumber = @pool + end + if @pool < 4 + @pool = @pool + 1 + else + @pool =1 + end + end + return wrestlers + end + + def onePoolNumbers(wrestlers) + wrestlers.sort_by{|x|[x.original_seed]}.each do |w| + w.poolNumber = 1 + end + return wrestlers + + end + + + def twoPoolNumbers(wrestlers) + pool = 1 + wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| + if w.original_seed == 1 + w.poolNumber = 1 + elsif w.original_seed == 2 + w.poolNumber = 2 + elsif w.original_seed == 3 + w.poolNumber = 2 + elsif w.original_seed == 4 + w.poolNumber = 1 + else + w.poolNumber = pool + end + if pool < 2 + pool = pool + 1 + else + pool =1 + end + end + return wrestlers + end + def bracket_size @wrestlers = Wrestler.where(weight_id: self.id) return @wrestlers.size @@ -35,16 +102,15 @@ class Weight < ActiveRecord::Base def generateMatchups(matches) @wrestlers = self.wrestlers - #@wrestlers.sort_by{|w| [w.original_seed]} if self.pools == 1 @pool = Pool.new - @matches = @pool.onePool(@wrestlers,self,self.tournament_id,matches) + @matches = @pool.generatePools(1,@wrestlers,self,self.tournament_id,matches) elsif self.pools == 2 @pool = Pool.new - @matches = @pool.twoPools(@wrestlers,self,self.tournament_id,matches) + @matches = @pool.generatePools(2,@wrestlers,self,self.tournament_id,matches) elsif self.pools == 4 @pool = Pool.new - @matches = @pool.fourPools(@wrestlers,self,self.tournament_id,matches) + @matches = @pool.generatePools(4,@wrestlers,self,self.tournament_id,matches) end return @matches end diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index 6817630..c266dd1 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -1,7 +1,7 @@ class Wrestler < ActiveRecord::Base belongs_to :school belongs_to :weight - attr_accessor :matches_all, :isWrestlingThisRound, :boutByRound, :seasonWinPercentage + attr_accessor :matches_all, :isWrestlingThisRound, :boutByRound, :seasonWinPercentage, :poolNumber def isWrestlingThisRound(matchRound) @gMatches = Match.where(g_id: self.id, round: matchRound) @@ -14,6 +14,11 @@ class Wrestler < ActiveRecord::Base end end + def generatePoolNumber + @pool = self.weight.returnPoolNumber(self) + return @pool + end + def boutByRound(round,matches) @matches = matches.select{|m| m.w1 == self.id || m.w2 == self.id} @match = @matches.select{|m| m.round == round}.first diff --git a/db/migrate/20150317122709_drop_pool_number.rb b/db/migrate/20150317122709_drop_pool_number.rb new file mode 100644 index 0000000..337cac0 --- /dev/null +++ b/db/migrate/20150317122709_drop_pool_number.rb @@ -0,0 +1,5 @@ +class DropPoolNumber < ActiveRecord::Migration + def change + remove_column :wrestlers, :poolNumber + end +end diff --git a/db/schema.rb b/db/schema.rb index 4835122..3a4d587 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: 20150316131043) do +ActiveRecord::Schema.define(version: 20150317122709) do create_table "matches", force: :cascade do |t| t.integer "r_id" @@ -89,7 +89,6 @@ ActiveRecord::Schema.define(version: 20150316131043) do t.integer "season_win" t.integer "season_loss" t.string "criteria" - t.integer "poolNumber" t.boolean "extra" end diff --git a/db/seeds.rb b/db/seeds.rb index 7436067..7b11314 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -46,6 +46,11 @@ if Rails.env.development? Wrestler.create(name: 'Guy 27', school_id: 202, weight_id: 201, original_seed: 11, season_win: 0, season_loss: 0, criteria: 'N/A') Wrestler.create(name: 'Guy 28', school_id: 203, weight_id: 201, original_seed: 12, season_win: 0, season_loss: 0, criteria: 'N/A') Wrestler.create(name: 'Guy 29', school_id: 204, weight_id: 201, original_seed: 13, season_win: 0, season_loss: 0, criteria: 'N/A') + Wrestler.create(name: 'Guy 30', school_id: 204, weight_id: 202, original_seed: 13, season_win: 0, season_loss: 0, criteria: 'N/A') + Wrestler.create(name: 'Guy 31', school_id: 204, weight_id: 202, original_seed: 13, season_win: 0, season_loss: 0, criteria: 'N/A') + Wrestler.create(name: 'Guy 32', school_id: 204, weight_id: 202, original_seed: 13, season_win: 0, season_loss: 0, criteria: 'N/A') + Wrestler.create(name: 'Guy 33', school_id: 204, weight_id: 202, original_seed: 13, season_win: 0, season_loss: 0, criteria: 'N/A') + Wrestler.create(name: 'Guy 34', school_id: 204, weight_id: 202, original_seed: 13, season_win: 0, season_loss: 0, criteria: 'N/A') end if Rails.env.production?