class MashesController < ApplicationController
  def redirect_to_new
    flash[:notice] = 'Sneaky pete.'
    redirect_to(:action => 'new')
  end

  alias_method :index, :redirect_to_new
  alias_method :show, :redirect_to_new

  def new
    @map_a, @map_b = Map.pair

    @mash = Mash.new(
      :map_a => @map_a,
      :map_b => @map_b,
      :winner => @map_a
    )

    respond_to do |format|
      format.html
    end
  end

  def create
    mash_params = params[:mash].clone
    logger.info("Got params: #{params.inspect}")

    requester = Requester.find_or_initialize_by_ip(request.remote_ip)
    requester.save!
    requester.reload
    logger.info("Setting mash.requester_id from #{requester.inspect}")

    logger.info("Creating mash with: #{mash_params.inspect}")
    @mash = Mash.new(mash_params)

    @mash.requester_id = requester.id
    @winner = Map.find(@mash.winner_id.to_i)
    @winner.points += 1

    logger.info("About to save #{@mash.inspect}")

    respond_to do |format|
      if @winner.save && @mash.save
        flash[:notice] = 'Mash was successfully created.'
        format.html { redirect_to(:action => 'new') }
      else
        format.html { render :action => 'new' }
      end
    end
  end
end
