parent
f0297f3101
commit
8ec802a2bf
@ -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
|
||||
@mash_tournament = MashTournament.find(params[:id])
|
||||
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
|
||||
private
|
||||
def should_start_mashing?(ip)
|
||||
if requester = Requester.find_by_ip(ip)
|
||||
redirect_to :controller => 'mashes', :action => 'new'
|
||||
return true
|
||||
end
|
||||
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
|
||||
|
@ -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
|
||||
|
@ -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
|
@ -1,3 +1,7 @@
|
||||
class Requester < ActiveRecord::Base
|
||||
has_many :mashes
|
||||
has_many :mash_tournaments
|
||||
|
||||
def current_tournament
|
||||
self.mash_tournaments.last
|
||||
end
|
||||
end
|
||||
|
@ -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 %>
|
@ -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' %>
|
||||
|
@ -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
|
||||
|
@ -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
|
@ -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
|
||||
|
@ -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
|
@ -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
|
@ -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
|
Loading…
Reference in new issue