diff --git a/rails/map-mash/app/controllers/mash_tournaments_controller.rb b/rails/map-mash/app/controllers/mash_tournaments_controller.rb index 8fe9c82..1a24b34 100644 --- a/rails/map-mash/app/controllers/mash_tournaments_controller.rb +++ b/rails/map-mash/app/controllers/mash_tournaments_controller.rb @@ -15,18 +15,26 @@ class MashTournamentsController < ApplicationController end if not @mash_tournament.done? - return if should_start_mashing?(request.remote_ip) + redirect_to new_mash_path + flash[:notice] = "You're not quite done there." + return end + logger.info("Mash tournament #{@mash_tournament.id} is done!") respond_to do |format| format.html end end def new - return if should_start_mashing?(request.remote_ip) + requester = Requester.find_or_initialize_by_ip(request.remote_ip) + if requester.current_tournament && !requester.current_tournament.done? + flash[:notice] = "But you haven't finished this one yet :-(" + redirect_to new_mash_path and return + end - @mash_tournament = MashTournament.new + @mash_tournament = MashTournament.new(:requester => requester) + requester.save respond_to do |format| format.html @@ -34,32 +42,35 @@ class MashTournamentsController < ApplicationController end def create - return if should_start_mashing?(request.remote_ip) + if not (requester = Requester.find_by_ip(request.remote_ip)) + flash[:notice] = "Not so fast..." + redirect_to new_mash_tournament_path and return + end + if requester.current_tournament && !requester.current_tournament.done? + flash[:notice] = "Seriously, you haven't finished this one yet! :-P" + redirect_to new_mash_path and return + end + + @mash_tournament = MashTournament.new(:requester => requester) + rounds = params.fetch(:mash_tournament, {}).fetch(:total_rounds, 3).to_i - @mash_tournament = MashTournament.new( - :requester => Requester.new(:ip => request.remote_ip) - ) respond_to do |format| - 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' } + action_new = lambda { format.html { render :action => 'new' } } + + if @mash_tournament.save + builder = @mash_tournament.builder + if builder.create_rounds_for_contenders(2 ** rounds) == rounds and + builder.fill_in_next_round.length == (2 ** rounds) / 2 + flash[:notice] = "Let's start mashing!" + format.html do + redirect_to :controller => 'mashes', :action => 'new' + end + else + action_new.call + end else - format.html { render :action => "new" } + action_new.call end end end - - private - def should_start_mashing?(ip) - if requester = Requester.find_by_ip(ip) - redirect_to :controller => 'mashes', :action => 'new' - return true - end - - false - end end diff --git a/rails/map-mash/app/helpers/mash_tournaments_helper.rb b/rails/map-mash/app/helpers/mash_tournaments_helper.rb index caf8d9d..03ce2be 100644 --- a/rails/map-mash/app/helpers/mash_tournaments_helper.rb +++ b/rails/map-mash/app/helpers/mash_tournaments_helper.rb @@ -1,2 +1,9 @@ module MashTournamentsHelper + def total_rounds_options_for_select + options = [] + @mash_tournament.total_rounds_options.each do |n| + options << [n, n] + end + options + end end diff --git a/rails/map-mash/app/models/mash_tournament.rb b/rails/map-mash/app/models/mash_tournament.rb index 8aba358..520cc6c 100644 --- a/rails/map-mash/app/models/mash_tournament.rb +++ b/rails/map-mash/app/models/mash_tournament.rb @@ -17,6 +17,10 @@ class MashTournament < ActiveRecord::Base end end + def total_rounds_options + @total_rounds_options ||= [3, 4, 5, 6, 7] + end + def done? self.rounds.collect(&:done?).uniq == [true] end diff --git a/rails/map-mash/app/views/mash_tournaments/new.html.erb b/rails/map-mash/app/views/mash_tournaments/new.html.erb deleted file mode 100644 index c3c3894..0000000 --- a/rails/map-mash/app/views/mash_tournaments/new.html.erb +++ /dev/null @@ -1,9 +0,0 @@ -

New Map Mash tournament!

- -<% form_for(@mash_tournament) do |f| %> - <%= f.error_messages %> - -

- <%= f.submit 'Start' %> -

-<% end %> diff --git a/rails/map-mash/app/views/mash_tournaments/new.html.haml b/rails/map-mash/app/views/mash_tournaments/new.html.haml new file mode 100644 index 0000000..d42174a --- /dev/null +++ b/rails/map-mash/app/views/mash_tournaments/new.html.haml @@ -0,0 +1,9 @@ +%h1 New Map Mash tournament! + +- form_for(@mash_tournament) do |f| + = f.error_messages + + = f.label(:total_rounds) + = f.select(:total_rounds, total_rounds_options_for_select) + %p + = f.submit 'Start' diff --git a/rails/map-mash/app/views/mash_tournaments/show.html.haml b/rails/map-mash/app/views/mash_tournaments/show.html.haml index 4725321..74c647c 100644 --- a/rails/map-mash/app/views/mash_tournaments/show.html.haml +++ b/rails/map-mash/app/views/mash_tournaments/show.html.haml @@ -1,5 +1,5 @@ %h1 - You chose + Tournament #{@mash_tournament.id} Complete! You chose %em #{@mash_tournament.rounds.reverse.first.mashes.first.winner.name}! - @mash_tournament.rounds.reverse.each do |round|