Trying to get round generation and fill figured out TDD style

This commit is contained in:
Dan Buch
2012-03-10 21:13:03 -05:00
parent f177ecde2a
commit e1f1aa6038
6 changed files with 55 additions and 26 deletions

View File

@@ -3,7 +3,7 @@ class MashTournament < ActiveRecord::Base
has_many :mashes
has_many :rounds, :class_name => 'MashTournamentRound'
#after_create :create_rounds
#after_save :maybe_fill_in_next_round
#after_save :fill_in_next_round
def next_unplayed_mash
self.mashes.unplayed.first
@@ -19,25 +19,38 @@ class MashTournament < ActiveRecord::Base
)
end
private
def create_rounds
n_contenders = Map.count
#ap "Original n_contenders = #{n_contenders}"
while n_contenders % 4 != 0
n_contenders -= 1
end
round = 0
#ap "Resulting n_contenders divisible by 4 = #{n_contenders}"
while n_contenders > 2
round = 1
while n_contenders > 1
#ap "Generating round #{round}, n_contenders = #{n_contenders}"
create_round(round, n_contenders)
n_contenders = n_contenders / 2
round += 1
end
assign_maps_for_round_zero
self.total_rounds = round - 1
end
def fill_in_next_round
self.rounds.sort(&:number).each do |round|
if not round.done?
assign_maps_for_round(round)
return
end
end
end
private
def create_round(round_number, n_contenders)
round = MashTournamentRound.new(
:mash_tournament_id => self.id,
@@ -54,15 +67,6 @@ class MashTournament < ActiveRecord::Base
end
end
def maybe_fill_in_next_round
self.rounds.sort(&:number).each do |round|
if not round.done?
assign_maps_for_round(round)
return
end
end
end
def assign_maps_for_round(round)
previous = MashTournamentRound.find_by_mash_tournament_id_and_number(
self.id, round.number - 1