mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
A Tournament can now generate its own matches.
This was again extracted as a module. Modules get more powerful as they get more generic. This is far from generic.
This commit is contained in:
28
app/models/generates_tournament_matches.rb
Normal file
28
app/models/generates_tournament_matches.rb
Normal 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
|
||||||
@@ -1,26 +1,23 @@
|
|||||||
class Tournament < ActiveRecord::Base
|
class Tournament < ActiveRecord::Base
|
||||||
|
|
||||||
include GeneratesLoserNames
|
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'
|
||||||
Weight::HS_WEIGHT_CLASSES.each do |w|
|
Weight::HS_WEIGHT_CLASSES.each do |w|
|
||||||
self.weights.create(max: w)
|
weights.create(max: w)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise "Unspecified behavior"
|
raise "Unspecified behavior"
|
||||||
@@ -28,15 +25,7 @@ class Tournament < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def upcomingMatches
|
def upcomingMatches
|
||||||
if matches.nil?
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
matches
|
matches
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def generateMatchups
|
|
||||||
@matches = Tournamentmatchgen.new(self).genMatches()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroyAllMatches
|
def destroyAllMatches
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
class Tournamentmatchgen
|
|
||||||
|
|
||||||
def initialize(tournament)
|
|
||||||
@tournament = tournament
|
|
||||||
end
|
|
||||||
|
|
||||||
def genMatches
|
|
||||||
if @tournament.tournament_type == "Pool to bracket"
|
|
||||||
poolToBracket()
|
|
||||||
end
|
|
||||||
@tournament.matches
|
|
||||||
end
|
|
||||||
|
|
||||||
def poolToBracket
|
|
||||||
destroyMatches
|
|
||||||
buildTournamentWeights
|
|
||||||
generateMatches
|
|
||||||
end
|
|
||||||
|
|
||||||
def destroyMatches
|
|
||||||
@tournament.destroyAllMatches
|
|
||||||
end
|
|
||||||
|
|
||||||
def buildTournamentWeights
|
|
||||||
@tournament.weights.order(:max).each do |weight|
|
|
||||||
Pool.new(weight).generatePools()
|
|
||||||
last_match = @tournament.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
|
|
||||||
@tournament.assignBouts
|
|
||||||
@tournament.assignLoserNames
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -124,7 +124,6 @@ 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
|
||||||
(5...16).each do |count|
|
(5...16).each do |count|
|
||||||
tourney = createTournament(count)
|
tourney = createTournament(count)
|
||||||
|
|||||||
Reference in New Issue
Block a user