From 328898f1e830d984aa99109ad5d437be7598d124 Mon Sep 17 00:00:00 2001 From: jcwimer Date: Mon, 16 Nov 2015 16:59:08 +0000 Subject: [PATCH] Tie breaker for team score --- app/models/poolorder.rb | 14 ++++++ app/models/school.rb | 57 ++++------------------- app/models/wrestler.rb | 27 ++++++++++- test/integration/pool_advancement_test.rb | 34 ++++++++++++++ 4 files changed, 83 insertions(+), 49 deletions(-) diff --git a/app/models/poolorder.rb b/app/models/poolorder.rb index 4e88390..0bb767c 100644 --- a/app/models/poolorder.rb +++ b/app/models/poolorder.rb @@ -45,6 +45,7 @@ class Poolorder ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { headToHead } end ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { deductedPoints } + ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { teamPoints } ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { coinFlip } end @@ -83,6 +84,19 @@ class Poolorder end end + def teamPoints + pointsArray = [] + wrestlersWithSamePoints.each do |w| + pointsArray << w.totalTeamPoints + end + mostPoints = pointsArray.max + wrestlersWithLeastDeductedPoints = wrestlersWithSamePoints.select{|w| w.totalTeamPoints == mostPoints} + addPointsToWrestlersAhead(wrestlersWithLeastDeductedPoints.first) + wrestlersWithLeastDeductedPoints.each do |wr| + addPoints(wr) + end + end + def coinFlip wrestler = wrestlersWithSamePoints.sample addPointsToWrestlersAhead(wrestler) diff --git a/app/models/school.rb b/app/models/school.rb index 9d0455f..d9f2cdc 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -6,14 +6,21 @@ class School < ActiveRecord::Base #calculate score here def score - #Add score per wrestler. Calculate score in wrestler model. - return 0 + calcScore end def calcScore - #calc and save score + totalWrestlerPoints - totalDeductedPoints end + def totalWrestlerPoints + points = 0 + self.wrestlers.each do |w| + points = points + w.totalTeamPoints + end + points + end + def totalDeductedPoints points = 0 self.deductedPoints.each do |d| @@ -21,48 +28,4 @@ class School < ActiveRecord::Base end points end - - def poolWins - - end - - def pinDefaultDqWins - - end - - def techFallWins - - end - - def majorWins - - end - - def firstPlace - - end - - def secondPlace - - end - - def thirdPlace - - end - - def fourthPlace - - end - - def fifthPlace - - end - - def sixthPlace - - end - - def seventhPlace - - end end diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index 772301a..9e20482 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -14,6 +14,11 @@ class Wrestler < ActiveRecord::Base allMatches.select{|m| m.finished == 1}.sort_by{|m| m.bout_number}.last end + def totalTeamPoints + points = 0.0 + points = points + (poolWins.size * 1) + (pinWins.size * 2) + (techWins.size * 1.5) + (majorWins.size * 1) + end + def totalDeductedPoints points = 0 self.deductedPoints.each do |d| @@ -81,9 +86,27 @@ class Wrestler < ActiveRecord::Base def finishedPoolMatches finishedMatches.select{|m| m.bracket_position == "Pool"} end - def poolWins - allMatches.select{|m| m.winner_id == self.id && m.bracket_position == "Pool"} + + def matchesWon + allMatches.select{|m| m.winner_id == self.id} end + + def poolWins + matchesWon.select{|m| m.bracket_position == "Pool"} + end + + def pinWins + matchesWon.select{|m| m.win_type == "Pin" || m.win_type == "Forfeit" || m.win_type == "Injury Default" || m.win_type == "Default" || m.win_type == "DQ"} + end + + def techWins + matchesWon.select{|m| m.win_type == "Tech Fall" } + end + + def majorWins + matchesWon.select{|m| m.win_type == "Major" } + end + def seasonWinPercentage @win = self.season_win.to_f @loss = self.season_loss.to_f diff --git a/test/integration/pool_advancement_test.rb b/test/integration/pool_advancement_test.rb index bf4bcfe..3467501 100644 --- a/test/integration/pool_advancement_test.rb +++ b/test/integration/pool_advancement_test.rb @@ -67,6 +67,16 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest deduct.save end + def nineManBracketPoolTwoGuyThreeTeamPoints + matches = @tournament.matches.select{|m| m.weight_id == 3 && m.bracket_position == "Pool"} + endMatch(1004,"Guy9",matches) + endMatch(1005,"Guy7",matches) + endMatchWithMajor(2004,"Guy3",matches) + endMatch(2005,"Guy9",matches) + endMatch(3004,"Guy7",matches) + endMatch(3005,"Guy3",matches) + end + def endMatch(bout,winner,matches) match = matches.select{|m| m.bout_number == bout}.first match.finished = 1 @@ -77,6 +87,18 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.mat_id = mat.id match.save end + + def endMatchWithMajor(bout,winner,matches) + match = matches.select{|m| m.bout_number == bout}.first + match.finished = 1 + match.winner_id = translateNameToId(winner) + match.win_type = "Major" + #Need to manually assign mat_id because thise weight class is not currently assigned a mat + mat = @tournament.mats.first + match.mat_id = mat.id + match.save + end + def translateNameToId(wrestler) Wrestler.where("name = ?", wrestler).first.id end @@ -128,4 +150,16 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest wrestler = Wrestler.where("name = ?", "Guy9").first assert_equal 6001, wrestler.boutByRound(6) end + + test "nine man pool 2 teamPoints tie breaker finalist guy 3" do + wrestler = Wrestler.where("name = ?", "Guy3").first + nineManBracketPoolTwoGuyThreeTeamPoints + assert_equal 6000, wrestler.boutByRound(6) + end + + test "nine man conso finals teamPoints tie breaker guy 9" do + nineManBracketPoolTwoGuyThreeTeamPoints + wrestler = Wrestler.where("name = ?", "Guy9").first + assert_equal 6001, wrestler.boutByRound(6) + end end