BUSTED: what a mess. trying to get tournament round generation bits working. ack.

This commit is contained in:
Dan Buch 2012-03-10 08:47:47 -05:00
parent f0297f3101
commit 8ec802a2bf
19 changed files with 241 additions and 387 deletions

View File

@ -1,85 +1,62 @@
class MashTournamentsController < ApplicationController
# GET /mash_tournaments
# GET /mash_tournaments.xml
def index
@mash_tournaments = MashTournament.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @mash_tournaments }
format.html
end
end
# GET /mash_tournaments/1
# GET /mash_tournaments/1.xml
def show
begin
@mash_tournament = MashTournament.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to :action => 'new' and return
end
if not @mash_tournament.done?
return if should_start_mashing?(request.remote_ip)
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @mash_tournament }
format.html
end
end
# GET /mash_tournaments/new
# GET /mash_tournaments/new.xml
def new
return if should_start_mashing?(request.remote_ip)
@mash_tournament = MashTournament.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @mash_tournament }
format.html
end
end
# GET /mash_tournaments/1/edit
def edit
@mash_tournament = MashTournament.find(params[:id])
end
# POST /mash_tournaments
# POST /mash_tournaments.xml
def create
@mash_tournament = MashTournament.new(params[:mash_tournament])
return if should_start_mashing?(request.remote_ip)
@mash_tournament = MashTournament.new(
:requester => Requester.new(:ip => request.remote_ip)
)
respond_to do |format|
if @mash_tournament.save
flash[:notice] = 'MashTournament was successfully created.'
format.html { redirect_to(@mash_tournament) }
format.xml { render :xml => @mash_tournament, :status => :created, :location => @mash_tournament }
flash[:notice] = "Let's start mashing!"
format.html { redirect_to :controller => 'mashes', :action => 'new' }
else
format.html { render :action => "new" }
format.xml { render :xml => @mash_tournament.errors, :status => :unprocessable_entity }
end
end
end
# PUT /mash_tournaments/1
# PUT /mash_tournaments/1.xml
def update
@mash_tournament = MashTournament.find(params[:id])
respond_to do |format|
if @mash_tournament.update_attributes(params[:mash_tournament])
flash[:notice] = 'MashTournament was successfully updated.'
format.html { redirect_to(@mash_tournament) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @mash_tournament.errors, :status => :unprocessable_entity }
end
end
private
def should_start_mashing?(ip)
if requester = Requester.find_by_ip(ip)
redirect_to :controller => 'mashes', :action => 'new'
return true
end
# DELETE /mash_tournaments/1
# DELETE /mash_tournaments/1.xml
def destroy
@mash_tournament = MashTournament.find(params[:id])
@mash_tournament.destroy
respond_to do |format|
format.html { redirect_to(mash_tournaments_url) }
format.xml { head :ok }
end
false
end
end

View File

@ -8,13 +8,16 @@ class MashesController < ApplicationController
alias_method :show, :redirect_to_new
def new
@map_a, @map_b = Map.pair
return if not already_registered?(request.remote_ip)
@mash = Mash.new(
:map_a => @map_a,
:map_b => @map_b,
:winner => @map_a
)
requester = Requester.find_by_ip(request.remote_ip)
@mash = requester.current_tournament.next_unplayed_mash
if not @mash
flash[:notice] = "You're done!"
redirect_to requester.current_tournament, :action => 'show'
return
end
respond_to do |format|
format.html
@ -22,18 +25,18 @@ class MashesController < ApplicationController
end
def create
return if not already_registered?(request.remote_ip)
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}")
requester = Requester.find_by_ip(request.remote_ip)
tournament = requester.mash_tournaments.first
mash_params[:tournament_id] = tournament.id
logger.info("Creating mash with: #{mash_params.inspect}")
@mash = Mash.new(mash_params)
@mash.requester_id = requester.id
@winner = Map.find(@mash.winner_id.to_i)
@winner.points += 1
@ -48,4 +51,13 @@ class MashesController < ApplicationController
end
end
end
def already_registered?(ip)
if not Requester.find_by_ip(ip)
redirect_to :controller => :mash_tournaments, :action => 'new'
return false
end
true
end
end

View File

@ -2,6 +2,8 @@ require 'fastercsv'
class Map < ActiveRecord::Base
belongs_to :mash, :class_name => 'Mash'
def self.from_city_name(city_name)
self.find_or_initialize_by_name(city_name).save!
end

View File

@ -2,5 +2,14 @@ class Mash < ActiveRecord::Base
has_one :map_a, :class_name => 'Map'
has_one :map_b, :class_name => 'Map'
has_one :winner, :class_name => 'Map'
belongs_to :requester
belongs_to :tournament, :class_name => 'MashTournament'
belongs_to :round, :class_name => 'MashTournamentRound'
named_scope :unplayed, {
:conditions => %{
winner_id IS NULL
AND map_a_id IS NOT NULL
AND map_b_id IS NOT NULL
}
}
end

View File

@ -1,2 +1,106 @@
class MashTournament < ActiveRecord::Base
belongs_to :requester
has_many :mashes
has_many :rounds, :class_name => 'MashTournamentRound'
after_create :create_rounds
after_save :maybe_fill_in_next_round
def next_unplayed_mash
self.mashes.unplayed.first
end
def done?
true
end
def round(number = 0)
MashTournamentRound.find_by_mash_tournament_id(self.id,
:conditions => {:number => number}
)
end
private
def create_rounds
n_contenders = Map.count
while n_contenders % 4 != 0
n_contenders -= 1
end
round = 0
while n_contenders > 2
create_round(round, n_contenders)
n_contenders = n_contenders / 2
round += 1
end
assign_maps_for_round_zero
self.total_rounds = round - 1
end
def create_round(round_number, n_contenders)
round = MashTournamentRound.new(
:mash_tournament_id => self.id,
:number => round_number,
:mash_count => n_contenders / 2
)
round.save!
n_contenders.times do
Mash.new(
:mash_tournament_id => self.id,
:mash_tournament_round_id => round.id
).save!
end
end
def maybe_fill_in_next_round
self.rounds.sort(&:number).each do |round|
if not round.done?
assign_maps_for_round(round)
return
end
end
end
def assign_maps_for_round(round)
previous = MashTournamentRound.find_by_mash_tournament_id_and_number(
self.id, round.number - 1
)
previous_winners = previous.mashes.collect(&:winner_id)
pool = Map.all(
:order => 'RANDOM()',
:conditions => ['id in ?', previous_winners]
)
round.mashes.each do |mash|
mash.update_attributes(
:map_a_id => pool.pop.id,
:map_b_id => pool.pop.id
)
end
end
def assign_maps_for_round_zero
round = MashTournamentRound.for_round(self.id, 0)
pool = Map.all(
:order => 'RANDOM()',
:limit => round.mash_count * 2
)
logger.info("Populating mashes from pool: #{pool.inspect}")
round.mashes.each do |mash|
map_a = pool.pop
map_b = pool.pop
logger.info("Assigning `map_a` from #{map_a.inspect}, " +
"`map_b` from #{map_b.inspect} to mash #{mash.inspect}")
logger.info("`map_a`.`id` = #{map_a.id}, `map_b`.`id` = #{map_b.id} ")
mash.update_attributes(
:map_a_id => map_a.id,
:map_b_id => map_b.id
)
end
end
end

View File

@ -0,0 +1,8 @@
class MashTournamentRound < ActiveRecord::Base
belongs_to :tournament, :class_name => 'MashTournament'
has_many :mashes
def self.for_round(tournament, round_number)
self.find_by_mash_tournament_id_and_number(tournament, round_number)
end
end

View File

@ -1,3 +1,7 @@
class Requester < ActiveRecord::Base
has_many :mashes
has_many :mash_tournaments
def current_tournament
self.mash_tournaments.last
end
end

View File

@ -1,11 +1,9 @@
<h1>New mash_tournament</h1>
<h1>New Map Mash tournament!</h1>
<% form_for(@mash_tournament) do |f| %>
<%= f.error_messages %>
<p>
<%= f.submit 'Create' %>
<%= f.submit 'Start' %>
</p>
<% end %>
<%= link_to 'Back', mash_tournaments_path %>

View File

@ -1,3 +1,8 @@
<ul>
<% @mash_tournament.mashes.each do |mash| %>
<li><%= mash.map_a.name %> vs. <%= mash.map_b.name %>, winner = <%= mash.winner.name %></li>
<% end %>
</ul>
<%= link_to 'Edit', edit_mash_tournament_path(@mash_tournament) %> |
<%= link_to 'Back', mash_tournaments_path %>
<%= link_to 'New!', :controller => 'mash_tournaments', :action => 'new' %>

View File

@ -3,5 +3,5 @@ ActionController::Routing::Routes.draw do |map|
map.resources :mashes
map.resources :mash_tournaments
map.root :controller => 'mashes', :action => 'index'
map.root :controller => 'mash_tournaments', :action => 'new'
end

View File

@ -1,21 +1,22 @@
class CreateMashes < ActiveRecord::Migration
def self.up
create_table :mashes do |t|
t.integer :requester_id, :null => false
t.integer :map_a_id, :null => false
t.integer :map_b_id, :null => false
t.integer :winner_id, :null => false
t.integer :mash_tournament_id, :null => false
t.integer :map_a_id
t.integer :map_b_id
t.integer :winner_id
t.integer :mash_tournament_round_id
t.timestamps
end
add_index :mashes, [:winner_id]
add_index :mashes, [:requester_id]
add_index :mashes, [:mash_tournament_id]
end
def self.down
remove_index :mashes, [:winner_id]
remove_index :mashes, [:requester_id]
remove_index :mashes, [:mash_tournament_id]
drop_table :mashes
end
end

View File

@ -2,15 +2,18 @@ class CreateMashTournaments < ActiveRecord::Migration
def self.up
create_table :mash_tournaments do |t|
t.references :requester, :null => false
t.integer :total_rounds, :default => 1
t.timestamps
end
add_index :mash_tournaments, [:requester_id]
add_index :mash_tournaments, [:total_rounds]
end
def self.down
drop_table :mash_tournaments
remove_index :mash_tournaments, [:total_rounds]
remove_index :mash_tournaments, [:requester_id]
drop_table :mash_tournaments
end
end

View File

@ -0,0 +1,20 @@
class CreateMashTournamentRounds < ActiveRecord::Migration
def self.up
create_table :mash_tournament_rounds do |t|
t.references :mash_tournament
t.integer :number, :null => false
t.integer :mash_count, :null => false
t.timestamps
end
add_index :mash_tournament_rounds, [:mash_tournament_id, :number]
add_index :mash_tournament_rounds, [:mash_count]
end
def self.down
remove_index :mash_tournament_rounds, [:mash_tournament_id, :number]
remove_index :mash_tournament_rounds, [:mash_count]
drop_table :mash_tournament_rounds
end
end

View File

@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120309130609) do
ActiveRecord::Schema.define(:version => 20120310035133) do
create_table "maps", :force => true do |t|
t.string "name", :null => false
@ -21,24 +21,38 @@ ActiveRecord::Schema.define(:version => 20120309130609) do
add_index "maps", ["name"], :name => "index_maps_on_name"
add_index "maps", ["points"], :name => "index_maps_on_points"
create_table "mash_tournament_rounds", :force => true do |t|
t.integer "mash_tournament_id"
t.integer "number", :null => false
t.integer "mash_count", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "mash_tournament_rounds", ["mash_count"], :name => "index_mash_tournament_rounds_on_mash_count"
add_index "mash_tournament_rounds", ["mash_tournament_id", "number"], :name => "index_mash_tournament_rounds_on_mash_tournament_id_and_number"
create_table "mash_tournaments", :force => true do |t|
t.integer "requester_id", :null => false
t.integer "total_rounds", :default => 1
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "mash_tournaments", ["requester_id"], :name => "index_mash_tournaments_on_requester_id"
add_index "mash_tournaments", ["total_rounds"], :name => "index_mash_tournaments_on_total_rounds"
create_table "mashes", :force => true do |t|
t.integer "requester_id", :null => false
t.integer "map_a_id", :null => false
t.integer "map_b_id", :null => false
t.integer "winner_id", :null => false
t.integer "mash_tournament_id", :null => false
t.integer "map_a_id"
t.integer "map_b_id"
t.integer "winner_id"
t.integer "mash_tournament_round_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "mashes", ["requester_id"], :name => "index_mashes_on_requester_id"
add_index "mashes", ["mash_tournament_id"], :name => "index_mashes_on_mash_tournament_id"
add_index "mashes", ["winner_id"], :name => "index_mashes_on_winner_id"
create_table "requesters", :force => true do |t|

View File

@ -2,125 +2,15 @@ require 'spec_helper'
describe MapsController do
def mock_map(stubs={})
@mock_map ||= mock_model(Map, stubs)
end
describe "GET index" do
it "assigns all maps as @maps" do
Map.stub(:find).with(:all).and_return([mock_map])
get :index
assigns[:maps].should == [mock_map]
end
end
describe "GET show" do
end
describe "GET new" do
it "assigns a new map as @map" do
Map.stub(:new).and_return(mock_map)
get :new
assigns[:map].should equal(mock_map)
end
end
describe "GET edit" do
it "assigns the requested map as @map" do
Map.stub(:find).with("37").and_return(mock_map)
get :edit, :id => "37"
assigns[:map].should equal(mock_map)
end
end
describe "POST create" do
describe "with valid params" do
it "assigns a newly created map as @map" do
Map.stub(:new).with({'these' => 'params'}).and_return(mock_map(:save => true))
post :create, :map => {:these => 'params'}
assigns[:map].should equal(mock_map)
end
it "redirects to the created map" do
Map.stub(:new).and_return(mock_map(:save => true))
post :create, :map => {}
response.should redirect_to(map_url(mock_map))
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved map as @map" do
Map.stub(:new).with({'these' => 'params'}).and_return(mock_map(:save => false))
post :create, :map => {:these => 'params'}
assigns[:map].should equal(mock_map)
end
it "re-renders the 'new' template" do
Map.stub(:new).and_return(mock_map(:save => false))
post :create, :map => {}
response.should render_template('new')
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested map" do
Map.should_receive(:find).with("37").and_return(mock_map)
mock_map.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :map => {:these => 'params'}
end
it "assigns the requested map as @map" do
Map.stub(:find).and_return(mock_map(:update_attributes => true))
put :update, :id => "1"
assigns[:map].should equal(mock_map)
end
it "redirects to the map" do
Map.stub(:find).and_return(mock_map(:update_attributes => true))
put :update, :id => "1"
response.should redirect_to(map_url(mock_map))
end
end
describe "with invalid params" do
it "updates the requested map" do
Map.should_receive(:find).with("37").and_return(mock_map)
mock_map.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :map => {:these => 'params'}
end
it "assigns the map as @map" do
Map.stub(:find).and_return(mock_map(:update_attributes => false))
put :update, :id => "1"
assigns[:map].should equal(mock_map)
end
it "re-renders the 'edit' template" do
Map.stub(:find).and_return(mock_map(:update_attributes => false))
put :update, :id => "1"
response.should render_template('edit')
end
end
end
describe "DELETE destroy" do
it "destroys the requested map" do
Map.should_receive(:find).with("37").and_return(mock_map)
mock_map.should_receive(:destroy)
delete :destroy, :id => "37"
end
it "redirects to the maps list" do
Map.stub(:find).and_return(mock_map(:destroy => true))
delete :destroy, :id => "1"
response.should redirect_to(maps_url)
end
end
end

View File

@ -1,131 +1,15 @@
require 'spec_helper'
describe MashTournamentsController do
def mock_mash_tournament(stubs={})
@mock_mash_tournament ||= mock_model(MashTournament, stubs)
end
describe "GET index" do
it "assigns all mash_tournaments as @mash_tournaments" do
MashTournament.stub(:find).with(:all).and_return([mock_mash_tournament])
get :index
assigns[:mash_tournaments].should == [mock_mash_tournament]
end
end
describe "GET show" do
it "assigns the requested mash_tournament as @mash_tournament" do
MashTournament.stub(:find).with("37").and_return(mock_mash_tournament)
get :show, :id => "37"
assigns[:mash_tournament].should equal(mock_mash_tournament)
end
end
describe "GET new" do
it "assigns a new mash_tournament as @mash_tournament" do
MashTournament.stub(:new).and_return(mock_mash_tournament)
get :new
assigns[:mash_tournament].should equal(mock_mash_tournament)
end
end
describe "GET edit" do
it "assigns the requested mash_tournament as @mash_tournament" do
MashTournament.stub(:find).with("37").and_return(mock_mash_tournament)
get :edit, :id => "37"
assigns[:mash_tournament].should equal(mock_mash_tournament)
end
end
describe "POST create" do
describe "with valid params" do
it "assigns a newly created mash_tournament as @mash_tournament" do
MashTournament.stub(:new).with({'these' => 'params'}).and_return(mock_mash_tournament(:save => true))
post :create, :mash_tournament => {:these => 'params'}
assigns[:mash_tournament].should equal(mock_mash_tournament)
end
it "redirects to the created mash_tournament" do
MashTournament.stub(:new).and_return(mock_mash_tournament(:save => true))
post :create, :mash_tournament => {}
response.should redirect_to(mash_tournament_url(mock_mash_tournament))
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved mash_tournament as @mash_tournament" do
MashTournament.stub(:new).with({'these' => 'params'}).and_return(mock_mash_tournament(:save => false))
post :create, :mash_tournament => {:these => 'params'}
assigns[:mash_tournament].should equal(mock_mash_tournament)
end
it "re-renders the 'new' template" do
MashTournament.stub(:new).and_return(mock_mash_tournament(:save => false))
post :create, :mash_tournament => {}
response.should render_template('new')
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested mash_tournament" do
MashTournament.should_receive(:find).with("37").and_return(mock_mash_tournament)
mock_mash_tournament.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :mash_tournament => {:these => 'params'}
end
it "assigns the requested mash_tournament as @mash_tournament" do
MashTournament.stub(:find).and_return(mock_mash_tournament(:update_attributes => true))
put :update, :id => "1"
assigns[:mash_tournament].should equal(mock_mash_tournament)
end
it "redirects to the mash_tournament" do
MashTournament.stub(:find).and_return(mock_mash_tournament(:update_attributes => true))
put :update, :id => "1"
response.should redirect_to(mash_tournament_url(mock_mash_tournament))
end
end
describe "with invalid params" do
it "updates the requested mash_tournament" do
MashTournament.should_receive(:find).with("37").and_return(mock_mash_tournament)
mock_mash_tournament.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :mash_tournament => {:these => 'params'}
end
it "assigns the mash_tournament as @mash_tournament" do
MashTournament.stub(:find).and_return(mock_mash_tournament(:update_attributes => false))
put :update, :id => "1"
assigns[:mash_tournament].should equal(mock_mash_tournament)
end
it "re-renders the 'edit' template" do
MashTournament.stub(:find).and_return(mock_mash_tournament(:update_attributes => false))
put :update, :id => "1"
response.should render_template('edit')
end
end
end
describe "DELETE destroy" do
it "destroys the requested mash_tournament" do
MashTournament.should_receive(:find).with("37").and_return(mock_mash_tournament)
mock_mash_tournament.should_receive(:destroy)
delete :destroy, :id => "37"
end
it "redirects to the mash_tournaments list" do
MashTournament.stub(:find).and_return(mock_mash_tournament(:destroy => true))
delete :destroy, :id => "1"
response.should redirect_to(mash_tournaments_url)
end
end
end

View File

@ -1,26 +0,0 @@
require 'spec_helper'
describe "/mashes/edit.html.erb" do
include MashesHelper
before(:each) do
assigns[:mash] = @mash = stub_model(Mash,
:new_record? => false,
:requester => "value for requester",
:map_a => 1,
:map_b => 1,
:winner => 1
)
end
it "renders the edit mash form" do
render
response.should have_tag("form[action=#{mash_path(@mash)}][method=post]") do
with_tag('input#mash_requester[name=?]', "mash[requester]")
with_tag('input#mash_map_a[name=?]', "mash[map_a]")
with_tag('input#mash_map_b[name=?]', "mash[map_b]")
with_tag('input#mash_winner[name=?]', "mash[winner]")
end
end
end

View File

@ -1,30 +0,0 @@
require 'spec_helper'
describe "/mashes/index.html.erb" do
include MashesHelper
before(:each) do
assigns[:mashes] = [
stub_model(Mash,
:requester => "value for requester",
:map_a => 1,
:map_b => 1,
:winner => 1
),
stub_model(Mash,
:requester => "value for requester",
:map_a => 1,
:map_b => 1,
:winner => 1
)
]
end
it "renders a list of mashes" do
render
response.should have_tag("tr>td", "value for requester".to_s, 2)
response.should have_tag("tr>td", 1.to_s, 2)
response.should have_tag("tr>td", 1.to_s, 2)
response.should have_tag("tr>td", 1.to_s, 2)
end
end

View File

@ -1,21 +0,0 @@
require 'spec_helper'
describe "/mashes/show.html.erb" do
include MashesHelper
before(:each) do
assigns[:mash] = @mash = stub_model(Mash,
:requester => "value for requester",
:map_a => 1,
:map_b => 1,
:winner => 1
)
end
it "renders attributes in <p>" do
render
response.should have_text(/value\ for\ requester/)
response.should have_text(/1/)
response.should have_text(/1/)
response.should have_text(/1/)
end
end