From b31926f16f5217c12744cbbcadc7d3b99fe1e6af Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 7 Mar 2012 21:22:35 -0500 Subject: [PATCH] Starting to fill in tournament ranking bits --- .../app/controllers/mashes_controller.rb | 2 +- rails/map-mash/app/models/map.rb | 55 ++++++++++++++++++- .../db/migrate/20120304151123_create_maps.rb | 3 + .../migrate/20120304164625_create_mashes.rb | 3 + rails/map-mash/db/schema.rb | 4 ++ 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/rails/map-mash/app/controllers/mashes_controller.rb b/rails/map-mash/app/controllers/mashes_controller.rb index 202243f..b7acf49 100644 --- a/rails/map-mash/app/controllers/mashes_controller.rb +++ b/rails/map-mash/app/controllers/mashes_controller.rb @@ -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, diff --git a/rails/map-mash/app/models/map.rb b/rails/map-mash/app/models/map.rb index 651c768..ca1e05c 100644 --- a/rails/map-mash/app/models/map.rb +++ b/rails/map-mash/app/models/map.rb @@ -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 diff --git a/rails/map-mash/db/migrate/20120304151123_create_maps.rb b/rails/map-mash/db/migrate/20120304151123_create_maps.rb index 2878d28..a8eab10 100644 --- a/rails/map-mash/db/migrate/20120304151123_create_maps.rb +++ b/rails/map-mash/db/migrate/20120304151123_create_maps.rb @@ -5,9 +5,12 @@ class CreateMaps < ActiveRecord::Migration t.timestamps end + + add_index :maps, :name end def self.down drop_table :maps + remove_index :maps, :name end end diff --git a/rails/map-mash/db/migrate/20120304164625_create_mashes.rb b/rails/map-mash/db/migrate/20120304164625_create_mashes.rb index 647b77a..6d5aecc 100644 --- a/rails/map-mash/db/migrate/20120304164625_create_mashes.rb +++ b/rails/map-mash/db/migrate/20120304164625_create_mashes.rb @@ -8,9 +8,12 @@ class CreateMashes < ActiveRecord::Migration t.timestamps end + + add_index :mashes, :winner end def self.down drop_table :mashes + remove_index :mashes, :winner end end diff --git a/rails/map-mash/db/schema.rb b/rails/map-mash/db/schema.rb index 288cd51..83c2415 100644 --- a/rails/map-mash/db/schema.rb +++ b/rails/map-mash/db/schema.rb @@ -17,6 +17,8 @@ ActiveRecord::Schema.define(:version => 20120304164625) do t.datetime "updated_at" end + add_index "maps", ["name"], :name => "index_maps_on_name" + create_table "mashes", :force => true do |t| t.string "requester", :null => false t.integer "map_a", :null => false @@ -26,4 +28,6 @@ ActiveRecord::Schema.define(:version => 20120304164625) do t.datetime "updated_at" end + add_index "mashes", ["winner"], :name => "index_mashes_on_winner" + end