2010-10-10 19:18:42 -07:00
|
|
|
class PetsController < ApplicationController
|
2010-11-05 15:45:05 -07:00
|
|
|
rescue_from Pet::PetNotFound, :with => :pet_not_found
|
2011-02-19 19:49:13 -08:00
|
|
|
rescue_from PetType::DownloadError, SwfAsset::DownloadError, :with => :asset_download_error
|
|
|
|
rescue_from Pet::DownloadError, :with => :pet_download_error
|
2013-12-08 20:59:36 -08:00
|
|
|
|
|
|
|
protect_from_forgery except: :submit
|
2023-08-02 16:05:02 -07:00
|
|
|
before_action :local_only, only: :submit
|
2010-11-05 15:45:05 -07:00
|
|
|
|
|
|
|
def load
|
2011-01-27 13:35:46 -08:00
|
|
|
if params[:name] == '!'
|
|
|
|
redirect_to roulette_path
|
2010-11-06 16:07:15 -07:00
|
|
|
else
|
2011-01-27 13:35:46 -08:00
|
|
|
raise Pet::PetNotFound unless params[:name]
|
2015-07-07 09:27:13 -07:00
|
|
|
@pet = Pet.load(
|
|
|
|
params[:name],
|
|
|
|
:item_scope => Item.includes(:translations),
|
2023-10-12 22:18:01 -07:00
|
|
|
:timeout => 1
|
2015-07-07 09:27:13 -07:00
|
|
|
)
|
2013-12-08 20:59:36 -08:00
|
|
|
points = contribute(current_user, @pet)
|
2013-01-13 18:10:01 -08:00
|
|
|
|
2011-01-27 13:35:46 -08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2012-08-06 18:15:31 -07:00
|
|
|
path = destination + @pet.wardrobe_query
|
2011-01-27 13:35:46 -08:00
|
|
|
redirect_to path
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
2012-08-06 18:15:31 -07:00
|
|
|
render :json => {:points => points, :query => @pet.wardrobe_query}
|
2011-01-27 13:35:46 -08:00
|
|
|
end
|
2010-11-05 17:09:03 -07:00
|
|
|
end
|
|
|
|
end
|
2010-11-05 15:45:05 -07:00
|
|
|
end
|
2013-12-08 20:59:36 -08:00
|
|
|
|
|
|
|
def submit
|
|
|
|
viewer_data = HashWithIndifferentAccess.new(JSON.parse(params[:viewer_data]))
|
|
|
|
@pet = Pet.from_viewer_data(viewer_data, :item_scope => Item.includes(:translations))
|
|
|
|
@user = params[:user_id].present? ? User.find(params[:user_id]) : nil
|
|
|
|
render json: {points: contribute(@user, @pet)}
|
|
|
|
end
|
2010-11-05 15:45:05 -07:00
|
|
|
|
|
|
|
protected
|
2013-12-08 20:59:36 -08:00
|
|
|
|
|
|
|
def contribute(user, pet)
|
|
|
|
if user.present?
|
|
|
|
points = user.contribute! pet
|
|
|
|
else
|
|
|
|
pet.save!
|
|
|
|
points = true
|
|
|
|
end
|
|
|
|
pet.translate_items
|
|
|
|
points
|
|
|
|
end
|
2010-11-05 15:45:05 -07:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
def destination
|
|
|
|
case (params[:destination] || params[:origin])
|
2023-10-12 22:30:16 -07:00
|
|
|
when 'wardrobe' then wardrobe_path + '?'
|
2012-08-06 18:15:31 -07:00
|
|
|
when 'needed_items' then needed_items_path + '?'
|
|
|
|
else root_path + '#'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-05 15:45:05 -07:00
|
|
|
def pet_not_found
|
2013-01-09 20:05:07 -08:00
|
|
|
pet_load_error :long_message => t('pets.load.not_found'),
|
|
|
|
:status => :not_found
|
2011-02-19 19:09:12 -08:00
|
|
|
end
|
|
|
|
|
2011-02-19 19:49:13 -08:00
|
|
|
def asset_download_error(e)
|
2011-02-19 19:09:12 -08:00
|
|
|
Rails.logger.warn e.message
|
2013-01-09 20:05:07 -08:00
|
|
|
pet_load_error :long_message => t('pets.load.asset_download_error'),
|
|
|
|
:status => :gateway_timeout
|
2011-02-19 19:09:12 -08:00
|
|
|
end
|
|
|
|
|
2011-02-19 19:49:13 -08:00
|
|
|
def pet_download_error(e)
|
|
|
|
Rails.logger.warn e.message
|
2013-02-24 22:14:45 -08:00
|
|
|
Rails.logger.warn e.backtrace.join("\n")
|
2013-01-09 20:05:07 -08:00
|
|
|
pet_load_error :long_message => t('pets.load.pet_download_error'),
|
2011-02-19 19:49:13 -08:00
|
|
|
:status => :gateway_timeout
|
|
|
|
end
|
|
|
|
|
2011-02-19 19:09:12 -08:00
|
|
|
def pet_load_error(options)
|
2010-11-05 17:09:03 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
path = params[:origin] || root_path
|
|
|
|
path += "?name=#{params[:name]}"
|
2011-02-19 19:09:12 -08:00
|
|
|
redirect_to path, :alert => options[:long_message]
|
2010-11-05 17:09:03 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
2023-10-12 18:08:43 -07:00
|
|
|
render :json => options[:long_message], :status => options[:status]
|
2010-11-05 17:09:03 -07:00
|
|
|
end
|
|
|
|
end
|
2010-10-10 11:33:54 -07:00
|
|
|
end
|
|
|
|
end
|