2011-05-31 07:36:32 -07:00
|
|
|
require 'rocketamf/remote_gateway'
|
2013-01-28 13:18:11 -08:00
|
|
|
require 'ostruct'
|
2011-05-31 07:36:32 -07:00
|
|
|
|
2023-08-02 16:05:02 -07:00
|
|
|
class Pet < ApplicationRecord
|
Use secret NEOPETS_URL_ORIGIN to bypass HTTPS
Oops, neopets.com finally stopped accepting `http://` connections, so our AMFPHP requests stopped working! And our current dependencies make it hard to make modern HTTPS requests :(
Instead, we're doing this quick-fix: we have a connection who knows the internal address for the Neopets origin server behind their CDN, which *does* still accept `http://` requests!
So, when `NEOPETS_URL_ORIGIN` is specified in the secret `.env` file (not committed to the repository), we'll use it instead of `http://www.neopets.com`. However, we still have that in the code as a fallback, just to be a bit less surprising to some theoretical future dev so they can see the real error message, and to self-document a bit of what that value is semantically doing! (The documentation angle is more of why it's there, rather than an actual expectation that any actual person in the future will run the code and get the fallback.)
2022-08-02 20:46:47 -07:00
|
|
|
NEOPETS_URL_ORIGIN = ENV['NEOPETS_URL_ORIGIN'] || 'http://www.neopets.com'
|
|
|
|
GATEWAY_URL = NEOPETS_URL_ORIGIN + '/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
|
|
|
|
2023-07-22 14:04:01 -07:00
|
|
|
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))
|
|
|
|
}
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2013-01-13 18:10:01 -08:00
|
|
|
def load!(options={})
|
|
|
|
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
|
2010-10-07 07:46:23 -07:00
|
|
|
true
|
|
|
|
end
|
2013-12-08 20:59:36 -08:00
|
|
|
|
|
|
|
def use_viewer_data(viewer_data, options={})
|
|
|
|
options[:item_scope] ||= Item.scoped
|
|
|
|
|
|
|
|
pet_data = viewer_data[:custom_pet]
|
|
|
|
|
2023-10-12 18:05:01 -07:00
|
|
|
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
|
2013-01-28 13:18:11 -08:00
|
|
|
|
2015-07-27 10:23:46 -07:00
|
|
|
def fetch_viewer_data(timeout=4, locale=nil)
|
|
|
|
locale ||= I18n.default_locale
|
2013-01-28 13:18:11 -08:00
|
|
|
begin
|
2015-07-27 10:23:46 -07:00
|
|
|
neopets_language_code = I18n.compatible_neopets_language_code_for(locale)
|
2013-01-28 13:18:11 -08:00
|
|
|
envelope = PET_VIEWER.request([name, 0]).post(
|
2013-07-02 11:30:38 -07:00
|
|
|
:timeout => timeout,
|
2013-01-28 13:18:11 -08:00
|
|
|
: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
|
2013-01-28 13:18:11 -08:00
|
|
|
end
|
2013-03-05 13:41:55 -08:00
|
|
|
HashWithIndifferentAccess.new(envelope.messages[0].data.body)
|
2013-01-28 13:18:11 -08:00
|
|
|
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?
|
2013-01-28 13:18:11 -08:00
|
|
|
# Organize known items by ID
|
|
|
|
items_by_id = {}
|
|
|
|
@items.each { |i| items_by_id[i.id] = i }
|
|
|
|
|
|
|
|
# Fetch registry data in parallel
|
|
|
|
registries = Parallel.map(candidates.keys, :in_threads => 8) do |locale|
|
2015-07-27 10:23:46 -07:00
|
|
|
viewer_data = fetch_viewer_data(4, locale) # TODO: ew, don't copy-paste the default timeout
|
2013-03-05 13:41:55 -08:00
|
|
|
[locale, viewer_data[:object_info_registry]]
|
2013-01-13 18:10:01 -08:00
|
|
|
end
|
2013-01-28 13:18:11 -08:00
|
|
|
|
|
|
|
# Look up any newly applied items on this pet, just in case
|
|
|
|
new_item_ids = []
|
|
|
|
registries.each do |locale, registry|
|
|
|
|
registry.each do |item_id, item_info|
|
|
|
|
item_id = item_id.to_i
|
|
|
|
new_item_ids << item_id unless items_by_id.has_key?(item_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Item.includes(:translations).find(new_item_ids).each do |item|
|
|
|
|
items_by_id[item.id] = item
|
|
|
|
end
|
|
|
|
|
|
|
|
# Apply translations, and figure out what items are currently being worn
|
2013-01-28 15:02:33 -08:00
|
|
|
current_items = Set.new
|
2013-01-28 13:18:11 -08:00
|
|
|
registries.each do |locale, registry|
|
2015-07-27 10:23:46 -07:00
|
|
|
registry.each do |item_id, item_info|
|
|
|
|
item = items_by_id[item_id.to_i]
|
|
|
|
item.add_origin_registry_info(item_info, locale)
|
|
|
|
current_items << item
|
2013-01-28 13:18:11 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@items = current_items
|
|
|
|
Item.transaction { @items.each { |i| i.save! if i.changed? } }
|
|
|
|
|
2013-01-13 18:10:01 -08:00
|
|
|
previous_candidates = candidates
|
2013-01-28 13:18:11 -08:00
|
|
|
candidates = item_translation_candidates
|
2013-01-13 18:10:01 -08:00
|
|
|
|
|
|
|
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={})
|
2023-10-12 18:05:01 -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
|
|
|
|
2013-12-08 20:59:36 -08:00
|
|
|
def self.from_viewer_data(viewer_data, options={})
|
2023-10-12 18:05:01 -07:00
|
|
|
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
|
|
|
|
|
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
|
|
|
|