Might actually have tournament round filling working correctly (?)

This commit is contained in:
Dan Buch
2012-03-11 23:24:45 -04:00
parent efd2178294
commit 73cff897a3
2 changed files with 35 additions and 20 deletions

View File

@@ -1,13 +1,22 @@
require 'logger'
class MashTournamentBuilder
attr_accessor :tournament, :tournament_model, :round_model
attr_accessor :map_model, :mash_model
def initialize(tournament, tournament_model, round_model, map_model, mash_model)
def initialize(tournament, tournament_model, round_model,
map_model, mash_model)
@tournament = tournament
@tournament_model = tournament_model
@round_model = round_model
@map_model = map_model
@mash_model = mash_model
@logger = Logger.new(
File.expand_path(
'../log/mash-tournament-builder.log', File.dirname(__FILE__)
)
)
end
def valid_number_of_contenders?(n_contenders)
@@ -43,14 +52,18 @@ class MashTournamentBuilder
private
def create_round(round_number, n_contenders)
round = @round_model.new(
round_options = {
:mash_tournament_id => @tournament.id,
:number => round_number,
:mash_count => n_contenders / 2
)
}
@logger.info("Creating round #{round_number} of #{n_contenders} contenders " +
"with options: #{round_options.inspect}")
round = @round_model.new(round_options)
round.save!
n_contenders.times do
round.mash_count.times do
@mash_model.new(
:mash_tournament_id => @tournament.id,
:mash_tournament_round_id => round.id
@@ -86,19 +99,21 @@ class MashTournamentBuilder
end
def assign_maps_for_round_one(round)
pool = @map_model.all(
pool_options = {
:order => 'RANDOM()',
:limit => round.mash_count * 2
)
}
@logger.info("Allocating pool with options: #{pool_options.inspect}")
pool = @map_model.all(pool_options)
logger.info("Populating mashes from pool: #{pool.inspect}")
@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}, " +
@logger.info("Assigning `map_a` from #{map_a.inspect}, " +
"`map_b` from #{map_b.inspect} to mash #{mash.inspect}")
mash.map_a = map_a