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|
|
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!"
|
flash[:notice] = "Let's start mashing!"
|
||||||
format.html { redirect_to :controller => 'mashes', :action => 'new' }
|
format.html { redirect_to :controller => 'mashes', :action => 'new' }
|
||||||
else
|
else
|
||||||
|
@ -2,8 +2,6 @@ require 'fastercsv'
|
|||||||
|
|
||||||
|
|
||||||
class Map < ActiveRecord::Base
|
class Map < ActiveRecord::Base
|
||||||
belongs_to :mash, :class_name => 'Mash'
|
|
||||||
|
|
||||||
def self.from_city_name(city_name)
|
def self.from_city_name(city_name)
|
||||||
self.find_or_initialize_by_name(city_name).save!
|
self.find_or_initialize_by_name(city_name).save!
|
||||||
end
|
end
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
class Mash < ActiveRecord::Base
|
class Mash < ActiveRecord::Base
|
||||||
has_one :map_a, :class_name => 'Map'
|
belongs_to :map_a, :class_name => 'Map'
|
||||||
has_one :map_b, :class_name => 'Map'
|
belongs_to :map_b, :class_name => 'Map'
|
||||||
has_one :winner, :class_name => 'Map'
|
belongs_to :winner, :class_name => 'Map'
|
||||||
belongs_to :tournament, :class_name => 'MashTournament'
|
has_one :tournament, :class_name => 'MashTournament'
|
||||||
belongs_to :round, :class_name => 'MashTournamentRound'
|
has_one :round, :class_name => 'MashTournamentRound'
|
||||||
|
|
||||||
named_scope :unplayed, {
|
named_scope :unplayed, {
|
||||||
:conditions => %{
|
:conditions => %{
|
||||||
|
@ -2,7 +2,6 @@ class MashTournament < ActiveRecord::Base
|
|||||||
belongs_to :requester
|
belongs_to :requester
|
||||||
has_many :mashes
|
has_many :mashes
|
||||||
has_many :rounds, :class_name => 'MashTournamentRound'
|
has_many :rounds, :class_name => 'MashTournamentRound'
|
||||||
#after_save :fill_in_next_round
|
|
||||||
|
|
||||||
def next_unplayed_mash
|
def next_unplayed_mash
|
||||||
self.mashes.unplayed.first
|
self.mashes.unplayed.first
|
||||||
@ -22,7 +21,7 @@ class MashTournament < ActiveRecord::Base
|
|||||||
[8, 16, 32, 64, 128].include?(n_contenders)
|
[8, 16, 32, 64, 128].include?(n_contenders)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_rounds(n_contenders)
|
def create_rounds_for_contenders(n_contenders)
|
||||||
if not valid_number_of_contenders?(n_contenders)
|
if not valid_number_of_contenders?(n_contenders)
|
||||||
raise StandardError.new(
|
raise StandardError.new(
|
||||||
"The number of contenders must be 8, 16, 32, 64, or 128! " +
|
"The number of contenders must be 8, 16, 32, 64, or 128! " +
|
||||||
@ -42,10 +41,9 @@ class MashTournament < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fill_in_next_round
|
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?
|
if not round.done?
|
||||||
assign_maps_for_round(round)
|
return assign_maps_for_round(round)
|
||||||
return
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -68,6 +66,10 @@ class MashTournament < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assign_maps_for_round(round)
|
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(
|
previous = MashTournamentRound.find_by_mash_tournament_id_and_number(
|
||||||
self.id, round.number - 1
|
self.id, round.number - 1
|
||||||
)
|
)
|
||||||
@ -77,16 +79,20 @@ class MashTournament < ActiveRecord::Base
|
|||||||
:conditions => ['id in ?', previous_winners]
|
:conditions => ['id in ?', previous_winners]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
filled_in = []
|
||||||
|
|
||||||
round.mashes.each do |mash|
|
round.mashes.each do |mash|
|
||||||
mash.update_attributes(
|
mash.map_a = pool.pop.id
|
||||||
:map_a_id => pool.pop.id,
|
mash.map_b = pool.pop.id
|
||||||
:map_b_id => pool.pop.id
|
mash.save!
|
||||||
)
|
|
||||||
|
filled_in << mash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
filled_in
|
||||||
end
|
end
|
||||||
|
|
||||||
def assign_maps_for_round_zero
|
def assign_maps_for_round_one(round)
|
||||||
round = MashTournamentRound.for_round(self.id, 0)
|
|
||||||
pool = Map.all(
|
pool = Map.all(
|
||||||
:order => 'RANDOM()',
|
:order => 'RANDOM()',
|
||||||
:limit => round.mash_count * 2
|
:limit => round.mash_count * 2
|
||||||
@ -94,17 +100,21 @@ class MashTournament < ActiveRecord::Base
|
|||||||
|
|
||||||
logger.info("Populating mashes from pool: #{pool.inspect}")
|
logger.info("Populating mashes from pool: #{pool.inspect}")
|
||||||
|
|
||||||
|
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}")
|
||||||
logger.info("`map_a`.`id` = #{map_a.id}, `map_b`.`id` = #{map_b.id} ")
|
|
||||||
|
|
||||||
mash.update_attributes(
|
mash.map_a = map_a
|
||||||
:map_a_id => map_a.id,
|
mash.map_b = map_b
|
||||||
:map_b_id => map_b.id
|
mash.save!
|
||||||
)
|
|
||||||
|
filled_in << mash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
filled_in
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,4 +5,12 @@ class MashTournamentRound < ActiveRecord::Base
|
|||||||
def self.for_round(tournament, round_number)
|
def self.for_round(tournament, round_number)
|
||||||
self.find_by_mash_tournament_id_and_number(tournament, round_number)
|
self.find_by_mash_tournament_id_and_number(tournament, round_number)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def done?
|
||||||
|
winners = []
|
||||||
|
self.mashes.collect do |mash|
|
||||||
|
winners << mash.winner_id
|
||||||
|
end
|
||||||
|
!winners.include?(nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,11 +18,10 @@ describe MashTournament do
|
|||||||
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|
|
||||||
expect do
|
expect do
|
||||||
subject.create_rounds(n)
|
subject.create_rounds_for_contenders(n)
|
||||||
end.to raise_error
|
end.to raise_error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -37,10 +36,25 @@ describe MashTournament do
|
|||||||
[[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
|
||||||
subject.create_rounds(n_contenders)
|
subject.create_rounds_for_contenders(n_contenders)
|
||||||
subject.total_rounds.should == n_rounds
|
subject.total_rounds.should == n_rounds
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user