diff --git a/rails/map-mash/app/controllers/mash_tournaments_controller.rb b/rails/map-mash/app/controllers/mash_tournaments_controller.rb index 9df47ac..8fe9c82 100644 --- a/rails/map-mash/app/controllers/mash_tournaments_controller.rb +++ b/rails/map-mash/app/controllers/mash_tournaments_controller.rb @@ -39,9 +39,12 @@ class MashTournamentsController < ApplicationController @mash_tournament = MashTournament.new( :requester => Requester.new(:ip => request.remote_ip) ) - respond_to do |format| - if @mash_tournament.save && @mash_tournament.create_rounds_for_contenders(8) + if @mash_tournament.save && ( + @mash_tournament.builder.create_rounds_for_contenders(8) == 3 + ) && ( + @mash_tournament.builder.fill_in_next_round.length == 4 + ) flash[:notice] = "Let's start mashing!" format.html { redirect_to :controller => 'mashes', :action => 'new' } else diff --git a/rails/map-mash/app/controllers/mashes_controller.rb b/rails/map-mash/app/controllers/mashes_controller.rb index 91b6b16..5aa68d4 100644 --- a/rails/map-mash/app/controllers/mashes_controller.rb +++ b/rails/map-mash/app/controllers/mashes_controller.rb @@ -24,27 +24,18 @@ class MashesController < ApplicationController end end - def create + def update return if not already_registered?(request.remote_ip) - - mash_params = params[:mash].clone logger.info("Got params: #{params.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) - - @winner = Map.find(@mash.winner_id.to_i) - @winner.points += 1 + @mash = Mash.find(params[:id]) + @mash.winner = Map.find((params[:mash][:winner_id] || -1).to_i) logger.info("About to save #{@mash.inspect}") respond_to do |format| - if @winner.save && @mash.save - flash[:notice] = 'Mash was successfully created.' + if @mash.save + flash[:notice] = 'Mashed!' format.html { redirect_to(:action => 'new') } else format.html { render :action => 'new' } diff --git a/rails/map-mash/app/models/mash.rb b/rails/map-mash/app/models/mash.rb index bd70266..8846de2 100644 --- a/rails/map-mash/app/models/mash.rb +++ b/rails/map-mash/app/models/mash.rb @@ -5,11 +5,9 @@ class Mash < ActiveRecord::Base has_one :tournament, :class_name => 'MashTournament' has_one :round, :class_name => 'MashTournamentRound' - named_scope :unplayed, { - :conditions => %{ + named_scope :unplayed, :conditions => [%{ winner_id IS NULL AND map_a_id IS NOT NULL AND map_b_id IS NOT NULL - } - } + }] end diff --git a/rails/map-mash/app/models/mash_tournament.rb b/rails/map-mash/app/models/mash_tournament.rb index 03291be..8aba358 100644 --- a/rails/map-mash/app/models/mash_tournament.rb +++ b/rails/map-mash/app/models/mash_tournament.rb @@ -4,11 +4,21 @@ class MashTournament < ActiveRecord::Base has_many :rounds, :class_name => 'MashTournamentRound' def next_unplayed_mash - self.mashes.unplayed.first + mash = self.mashes.unplayed.first + if not mash + filled = self.builder.fill_in_next_round + if filled + return self.mashes.unplayed.first + else + return nil + end + else + return mash + end end def done? - true + self.rounds.collect(&:done?).uniq == [true] end def round(number = 0) @@ -16,4 +26,10 @@ class MashTournament < ActiveRecord::Base :conditions => {:number => number} ) end + + def builder + MashTournamentBuilder.new( + self, MashTournament, MashTournamentRound, Map, Mash + ) + end end diff --git a/rails/map-mash/app/views/mash_tournaments/show.html.erb b/rails/map-mash/app/views/mash_tournaments/show.html.erb deleted file mode 100644 index aaa5394..0000000 --- a/rails/map-mash/app/views/mash_tournaments/show.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -
Choose your fave.
- -<% form_for(@mash) do |f| %> - <%= f.error_messages %> - -" do - render - end -end