A bit cleaner way of selecting map pairs, bunch of other cleanup crap generally characterized as removing generated Rails code
This commit is contained in:
@@ -2,42 +2,22 @@ require 'fastercsv'
|
||||
|
||||
|
||||
class Map < ActiveRecord::Base
|
||||
named_scope :lowest_scoring, :conditions => [
|
||||
%{`maps`.`points` = (
|
||||
SELECT `points` FROM `maps`
|
||||
ORDER BY `points` ASC
|
||||
LIMIT 1
|
||||
)}]
|
||||
|
||||
def self.from_city_name(city_name)
|
||||
self.find_or_initialize_by_name(city_name).save!
|
||||
end
|
||||
|
||||
def self.pair
|
||||
if Mash.count == 0
|
||||
self.all(:order => 'RANDOM()', :limit => count)
|
||||
self.all(:order => 'RANDOM()', :limit => 2)
|
||||
else
|
||||
first_conditions = <<-SQL
|
||||
NOT EXISTS (
|
||||
SELECT * FROM `mashes`
|
||||
WHERE `winner` = `maps`.`id`
|
||||
)
|
||||
SQL
|
||||
second_conditions = <<-SQL
|
||||
`maps`.`id` != :first_id
|
||||
AND (
|
||||
SELECT COUNT(*) FROM `mashes`
|
||||
WHERE `winner` = :first_id
|
||||
) = (
|
||||
SELECT COUNT(*) FROM `mashes`
|
||||
WHERE `winner` = `maps`.`id`
|
||||
)
|
||||
SQL
|
||||
|
||||
first = self.all(
|
||||
:order => 'RANDOM()',
|
||||
:limit => 1,
|
||||
:conditions => [first_conditions]
|
||||
).first
|
||||
second = self.all(
|
||||
:order => 'RANDOM()',
|
||||
:limit => 1,
|
||||
:conditions => [second_conditions, {:first_id => first.id}]
|
||||
).first
|
||||
[first, second]
|
||||
self.lowest_scoring.all(:order => 'RANDOM()', :limit => 2)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -54,23 +34,7 @@ class Map < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def >(other)
|
||||
self.points > other.points
|
||||
end
|
||||
|
||||
def <(other)
|
||||
self.points < other.points
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
self.points == other.points
|
||||
end
|
||||
|
||||
def rounds
|
||||
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
|
||||
end
|
||||
|
||||
def points
|
||||
Mash.count(:conditions => ['winner = ?', self.id])
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user