Correctly handling multiple tournaments now, yay!
This commit is contained in:
parent
b14cdd3c9e
commit
ec948315aa
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 %>
|
9
rails/map-mash/app/views/mash_tournaments/new.html.haml
Normal file
9
rails/map-mash/app/views/mash_tournaments/new.html.haml
Normal 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'
|
@ -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|
|
||||
|
Loading…
Reference in New Issue
Block a user