Making more messes with mashes, maps, and requesters. Maybe a teensy

bit closer on the tournament pairing algorithm (???)  ActiveRecord
associations are kind of a pain, btw.
This commit is contained in:
Dan Buch
2012-03-09 00:24:48 -05:00
parent ec6d9d5146
commit 8ad33d18b7
10 changed files with 123 additions and 29 deletions

View File

@@ -7,7 +7,43 @@ class Map < ActiveRecord::Base
end
def self.pair
self.all(:order => 'RANDOM()', :limit => 2)
second = nil
max_tries = self.count
tries = 0
while second.nil? && tries < max_tries
first = self.all(:order => 'RANDOM()', :limit => 1).first
second = self.all(:order => 'RANDOM()', :limit => 1,
:conditions => [%{
(
points >= :first_points
AND id <> :first_id
) OR (
id NOT IN (
SELECT map_a_id
FROM mashes
WHERE map_b_id = :first_id
)
AND id NOT IN (
SELECT map_b_id
FROM mashes
WHERE map_a_id = :first_id
)
)}, {
:first_id => first.id,
:first_points => first.points
}
]
).first
tries += 1
end
if second.nil?
logger.error('OH CRAP thar be no more pairings for ye olde mashes')
[first, first]
else
[first, second]
end
end
def self.import(csv_filename)
@@ -21,6 +57,10 @@ class Map < ActiveRecord::Base
end
end
def self.mashes
Mash.all(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
end
def rounds
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
end

View File

@@ -1,2 +1,6 @@
class Mash < ActiveRecord::Base
has_one :map_a, :class_name => 'Map'
has_one :map_b, :class_name => 'Map'
has_one :winner, :class_name => 'Map'
has_one :requester
end

View File

@@ -0,0 +1,2 @@
class Requester < ActiveRecord::Base
end