From 386dc1605fe734b5d688b6042ad444b25d613649 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Wed, 14 Oct 2015 17:58:28 -0400 Subject: [PATCH] Finished pool advance logic still need tests --- app/models/match.rb | 24 +++++++- app/models/pool.rb | 2 +- app/models/pooladvance.rb | 55 +++++++++++-------- app/models/weight.rb | 26 ++++++--- app/models/wrestler.rb | 13 +++++ .../_fourPoolQuarterBracket.html.erb | 14 ++--- .../_fourPoolSemiBracket.html.erb | 12 ++-- app/views/static_pages/_pool.html.erb | 2 +- .../_twoPoolFinalBracket.html.erb | 4 +- .../static_pages/_twoPoolSemiBracket.html.erb | 8 +-- test/integration/pool_advancement_test.rb | 30 +++++----- 11 files changed, 120 insertions(+), 70 deletions(-) diff --git a/app/models/match.rb b/app/models/match.rb index d1db5c2..cab7721 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -3,9 +3,31 @@ class Match < ActiveRecord::Base belongs_to :weight belongs_to :mat + after_save do + if self.finished == 1 + advance_wrestlers + end + end WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"] + def advance_wrestlers + if self.w1? && self.w2? + @w1 = Wrestler.find(self.w1) + @w2 = Wrestler.find(self.w2) + @w1.advanceInBracket + @w2.advanceInBracket + end + end + + def bracketScore + if self.finished != 1 + return "" + end + if self.finished == 1 + return "(#{self.score})" + end + end def w1_name if self.w1 @@ -38,7 +60,7 @@ class Match < ActiveRecord::Base end end def poolNumber - if self.w1 + if self.w1? Wrestler.find(self.w1).generatePoolNumber end end diff --git a/app/models/pool.rb b/app/models/pool.rb index f890040..f9bdd37 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -17,7 +17,7 @@ class Pool def roundRobin matches = [] - wrestlers = @weight.wrestlers_for_pool(@pool) + wrestlers = @weight.wrestlersForPool(@pool) poolMatches = RoundRobinTournament.schedule(wrestlers).reverse poolMatches.each_with_index do |b, index| round = index + 1 diff --git a/app/models/pooladvance.rb b/app/models/pooladvance.rb index 466025a..5fe3b5b 100644 --- a/app/models/pooladvance.rb +++ b/app/models/pooladvance.rb @@ -2,29 +2,17 @@ class Pooladvance attr_accessor :wrestler - def poolRank - #Give wrestlers pool points then give extra points for tie breakers and spit them out - #in order from first to last - #Calculate wrestlers pool points in their model - #Order wrestlers by pool in the weight model - end - def advanceWrestler - if self.wrestler.poolMatches.size > self.wrestler.finishedMatches.size - return nil + if self.wrestler.weight.allPoolMatchesFinished(self.wrestler.generatePoolNumber) && self.wrestler.finishedBracketMatches.size == 0 + poolToBracketAdvancment + end + if self.wrestler.finishedBracketMatches.size != 0 + bracketAdvancment end - poolToBracketAdvancment - bracketAdvancment end def poolToBracketAdvancment @pool = self.wrestler.generatePoolNumber - @poolWrestlers = self.wrestler.weight.wrestlers_for_pool(@pool) - @poolWrestlers.each do |w| - if w.finishedBracketMatches.size > 0 - return nil - end - end #Move to correct spot in bracket from pool #Pool criteria #Wins @@ -35,8 +23,7 @@ class Pooladvance #Coin flip #if not one pool if self.wrestler.weight.wrestlers.size > 6 - - @poolOrder = self.wrestler.weight.poolOrder + @poolOrder = self.wrestler.weight.poolOrder(@pool) #Take pool order and move winner and runner up to correct match based on w1_name and w2_name @matches = self.wrestler.weight.matches @winnerMatch = @matches.select{|m| m.loser1_name == "Winner Pool #{@pool}" || m.loser2_name == "Winner Pool #{@pool}"}.first @@ -49,9 +36,6 @@ class Pooladvance end def bracketAdvancment - if self.wrestler.finishedBracketMatches.size == 0 - return nil - end #Move to next correct spot in bracket @matches = self.wrestler.finishedMatches.sort_by{|m| m.round}.reverse @last_match = @matches.first @@ -64,12 +48,35 @@ end end def winnerAdvance(last_match) - + #Quarter to Semis + #Semis to 1/2 + #Conso Semis to 5/6 + @pos = last_match.bracket_position_number + @new_pos = (@pos/2.0) + if last_match.bracket_position == "Quarter" + @new_match = Match.where("bracket_position = ? AND bracket_position_number = ?","Semis",@new_pos.ceil).first + end + if last_match.bracket_position == "Semis" + @new_match = Match.where("bracket_position = ? AND bracket_position_number = ?","1/2",@new_pos.ceil).first + end + if last_match.bracket_position == "Conso Semis" + @new_match = Match.where("bracket_position = ? AND bracket_position_number = ?","5/6",@new_pos.ceil).first + end + if @new_match + if @new_pos == @new_pos.ceil + @new_match.w2 = self.wrestler.id + @new_match.save + end + if @new_pos != @new_pos.ceil + @new_match.w1 = self.wrestler.id + @new_match.save + end + end end def loserAdvance(last_match) @bout = last_match.bout_number - @next_match = Match.where("loser_name = ?","Loser of #{@bout}") + @next_match = Match.where("loser1_name = ? or loser2_name = ?","Loser of #{@bout}","Loser of #{@bout}") if @next_match.size > 0 @next_match.first.replaceLoserNameWithWrestler(self.wrestler,"Loser of #{@bout}") end diff --git a/app/models/weight.rb b/app/models/weight.rb index 0a0ff00..22963ee 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -11,8 +11,18 @@ class Weight < ActiveRecord::Base self.tournament.destroyAllMatches end - def wrestlers_for_pool(pool) - wrestlers.select{|w| w.generatePoolNumber == pool}.to_a + def wrestlersForPool(pool) + self.wrestlers.select{|w| w.generatePoolNumber == pool} + end + + def allPoolMatchesFinished(pool) + @wrestlers = wrestlersForPool(pool) + @wrestlers.each do |w| + if w.poolMatches.size != w.finishedPoolMatches.size + return false + end + end + return true end def pools @@ -118,19 +128,19 @@ class Weight < ActiveRecord::Base def totalRounds(matches) @matchups = matches.select{|m| m.weight_id == self.id} @lastRound = matches.sort_by{|m| m.round}.last.round - @count = 0 + count = 0 @round =1 until @round > @lastRound do if @matchups.select{|m| m.round == @round} - @count = @count + 1 + count = count + 1 end @round = @round + 1 end - return @count + return count end - def poolOrder - @wrestlers = self.wrestlers - @wrestlers.sort_by{|w| w.poolWins.count}.reverse! + def poolOrder(pool) + @wrestlers = wrestlersForPool(pool) + @wrestlers.sort_by{|w| w.poolWins.size}.reverse! end end diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index c6fe43e..f0a5533 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -8,6 +8,19 @@ class Wrestler < ActiveRecord::Base self.tournament.destroyAllMatches end + def resultByBout(bout) + @match = Match.where("bout_number = ? AND finished = ?",bout,1) + if @match.size == 0 + return "" + end + if @match.first.winner_id == self.id + return "W #{@match.first.score}" + end + if @match.first.winner_id != self.id + return "L #{@match.first.score}" + end + end + def isWrestlingThisRound(matchRound) if allMatches.blank? diff --git a/app/views/static_pages/_fourPoolQuarterBracket.html.erb b/app/views/static_pages/_fourPoolQuarterBracket.html.erb index 9a629f5..c686184 100644 --- a/app/views/static_pages/_fourPoolQuarterBracket.html.erb +++ b/app/views/static_pages/_fourPoolQuarterBracket.html.erb @@ -66,7 +66,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -78,7 +78,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -90,7 +90,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -115,7 +115,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -140,7 +140,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -152,7 +152,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -177,7 +177,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • diff --git a/app/views/static_pages/_fourPoolSemiBracket.html.erb b/app/views/static_pages/_fourPoolSemiBracket.html.erb index c6a71de..a8e5294 100644 --- a/app/views/static_pages/_fourPoolSemiBracket.html.erb +++ b/app/views/static_pages/_fourPoolSemiBracket.html.erb @@ -66,7 +66,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -78,7 +78,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -104,7 +104,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -131,7 +131,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -143,7 +143,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -168,7 +168,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • diff --git a/app/views/static_pages/_pool.html.erb b/app/views/static_pages/_pool.html.erb index d574f88..d4a885f 100644 --- a/app/views/static_pages/_pool.html.erb +++ b/app/views/static_pages/_pool.html.erb @@ -21,7 +21,7 @@ <% @round = 1 %> <% until @matches.select{|m| m.round == @round}.blank? %> <% if @round <= @pools %> - <%= w.boutByRound(@round) %>
    Result + <%= w.boutByRound(@round) %>
    <%= w.resultByBout(w.boutByRound(@round)) %> <% end %> <% @round = @round + 1 %> <% end %> diff --git a/app/views/static_pages/_twoPoolFinalBracket.html.erb b/app/views/static_pages/_twoPoolFinalBracket.html.erb index 2d5d0ab..d2d3c5c 100644 --- a/app/views/static_pages/_twoPoolFinalBracket.html.erb +++ b/app/views/static_pages/_twoPoolFinalBracket.html.erb @@ -66,7 +66,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -90,7 +90,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • diff --git a/app/views/static_pages/_twoPoolSemiBracket.html.erb b/app/views/static_pages/_twoPoolSemiBracket.html.erb index 4b839ef..28a6613 100644 --- a/app/views/static_pages/_twoPoolSemiBracket.html.erb +++ b/app/views/static_pages/_twoPoolSemiBracket.html.erb @@ -66,7 +66,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -78,7 +78,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -104,7 +104,7 @@ li.game{
  •  
  • <%= match.w1_name %> Score
  • -
  • <%= match.bout_number %>  
  • +
  • <%= match.bout_number %> <%= match.bracketScore %> 
  • <%= match.w2_name %>Score
  •  
  • @@ -120,4 +120,4 @@ li.game{
  •  
  • - \ No newline at end of file + diff --git a/test/integration/pool_advancement_test.rb b/test/integration/pool_advancement_test.rb index 828c811..eb82583 100644 --- a/test/integration/pool_advancement_test.rb +++ b/test/integration/pool_advancement_test.rb @@ -8,28 +8,25 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest def setup @tournament = Tournament.find(1) @tournament.generateMatchups - @matches = @tournament.matches.select{|m| m.weight_id == 3 && m.bracket_position == "Pool"} nineManBracketPoolOne - @wrestlers = Wrestler.where("weight_id = ?", 3) - @wrestlers.each do |w| - w.advanceInBracket - end end def nineManBracketPoolOne - endMatch(1002,"Guy8") - endMatch(1003,"Guy10") - endMatch(2002,"Guy2") - endMatch(2003,"Guy8") - endMatch(3003,"Guy2") - endMatch(4002,"Guy8") - endMatch(4003,"Guy2") - endMatch(5002,"Guy2") - endMatch(5003,"Guy5") + @matches = @tournament.matches.select{|m| m.weight_id == 3 && m.bracket_position == "Pool"} + endMatch(1002,"Guy8",@matches) + endMatch(1003,"Guy10",@matches) + endMatch(2002,"Guy2",@matches) + endMatch(2003,"Guy8",@matches) + endMatch(3003,"Guy2",@matches) + endMatch(4002,"Guy8",@matches) + endMatch(4003,"Guy2",@matches) + endMatch(5002,"Guy2",@matches) + endMatch(5003,"Guy5",@matches) + endMatch(3002,"Guy5",@matches) end - def endMatch(bout,winner) - @match = @matches.select{|m| m.bout_number == bout}.first + def endMatch(bout,winner,matches) + @match = matches.select{|m| m.bout_number == bout}.first @match.finished = 1 @match.winner_id = translateNameToId(winner) @match.win_type = "Decision" @@ -41,6 +38,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest test "nine man outright finals advance" do @wrestler = Wrestler.where("name = ?", "Guy2").first + @match = Match.where("bout_number = ?",6000).first assert_equal 6000, @wrestler.boutByRound(6) end