From 8ad33d18b70327b4f5f8cbe14ef20d2989aa23ec Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 9 Mar 2012 00:24:48 -0500 Subject: [PATCH] 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. --- .../app/controllers/mashes_controller.rb | 27 ++++++++---- rails/map-mash/app/models/map.rb | 42 ++++++++++++++++++- rails/map-mash/app/models/mash.rb | 4 ++ rails/map-mash/app/models/requester.rb | 2 + rails/map-mash/app/views/mashes/new.html.erb | 8 ++-- .../migrate/20120304164625_create_mashes.rb | 16 +++---- .../20120309000749_create_requesters.rb | 13 ++++++ rails/map-mash/db/schema.rb | 20 +++++---- rails/map-mash/spec/fixtures/requesters.yml | 7 ++++ rails/map-mash/spec/models/requester_spec.rb | 13 ++++++ 10 files changed, 123 insertions(+), 29 deletions(-) create mode 100644 rails/map-mash/app/models/requester.rb create mode 100644 rails/map-mash/db/migrate/20120309000749_create_requesters.rb create mode 100644 rails/map-mash/spec/fixtures/requesters.yml create mode 100644 rails/map-mash/spec/models/requester_spec.rb diff --git a/rails/map-mash/app/controllers/mashes_controller.rb b/rails/map-mash/app/controllers/mashes_controller.rb index 0d1a269..28df238 100644 --- a/rails/map-mash/app/controllers/mashes_controller.rb +++ b/rails/map-mash/app/controllers/mashes_controller.rb @@ -11,10 +11,9 @@ class MashesController < ApplicationController @map_a, @map_b = Map.pair @mash = Mash.new( - :requester => request.remote_ip, - :map_a => @map_a.id, - :map_b => @map_b.id, - :winner => @map_a.id + :map_a => @map_a, + :map_b => @map_b, + :winner => @map_a ) respond_to do |format| @@ -23,15 +22,25 @@ class MashesController < ApplicationController end def create - mash_params = params[:mash] - mash_params[:requester] = request.remote_ip - logger.info("Got mash params: #{mash_params.inspect}") + mash_params = params[:mash].clone + logger.info("Got params: #{params.inspect}") + + requester = Requester.find_or_initialize_by_ip(request.remote_ip) + requester.save! + requester.reload + logger.info("Setting mash.requester_id from #{requester.inspect}") + + logger.info("Creating mash with: #{mash_params.inspect}") @mash = Mash.new(mash_params) - @winner = Map.find(@mash.winner) + + @mash.requester_id = requester.id + @winner = Map.find(@mash.winner_id.to_i) @winner.points += 1 + logger.info("About to save #{@mash.inspect}") + respond_to do |format| - if @mash.save && @winner.save + if @winner.save && @mash.save flash[:notice] = 'Mash was successfully created.' format.html { redirect_to(:action => 'new') } else diff --git a/rails/map-mash/app/models/map.rb b/rails/map-mash/app/models/map.rb index 3ccd4b1..4eb3db2 100644 --- a/rails/map-mash/app/models/map.rb +++ b/rails/map-mash/app/models/map.rb @@ -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 diff --git a/rails/map-mash/app/models/mash.rb b/rails/map-mash/app/models/mash.rb index be0264f..91d0b41 100644 --- a/rails/map-mash/app/models/mash.rb +++ b/rails/map-mash/app/models/mash.rb @@ -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 diff --git a/rails/map-mash/app/models/requester.rb b/rails/map-mash/app/models/requester.rb new file mode 100644 index 0000000..2b1308b --- /dev/null +++ b/rails/map-mash/app/models/requester.rb @@ -0,0 +1,2 @@ +class Requester < ActiveRecord::Base +end diff --git a/rails/map-mash/app/views/mashes/new.html.erb b/rails/map-mash/app/views/mashes/new.html.erb index 70d0b6e..a1cc236 100644 --- a/rails/map-mash/app/views/mashes/new.html.erb +++ b/rails/map-mash/app/views/mashes/new.html.erb @@ -2,7 +2,7 @@