BUSTED: what a mess. trying to get tournament round generation bits working. ack.

This commit is contained in:
Dan Buch
2012-03-10 08:47:47 -05:00
parent f0297f3101
commit 8ec802a2bf
19 changed files with 241 additions and 387 deletions

View File

@@ -2,6 +2,8 @@ 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

View File

@@ -2,5 +2,14 @@ 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 :requester
belongs_to :tournament, :class_name => 'MashTournament'
belongs_to :round, :class_name => 'MashTournamentRound'
named_scope :unplayed, {
:conditions => %{
winner_id IS NULL
AND map_a_id IS NOT NULL
AND map_b_id IS NOT NULL
}
}
end

View File

@@ -1,2 +1,106 @@
class MashTournament < ActiveRecord::Base
belongs_to :requester
has_many :mashes
has_many :rounds, :class_name => 'MashTournamentRound'
after_create :create_rounds
after_save :maybe_fill_in_next_round
def next_unplayed_mash
self.mashes.unplayed.first
end
def done?
true
end
def round(number = 0)
MashTournamentRound.find_by_mash_tournament_id(self.id,
:conditions => {:number => number}
)
end
private
def create_rounds
n_contenders = Map.count
while n_contenders % 4 != 0
n_contenders -= 1
end
round = 0
while n_contenders > 2
create_round(round, n_contenders)
n_contenders = n_contenders / 2
round += 1
end
assign_maps_for_round_zero
self.total_rounds = round - 1
end
def create_round(round_number, n_contenders)
round = MashTournamentRound.new(
:mash_tournament_id => self.id,
:number => round_number,
:mash_count => n_contenders / 2
)
round.save!
n_contenders.times do
Mash.new(
:mash_tournament_id => self.id,
:mash_tournament_round_id => round.id
).save!
end
end
def maybe_fill_in_next_round
self.rounds.sort(&:number).each do |round|
if not round.done?
assign_maps_for_round(round)
return
end
end
end
def assign_maps_for_round(round)
previous = MashTournamentRound.find_by_mash_tournament_id_and_number(
self.id, round.number - 1
)
previous_winners = previous.mashes.collect(&:winner_id)
pool = Map.all(
:order => 'RANDOM()',
:conditions => ['id in ?', previous_winners]
)
round.mashes.each do |mash|
mash.update_attributes(
:map_a_id => pool.pop.id,
:map_b_id => pool.pop.id
)
end
end
def assign_maps_for_round_zero
round = MashTournamentRound.for_round(self.id, 0)
pool = Map.all(
:order => 'RANDOM()',
:limit => round.mash_count * 2
)
logger.info("Populating mashes from pool: #{pool.inspect}")
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
end
end

View File

@@ -0,0 +1,8 @@
class MashTournamentRound < ActiveRecord::Base
belongs_to :tournament, :class_name => 'MashTournament'
has_many :mashes
def self.for_round(tournament, round_number)
self.find_by_mash_tournament_id_and_number(tournament, round_number)
end
end

View File

@@ -1,3 +1,7 @@
class Requester < ActiveRecord::Base
has_many :mashes
has_many :mash_tournaments
def current_tournament
self.mash_tournaments.last
end
end