2012-03-12 03:24:45 +00:00
|
|
|
require 'logger'
|
|
|
|
|
|
|
|
|
2012-03-12 01:22:48 +00:00
|
|
|
class MashTournamentBuilder
|
|
|
|
attr_accessor :tournament, :tournament_model, :round_model
|
|
|
|
attr_accessor :map_model, :mash_model
|
|
|
|
|
2012-03-12 03:24:45 +00:00
|
|
|
def initialize(tournament, tournament_model, round_model,
|
|
|
|
map_model, mash_model)
|
2012-03-12 01:22:48 +00:00
|
|
|
@tournament = tournament
|
|
|
|
@tournament_model = tournament_model
|
|
|
|
@round_model = round_model
|
|
|
|
@map_model = map_model
|
|
|
|
@mash_model = mash_model
|
2012-03-12 03:24:45 +00:00
|
|
|
@logger = Logger.new(
|
|
|
|
File.expand_path(
|
|
|
|
'../log/mash-tournament-builder.log', File.dirname(__FILE__)
|
|
|
|
)
|
|
|
|
)
|
2012-03-12 01:22:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_number_of_contenders?(n_contenders)
|
|
|
|
[8, 16, 32, 64, 128].include?(n_contenders)
|
|
|
|
end
|
|
|
|
|
|
|
|
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! " +
|
|
|
|
"Got '#{n_contenders}'"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
round = 1
|
|
|
|
|
|
|
|
while n_contenders > 1
|
|
|
|
create_round(round, n_contenders)
|
|
|
|
n_contenders = n_contenders / 2
|
|
|
|
round += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
@tournament.total_rounds = round - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_in_next_round
|
|
|
|
@tournament.rounds.sort{ |a,b| a.number <=> b.number}.each do |round|
|
|
|
|
if not round.done?
|
|
|
|
return assign_maps_for_round(round)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def create_round(round_number, n_contenders)
|
2012-03-12 03:24:45 +00:00
|
|
|
round_options = {
|
2012-03-12 01:22:48 +00:00
|
|
|
:mash_tournament_id => @tournament.id,
|
|
|
|
:number => round_number,
|
|
|
|
:mash_count => n_contenders / 2
|
2012-03-12 03:24:45 +00:00
|
|
|
}
|
|
|
|
@logger.info("Creating round #{round_number} of #{n_contenders} contenders " +
|
|
|
|
"with options: #{round_options.inspect}")
|
|
|
|
|
|
|
|
round = @round_model.new(round_options)
|
2012-03-12 01:22:48 +00:00
|
|
|
round.save!
|
|
|
|
|
2012-03-12 03:24:45 +00:00
|
|
|
round.mash_count.times do
|
2012-03-12 01:22:48 +00:00
|
|
|
@mash_model.new(
|
|
|
|
:mash_tournament_id => @tournament.id,
|
|
|
|
:mash_tournament_round_id => round.id
|
|
|
|
).save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_maps_for_round(round)
|
|
|
|
if round.number == 1
|
|
|
|
return assign_maps_for_round_one(round)
|
|
|
|
end
|
|
|
|
|
|
|
|
previous = @round_model.find_by_mash_tournament_id_and_number(
|
|
|
|
@tournament.id, round.number - 1
|
|
|
|
)
|
|
|
|
previous_winners = previous.mashes.collect(&:winner_id)
|
|
|
|
pool = @map_model.all(
|
|
|
|
:order => 'RANDOM()',
|
2012-03-12 04:18:42 +00:00
|
|
|
:conditions => {:id => previous_winners}
|
2012-03-12 01:22:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
filled_in = []
|
|
|
|
|
|
|
|
round.mashes.each do |mash|
|
2012-03-12 04:18:42 +00:00
|
|
|
mash.map_a = pool.pop
|
|
|
|
mash.map_b = pool.pop
|
2012-03-12 01:22:48 +00:00
|
|
|
mash.save!
|
|
|
|
|
|
|
|
filled_in << mash
|
|
|
|
end
|
|
|
|
|
|
|
|
filled_in
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_maps_for_round_one(round)
|
2012-03-12 03:24:45 +00:00
|
|
|
pool_options = {
|
2012-03-12 01:22:48 +00:00
|
|
|
:order => 'RANDOM()',
|
|
|
|
:limit => round.mash_count * 2
|
2012-03-12 03:24:45 +00:00
|
|
|
}
|
|
|
|
@logger.info("Allocating pool with options: #{pool_options.inspect}")
|
|
|
|
pool = @map_model.all(pool_options)
|
2012-03-12 01:22:48 +00:00
|
|
|
|
2012-03-12 03:24:45 +00:00
|
|
|
@logger.info("Populating mashes from pool: #{pool.inspect}")
|
2012-03-12 01:22:48 +00:00
|
|
|
|
|
|
|
filled_in = []
|
|
|
|
|
|
|
|
round.mashes.each do |mash|
|
|
|
|
map_a = pool.pop
|
|
|
|
map_b = pool.pop
|
2012-03-12 03:24:45 +00:00
|
|
|
@logger.info("Assigning `map_a` from #{map_a.inspect}, " +
|
2012-03-12 01:22:48 +00:00
|
|
|
"`map_b` from #{map_b.inspect} to mash #{mash.inspect}")
|
|
|
|
|
|
|
|
mash.map_a = map_a
|
|
|
|
mash.map_b = map_b
|
|
|
|
mash.save!
|
|
|
|
|
|
|
|
filled_in << mash
|
|
|
|
end
|
|
|
|
|
|
|
|
filled_in
|
|
|
|
end
|
|
|
|
end
|