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:
@@ -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
|
||||
|
@@ -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
|
||||
|
2
rails/map-mash/app/models/requester.rb
Normal file
2
rails/map-mash/app/models/requester.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Requester < ActiveRecord::Base
|
||||
end
|
Reference in New Issue
Block a user