|
|
@ -2,42 +2,22 @@ require 'fastercsv'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Map < ActiveRecord::Base
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def self.pair
|
|
|
|
def self.pair
|
|
|
|
if Mash.count == 0
|
|
|
|
if Mash.count == 0
|
|
|
|
self.all(:order => 'RANDOM()', :limit => count)
|
|
|
|
self.all(:order => 'RANDOM()', :limit => 2)
|
|
|
|
else
|
|
|
|
else
|
|
|
|
first_conditions = <<-SQL
|
|
|
|
self.lowest_scoring.all(:order => 'RANDOM()', :limit => 2)
|
|
|
|
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]
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -54,23 +34,7 @@ class Map < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
|
|
|
def rounds
|
|
|
|
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
|
|
|
|
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def points
|
|
|
|
|
|
|
|
Mash.count(:conditions => ['winner = ?', self.id])
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|