diff --git a/app/models/match.rb b/app/models/match.rb index b573e7d..698cc19 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -3,15 +3,14 @@ class Match < ActiveRecord::Base belongs_to :weight, touch: true belongs_to :mat, touch: true has_many :wrestlers, :through => :weight - after_update :after_finished_actions, :if => :saved_change_to_finished? - after_update :after_finished_actions, :if => :saved_change_to_winner_id? - after_update :after_finished_actions, :if => :saved_change_to_win_type? - after_update :after_finished_actions, :if => :saved_change_to_score? + after_update :after_finished_actions, :if => :saved_change_to_finished? or :saved_change_to_winner_id? or :saved_change_to_win_type? or :saved_change_to_score? def after_finished_actions if self.finished == 1 && self.winner_id != nil - if self.w1 && self.w2 + if self.w1 wrestler1.touch + end + if self.w2 wrestler2.touch end if self.mat @@ -77,10 +76,10 @@ class Match < ActiveRecord::Base def advance_wrestlers if self.w1 - AdvanceWrestler.new(wrestler1).advance + AdvanceWrestler.new(wrestler1, self).advance end if self.w2 - AdvanceWrestler.new(wrestler2).advance + AdvanceWrestler.new(wrestler2, self).advance end end diff --git a/app/services/bracket_advancement/advance_wrestler.rb b/app/services/bracket_advancement/advance_wrestler.rb index b0b91ea..3e9cdbc 100644 --- a/app/services/bracket_advancement/advance_wrestler.rb +++ b/app/services/bracket_advancement/advance_wrestler.rb @@ -1,7 +1,8 @@ class AdvanceWrestler - def initialize( wrestler ) + def initialize( wrestler, last_match ) @wrestler = wrestler @tournament = @wrestler.tournament + @last_match = last_match end def advance @@ -14,7 +15,7 @@ class AdvanceWrestler def advance_raw pool_to_bracket_advancement if @tournament.tournament_type == "Pool to bracket" - DoubleEliminationAdvance.new(@wrestler).bracket_advancement if @tournament.tournament_type == "Modified 16 Man Double Elimination" or + DoubleEliminationAdvance.new(@wrestler, @last_match).bracket_advancement if @tournament.tournament_type == "Modified 16 Man Double Elimination" or @tournament.tournament_type == "Double Elimination 1-6" end diff --git a/app/services/bracket_advancement/double_elimination_advance.rb b/app/services/bracket_advancement/double_elimination_advance.rb index fbd64d5..ca10e38 100644 --- a/app/services/bracket_advancement/double_elimination_advance.rb +++ b/app/services/bracket_advancement/double_elimination_advance.rb @@ -1,8 +1,9 @@ class DoubleEliminationAdvance - def initialize(wrestler) + def initialize(wrestler,last_match) @wrestler = wrestler - @last_match = @wrestler.last_match + @last_match = last_match + @next_match_position_number = (@last_match.bracket_position_number / 2.0) end def bracket_advancement @@ -19,56 +20,59 @@ class DoubleEliminationAdvance if (@last_match.loser1_name == "BYE" or @last_match.loser2_name == "BYE") and @last_match.is_championship_match update_consolation_bye end - if @wrestler.last_match.bracket_position == "Quarter" - new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Semis",@wrestler.next_match_position_number.ceil,@wrestler.weight_id).first + if @last_match.bracket_position == "Quarter" + new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Semis",@next_match_position_number.ceil,@wrestler.weight_id).first update_new_match(new_match, get_wrestler_number) - elsif @wrestler.last_match.bracket_position == "Semis" - new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","1/2",@wrestler.next_match_position_number.ceil,@wrestler.weight_id).first + elsif @last_match.bracket_position == "Semis" + new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","1/2",@next_match_position_number.ceil,@wrestler.weight_id).first update_new_match(new_match, get_wrestler_number) - elsif @wrestler.last_match.bracket_position == "Conso Semis" + elsif @last_match.bracket_position == "Conso Semis" # if its a regular double elim - if Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Conso Semis",@wrestler.next_match_position_number.ceil,@wrestler.weight_id).first.loser1_name != nil - new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","3/4",@wrestler.next_match_position_number.ceil,@wrestler.weight_id).first + if Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Conso Semis",@next_match_position_number.ceil,@wrestler.weight_id).first.loser1_name != nil + new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","3/4",@next_match_position_number.ceil,@wrestler.weight_id).first update_new_match(new_match, get_wrestler_number) # if it's a special bracket where consolations wrestler for 5th else - new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","5/6",@wrestler.next_match_position_number.ceil,@wrestler.weight_id).first + new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","5/6",@next_match_position_number.ceil,@wrestler.weight_id).first update_new_match(new_match, get_wrestler_number) end - elsif @wrestler.last_match.bracket_position == "Conso Quarter" + elsif @last_match.bracket_position == "Conso Quarter" next_round_matches = Match.where("weight_id = ? and bracket_position = ?", @wrestler.weight_id, "Conso Semis").sort_by{|m| m.round} - this_round_matches = Match.where("weight_id = ? and round = ? and bracket_position = ?", @wrestler.weight_id, @wrestler.last_match.round, "Conso Quarter") + this_round_matches = Match.where("weight_id = ? and round = ? and bracket_position = ?", @wrestler.weight_id, @last_match.round, "Conso Quarter") if next_round_matches.size == this_round_matches.size # if a semi loser is dropping down - new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Conso Semis",@wrestler.last_match.bracket_position_number,@wrestler.weight_id).first + new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Conso Semis",@last_match.bracket_position_number,@wrestler.weight_id).first update_new_match(new_match,2) else # if it's a special bracket where a semi loser is not dropping down - new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Conso Semis",@wrestler.next_match_position_number.ceil,@wrestler.weight_id).first + new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Conso Semis",@next_match_position_number.ceil,@wrestler.weight_id).first update_new_match(new_match, get_wrestler_number) end - elsif @wrestler.last_match.bracket_position == "Bracket" - new_match = Match.where("bracket_position_number = ? and weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @wrestler.next_match_position_number.ceil,@wrestler.weight_id, @wrestler.last_match.round , "Bracket", "Quarter").sort_by{|m| m.round}.first + elsif @last_match.bracket_position == "Bracket" + new_match = Match.where("bracket_position_number = ? and weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @next_match_position_number.ceil,@wrestler.weight_id, @last_match.round , "Bracket", "Quarter").sort_by{|m| m.round}.first update_new_match(new_match, get_wrestler_number) - elsif @wrestler.last_match.bracket_position == "Conso" - next_round_matches = Match.where("weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @wrestler.weight_id, @wrestler.last_match.round, "Conso", "Conso Quarter").sort_by{|m| m.round} - this_round_matches = Match.where("weight_id = ? and round = ? and (bracket_position = ? or bracket_position = ?)", @wrestler.weight_id, @wrestler.last_match.round, "Conso", "Conso Quarter") + elsif @last_match.bracket_position == "Conso" + next_round = next_round_matches = Match.where("weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @wrestler.weight_id, @last_match.round, "Conso", "Conso Quarter").sort_by{|m| m.round}.first.round + next_round_matches = Match.where("weight_id = ? and round = ? and (bracket_position = ? or bracket_position = ?)", @wrestler.weight_id, next_round, "Conso", "Conso Quarter") + this_round_matches = Match.where("weight_id = ? and round = ? and (bracket_position = ? or bracket_position = ?)", @wrestler.weight_id, @last_match.round, "Conso", "Conso Quarter") # if a loser is dropping down if next_round_matches.size == this_round_matches.size - new_match = Match.where("bracket_position_number = ? and weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @wrestler.last_match.bracket_position_number,@wrestler.weight_id, @wrestler.last_match.round, "Conso", "Conso Quarter").sort_by{|m| m.round}.first + new_match = Match.where("bracket_position_number = ? and weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @last_match.bracket_position_number,@wrestler.weight_id, @last_match.round, "Conso", "Conso Quarter").sort_by{|m| m.round}.first update_new_match(new_match, 2) else - new_match = Match.where("bracket_position_number = ? and weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @wrestler.next_match_position_number.ceil,@wrestler.weight_id, @wrestler.last_match.round, "Conso", "Conso Quarter").sort_by{|m| m.round}.first + new_match = Match.where("bracket_position_number = ? and weight_id = ? and round > ? and (bracket_position = ? or bracket_position = ?)", @next_match_position_number.ceil,@wrestler.weight_id, @last_match.round, "Conso", "Conso Quarter").sort_by{|m| m.round}.first update_new_match(new_match, get_wrestler_number) end end end def update_new_match(match, wrestler_number) - if wrestler_number == 2 or match.loser1_name != nil + puts "!!!!!!!!!!!!!!!!!!!!!!" + puts "#{match.bout_number} is getting #{@wrestler.name} as wrestler #{wrestler_number}" + if wrestler_number == 2 or match.loser1_name.include? "Loser of" match.w2 = @wrestler.id match.save - elsif + elsif wrestler_number == 1 match.w1 = @wrestler.id match.save end @@ -83,15 +87,15 @@ class DoubleEliminationAdvance end def get_wrestler_number - if @wrestler.next_match_position_number != @wrestler.next_match_position_number.ceil + if @next_match_position_number != @next_match_position_number.ceil return 1 - elsif @wrestler.next_match_position_number == @wrestler.next_match_position_number.ceil + elsif @next_match_position_number == @next_match_position_number.ceil return 2 end end def loser_advance - bout = @wrestler.last_match.bout_number + bout = @last_match.bout_number next_match = Match.where("(loser1_name = ? OR loser2_name = ?) AND weight_id = ?","Loser of #{bout}","Loser of #{bout}",@wrestler.weight_id) if next_match.size > 0 next_match = next_match.first @@ -108,7 +112,7 @@ class DoubleEliminationAdvance def advance_double_byes weight = @wrestler.weight - weight.matches.select{|m| m.loser1_name == "BYE" and m.loser2_name == "BYE"}.each do |match| + weight.matches.select{|m| m.loser1_name == "BYE" and m.loser2_name == "BYE" and m.finished != 1}.each do |match| match.finished = 1 match.win_type = "BYE" next_match_position_number = (match.bracket_position_number / 2.0).ceil