Remove unused options when loading pets

Locale is the big one that's not really relevant anymore (I don't want
to be loading non-English item names anymore, now that we've simplified
to only support English like TNT has!), but there was also `item_scope`
and stuff.

The timeout option is technically not used in any call sites, but I
think that one's useful to leave around; timeout stuff is important,
and I don't want to rewrite it sometime if we need it again!
This commit is contained in:
Emi Matchu 2024-04-06 02:31:24 -07:00
parent 57dcc88b27
commit f0ac2adc78

View file

@ -17,20 +17,12 @@ class Pet < ApplicationRecord
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids)) joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
} }
def load!(options={}) def load!(timeout: nil)
options[:locale] ||= I18n.default_locale viewer_data = self.class.fetch_viewer_data(name, timeout:)
I18n.with_locale(options.delete(:locale)) do use_viewer_data(viewer_data)
use_viewer_data(
self.class.fetch_viewer_data(name, options.delete(:timeout)),
options,
)
end
true
end end
def use_viewer_data(viewer_data, options={}) def use_viewer_data(viewer_data)
options[:item_scope] ||= Item.all
pet_data = viewer_data[:custom_pet] pet_data = viewer_data[:custom_pet]
raise UnexpectedDataFormat unless pet_data[:species_id] raise UnexpectedDataFormat unless pet_data[:species_id]
@ -81,8 +73,7 @@ class Pet < ApplicationRecord
end end
@items = Item.collection_from_pet_type_and_registries(self.pet_type, @items = Item.collection_from_pet_type_and_registries(self.pet_type,
viewer_data[:object_info_registry], viewer_data[:object_asset_registry], viewer_data[:object_info_registry], viewer_data[:object_asset_registry])
options[:item_scope])
end end
def wardrobe_query def wardrobe_query
@ -124,31 +115,24 @@ class Pet < ApplicationRecord
end end
end end
def self.load(name, options={}) def self.load(name, **options)
pet = Pet.find_or_initialize_by(name: name) pet = Pet.find_or_initialize_by(name: name)
pet.load!(options) pet.load!(**options)
pet pet
end end
def self.from_viewer_data(viewer_data, options={}) def self.from_viewer_data(viewer_data)
pet = Pet.find_or_initialize_by(name: viewer_data[:custom_pet][:name]) pet = Pet.find_or_initialize_by(name: viewer_data[:custom_pet][:name])
pet.use_viewer_data(viewer_data, options) pet.use_viewer_data(viewer_data)
pet pet
end end
# NOTE: Ideally pet requests shouldn't take this long, but Neopets can be # 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 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. # slow down the rest of the request queue, like it used to be in the past.
def self.fetch_viewer_data(name, timeout=10, locale=nil) def self.fetch_viewer_data(name, timeout: 10)
locale ||= I18n.default_locale
begin begin
neopets_language_code = I18n.compatible_neopets_language_code_for(locale) envelope = PET_VIEWER.request([name, 0]).post(timeout: timeout)
envelope = PET_VIEWER.request([name, 0]).post(
:timeout => timeout,
:headers => {
'Cookie' => "lang=#{neopets_language_code}"
}
)
rescue RocketAMFExtensions::RemoteGateway::AMFError => e rescue RocketAMFExtensions::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist" raise PetNotFound, "Pet #{name.inspect} does not exist"