From e1f1aa6038619aabd58f586c9de860bf3eba92de Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 10 Mar 2012 21:13:03 -0500 Subject: [PATCH] Trying to get round generation and fill figured out TDD style --- .../mash_tournaments_controller.rb | 2 +- rails/map-mash/app/models/mash_tournament.rb | 32 +++++++++++-------- .../db/migrate/20120304151123_create_maps.rb | 3 -- rails/map-mash/db/schema.rb | 4 +-- rails/map-mash/spec/blueprints.rb | 14 ++++++-- .../spec/models/mash_tournament_spec.rb | 26 +++++++++++++-- 6 files changed, 55 insertions(+), 26 deletions(-) diff --git a/rails/map-mash/app/controllers/mash_tournaments_controller.rb b/rails/map-mash/app/controllers/mash_tournaments_controller.rb index 0d28493..5466c1d 100644 --- a/rails/map-mash/app/controllers/mash_tournaments_controller.rb +++ b/rails/map-mash/app/controllers/mash_tournaments_controller.rb @@ -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 diff --git a/rails/map-mash/app/models/mash_tournament.rb b/rails/map-mash/app/models/mash_tournament.rb index 2d2e729..0a6c4aa 100644 --- a/rails/map-mash/app/models/mash_tournament.rb +++ b/rails/map-mash/app/models/mash_tournament.rb @@ -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}" + + round = 1 - while n_contenders > 2 + 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 diff --git a/rails/map-mash/db/migrate/20120304151123_create_maps.rb b/rails/map-mash/db/migrate/20120304151123_create_maps.rb index 9ef3bc5..0b9e4a2 100644 --- a/rails/map-mash/db/migrate/20120304151123_create_maps.rb +++ b/rails/map-mash/db/migrate/20120304151123_create_maps.rb @@ -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 diff --git a/rails/map-mash/db/schema.rb b/rails/map-mash/db/schema.rb index f925af9..120299c 100644 --- a/rails/map-mash/db/schema.rb +++ b/rails/map-mash/db/schema.rb @@ -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" diff --git a/rails/map-mash/spec/blueprints.rb b/rails/map-mash/spec/blueprints.rb index 8d69f0d..7155caa 100644 --- a/rails/map-mash/spec/blueprints.rb +++ b/rails/map-mash/spec/blueprints.rb @@ -3,9 +3,13 @@ require 'sham' require 'faker' +Sham.location do + "#{Faker::Address.city}, #{Faker::Address.uk_country}" +end -Map.blueprint do +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 diff --git a/rails/map-mash/spec/models/mash_tournament_spec.rb b/rails/map-mash/spec/models/mash_tournament_spec.rb index dfde883..f8a9b14 100644 --- a/rails/map-mash/spec/models/mash_tournament_spec.rb +++ b/rails/map-mash/spec/models/mash_tournament_spec.rb @@ -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