1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-25 07:23:11 +00:00

Simplify construction a bit on some methods

This commit is contained in:
R.J. Osborne
2015-05-19 22:36:30 -04:00
parent 80dee9c234
commit 0e954c1789
3 changed files with 33 additions and 42 deletions

View File

@@ -3,7 +3,7 @@ class Pool
@pools = weight.pools @pools = weight.pools
@pool = 1 @pool = 1
while @pool <= @pools while @pool <= @pools
matches = roundRobin(weight.wrestlers, weight, tournament , matches) matches = roundRobin(weight.wrestlers, weight, tournament, matches)
@pool += 1 @pool += 1
end end
return matches return matches
@@ -12,19 +12,16 @@ class Pool
def roundRobin(wrestlers,weight,tournament,matches) def roundRobin(wrestlers,weight,tournament,matches)
@wrestlers = wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a @wrestlers = wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a
@poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse @poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse
@poolMatches.each_with_index do |b,index| @poolMatches.each_with_index do |b, index|
round = index + 1
@bout = b.map @bout = b.map
@bout.each do |bout| @bout.each do |bout|
if bout[0] != nil and bout[1] != nil if bout[0] != nil and bout[1] != nil
@match = Match.new @match = Match.new(w1: bout[0].id, w2: bout[1].id, weight_id: weight.id, round: round)
@match.w1 = bout[0].id
@match.w2 = bout[1].id
@match.weight_id = weight.id
@match.round = index + 1
matches << @match matches << @match
end end
end end
end end
return matches matches
end end
end end

View File

@@ -3,23 +3,22 @@ class Wrestler < ActiveRecord::Base
belongs_to :weight belongs_to :weight
has_one :tournament, through: :weight has_one :tournament, through: :weight
attr_accessor :poolNumber attr_accessor :poolNumber
before_save do before_save do
self.tournament.destroyAllMatches self.tournament.destroyAllMatches
end end
def isWrestlingThisRound(matchRound) def isWrestlingThisRound(matchRound)
if allMatches.blank? if allMatches.blank?
return false return false
else else
return true return true
end end
end end
def generatePoolNumber def generatePoolNumber
@pool = self.weight.returnPoolNumber(self) @pool = self.weight.returnPoolNumber(self)
return @pool
end end
def boutByRound(round,matches) def boutByRound(round,matches)
@@ -30,7 +29,7 @@ class Wrestler < ActiveRecord::Base
return @match.bout_number return @match.bout_number
end end
end end
def allMatches def allMatches
@matches = Match.where("w1 = ? or w2 = ?",self.id,self.id) @matches = Match.where("w1 = ? or w2 = ?",self.id,self.id)
return @matches return @matches
@@ -48,7 +47,7 @@ class Wrestler < ActiveRecord::Base
elsif self.season_win == 0 elsif self.season_win == 0
return 0 return 0
elsif self.season_win == nil or self.season_loss == nil elsif self.season_win == nil or self.season_loss == nil
return 0 return 0
end end
end end
end end

View File

@@ -5,9 +5,8 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
@tournament = Tournament.find(1) @tournament = Tournament.find(1)
@genMatchups = @tournament.upcomingMatches @genMatchups = @tournament.upcomingMatches
end end
def createTournament(numberOfWrestlers) def createTournament(numberOfWrestlers)
@count = 1
@id = 6000 + numberOfWrestlers @id = 6000 + numberOfWrestlers
@tournament3 = Tournament.new @tournament3 = Tournament.new
@tournament3.id = @id @tournament3.id = @id
@@ -26,21 +25,22 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
@weight3.id = @id @weight3.id = @id
@weight3.tournament_id = @id @weight3.tournament_id = @id
@weight3.save @weight3.save
until @count > numberOfWrestlers do numberOfWrestlers.times do |x|
@wrestler2 = Wrestler.new count = x + 1
@wrestler2.name = "Guy #{@count}" @wrestler2 = Wrestler.new(
@wrestler2.school_id = @id name: "Guy #{count}",
@wrestler2.weight_id = @id school_id: @id,
@wrestler2.original_seed = @count weight_id: @id,
@wrestler2.season_loss = 0 original_seed: count,
@wrestler2.season_win = 0 season_loss: 0,
@wrestler2.criteria = nil season_win: 0,
criteria: nil
)
@wrestler2.save @wrestler2.save
@count = @count + 1
end end
return @tournament3 return @tournament3
end end
def checkForByeInPool(tournament) def checkForByeInPool(tournament)
tournament.upcomingMatches tournament.upcomingMatches
@matchups = tournament.matches @matchups = tournament.matches
@@ -60,53 +60,48 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
end end
end end
end end
test "the truth" do
assert true
end
test "found tournament" do test "found tournament" do
refute_nil @tournament refute_nil @tournament
end end
test "tests bout_number matches round" do test "tests bout_number matches round" do
@matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first @matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first
assert_equal 4, @matchup_to_test.round assert_equal 4, @matchup_to_test.round
end end
test "tests bout_numbers are generated with smallest weight first regardless of id" do test "tests bout_numbers are generated with smallest weight first regardless of id" do
@weight = @tournament.weights.map.sort_by{|x|[x.max]}.first @weight = @tournament.weights.map.sort_by{|x|[x.max]}.first
@matchup = @genMatchups.select{|m| m.bout_number == 1000}.first @matchup = @genMatchups.select{|m| m.bout_number == 1000}.first
assert_equal @weight.max, @matchup.weight_max assert_equal @weight.max, @matchup.weight_max
end end
test "tests number of matches in 5 man one pool" do test "tests number of matches in 5 man one pool" do
@six_matches = @genMatchups.select{|m| m.weight_max == 106} @six_matches = @genMatchups.select{|m| m.weight_max == 106}
assert_equal 10, @six_matches.length assert_equal 10, @six_matches.length
end end
test "tests number of matches in 11 man pool bracket" do test "tests number of matches in 11 man pool bracket" do
@twentysix_matches = @genMatchups.select{|m| m.weight_max == 126} @twentysix_matches = @genMatchups.select{|m| m.weight_max == 126}
assert_equal 22, @twentysix_matches.length assert_equal 22, @twentysix_matches.length
end end
test "tests number of matches in 9 man pool bracket" do test "tests number of matches in 9 man pool bracket" do
@twentysix_matches = @genMatchups.select{|m| m.weight_max == 113} @twentysix_matches = @genMatchups.select{|m| m.weight_max == 113}
assert_equal 18, @twentysix_matches.length assert_equal 18, @twentysix_matches.length
end end
test "tests number of matches in 7 man pool bracket" do test "tests number of matches in 7 man pool bracket" do
@twentysix_matches = @genMatchups.select{|m| m.weight_max == 120} @twentysix_matches = @genMatchups.select{|m| m.weight_max == 120}
assert_equal 13, @twentysix_matches.length assert_equal 13, @twentysix_matches.length
end end
test "tests number of matches in 16 man pool bracket" do test "tests number of matches in 16 man pool bracket" do
@twentysix_matches = @genMatchups.select{|m| m.weight_max == 132} @twentysix_matches = @genMatchups.select{|m| m.weight_max == 132}
assert_equal 32, @twentysix_matches.length assert_equal 32, @twentysix_matches.length
end end
test "test if a wrestler can exceed five matches" do test "test if a wrestler can exceed five matches" do
@count = 5 @count = 5
until @count > 16 do until @count > 16 do
@@ -115,6 +110,6 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
@count = @count + 1 @count = @count + 1
end end
end end
#todo test crazy movements through each bracket? #todo test crazy movements through each bracket?
end end