From 087c383996e4f3ba9bece151ede854cf9a56054a Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Tue, 13 Oct 2015 20:57:35 -0400 Subject: [PATCH] Continuing pool bracket movements --- app/models/match.rb | 5 ++ .../{pool_advance.rb => pooladvance.rb} | 25 ++++++++- app/models/weight.rb | 2 +- app/models/wrestler.rb | 11 +++- app/views/static_pages/_pool.html.erb | 4 +- test/integration/pool_advancement_test.rb | 53 +++++++++++++++++++ test/integration/poolbracket_matchups_test.rb | 2 +- 7 files changed, 94 insertions(+), 8 deletions(-) rename app/models/{pool_advance.rb => pooladvance.rb} (71%) create mode 100644 test/integration/pool_advancement_test.rb diff --git a/app/models/match.rb b/app/models/match.rb index 02708e9..d1db5c2 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -37,4 +37,9 @@ class Match < ActiveRecord::Base self.save end end + def poolNumber + if self.w1 + Wrestler.find(self.w1).generatePoolNumber + end + end end diff --git a/app/models/pool_advance.rb b/app/models/pooladvance.rb similarity index 71% rename from app/models/pool_advance.rb rename to app/models/pooladvance.rb index 7758c8b..466025a 100644 --- a/app/models/pool_advance.rb +++ b/app/models/pooladvance.rb @@ -11,7 +11,7 @@ class Pooladvance def advanceWrestler if self.wrestler.poolMatches.size > self.wrestler.finishedMatches.size - exit + return nil end poolToBracketAdvancment bracketAdvancment @@ -22,7 +22,7 @@ class Pooladvance @poolWrestlers = self.wrestler.weight.wrestlers_for_pool(@pool) @poolWrestlers.each do |w| if w.finishedBracketMatches.size > 0 - exit + return nil end end #Move to correct spot in bracket from pool @@ -49,8 +49,29 @@ 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 + if @last_match.winner_id == self.wrestler.id + winnerAdvance(@last_match) + end + if @last_match.winner_id != self.wrestler.id + loserAdvance(@last_match) + end end + def winnerAdvance(last_match) + end + + def loserAdvance(last_match) + @bout = last_match.bout_number + @next_match = Match.where("loser_name = ?","Loser of #{@bout}") + if @next_match.size > 0 + @next_match.first.replaceLoserNameWithWrestler(self.wrestler,"Loser of #{@bout}") + end + end end diff --git a/app/models/weight.rb b/app/models/weight.rb index 9d6dfa1..0a0ff00 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -131,6 +131,6 @@ class Weight < ActiveRecord::Base def poolOrder @wrestlers = self.wrestlers - @wrestlers.sort_by{|w| w.poolWins}.reverse + @wrestlers.sort_by{|w| w.poolWins.count}.reverse! end end diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index 8b1efc4..c6fe43e 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -21,7 +21,7 @@ class Wrestler < ActiveRecord::Base @pool = self.weight.returnPoolNumber(self) end - def boutByRound(round,matches) + def boutByRound(round) @match = allMatches.select{|m| m.round == round}.first if @match.blank? return "BYE" @@ -36,7 +36,8 @@ class Wrestler < ActiveRecord::Base end def poolMatches - allMatches.select{|m| m.bracket_position == "Pool"} + @poolMatches = allMatches.select{|m| m.bracket_position == "Pool"} + @poolMatches.select{|m| m.poolNumber == self.generatePoolNumber} end def finishedMatches @@ -67,4 +68,10 @@ class Wrestler < ActiveRecord::Base return 0 end end + + def advanceInBracket + @advance = Pooladvance.new + @advance.wrestler = self + @advance.advanceWrestler + end end diff --git a/app/views/static_pages/_pool.html.erb b/app/views/static_pages/_pool.html.erb index 209239f..d574f88 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,@matches) %>
Result + <%= w.boutByRound(@round) %>
Result <% end %> <% @round = @round + 1 %> <% end %> @@ -32,4 +32,4 @@ <% @pool = @pool + 1 %> <% @round = 1 %> -<% end %> \ No newline at end of file +<% end %> diff --git a/test/integration/pool_advancement_test.rb b/test/integration/pool_advancement_test.rb new file mode 100644 index 0000000..828c811 --- /dev/null +++ b/test/integration/pool_advancement_test.rb @@ -0,0 +1,53 @@ +require 'test_helper' + +class PoolAdvancementTest < ActionDispatch::IntegrationTest + test "the truth" do + assert true + end + + 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") + end + + def endMatch(bout,winner) + @match = @matches.select{|m| m.bout_number == bout}.first + @match.finished = 1 + @match.winner_id = translateNameToId(winner) + @match.win_type = "Decision" + @match.save + end + def translateNameToId(wrestler) + Wrestler.where("name = ?", wrestler).first.id + end + + test "nine man outright finals advance" do + @wrestler = Wrestler.where("name = ?", "Guy2").first + assert_equal 6000, @wrestler.boutByRound(6) + end + + test "nine man outright conso finals advance" do + @wrestler = Wrestler.where("name = ?", "Guy8").first + assert_equal 6001, @wrestler.boutByRound(6) + end + + +end diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index c1d51a4..81f9a65 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -75,7 +75,7 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest round = 1 if w.totalRounds(matchups) > 5 until round > w.poolRounds(matchups) do - if wr.boutByRound(round, matchups) == "BYE" + if wr.boutByRound(round) == "BYE" message = "BYE" end round += 1