From dd44efa8696ad5c0d755e5da3a3dbc0304d3fb32 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Sun, 26 Apr 2015 15:49:13 -0400 Subject: [PATCH] Got stuff working with matches being saved individually. --- app/models/boutgen.rb | 53 ------------------ app/models/losernamegen.rb | 56 +++++++++++++++++++ app/models/match.rb | 16 +++++- app/models/matchup.rb | 19 ------- app/models/pool.rb | 5 +- app/models/poolbracket.rb | 7 +-- app/models/tournament.rb | 34 +---------- app/models/wrestler.rb | 8 +-- .../20150426190405_add_w_names_to_match.rb | 6 ++ ...50426191006_change_w_name_to_loser_name.rb | 6 ++ db/schema.rb | 4 +- test/integration/poolbracket_matchups_test.rb | 11 +--- 12 files changed, 97 insertions(+), 128 deletions(-) create mode 100644 app/models/losernamegen.rb delete mode 100644 app/models/matchup.rb create mode 100644 db/migrate/20150426190405_add_w_names_to_match.rb create mode 100644 db/migrate/20150426191006_change_w_name_to_loser_name.rb diff --git a/app/models/boutgen.rb b/app/models/boutgen.rb index f6a8be1..651155b 100644 --- a/app/models/boutgen.rb +++ b/app/models/boutgen.rb @@ -20,61 +20,8 @@ class Boutgen giveBout(@matches) @round += 1 end - assignLoserNames(matches,weights) return matches end - 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 - end - def twoPoolsToSemiLoser(matches) - @match1 = matches.select{|m| m.w1_name == "Winner Pool 1"}.first - @match2 = matches.select{|m| m.w1_name == "Winner Pool 2"}.first - @matchChange = matches.select{|m| m.bracket_position == "3/4"}.first - @matchChange.w1_name = "Loser of #{@match1.boutNumber}" - @matchChange.w2_name = "Loser of #{@match2.boutNumber}" - 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.w1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 1}.first.boutNumber}" - match.w2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 2}.first.boutNumber}" - elsif match.bracket_position_number == 2 - match.w1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 3}.first.boutNumber}" - match.w2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 4}.first.boutNumber}" - end - end - @thirdFourth.w1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.first.boutNumber}" - @thirdFourth.w2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" - @consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} - @seventhEighth.w1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.boutNumber}" - @seventhEighth.w2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" - 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.w1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.first.boutNumber}" - @thirdFourth.w2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" - @seventhEighth.w1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.boutNumber}" - @seventhEighth.w2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" - end end \ No newline at end of file diff --git a/app/models/losernamegen.rb b/app/models/losernamegen.rb new file mode 100644 index 0000000..0f939f0 --- /dev/null +++ b/app/models/losernamegen.rb @@ -0,0 +1,56 @@ +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.boutNumber}" + @matchChange.loser2_name = "Loser of #{@match2.boutNumber}" + 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.boutNumber}" + match.loser2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 2}.first.boutNumber}" + elsif match.bracket_position_number == 2 + match.loser1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 3}.first.boutNumber}" + match.loser2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 4}.first.boutNumber}" + end + end + @thirdFourth.loser1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.first.boutNumber}" + @thirdFourth.loser2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" + @consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} + @seventhEighth.loser1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.boutNumber}" + @seventhEighth.loser2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" + 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.boutNumber}" + @thirdFourth.loser2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" + @seventhEighth.loser1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.boutNumber}" + @seventhEighth.loser2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.boutNumber}" + end +end \ No newline at end of file diff --git a/app/models/match.rb b/app/models/match.rb index 9562f58..7bad4a1 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -4,11 +4,23 @@ class Match < ActiveRecord::Base def w1_name - Wrestler.find(self.w1).name + if self.w1 + Wrestler.find(self.w1).name + else + self.loser1_name + end end def w2_name - Wrestler.find(self.w2).name + if self.w2 + Wrestler.find(self.w2).name + else + self.loser2_name + end + end + + def weight_max + Weight.find(self.weight_id).max end end diff --git a/app/models/matchup.rb b/app/models/matchup.rb deleted file mode 100644 index b884837..0000000 --- a/app/models/matchup.rb +++ /dev/null @@ -1,19 +0,0 @@ -class Matchup - attr_accessor :w1, :w2, :round, :weight_id, :boutNumber, :w1_name, :w2_name, :bracket_position, :bracket_position_number, :weight_max - - def to_hash - hash = {} - instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) } - hash - end - - def convert_to_obj(h) - h.each do |k,v| - self.class.send(:attr_accessor, k) - instance_variable_set("@#{k}", v) - convert_to_obj(v) if v.is_a? Hash - end - end - - -end \ No newline at end of file diff --git a/app/models/pool.rb b/app/models/pool.rb index 467857b..043cba3 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -16,14 +16,11 @@ class Pool @bout = b.map @bout.each do |bout| if bout[0] != nil and bout[1] != nil - @match = Matchup.new + @match = Match.new @match.w1 = bout[0].id - @match.w1_name = bout[0].name @match.w2 = bout[1].id - @match.w2_name = bout[1].name @match.weight_id = weight.id @match.round = index + 1 - @match.weight_max = weight.max matches << @match end end diff --git a/app/models/poolbracket.rb b/app/models/poolbracket.rb index a8edf76..aa99e24 100644 --- a/app/models/poolbracket.rb +++ b/app/models/poolbracket.rb @@ -64,14 +64,13 @@ class Poolbracket end def createMatchup(matches,weight,round,w1_name,w2_name,bracket_position,bracket_position_number) - @match = Matchup.new - @match.w1_name = w1_name - @match.w2_name = w2_name + @match = Match.new + @match.loser1_name = w1_name + @match.loser2_name = w2_name @match.weight_id = weight.id @match.round = round @match.bracket_position = bracket_position @match.bracket_position_number = bracket_position_number - @match.weight_max = weight.max matches << @match return matches end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index b741c65..d7a8e1d 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -8,12 +8,10 @@ class Tournament < ActiveRecord::Base serialize :matchups_array def upcomingMatches - # @matches = generateMatchups if self.matches.nil? return self.matches else @matches = generateMatchups - puts @matches.inspect saveMatchups(@matches) return @matches end @@ -25,6 +23,7 @@ class Tournament < ActiveRecord::Base @matches = weight.generateMatchups(@matches) end @matches = assignBouts(@matches) + @matches = Losernamegen.new.assignLoserNames(@matches,self.weights) return @matches end @@ -36,38 +35,9 @@ class Tournament < ActiveRecord::Base def saveMatchups(matches) matches.each do |m| - @match = Match.new - @match.w1 = m.w1 - @match.w2 = m.w2 - @match.round = m.round - @match.boutNumber = m.boutNumber - @match.bracket_position = m.bracket_position - @match.bracket_position_number = m.bracket_position_number - @match.tournament_id = self.id - puts @match.inspect - @match.save + m.save end end - - - def matchupObjectsToHash(matches) - array_of_hashes = [] - matches.each do |m| - @matchHash = m.to_hash - array_of_hashes << @matchHash - end - return array_of_hashes - end - - def matchupHashesToObjects(matches) - array_of_objects = [] - matches.each do |m| - @match = Matchup.new - @match.convert_to_obj(m) - array_of_objects << @match - end - return array_of_objects - end end diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index af41d55..ab281f3 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -11,7 +11,7 @@ class Wrestler < ActiveRecord::Base def isWrestlingThisRound(matchRound) - if allMatches(self.tournament.upcomingMatches).blank? + if allMatches.blank? return false else return true @@ -24,7 +24,7 @@ class Wrestler < ActiveRecord::Base end def boutByRound(round,matches) - @match = allMatches(matches).select{|m| m.round == round}.first + @match = allMatches.select{|m| m.round == round}.first if @match.blank? return "BYE" else @@ -32,8 +32,8 @@ class Wrestler < ActiveRecord::Base end end - def allMatches(matches) - @matches = matches.select{|m| m.w1 == self.id || m.w2 == self.id} + def allMatches + @matches = Match.where("w1 = ? or w2 = ?",self.id,self.id) return @matches end diff --git a/db/migrate/20150426190405_add_w_names_to_match.rb b/db/migrate/20150426190405_add_w_names_to_match.rb new file mode 100644 index 0000000..a2ef847 --- /dev/null +++ b/db/migrate/20150426190405_add_w_names_to_match.rb @@ -0,0 +1,6 @@ +class AddWNamesToMatch < ActiveRecord::Migration + def change + add_column :matches, :w1_name, :string + add_column :matches, :w2_name, :string + end +end diff --git a/db/migrate/20150426191006_change_w_name_to_loser_name.rb b/db/migrate/20150426191006_change_w_name_to_loser_name.rb new file mode 100644 index 0000000..b8f66b5 --- /dev/null +++ b/db/migrate/20150426191006_change_w_name_to_loser_name.rb @@ -0,0 +1,6 @@ +class ChangeWNameToLoserName < ActiveRecord::Migration + def change + rename_column :matches, :w1_name, :loser1_name + rename_column :matches, :w2_name, :loser2_name + end +end diff --git a/db/schema.rb b/db/schema.rb index e0374b7..58f92c9 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: 20150425173224) do +ActiveRecord::Schema.define(version: 20150426191006) do create_table "matches", force: :cascade do |t| t.integer "w1" @@ -30,6 +30,8 @@ ActiveRecord::Schema.define(version: 20150425173224) do t.integer "weight_id" t.string "bracket_position" t.integer "bracket_position_number" + t.string "loser1_name" + t.string "loser2_name" end create_table "mats", force: :cascade do |t| diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index 6c7d4e4..a15507c 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -4,7 +4,7 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest def setup @tournament = Tournament.find(1) @tournament.upcomingMatches - @genMatchups = @tournament.generateMatchups + @genMatchups = @tournament.upcomingMatches end def createTournament(numberOfWrestlers) @@ -24,7 +24,6 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest @school3.save @weight3 = Weight.new @weight3.id = @id - @weight3.max = 350 @weight3.tournament_id = @id @weight3.save until @count > numberOfWrestlers do @@ -105,13 +104,7 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest @twentysix_matches = @genMatchups.select{|m| m.weight_max == 132} assert_equal 32, @twentysix_matches.length end - - test "tests serialization for matchups" do - @tournament_saved = Tournament.find(1) - @genMatchup = @genMatchups.select{|m| m.boutNumber == 4000}.first - @matchup = @tournament_saved.upcomingMatches.select{|m| m.boutNumber == 4000}.first - assert_equal @genMatchup.w1_name, @matchup.w1_name - end + test "test if a wrestler can exceed five matches" do @count = 5