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:
parent
ec6d9d5146
commit
8ad33d18b7
@ -11,10 +11,9 @@ class MashesController < ApplicationController
|
|||||||
@map_a, @map_b = Map.pair
|
@map_a, @map_b = Map.pair
|
||||||
|
|
||||||
@mash = Mash.new(
|
@mash = Mash.new(
|
||||||
:requester => request.remote_ip,
|
:map_a => @map_a,
|
||||||
:map_a => @map_a.id,
|
:map_b => @map_b,
|
||||||
:map_b => @map_b.id,
|
:winner => @map_a
|
||||||
:winner => @map_a.id
|
|
||||||
)
|
)
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
@ -23,15 +22,25 @@ class MashesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
mash_params = params[:mash]
|
mash_params = params[:mash].clone
|
||||||
mash_params[:requester] = request.remote_ip
|
logger.info("Got params: #{params.inspect}")
|
||||||
logger.info("Got mash params: #{mash_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)
|
@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
|
@winner.points += 1
|
||||||
|
|
||||||
|
logger.info("About to save #{@mash.inspect}")
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @mash.save && @winner.save
|
if @winner.save && @mash.save
|
||||||
flash[:notice] = 'Mash was successfully created.'
|
flash[:notice] = 'Mash was successfully created.'
|
||||||
format.html { redirect_to(:action => 'new') }
|
format.html { redirect_to(:action => 'new') }
|
||||||
else
|
else
|
||||||
|
@ -7,7 +7,43 @@ class Map < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.pair
|
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
|
end
|
||||||
|
|
||||||
def self.import(csv_filename)
|
def self.import(csv_filename)
|
||||||
@ -21,6 +57,10 @@ class Map < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.mashes
|
||||||
|
Mash.all(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
|
||||||
|
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
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
class Mash < ActiveRecord::Base
|
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
|
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">
|
<script type="text/javascript">
|
||||||
$(function() {
|
$(function() {
|
||||||
$('.mash-image').click(function(elem) {
|
$('.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();
|
$('#new_mash').submit();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -24,9 +24,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= f.hidden_field :map_a, :value => @map_a.id %>
|
<%= f.hidden_field :map_a_id, :value => @map_a.id %>
|
||||||
<%= f.hidden_field :map_b, :value => @map_b.id %>
|
<%= f.hidden_field :map_b_id, :value => @map_b.id %>
|
||||||
<%= f.hidden_field :winner, :value => -1 %>
|
<%= f.hidden_field :winner_id, :value => -1 %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<%= link_to 'Back', mashes_path %>
|
<%= link_to 'Back', mashes_path %>
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
class CreateMashes < ActiveRecord::Migration
|
class CreateMashes < ActiveRecord::Migration
|
||||||
def self.up
|
def self.up
|
||||||
create_table :mashes do |t|
|
create_table :mashes do |t|
|
||||||
t.string :requester, :null => false
|
t.integer :requester_id, :null => false
|
||||||
t.integer :map_a, :null => false
|
t.integer :map_a_id, :null => false
|
||||||
t.integer :map_b, :null => false
|
t.integer :map_b_id, :null => false
|
||||||
t.integer :winner, :null => false
|
t.integer :winner_id, :null => false
|
||||||
|
|
||||||
t.timestamps
|
t.timestamps
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index :mashes, [:winner]
|
add_index :mashes, [:winner_id]
|
||||||
add_index :mashes, [:requester]
|
add_index :mashes, [:requester_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.down
|
def self.down
|
||||||
remove_index :mashes, [:winner]
|
remove_index :mashes, [:winner_id]
|
||||||
remove_index :mashes, [:requester]
|
remove_index :mashes, [:requester_id]
|
||||||
drop_table :mashes
|
drop_table :mashes
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
class CreateRequesters < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
create_table :requesters do |t|
|
||||||
|
t.string :ip
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
drop_table :requesters
|
||||||
|
end
|
||||||
|
end
|
@ -9,7 +9,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# It's strongly recommended to check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(:version => 20120304164625) do
|
ActiveRecord::Schema.define(:version => 20120309000749) do
|
||||||
|
|
||||||
create_table "maps", :force => true do |t|
|
create_table "maps", :force => true do |t|
|
||||||
t.string "name", :null => false
|
t.string "name", :null => false
|
||||||
@ -22,15 +22,21 @@ ActiveRecord::Schema.define(:version => 20120304164625) do
|
|||||||
add_index "maps", ["points"], :name => "index_maps_on_points"
|
add_index "maps", ["points"], :name => "index_maps_on_points"
|
||||||
|
|
||||||
create_table "mashes", :force => true do |t|
|
create_table "mashes", :force => true do |t|
|
||||||
t.string "requester", :null => false
|
t.integer "requester_id", :null => false
|
||||||
t.integer "map_a", :null => false
|
t.integer "map_a_id", :null => false
|
||||||
t.integer "map_b", :null => false
|
t.integer "map_b_id", :null => false
|
||||||
t.integer "winner", :null => false
|
t.integer "winner_id", :null => false
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "mashes", ["requester"], :name => "index_mashes_on_requester"
|
add_index "mashes", ["requester_id"], :name => "index_mashes_on_requester_id"
|
||||||
add_index "mashes", ["winner"], :name => "index_mashes_on_winner"
|
add_index "mashes", ["winner_id"], :name => "index_mashes_on_winner_id"
|
||||||
|
|
||||||
|
create_table "requesters", :force => true do |t|
|
||||||
|
t.string "ip"
|
||||||
|
t.datetime "created_at"
|
||||||
|
t.datetime "updated_at"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
7
rails/map-mash/spec/fixtures/requesters.yml
vendored
Normal file
7
rails/map-mash/spec/fixtures/requesters.yml
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
ip: MyString
|
||||||
|
|
||||||
|
two:
|
||||||
|
ip: MyString
|
13
rails/map-mash/spec/models/requester_spec.rb
Normal file
13
rails/map-mash/spec/models/requester_spec.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Requester do
|
||||||
|
before(:each) do
|
||||||
|
@valid_attributes = {
|
||||||
|
:ip => "value for ip"
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should create a new instance given valid attributes" do
|
||||||
|
Requester.create!(@valid_attributes)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user