box-o-sand/rails/map-mash/app/controllers/mash_tournaments_controller.rb

77 lines
2.1 KiB
Ruby
Raw Normal View History

class MashTournamentsController < ApplicationController
def index
@mash_tournaments = MashTournament.all
respond_to do |format|
format.html
end
end
def show
begin
@mash_tournament = MashTournament.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to :action => 'new' and return
end
if not @mash_tournament.done?
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
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(:requester => requester)
requester.save
respond_to do |format|
format.html
end
end
def create
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
respond_to do |format|
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
action_new.call
end
end
end
end