Might actually have tournament round filling working correctly (?)

cat-town
Dan Buch 13 years ago
parent efd2178294
commit 73cff897a3

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

@ -1,14 +1,14 @@
require 'spec_helper' require 'spec_helper'
describe MashTournamentBuilder do describe MashTournamentBuilder do
let(:subject) do let(ENV['MOCK'].nil? ? :subject : :real_subject) do
MashTournamentBuilder.new( MashTournamentBuilder.new(
MashTournament.make(:requester => Requester.make), MashTournament.make(:requester => Requester.make),
MashTournament, MashTournamentRound, Map, Mash MashTournament, MashTournamentRound, Map, Mash
) )
end end
let(:mock_subject) do let(ENV['MOCK'].nil? ? :mock_subject : :subject) do
@tournament = mock(Object) @tournament = mock(Object)
@tournament_model = mock(Object) @tournament_model = mock(Object)
@round_model = mock(Object) @round_model = mock(Object)
@ -21,6 +21,13 @@ describe MashTournamentBuilder do
) )
end end
before(:all) do
subject.map_model.destroy_all
40.times do
subject.map_model.make.save
end
end
context 'creating rounds' do context 'creating rounds' do
it 'should reject invalid numbers of contenders' do it 'should reject invalid numbers of contenders' do
[11, 24, 40].each do |n| [11, 24, 40].each do |n|
@ -30,13 +37,6 @@ describe MashTournamentBuilder do
end end
end end
before(:all) do
subject.map_model.destroy_all
40.times do
subject.map_model.make.save
end
end
[[8, 3], [16, 4], [32, 5]].each do |n_contenders,n_rounds| [[8, 3], [16, 4], [32, 5]].each do |n_contenders,n_rounds|
context "for #{n_contenders} total contenders" do context "for #{n_contenders} total contenders" do
it "should create #{n_rounds} rounds" do it "should create #{n_rounds} rounds" do
@ -49,13 +49,13 @@ describe MashTournamentBuilder do
context 'filling in rounds' do context 'filling in rounds' do
before(:each) do before(:each) do
subject.id.should_not be_nil subject.tournament.id.should_not be_nil
subject.create_rounds_for_contenders(8) subject.create_rounds_for_contenders(8)
end end
xit 'should fill in every map for every mash in a given round' do it 'should fill in every map for every mash in a given round' do
subject.fill_in_next_round subject.fill_in_next_round
subject.round(1).mashes.each do |mash| subject.tournament.round(1).mashes.each do |mash|
mash.map_a.should_not be_nil mash.map_a.should_not be_nil
mash.map_b.should_not be_nil mash.map_b.should_not be_nil
end end

Loading…
Cancel
Save