2011-05-31 07:36:32 -07:00
|
|
|
require 'rocketamf/remote_gateway'
|
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
class Pet < ActiveRecord::Base
|
|
|
|
GATEWAY_URL = 'http://www.neopets.com/amfphp/gateway.php'
|
2013-01-11 17:16:16 -08:00
|
|
|
PET_VIEWER = RocketAMF::RemoteGateway.new(GATEWAY_URL).
|
|
|
|
service('CustomPetService').action('getViewerData')
|
2010-10-07 07:46:23 -07:00
|
|
|
PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.'
|
2010-10-10 11:33:54 -07:00
|
|
|
WARDROBE_PATH = '/wardrobe'
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
belongs_to :pet_type
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2010-10-10 11:33:54 -07:00
|
|
|
attr_reader :items, :pet_state
|
2010-11-06 15:08:42 -07:00
|
|
|
attr_accessor :contributor
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2010-11-27 15:41:06 -08:00
|
|
|
scope :with_pet_type_color_ids, lambda { |color_ids|
|
|
|
|
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
|
|
|
|
}
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2013-01-13 18:10:01 -08:00
|
|
|
def load!(options={})
|
|
|
|
options[:item_scope] ||= Item.scoped
|
|
|
|
options[:locale] ||= I18n.default_locale
|
|
|
|
|
2013-01-25 13:09:48 -08:00
|
|
|
I18n.with_locale(options[:locale]) do
|
|
|
|
require 'ostruct'
|
|
|
|
begin
|
2013-01-27 18:43:08 -08:00
|
|
|
neopets_language_code = I18n.compatible_neopets_language_code_for(options[:locale])
|
2013-01-25 13:09:48 -08:00
|
|
|
envelope = PET_VIEWER.request([name, 0]).post(
|
|
|
|
:timeout => 2,
|
|
|
|
:headers => {
|
|
|
|
'Cookie' => "lang=#{neopets_language_code}"
|
|
|
|
}
|
2013-01-13 18:10:01 -08:00
|
|
|
)
|
2013-01-25 13:09:48 -08:00
|
|
|
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
|
|
|
|
raise DownloadError, e.message
|
|
|
|
end
|
|
|
|
contents = OpenStruct.new(envelope.messages[0].data.body)
|
|
|
|
pet_data = OpenStruct.new(contents.custom_pet)
|
|
|
|
|
|
|
|
# in case this is running in a thread, explicitly grab an ActiveRecord
|
|
|
|
# connection, to avoid connection conflicts
|
|
|
|
Pet.connection_pool.with_connection do
|
|
|
|
self.pet_type = PetType.find_or_initialize_by_species_id_and_color_id(
|
|
|
|
pet_data.species_id.to_i,
|
|
|
|
pet_data.color_id.to_i
|
|
|
|
)
|
|
|
|
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
|
|
|
|
@pet_state.label_by_pet(self, pet_data.owner)
|
|
|
|
@items = Item.collection_from_pet_type_and_registries(self.pet_type,
|
|
|
|
contents.object_info_registry, contents.object_asset_registry,
|
|
|
|
options[:item_scope])
|
|
|
|
end
|
2013-01-13 18:10:01 -08:00
|
|
|
end
|
2013-01-25 13:09:48 -08:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
true
|
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|
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
|
2010-10-10 11:33:54 -07:00
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|
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
|
2013-01-11 17:16:16 -08:00
|
|
|
|
|
|
|
def item_translation_candidates
|
|
|
|
{}.tap do |candidates|
|
|
|
|
if @items
|
|
|
|
@items.each do |item|
|
|
|
|
item.needed_translations.each do |locale|
|
|
|
|
candidates[locale] ||= []
|
|
|
|
candidates[locale] << item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-01-13 18:10:01 -08:00
|
|
|
|
|
|
|
def translate_items
|
|
|
|
candidates = self.item_translation_candidates
|
|
|
|
|
|
|
|
until candidates.empty?
|
|
|
|
last_pet_loaded = nil
|
|
|
|
reloaded_pets = Parallel.map(candidates.keys, :in_threads => 8) do |locale|
|
|
|
|
Rails.logger.info "Reloading #{name} in #{locale}"
|
2013-01-21 17:52:16 -08:00
|
|
|
reloaded_pet = Pet.load(name, :item_scope => Item.includes(:translations),
|
2013-01-17 20:16:34 -08:00
|
|
|
:locale => locale)
|
|
|
|
last_pet_loaded = reloaded_pet
|
2013-01-13 18:10:01 -08:00
|
|
|
end
|
2013-01-28 01:02:20 -08:00
|
|
|
reloaded_pets.each(&:save!)
|
2013-01-13 18:10:01 -08:00
|
|
|
previous_candidates = candidates
|
|
|
|
candidates = last_pet_loaded.item_translation_candidates
|
|
|
|
|
|
|
|
if previous_candidates == candidates
|
|
|
|
# This condition should never happen if Neopets responds with correct
|
|
|
|
# data, but, if Neopets somehow responds with incorrect data, this
|
|
|
|
# condition could throw us into an infinite loop if uncaught. Better
|
|
|
|
# safe than sorry when working with external services.
|
|
|
|
raise "No change when reloading #{name} for #{candidates}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|
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!
|
2012-01-13 13:56:31 -08:00
|
|
|
@pet_state.handle_assets!
|
2011-10-31 14:22:24 -07:00
|
|
|
end
|
|
|
|
|
2011-05-21 19:32:01 -07:00
|
|
|
if @items
|
|
|
|
@items.each do |item|
|
2013-01-28 00:10:25 -08:00
|
|
|
item.save! if item.changed?
|
2012-01-13 13:56:31 -08:00
|
|
|
item.handle_assets!
|
2011-05-21 19:32:01 -07:00
|
|
|
end
|
2010-11-06 15:08:42 -07:00
|
|
|
end
|
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2013-01-13 18:10:01 -08:00
|
|
|
def self.load(name, options={})
|
2010-10-07 07:46:23 -07:00
|
|
|
pet = Pet.find_or_initialize_by_name(name)
|
2013-01-13 18:10:01 -08:00
|
|
|
pet.load!(options)
|
2010-10-07 07:46:23 -07:00
|
|
|
pet
|
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
class PetNotFound < Exception;end
|
2011-02-19 19:49:13 -08:00
|
|
|
class DownloadError < Exception;end
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|