Correctly handling multiple tournaments now, yay!

This commit is contained in:
Dan Buch 2012-03-12 23:28:38 -04:00
parent b14cdd3c9e
commit ec948315aa
6 changed files with 57 additions and 35 deletions

View File

@ -15,18 +15,26 @@ class MashTournamentsController < ApplicationController
end end
if not @mash_tournament.done? 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 end
logger.info("Mash tournament #{@mash_tournament.id} is done!")
respond_to do |format| respond_to do |format|
format.html format.html
end end
end end
def new 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| respond_to do |format|
format.html format.html
@ -34,32 +42,35 @@ class MashTournamentsController < ApplicationController
end end
def create 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| respond_to do |format|
if @mash_tournament.save && ( action_new = lambda { format.html { render :action => 'new' } }
@mash_tournament.builder.create_rounds_for_contenders(8) == 3
) && ( if @mash_tournament.save
@mash_tournament.builder.fill_in_next_round.length == 4 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!" flash[:notice] = "Let's start mashing!"
format.html { redirect_to :controller => 'mashes', :action => 'new' } format.html do
else
format.html { render :action => "new" }
end
end
end
private
def should_start_mashing?(ip)
if requester = Requester.find_by_ip(ip)
redirect_to :controller => 'mashes', :action => 'new' redirect_to :controller => 'mashes', :action => 'new'
return true
end end
else
false action_new.call
end
else
action_new.call
end
end
end end
end end

View File

@ -1,2 +1,9 @@
module MashTournamentsHelper module MashTournamentsHelper
def total_rounds_options_for_select
options = []
@mash_tournament.total_rounds_options.each do |n|
options << [n, n]
end
options
end
end end

View File

@ -17,6 +17,10 @@ class MashTournament < ActiveRecord::Base
end end
end end
def total_rounds_options
@total_rounds_options ||= [3, 4, 5, 6, 7]
end
def done? def done?
self.rounds.collect(&:done?).uniq == [true] self.rounds.collect(&:done?).uniq == [true]
end end

View File

@ -1,9 +0,0 @@
<h1>New Map Mash tournament!</h1>
<% form_for(@mash_tournament) do |f| %>
<%= f.error_messages %>
<p>
<%= f.submit 'Start' %>
</p>
<% end %>

View File

@ -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'

View File

@ -1,5 +1,5 @@
%h1 %h1
You chose Tournament #{@mash_tournament.id} Complete! You chose
%em #{@mash_tournament.rounds.reverse.first.mashes.first.winner.name}! %em #{@mash_tournament.rounds.reverse.first.mashes.first.winner.name}!
- @mash_tournament.rounds.reverse.each do |round| - @mash_tournament.rounds.reverse.each do |round|