Trying to get round generation and fill figured out TDD style
This commit is contained in:
parent
f177ecde2a
commit
e1f1aa6038
@ -41,7 +41,7 @@ class MashTournamentsController < ApplicationController
|
||||
)
|
||||
|
||||
respond_to do |format|
|
||||
if @mash_tournament.save
|
||||
if @mash_tournament.save && @mash_tournament.create_rounds
|
||||
flash[:notice] = "Let's start mashing!"
|
||||
format.html { redirect_to :controller => 'mashes', :action => 'new' }
|
||||
else
|
||||
|
@ -3,7 +3,7 @@ class MashTournament < ActiveRecord::Base
|
||||
has_many :mashes
|
||||
has_many :rounds, :class_name => 'MashTournamentRound'
|
||||
#after_create :create_rounds
|
||||
#after_save :maybe_fill_in_next_round
|
||||
#after_save :fill_in_next_round
|
||||
|
||||
def next_unplayed_mash
|
||||
self.mashes.unplayed.first
|
||||
@ -19,25 +19,38 @@ class MashTournament < ActiveRecord::Base
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
def create_rounds
|
||||
n_contenders = Map.count
|
||||
|
||||
#ap "Original n_contenders = #{n_contenders}"
|
||||
while n_contenders % 4 != 0
|
||||
n_contenders -= 1
|
||||
end
|
||||
|
||||
round = 0
|
||||
#ap "Resulting n_contenders divisible by 4 = #{n_contenders}"
|
||||
|
||||
while n_contenders > 2
|
||||
round = 1
|
||||
|
||||
while n_contenders > 1
|
||||
#ap "Generating round #{round}, n_contenders = #{n_contenders}"
|
||||
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 fill_in_next_round
|
||||
self.rounds.sort(&:number).each do |round|
|
||||
if not round.done?
|
||||
assign_maps_for_round(round)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def create_round(round_number, n_contenders)
|
||||
round = MashTournamentRound.new(
|
||||
:mash_tournament_id => self.id,
|
||||
@ -54,15 +67,6 @@ class MashTournament < ActiveRecord::Base
|
||||
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
|
||||
|
@ -2,18 +2,15 @@ class CreateMaps < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :maps do |t|
|
||||
t.string :name, :null => false
|
||||
t.integer :points, :default => 0
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :maps, [:name]
|
||||
add_index :maps, [:points]
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_index :maps, [:name]
|
||||
remove_index :maps, [:points]
|
||||
drop_table :maps
|
||||
end
|
||||
end
|
||||
|
@ -12,14 +12,12 @@
|
||||
ActiveRecord::Schema.define(:version => 20120310035133) do
|
||||
|
||||
create_table "maps", :force => true do |t|
|
||||
t.string "name", :null => false
|
||||
t.integer "points", :default => 0
|
||||
t.string "name", :null => false
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
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"
|
||||
|
@ -3,9 +3,13 @@ require 'sham'
|
||||
require 'faker'
|
||||
|
||||
|
||||
Sham.location do
|
||||
"#{Faker::Address.city}, #{Faker::Address.uk_country}"
|
||||
end
|
||||
|
||||
|
||||
Map.blueprint do
|
||||
|
||||
name { Sham.location }
|
||||
end
|
||||
|
||||
|
||||
@ -25,5 +29,11 @@ end
|
||||
|
||||
|
||||
Requester.blueprint do
|
||||
|
||||
ip do
|
||||
parts = []
|
||||
4.times do
|
||||
parts << (rand * 100).to_i.to_s
|
||||
end
|
||||
parts.join('.')
|
||||
end
|
||||
end
|
||||
|
@ -2,12 +2,32 @@ require 'spec_helper'
|
||||
|
||||
describe MashTournament do
|
||||
before(:each) do
|
||||
@valid_attributes = {
|
||||
:requester_id => 1
|
||||
}
|
||||
@valid_attributes = {:requester_id => 1}
|
||||
end
|
||||
|
||||
it "should create a new instance given valid attributes" do
|
||||
MashTournament.create!(@valid_attributes)
|
||||
end
|
||||
|
||||
context 'creating rounds' do
|
||||
before(:all) do
|
||||
Map.destroy_all
|
||||
8.times do
|
||||
Map.make.save
|
||||
end
|
||||
@requester = Requester.make
|
||||
@requester.save!
|
||||
end
|
||||
|
||||
before(:each) do
|
||||
subject.requester_id = @requester.id
|
||||
subject.save!
|
||||
subject.reload
|
||||
end
|
||||
|
||||
it 'should create 3 rounds for 8 contenders' do
|
||||
subject.create_rounds
|
||||
subject.total_rounds.should == 3
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user