1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-08 15:29:20 +00:00

Merge pull request #4 from rjo1970/master

Getting rid of the matches collection
This commit is contained in:
2015-05-28 07:39:19 -04:00
13 changed files with 232 additions and 272 deletions

View File

@@ -8,10 +8,9 @@ class StaticPagesController < ApplicationController
@tournament = Tournament.find(params[:tournament]) @tournament = Tournament.find(params[:tournament])
end end
if @tournament if @tournament
if @tournament.matches.empty? @matches = @tournament.matches
if @matches.empty?
redirect_to "/static_pages/noMatches?tournament=#{@tournament.id}" redirect_to "/static_pages/noMatches?tournament=#{@tournament.id}"
else
@matches = @tournament.upcomingMatches
end end
end end
end end
@@ -30,12 +29,12 @@ class StaticPagesController < ApplicationController
if params[:tournament] if params[:tournament]
@tournament = Tournament.find(params[:tournament]) @tournament = Tournament.find(params[:tournament])
end end
if @tournament if @tournament
@matches = Match.where(tournament_id: @tournament.id) @matches = @tournament.matches
end end
@matches = @matches.where(finished: 1) @matches = @matches.where(finished: 1)
end end
def brackets def brackets
if params[:weight] if params[:weight]
@weight = Weight.find(params[:weight]) @weight = Weight.find(params[:weight])

View File

@@ -1,27 +0,0 @@
class Boutgen
def matchesByRound(round, matches)
@matches = matches.select {|m| m.round == round}
return @matches
end
def giveBout(matches)
@matches = matches.sort_by{|x|[x.weight_max]}
@matches.each_with_index do |m, i|
@bout = m.round * 1000 + i
m.bout_number = @bout
end
return @matches
end
def assignBouts(matches,weights)
@round = 1
until matchesByRound(@round, matches).blank? do
@matches = matchesByRound(@round, matches)
giveBout(@matches)
@round += 1
end
return matches
end
end

View File

@@ -0,0 +1,57 @@
module GeneratesLoserNames
def assignLoserNames
matches_by_weight = nil
weights.each do |w|
matches_by_weight = matches.where(weight_id: w.id)
if w.pool_bracket_type == "twoPoolsToSemi"
twoPoolsToSemiLoser(matches_by_weight)
elsif w.pool_bracket_type == "fourPoolsToQuarter"
fourPoolsToQuarterLoser(matches_by_weight)
elsif w.pool_bracket_type == "fourPoolsToSemi"
fourPoolsToSemiLoser(matches_by_weight)
end
end
return matches_by_weight
end
def twoPoolsToSemiLoser(matches_by_weight)
match1 = matches_by_weight.select{|m| m.loser1_name == "Winner Pool 1"}.first
match2 = matches_by_weight.select{|m| m.loser1_name == "Winner Pool 2"}.first
matchChange = matches_by_weight.select{|m| m.bracket_position == "3/4"}.first
matchChange.loser1_name = "Loser of #{match1.bout_number}"
matchChange.loser2_name = "Loser of #{match2.bout_number}"
end
def fourPoolsToQuarterLoser(matches_by_weight)
quarters = matches_by_weight.select{|m| m.bracket_position == "Quarter"}
consoSemis = matches_by_weight.select{|m| m.bracket_position == "Conso Semis"}
semis = matches_by_weight.select{|m| m.bracket_position == "Semis"}
thirdFourth = matches_by_weight.select{|m| m.bracket_position == "3/4"}.first
seventhEighth = matches_by_weight.select{|m| m.bracket_position == "7/8"}.first
consoSemis.each do |m|
if m.bracket_position_number == 1
m.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 1}.first.bout_number}"
m.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 2}.first.bout_number}"
elsif m.bracket_position_number == 2
m.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 3}.first.bout_number}"
m.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 4}.first.bout_number}"
end
end
thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
consoSemis = matches_by_weight.select{|m| m.bracket_position == "Conso Semis"}
seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
end
def fourPoolsToSemiLoser(matches_by_weight)
semis = matches_by_weight.select{|m| m.bracket_position == "Semis"}
thirdFourth = matches_by_weight.select{|m| m.bracket_position == "3/4"}.first
consoSemis = matches_by_weight.select{|m| m.bracket_position == "Conso Semis"}
seventhEighth = matches_by_weight.select{|m| m.bracket_position == "7/8"}.first
thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
end
end

View File

@@ -0,0 +1,28 @@
module GeneratesTournamentMatches
def generateMatchups
poolToBracket() if tournament_type == "Pool to bracket"
matches
end
def poolToBracket
destroyAllMatches
buildTournamentWeights
generateMatches
end
def buildTournamentWeights
weights.order(:max).each do |weight|
Pool.new(weight).generatePools()
last_match = matches.where(weight: weight).order(round: :desc).limit(1).first
highest_round = last_match.round
Poolbracket.new(weight, highest_round).generateBracketMatches()
end
end
def generateMatches
assignBouts
assignLoserNames
end
end

View File

@@ -1,56 +0,0 @@
class Losernamegen
def assignLoserNames(matches,weights)
weights.each do |w|
@matches = matches.select{|m| m.weight_id == w.id}
if w.pool_bracket_type == "twoPoolsToSemi"
twoPoolsToSemiLoser(@matches)
elsif w.pool_bracket_type == "fourPoolsToQuarter"
fourPoolsToQuarterLoser(@matches)
elsif w.pool_bracket_type == "fourPoolsToSemi"
fourPoolsToSemiLoser(@matches)
end
end
return matches
end
def twoPoolsToSemiLoser(matches)
@match1 = matches.select{|m| m.loser1_name == "Winner Pool 1"}.first
@match2 = matches.select{|m| m.loser1_name == "Winner Pool 2"}.first
@matchChange = matches.select{|m| m.bracket_position == "3/4"}.first
@matchChange.loser1_name = "Loser of #{@match1.bout_number}"
@matchChange.loser2_name = "Loser of #{@match2.bout_number}"
end
def fourPoolsToQuarterLoser(matches)
@quarters = matches.select{|m| m.bracket_position == "Quarter"}
@consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"}
@semis = matches.select{|m| m.bracket_position == "Semis"}
@thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first
@seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first
@consoSemis.each do |match|
if match.bracket_position_number == 1
match.loser1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 1}.first.bout_number}"
match.loser2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 2}.first.bout_number}"
elsif match.bracket_position_number == 2
match.loser1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 3}.first.bout_number}"
match.loser2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 4}.first.bout_number}"
end
end
@thirdFourth.loser1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
@thirdFourth.loser2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
@consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"}
@seventhEighth.loser1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
@seventhEighth.loser2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
end
def fourPoolsToSemiLoser(matches)
@semis = matches.select{|m| m.bracket_position == "Semis"}
@thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first
@consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"}
@seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first
@thirdFourth.loser1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
@thirdFourth.loser2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
@seventhEighth.loser1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}"
@seventhEighth.loser2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
end
end

View File

@@ -1,5 +1,8 @@
class Match < ActiveRecord::Base class Match < ActiveRecord::Base
belongs_to :tournament belongs_to :tournament
belongs_to :weight
WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"] WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"]

View File

@@ -1,6 +1,7 @@
class Pool class Pool
def initialize(weight) def initialize(weight)
@weight = weight @weight = weight
@tournament = @weight.tournament
@pool = 1 @pool = 1
end end
@@ -23,7 +24,7 @@ class Pool
bouts = b.map bouts = b.map
bouts.each do |bout| bouts.each do |bout|
if bout[0] != nil and bout[1] != nil if bout[0] != nil and bout[1] != nil
match = Match.new( match = @tournament.matches.create(
w1: bout[0].id, w1: bout[0].id,
w2: bout[1].id, w2: bout[1].id,
weight_id: @weight.id, weight_id: @weight.id,

View File

@@ -1,80 +1,80 @@
class Poolbracket class Poolbracket
def generateBracketMatches(matches,weight,highest_round) def initialize(weight, highest_round)
if weight.pool_bracket_type == "twoPoolsToSemi" @weight = weight
matches = twoPoolsToSemi(matches,weight,highest_round) @tournament = @weight.tournament
elsif weight.pool_bracket_type == "twoPoolsToFinal" @pool_bracket_type = @weight.pool_bracket_type
matches = twoPoolsToFinal(matches,weight,highest_round) @round = highest_round + 1
elsif weight.pool_bracket_type == "fourPoolsToQuarter" end
matches = fourPoolsToQuarter(matches,weight,highest_round)
elsif weight.pool_bracket_type == "fourPoolsToSemi"
matches = fourPoolsToSemi(matches,weight,highest_round)
end
return matches
end
def twoPoolsToSemi(matches,weight,round) def next_round
@round = round + 1 @round += 1
matches = createMatchup(matches,weight,@round,"Winner Pool 1","Runner Up Pool 2","Semis",1) end
matches = createMatchup(matches,weight,@round,"Winner Pool 2","Runner Up Pool 1","Semis",2)
@round = @round + 1
@matches = matches.select{|m| m.weight_id == weight.id}
matches = createMatchup(matches,weight,@round,"","","1/2",1)
matches = createMatchup(matches,weight,@round,"","","3/4",1)
return matches
end
def twoPoolsToFinal(matches,weight,round) def generateBracketMatches()
@round = round + 1 if @pool_bracket_type == "twoPoolsToSemi"
matches = createMatchup(matches,weight,@round,"Winner Pool 1","Winner Pool 2","1/2",1) return twoPoolsToSemi()
matches = createMatchup(matches,weight,@round,"Runner Up Pool 1","Runner Up Pool 2","3/4",1) elsif @pool_bracket_type == "twoPoolsToFinal"
return matches return twoPoolsToFinal()
elsif @pool_bracket_type == "fourPoolsToQuarter"
return fourPoolsToQuarter()
elsif @pool_bracket_type == "fourPoolsToSemi"
return fourPoolsToSemi()
end end
return []
end
def fourPoolsToQuarter(matches,weight,round) def twoPoolsToSemi()
@round = round + 1 createMatchup("Winner Pool 1", "Runner Up Pool 2", "Semis", 1)
matches = createMatchup(matches,weight,@round,"Winner Pool 1","Runner Up Pool 2","Quarter",1) createMatchup("Winner Pool 2", "Runner Up Pool 1", "Semis", 2)
matches = createMatchup(matches,weight,@round,"Winner Pool 4","Runner Up Pool 3","Quarter",2) next_round
matches = createMatchup(matches,weight,@round,"Winner Pool 2","Runner Up Pool 1","Quarter",3) createMatchup("","","1/2",1)
matches = createMatchup(matches,weight,@round,"Winner Pool 3","Runner Up Pool 4","Quarter",4) createMatchup("","","3/4",1)
@round = @round + 1 end
matches = createMatchup(matches,weight,@round,"","","Semis",1)
matches = createMatchup(matches,weight,@round,"","","Semis",2)
matches = createMatchup(matches,weight,@round,"","","Conso Semis",1)
matches = createMatchup(matches,weight,@round,"","","Conso Semis",2)
@round = @round + 1
matches = createMatchup(matches,weight,@round,"","","1/2",1)
matches = createMatchup(matches,weight,@round,"","","3/4",1)
matches = createMatchup(matches,weight,@round,"","","5/6",1)
matches = createMatchup(matches,weight,@round,"","","7/8",1)
return matches
end
def fourPoolsToSemi(matches,weight,round) def twoPoolsToFinal()
@round = round + 1 createMatchup("Winner Pool 1", "Winner Pool 2", "1/2", 1)
matches = createMatchup(matches,weight,@round,"Winner Pool 1","Winner Pool 4","Semis",1) createMatchup("Runner Up Pool 1", "Runner Up Pool 2", "3/4", 1)
matches = createMatchup(matches,weight,@round,"Winner Pool 2","Winner Pool 3","Semis",2) end
matches = createMatchup(matches,weight,@round,"Runner Up Pool 1","Runner Up Pool 4","Conso Semis",1)
matches = createMatchup(matches,weight,@round,"Runner Up Pool 2","Runner Up Pool 3","Conso Semis",2)
@round = @round + 1
matches = createMatchup(matches,weight,@round,"","","1/2",1)
matches = createMatchup(matches,weight,@round,"","","3/4",1)
matches = createMatchup(matches,weight,@round,"","","5/6",1)
matches = createMatchup(matches,weight,@round,"","","7/8",1)
return matches
end
def createMatchup(matches,weight,round,w1_name,w2_name,bracket_position,bracket_position_number) def fourPoolsToQuarter()
@match = Match.new createMatchup("Winner Pool 1", "Runner Up Pool 2", "Quarter", 1)
@match.loser1_name = w1_name createMatchup("Winner Pool 4", "Runner Up Pool 3", "Quarter", 2)
@match.loser2_name = w2_name createMatchup("Winner Pool 2", "Runner Up Pool 1", "Quarter", 3)
@match.weight_id = weight.id createMatchup("Winner Pool 3", "Runner Up Pool 4", "Quarter", 4)
@match.round = round next_round
@match.bracket_position = bracket_position createMatchup("", "", "Semis", 1)
@match.bracket_position_number = bracket_position_number createMatchup("", "", "Semis", 2)
matches << @match createMatchup("", "", "Conso Semis", 1)
return matches createMatchup("", "", "Conso Semis", 2)
end next_round
createMatchup("", "", "1/2", 1)
createMatchup("", "", "3/4", 1)
createMatchup("", "", "5/6", 1)
createMatchup("", "", "7/8", 1)
end
def fourPoolsToSemi()
createMatchup("Winner Pool 1", "Winner Pool 4", "Semis", 1)
createMatchup("Winner Pool 2", "Winner Pool 3", "Semis", 2)
createMatchup("Runner Up Pool 1", "Runner Up Pool 4", "Conso Semis", 1)
createMatchup("Runner Up Pool 2", "Runner Up Pool 3", "Conso Semis", 2)
next_round
createMatchup("", "", "1/2", 1)
createMatchup("", "", "3/4", 1)
createMatchup("", "", "5/6", 1)
createMatchup("", "", "7/8", 1)
end
def createMatchup(w1_name, w2_name, bracket_position, bracket_position_number)
@tournament.matches.create(
loser1_name: w1_name,
loser2_name: w2_name,
weight_id: @weight.id,
round: @round,
bracket_position: bracket_position,
bracket_position_number: bracket_position_number
)
end
end end

View File

@@ -1,45 +1,44 @@
class Tournament < ActiveRecord::Base class Tournament < ActiveRecord::Base
include GeneratesLoserNames
include GeneratesTournamentMatches
has_many :schools, dependent: :destroy has_many :schools, dependent: :destroy
has_many :weights, dependent: :destroy has_many :weights, dependent: :destroy
has_many :mats, dependent: :destroy has_many :mats, dependent: :destroy
has_many :wrestlers, through: :weights has_many :wrestlers, through: :weights
has_many :matches, dependent: :destroy
def tournament_types def tournament_types
["Pool to bracket"] ["Pool to bracket"]
end end
def matches def createCustomWeights(value)
@matches = Match.where(tournament_id: self.id) weights.destroy_all
end
def createCustomWeights(value)
self.weights.destroy_all
if value == 'hs' if value == 'hs'
@weights = [106,113,120,132,138,145,152,160,170,182,195,220,285] Weight::HS_WEIGHT_CLASSES.each do |w|
end weights.create(max: w)
@weights.each do |w| end
newWeight = Weight.new
newWeight.max = w
newWeight.tournament_id = self.id
newWeight.save
end
end
def upcomingMatches
if matches.nil?
return nil
else else
matches raise "Unspecified behavior"
end end
end end
def generateMatchups
@matches = Tournamentmatchgen.new(self).genMatches()
end
def destroyAllMatches def destroyAllMatches
matches.destroy_all matches.destroy_all
end end
def matchesByRound(round)
matches.joins(:weight).where(round: round).order("weights.max")
end
def assignBouts
bout_counts = Hash.new(0)
matches.each do |m|
m.bout_number = m.round * 1000 + bout_counts[m.round]
bout_counts[m.round] += 1
m.save!
end
end
end end

View File

@@ -1,51 +0,0 @@
class Tournamentmatchgen
def initialize(tournament)
@tournament = tournament
@matches = @tournament.matches
end
def genMatches
if @tournament.tournament_type == "Pool to bracket"
@matches = poolToBracket()
end
@matches
end
def poolToBracket
destroyMatches
buildTournamentWeights
generateMatches
saveMatches
@matches
end
def destroyMatches
@tournament.destroyAllMatches
@matches = []
end
def buildTournamentWeights
@tournament.weights.sort_by{|x|[x.max]}.each do |weight|
matches = Pool.new(weight).generatePools()
last_match = matches.sort_by{|m| m.round}.last
highest_round = last_match.round
@matches += Poolbracket.new.generateBracketMatches(matches, weight, highest_round)
end
end
def generateMatches
@matches =
Losernamegen.new.assignLoserNames(
Boutgen.new.assignBouts(@matches, @tournament.weights),
@tournament.weights)
end
def saveMatches
@matches.each do |m|
m.tournament_id = @tournament.id
m.save
end
end
end

View File

@@ -1,9 +1,12 @@
class Weight < ActiveRecord::Base class Weight < ActiveRecord::Base
belongs_to :tournament belongs_to :tournament
has_many :wrestlers, dependent: :destroy has_many :wrestlers, dependent: :destroy
has_many :matches, dependent: :destroy
attr_accessor :pools attr_accessor :pools
HS_WEIGHT_CLASSES = [106,113,120,132,138,145,152,160,170,182,195,220,285]
before_save do before_save do
self.tournament.destroyAllMatches self.tournament.destroyAllMatches
end end

View File

@@ -26,7 +26,7 @@
<% @tournament.weights.sort_by{|w| w.max}.each do |w| %> <% @tournament.weights.sort_by{|w| w.max}.each do |w| %>
<div class="pagebreak"> <div class="pagebreak">
<% @weight = w %> <% @weight = w %>
<% @matches = @tournament.upcomingMatches.select{|m| m.weight_id == @weight.id} %> <% @matches = @tournament.matches.select{|m| m.weight_id == @weight.id} %>
<% @wrestlers = Wrestler.where(weight_id: @weight.id) %> <% @wrestlers = Wrestler.where(weight_id: @weight.id) %>
<% @pools = w.poolRounds(@matches) %> <% @pools = w.poolRounds(@matches) %>
<h5><%= @weight.max %> lbs Bracket</h5> <h5><%= @weight.max %> lbs Bracket</h5>

View File

@@ -3,7 +3,7 @@ require 'test_helper'
class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
def setup def setup
@tournament = Tournament.find(1) @tournament = Tournament.find(1)
@genMatchups = @tournament.upcomingMatches @genMatchups = @tournament.generateMatchups
end end
def createTournament(numberOfWrestlers) def createTournament(numberOfWrestlers)
@@ -39,7 +39,8 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
def create_weight def create_weight
Weight.new( Weight.new(
id: @id, id: @id,
tournament_id: @id tournament_id: @id,
max: @id
).save! ).save!
end end
@@ -58,7 +59,7 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
end end
def checkForByeInPool(tournament) def checkForByeInPool(tournament)
tournament.upcomingMatches tournament.generateMatchups
matchups = tournament.matches matchups = tournament.matches
tournament.weights.each do |w| tournament.weights.each do |w|
w.wrestlers.each do |wr| w.wrestlers.each do |wr|
@@ -81,15 +82,21 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
refute_nil @tournament refute_nil @tournament
end end
test "tests bout_number matches round" do test "tournament can be set to high school weight classes" do
@matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first @tournament.weights.destroy_all
assert_equal 4, @matchup_to_test.round @tournament.createCustomWeights("hs")
assert_equal Weight::HS_WEIGHT_CLASSES.size, @tournament.weights.size
end
test "tests bout numbers correspond to round" do
matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first
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.order(:max).limit(1).first
@matchup = @genMatchups.select{|m| m.bout_number == 1000}.first matchup = @tournament.matches.where(bout_number: 1000).limit(1).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
@@ -117,13 +124,10 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
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 (5...16).each do |count|
until @count > 16 do tourney = createTournament(count)
@tournament2 = createTournament(@count) checkForByeInPool(tourney)
checkForByeInPool(@tournament2)
@count = @count + 1
end end
end end