|
|
|
@ -2,7 +2,6 @@ class MashTournament < ActiveRecord::Base
|
|
|
|
|
belongs_to :requester
|
|
|
|
|
has_many :mashes
|
|
|
|
|
has_many :rounds, :class_name => 'MashTournamentRound'
|
|
|
|
|
#after_save :fill_in_next_round
|
|
|
|
|
|
|
|
|
|
def next_unplayed_mash
|
|
|
|
|
self.mashes.unplayed.first
|
|
|
|
@ -22,7 +21,7 @@ class MashTournament < ActiveRecord::Base
|
|
|
|
|
[8, 16, 32, 64, 128].include?(n_contenders)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_rounds(n_contenders)
|
|
|
|
|
def create_rounds_for_contenders(n_contenders)
|
|
|
|
|
if not valid_number_of_contenders?(n_contenders)
|
|
|
|
|
raise StandardError.new(
|
|
|
|
|
"The number of contenders must be 8, 16, 32, 64, or 128! " +
|
|
|
|
@ -42,10 +41,9 @@ class MashTournament < ActiveRecord::Base
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def fill_in_next_round
|
|
|
|
|
self.rounds.sort(&:number).each do |round|
|
|
|
|
|
self.rounds.sort{ |a,b| a.number <=> b.number}.each do |round|
|
|
|
|
|
if not round.done?
|
|
|
|
|
assign_maps_for_round(round)
|
|
|
|
|
return
|
|
|
|
|
return assign_maps_for_round(round)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
@ -68,6 +66,10 @@ class MashTournament < ActiveRecord::Base
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def assign_maps_for_round(round)
|
|
|
|
|
if round.number == 1
|
|
|
|
|
return assign_maps_for_round_one(round)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
previous = MashTournamentRound.find_by_mash_tournament_id_and_number(
|
|
|
|
|
self.id, round.number - 1
|
|
|
|
|
)
|
|
|
|
@ -77,16 +79,20 @@ class MashTournament < ActiveRecord::Base
|
|
|
|
|
:conditions => ['id in ?', previous_winners]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
filled_in = []
|
|
|
|
|
|
|
|
|
|
round.mashes.each do |mash|
|
|
|
|
|
mash.update_attributes(
|
|
|
|
|
:map_a_id => pool.pop.id,
|
|
|
|
|
:map_b_id => pool.pop.id
|
|
|
|
|
)
|
|
|
|
|
mash.map_a = pool.pop.id
|
|
|
|
|
mash.map_b = pool.pop.id
|
|
|
|
|
mash.save!
|
|
|
|
|
|
|
|
|
|
filled_in << mash
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
filled_in
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def assign_maps_for_round_zero
|
|
|
|
|
round = MashTournamentRound.for_round(self.id, 0)
|
|
|
|
|
def assign_maps_for_round_one(round)
|
|
|
|
|
pool = Map.all(
|
|
|
|
|
:order => 'RANDOM()',
|
|
|
|
|
:limit => round.mash_count * 2
|
|
|
|
@ -94,17 +100,21 @@ class MashTournament < ActiveRecord::Base
|
|
|
|
|
|
|
|
|
|
logger.info("Populating mashes from pool: #{pool.inspect}")
|
|
|
|
|
|
|
|
|
|
filled_in = []
|
|
|
|
|
|
|
|
|
|
round.mashes.each do |mash|
|
|
|
|
|
map_a = pool.pop
|
|
|
|
|
map_b = pool.pop
|
|
|
|
|
logger.info("Assigning `map_a` from #{map_a.inspect}, " +
|
|
|
|
|
"`map_b` from #{map_b.inspect} to mash #{mash.inspect}")
|
|
|
|
|
logger.info("`map_a`.`id` = #{map_a.id}, `map_b`.`id` = #{map_b.id} ")
|
|
|
|
|
|
|
|
|
|
mash.update_attributes(
|
|
|
|
|
:map_a_id => map_a.id,
|
|
|
|
|
:map_b_id => map_b.id
|
|
|
|
|
)
|
|
|
|
|
mash.map_a = map_a
|
|
|
|
|
mash.map_b = map_b
|
|
|
|
|
mash.save!
|
|
|
|
|
|
|
|
|
|
filled_in << mash
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
filled_in
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|