Starting to fill in tournament ranking bits

This commit is contained in:
Dan Buch
2012-03-07 21:22:35 -05:00
parent a1aa03451f
commit b31926f16f
5 changed files with 64 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ class MashesController < ApplicationController
alias_method :show, :redirect_to_new
def new
@map_a, @map_b = Map.rand(2)
@map_a, @map_b = Map.pair
@mash = Mash.new(
:requester => request.remote_ip,

View File

@@ -6,8 +6,39 @@ class Map < ActiveRecord::Base
self.find_or_initialize_by_name(city_name).save!
end
def self.rand(count = 2)
self.find(:all, :order => 'RANDOM()', :limit => count)
def self.pair
if Mash.count == 0
self.all(:order => 'RANDOM()', :limit => count)
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]
end
end
def self.import(csv_filename)
@@ -22,4 +53,24 @@ class Map < ActiveRecord::Base
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
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
end
def points
Mash.count(:conditions => ['winner = ?', self.id])
end
end