diff --git a/app/models/pool.rb b/app/models/pool.rb index cc6cb98..cb823c8 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -1,24 +1,34 @@ class Pool - def generatePools(weight, tournament, matches) - @pools = weight.pools + def initialize(weight) + @weight = weight @pool = 1 - while @pool <= @pools - matches = roundRobin(weight.wrestlers, weight, tournament, matches) + end + + def generatePools + matches = [] + pools = @weight.pools + while @pool <= pools + matches += roundRobin() @pool += 1 end return matches end - def roundRobin(wrestlers,weight,tournament,matches) - @wrestlers = wrestlers.select{|w| w.generatePoolNumber == @pool}.to_a - @poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse - @poolMatches.each_with_index do |b, index| + def roundRobin + matches = [] + wrestlers = @weight.wrestlers_for_pool(@pool) + poolMatches = RoundRobinTournament.schedule(wrestlers).reverse + poolMatches.each_with_index do |b, index| round = index + 1 - @bout = b.map - @bout.each do |bout| + bouts = b.map + bouts.each do |bout| if bout[0] != nil and bout[1] != nil - @match = Match.new(w1: bout[0].id, w2: bout[1].id, weight_id: weight.id, round: round) - matches << @match + match = Match.new( + w1: bout[0].id, + w2: bout[1].id, + weight_id: @weight.id, + round: round) + matches << match end end end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 886518e..b41c740 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -28,15 +28,15 @@ class Tournament < ActiveRecord::Base def upcomingMatches if matches.nil? - return matches + return nil else generateMatchups - return matches + matches end end def generateMatchups - @matches = Tournamentmatchgen.new.genMatches(self) + @matches = Tournamentmatchgen.new(self).genMatches() end def destroyAllMatches @@ -44,6 +44,3 @@ class Tournament < ActiveRecord::Base end end - - - diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index e518363..5285559 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -1,36 +1,51 @@ class Tournamentmatchgen - def genMatches(tournament) - if tournament.tournament_type == "Pool to bracket" - @matches = poolToBracket(tournament) - end - return @matches + + def initialize(tournament) + @tournament = tournament + @matches = @tournament.matches end - def poolToBracket(tournament) - tournament.destroyAllMatches + 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 = [] - tournament.weights.sort_by{|x|[x.max]}.each do |w| - buildTournamentWeights(tournament.id, w) + 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 - @matches = Boutgen.new.assignBouts(@matches,tournament.weights) - @matches = Losernamegen.new.assignLoserNames(@matches,tournament.weights) - saveMatches(tournament,@matches) - return @matches end - def buildTournamentWeights(tournament_id, weight) - @wrestlers = weight.wrestlers - @matches = Pool.new.generatePools(weight, tournament_id, @matches) - @weight_matches = @matches.select{|m| m.weight_id == weight.id } - @last_match = @weight_matches.sort_by{|m| m.round}.last - @highest_round = @last_match.round - @matches = Poolbracket.new.generateBracketMatches(@matches, weight, @highest_round) + def generateMatches + @matches = + Losernamegen.new.assignLoserNames( + Boutgen.new.assignBouts(@matches, @tournament.weights), + @tournament.weights) end - def saveMatches(tournament,matches) - matches.each do |m| - m.tournament_id = tournament.id + def saveMatches + @matches.each do |m| + m.tournament_id = @tournament.id m.save end end + end diff --git a/app/models/weight.rb b/app/models/weight.rb index 0298e8b..3ae2cf8 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -8,6 +8,10 @@ class Weight < ActiveRecord::Base self.tournament.destroyAllMatches end + def wrestlers_for_pool(pool) + wrestlers.select{|w| w.generatePoolNumber == pool}.to_a + end + def pools @wrestlers = self.wrestlers if @wrestlers.size <= 6 diff --git a/db/migrate/20150523121319_introduce_indexes.rb b/db/migrate/20150523121319_introduce_indexes.rb new file mode 100644 index 0000000..ff24411 --- /dev/null +++ b/db/migrate/20150523121319_introduce_indexes.rb @@ -0,0 +1,10 @@ +class IntroduceIndexes < ActiveRecord::Migration + def change + add_index :weights, :tournament_id + add_index :schools, :tournament_id + add_index :mats, :tournament_id + add_index :matches, :tournament_id + add_index :matches, [:w1, :w2], :unique => true + add_index :wrestlers, :weight_id + end +end diff --git a/db/schema.rb b/db/schema.rb index d9d5f13..ffedbe8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150517075923) do +ActiveRecord::Schema.define(version: 20150523121319) do create_table "matches", force: :cascade do |t| t.integer "w1" @@ -34,6 +34,9 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.string "loser2_name" end + add_index "matches", ["tournament_id"], name: "index_matches_on_tournament_id" + add_index "matches", ["w1", "w2"], name: "index_matches_on_w1_and_w2", unique: true + create_table "mats", force: :cascade do |t| t.string "name" t.integer "tournament_id" @@ -41,6 +44,8 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.datetime "updated_at" end + add_index "mats", ["tournament_id"], name: "index_mats_on_tournament_id" + create_table "schools", force: :cascade do |t| t.string "name" t.datetime "created_at" @@ -48,6 +53,8 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.integer "tournament_id" end + add_index "schools", ["tournament_id"], name: "index_schools_on_tournament_id" + create_table "tournaments", force: :cascade do |t| t.string "name" t.string "address" @@ -83,6 +90,8 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.integer "tournament_id" end + add_index "weights", ["tournament_id"], name: "index_weights_on_tournament_id" + create_table "wrestlers", force: :cascade do |t| t.string "name" t.integer "school_id" @@ -97,4 +106,6 @@ ActiveRecord::Schema.define(version: 20150517075923) do t.boolean "extra" end + add_index "wrestlers", ["weight_id"], name: "index_wrestlers_on_weight_id" + end