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.
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
2
rails/map-mash/app/models/requester.rb
Normal file
2
rails/map-mash/app/models/requester.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Requester < ActiveRecord::Base
|
||||
end
|
@@ -2,7 +2,7 @@
|
||||
<script type="text/javascript">
|
||||
$(function() {
|
||||
$('.mash-image').click(function(elem) {
|
||||
$('#mash_winner').val($(elem.target).attr('data-map-id'));
|
||||
$('#mash_winner_id').val($(elem.target).attr('data-map-id'));
|
||||
$('#new_mash').submit();
|
||||
});
|
||||
});
|
||||
@@ -24,9 +24,9 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= f.hidden_field :map_a, :value => @map_a.id %>
|
||||
<%= f.hidden_field :map_b, :value => @map_b.id %>
|
||||
<%= f.hidden_field :winner, :value => -1 %>
|
||||
<%= f.hidden_field :map_a_id, :value => @map_a.id %>
|
||||
<%= f.hidden_field :map_b_id, :value => @map_b.id %>
|
||||
<%= f.hidden_field :winner_id, :value => -1 %>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Back', mashes_path %>
|
||||
|
Reference in New Issue
Block a user