impress/app/models/pet.rb

123 lines
3.6 KiB
Ruby
Raw Normal View History

2011-05-31 07:36:32 -07:00
require 'rocketamf/remote_gateway'
require 'ostruct'
2011-05-31 07:36:32 -07:00
class Pet < ApplicationRecord
NEOPETS_URL_ORIGIN = ENV['NEOPETS_URL_ORIGIN'] || 'https://www.neopets.com'
GATEWAY_URL = NEOPETS_URL_ORIGIN + '/amfphp/gateway.php'
PET_VIEWER = RocketAMF::RemoteGateway.new(GATEWAY_URL).
service('CustomPetService').action('getViewerData')
PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.'
WARDROBE_PATH = '/wardrobe'
belongs_to :pet_type
attr_reader :items, :pet_state
scope :with_pet_type_color_ids, ->(color_ids) {
2010-11-27 15:41:06 -08:00
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
}
class ModelingDisabled < RuntimeError;end
2013-01-13 18:10:01 -08:00
def load!(options={})
raise ModelingDisabled
2013-01-13 18:10:01 -08:00
options[:locale] ||= I18n.default_locale
2013-12-08 20:59:36 -08:00
I18n.with_locale(options.delete(:locale)) do
use_viewer_data(fetch_viewer_data(options.delete(:timeout)), options)
2013-01-13 18:10:01 -08:00
end
true
end
2013-12-08 20:59:36 -08:00
def use_viewer_data(viewer_data, options={})
options[:item_scope] ||= Item.all
2013-12-08 20:59:36 -08:00
pet_data = viewer_data[:custom_pet]
self.pet_type = PetType.find_or_initialize_by(
species_id: pet_data[:species_id].to_i,
color_id: pet_data[:color_id].to_i
2013-12-08 20:59:36 -08:00
)
self.pet_type.body_id = pet_data[:body_id]
self.pet_type.origin_pet = self
biology = pet_data[:biology_by_zone]
biology[0] = nil # remove effects if present
@pet_state = self.pet_type.add_pet_state_from_biology! biology
@items = Item.collection_from_pet_type_and_registries(self.pet_type,
viewer_data[:object_info_registry], viewer_data[:object_asset_registry],
options[:item_scope])
end
2024-01-23 22:10:42 -08:00
# NOTE: Ideally pet requests shouldn't take this long, but Neopets can be
# slow sometimes! Since we're on the Falcon server, long timeouts shouldn't
# slow down the rest of the request queue, like it used to be in the past.
def fetch_viewer_data(timeout=10, locale=nil)
locale ||= I18n.default_locale
begin
neopets_language_code = I18n.compatible_neopets_language_code_for(locale)
envelope = PET_VIEWER.request([name, 0]).post(
2013-07-02 11:30:38 -07:00
:timeout => timeout,
:headers => {
'Cookie' => "lang=#{neopets_language_code}"
}
)
rescue RocketAMF::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist"
end
raise DownloadError, e.message
rescue RocketAMF::RemoteGateway::ConnectionError => e
2013-02-24 22:14:45 -08:00
raise DownloadError, e.message, e.backtrace
end
HashWithIndifferentAccess.new(envelope.messages[0].data.body)
end
2010-10-10 19:18:42 -07:00
def wardrobe_query
{
:name => self.name,
:color => self.pet_type.color.id,
:species => self.pet_type.species.id,
:state => self.pet_state.id,
:objects => self.items.map(&:id)
}.to_query
end
2010-11-06 16:07:15 -07:00
def contributables
contributables = [pet_type, @pet_state]
items.each do |item|
contributables << item
contributables += item.pending_swf_assets
end
contributables
end
2010-11-06 15:08:42 -07:00
before_validation do
2010-11-06 16:07:15 -07:00
pet_type.save!
2011-10-31 14:22:24 -07:00
if @pet_state
@pet_state.save!
@pet_state.handle_assets!
2011-10-31 14:22:24 -07:00
end
if @items
@items.each do |item|
2013-01-28 00:10:25 -08:00
item.save! if item.changed?
item.handle_assets!
end
2010-11-06 15:08:42 -07:00
end
end
2013-01-13 18:10:01 -08:00
def self.load(name, options={})
pet = Pet.find_or_initialize_by(name: name)
2013-01-13 18:10:01 -08:00
pet.load!(options)
pet
end
2013-12-08 20:59:36 -08:00
def self.from_viewer_data(viewer_data, options={})
pet = Pet.find_or_initialize_by(name: viewer_data[:custom_pet][:name])
2013-12-08 20:59:36 -08:00
pet.use_viewer_data(viewer_data, options)
pet
end
class PetNotFound < Exception;end
class DownloadError < Exception;end
end