STILL trying to get tournament rounds filled in correctly. Wholly freaking ActiveRecord.
This commit is contained in:
parent
5cf5a8035d
commit
0b853163bf
@ -41,7 +41,7 @@ class MashTournamentsController < ApplicationController
|
||||
)
|
||||
|
||||
respond_to do |format|
|
||||
if @mash_tournament.save && @mash_tournament.create_rounds
|
||||
if @mash_tournament.save && @mash_tournament.create_rounds_for_contenders(8)
|
||||
flash[:notice] = "Let's start mashing!"
|
||||
format.html { redirect_to :controller => 'mashes', :action => 'new' }
|
||||
else
|
||||
|
@ -2,8 +2,6 @@ require 'fastercsv'
|
||||
|
||||
|
||||
class Map < ActiveRecord::Base
|
||||
belongs_to :mash, :class_name => 'Mash'
|
||||
|
||||
def self.from_city_name(city_name)
|
||||
self.find_or_initialize_by_name(city_name).save!
|
||||
end
|
||||
|
@ -1,9 +1,9 @@
|
||||
class Mash < ActiveRecord::Base
|
||||
has_one :map_a, :class_name => 'Map'
|
||||
has_one :map_b, :class_name => 'Map'
|
||||
has_one :winner, :class_name => 'Map'
|
||||
belongs_to :tournament, :class_name => 'MashTournament'
|
||||
belongs_to :round, :class_name => 'MashTournamentRound'
|
||||
belongs_to :map_a, :class_name => 'Map'
|
||||
belongs_to :map_b, :class_name => 'Map'
|
||||
belongs_to :winner, :class_name => 'Map'
|
||||
has_one :tournament, :class_name => 'MashTournament'
|
||||
has_one :round, :class_name => 'MashTournamentRound'
|
||||
|
||||
named_scope :unplayed, {
|
||||
:conditions => %{
|
||||
|
@ -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
|
||||
)
|
||||
end
|
||||
mash.map_a = pool.pop.id
|
||||
mash.map_b = pool.pop.id
|
||||
mash.save!
|
||||
|
||||
filled_in << mash
|
||||
end
|
||||
|
||||
def assign_maps_for_round_zero
|
||||
round = MashTournamentRound.for_round(self.id, 0)
|
||||
filled_in
|
||||
end
|
||||
|
||||
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
|
||||
)
|
||||
end
|
||||
mash.map_a = map_a
|
||||
mash.map_b = map_b
|
||||
mash.save!
|
||||
|
||||
filled_in << mash
|
||||
end
|
||||
|
||||
filled_in
|
||||
end
|
||||
end
|
||||
|
@ -5,4 +5,12 @@ class MashTournamentRound < ActiveRecord::Base
|
||||
def self.for_round(tournament, round_number)
|
||||
self.find_by_mash_tournament_id_and_number(tournament, round_number)
|
||||
end
|
||||
|
||||
def done?
|
||||
winners = []
|
||||
self.mashes.collect do |mash|
|
||||
winners << mash.winner_id
|
||||
end
|
||||
!winners.include?(nil)
|
||||
end
|
||||
end
|
||||
|
@ -18,11 +18,10 @@ describe MashTournament do
|
||||
end
|
||||
|
||||
context 'creating rounds' do
|
||||
|
||||
it 'should reject invalid numbers of contenders' do
|
||||
[11, 24, 40].each do |n|
|
||||
expect do
|
||||
subject.create_rounds(n)
|
||||
subject.create_rounds_for_contenders(n)
|
||||
end.to raise_error
|
||||
end
|
||||
end
|
||||
@ -37,10 +36,25 @@ describe MashTournament do
|
||||
[[8, 3], [16, 4], [32, 5]].each do |n_contenders,n_rounds|
|
||||
context "for #{n_contenders} total contenders" do
|
||||
it "should create #{n_rounds} rounds" do
|
||||
subject.create_rounds(n_contenders)
|
||||
subject.create_rounds_for_contenders(n_contenders)
|
||||
subject.total_rounds.should == n_rounds
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'filling in rounds' do
|
||||
before(:each) do
|
||||
subject.id.should_not be_nil
|
||||
subject.create_rounds_for_contenders(8)
|
||||
end
|
||||
|
||||
xit 'should fill in every map for every mash in a given round' do
|
||||
subject.fill_in_next_round
|
||||
subject.round(1).mashes.each do |mash|
|
||||
mash.map_a.should_not be_nil
|
||||
mash.map_b.should_not be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user