2023-08-02 16:05:02 -07:00
|
|
|
class Pet < ApplicationRecord
|
2010-10-07 07:46:23 -07:00
|
|
|
belongs_to :pet_type
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2024-01-24 03:25:23 -08:00
|
|
|
attr_reader :items, :pet_state, :alt_style
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2024-04-06 02:31:24 -07:00
|
|
|
def load!(timeout: nil)
|
2024-11-10 11:39:51 -08:00
|
|
|
raise ModelingDisabled unless Rails.configuration.modeling_enabled
|
|
|
|
|
2024-11-10 11:36:23 -08:00
|
|
|
viewer_data_hash = Neopets::CustomPets.fetch_viewer_data(name, timeout:)
|
|
|
|
use_viewer_data(ViewerData.new(viewer_data_hash))
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
2013-12-08 20:59:36 -08:00
|
|
|
|
2024-11-06 14:31:16 -08:00
|
|
|
def use_viewer_data(viewer_data)
|
2024-11-10 11:36:23 -08:00
|
|
|
self.pet_type = viewer_data.pet_type
|
|
|
|
@pet_state = viewer_data.pet_state
|
|
|
|
@alt_style = viewer_data.alt_style
|
|
|
|
@items = viewer_data.items
|
2013-12-08 20:59:36 -08:00
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2010-10-10 19:18:42 -07:00
|
|
|
def wardrobe_query
|
|
|
|
{
|
Oops, fix bug with saving outfits of pets loaded from Neopets.com
Okay right, the wardrobe-2020 app treats `state` as a bit of an
override thing, and `pose` is the main canonical field for how a pet
looks. We were missing a few pieces here:
1. After loading a pet, we weren't including the `pose` field in the
initial query string for the wardrobe URL, but we _were_ including
the `state` field, so the outfit would get set up with a conflicting
pet state ID vs pose.
2. When saving an outfit, we weren't taking the `state` field into
account at all. This could cause the saved outfit to not quite match
how it actually looked in-app, because the default pet state for
that species/color/pose trio could be different; and regardless, the
outfit state would come back with `appearanceId` set to `null`,
which wouldn't match the local outfit state, which would trigger an
infinite loop.
Here, we complete the round-trip of the `state` field, from pet loading
to outfit saving to the outfit data that comes back after saving!
2024-02-08 09:51:31 -08:00
|
|
|
name: self.name,
|
2024-02-16 23:22:41 -08:00
|
|
|
color: self.pet_type.color_id,
|
|
|
|
species: self.pet_type.species_id,
|
Oops, fix bug with saving outfits of pets loaded from Neopets.com
Okay right, the wardrobe-2020 app treats `state` as a bit of an
override thing, and `pose` is the main canonical field for how a pet
looks. We were missing a few pieces here:
1. After loading a pet, we weren't including the `pose` field in the
initial query string for the wardrobe URL, but we _were_ including
the `state` field, so the outfit would get set up with a conflicting
pet state ID vs pose.
2. When saving an outfit, we weren't taking the `state` field into
account at all. This could cause the saved outfit to not quite match
how it actually looked in-app, because the default pet state for
that species/color/pose trio could be different; and regardless, the
outfit state would come back with `appearanceId` set to `null`,
which wouldn't match the local outfit state, which would trigger an
infinite loop.
Here, we complete the round-trip of the `state` field, from pet loading
to outfit saving to the outfit data that comes back after saving!
2024-02-08 09:51:31 -08:00
|
|
|
pose: self.pet_state.pose,
|
|
|
|
state: self.pet_state.id,
|
|
|
|
objects: self.items.map(&:id),
|
2024-10-18 19:16:41 -07:00
|
|
|
style: self.alt_style ? self.alt_style.id : nil,
|
2010-10-10 19:18:42 -07:00
|
|
|
}.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
|
2024-01-24 03:54:43 -08:00
|
|
|
contributables = [pet_type, @pet_state, @alt_style].filter(&:present?)
|
2010-11-06 16:07:15 -07:00
|
|
|
items.each do |item|
|
|
|
|
contributables << item
|
|
|
|
contributables += item.pending_swf_assets
|
|
|
|
end
|
|
|
|
contributables
|
|
|
|
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!
|
2024-11-10 11:36:23 -08:00
|
|
|
@pet_state.save! if @pet_state
|
|
|
|
|
2024-11-06 14:31:16 -08:00
|
|
|
if @items
|
|
|
|
@items.each do |item|
|
|
|
|
item.save! if item.changed?
|
|
|
|
item.handle_assets!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @alt_style
|
|
|
|
@alt_style.save!
|
|
|
|
end
|
2010-11-06 15:08:42 -07:00
|
|
|
end
|
2011-05-21 19:32:01 -07:00
|
|
|
|
2024-04-06 02:31:24 -07:00
|
|
|
def self.load(name, **options)
|
2023-10-12 18:05:01 -07:00
|
|
|
pet = Pet.find_or_initialize_by(name: name)
|
2024-04-06 02:31:24 -07:00
|
|
|
pet.load!(**options)
|
2010-10-07 07:46:23 -07:00
|
|
|
pet
|
|
|
|
end
|
2024-10-18 18:14:01 -07:00
|
|
|
|
2024-11-02 21:34:19 -07:00
|
|
|
class UnexpectedDataFormat < RuntimeError;end
|
2024-11-10 11:39:51 -08:00
|
|
|
class ModelingDisabled < RuntimeError;end
|
2024-11-10 11:36:23 -08:00
|
|
|
|
|
|
|
# A representation of a Neopets::CustomPets viewer data response, translated
|
|
|
|
# to DTI's database models!
|
|
|
|
class ViewerData
|
|
|
|
def initialize(viewer_data_hash)
|
|
|
|
@custom_pet = viewer_data_hash[:custom_pet]
|
|
|
|
@object_info_registry = viewer_data_hash[:object_info_registry]
|
|
|
|
@object_asset_registry = viewer_data_hash[:object_asset_registry]
|
|
|
|
end
|
|
|
|
|
|
|
|
def pet_type
|
|
|
|
@pet_type ||= begin
|
|
|
|
raise UnexpectedDataFormat unless @custom_pet[:species_id]
|
|
|
|
raise UnexpectedDataFormat unless @custom_pet[:color_id]
|
|
|
|
raise UnexpectedDataFormat unless @custom_pet[:body_id]
|
|
|
|
|
|
|
|
@custom_pet => {species_id:, color_id:}
|
|
|
|
PetType.find_or_initialize_by(species_id:, color_id:).tap do |pet_type|
|
|
|
|
# Apply the pet's body ID to the pet type, unless it's wearing an alt
|
|
|
|
# style, in which case ignore it, because it's the *alt style*'s body ID.
|
|
|
|
# (This can theoretically cause a problem saving a new pet type when
|
|
|
|
# there's an alt style too!)
|
|
|
|
pet_type.body_id = @custom_pet[:body_id] unless @custom_pet[:alt_style]
|
|
|
|
if pet_type.body_id.nil?
|
|
|
|
raise UnexpectedDataFormat,
|
|
|
|
"can't process alt style on first occurrence of pet type"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Try using this pet for the pet type's thumbnail, but don't worry
|
|
|
|
# if it fails.
|
|
|
|
begin
|
|
|
|
pet_type.consider_pet_image(@custom_pet[:name])
|
|
|
|
rescue => error
|
|
|
|
Rails.logger.warn "Failed to load pet image: #{error.full_message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pet_state
|
|
|
|
@pet_state ||= begin
|
2024-11-03 12:16:27 -08:00
|
|
|
swf_asset_ids = biology_assets.map(&:remote_id)
|
2024-11-10 11:36:23 -08:00
|
|
|
pet_type.pet_states.find_or_initialize_by(swf_asset_ids:).tap do |pet_state|
|
|
|
|
pet_state.swf_assets = biology_assets
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def alt_style
|
|
|
|
@alt_style ||= begin
|
|
|
|
return nil unless @custom_pet[:alt_style]
|
|
|
|
raise UnexpectedDataFormat unless @custom_pet[:alt_color]
|
|
|
|
|
|
|
|
id = @custom_pet[:alt_style].to_i
|
|
|
|
AltStyle.find_or_initialize_by(id:).tap do |alt_style|
|
|
|
|
alt_style.assign_attributes(
|
|
|
|
color_id: @custom_pet[:alt_color].to_i,
|
|
|
|
species_id: @custom_pet[:species_id].to_i,
|
|
|
|
body_id: @custom_pet[:body_id].to_i,
|
|
|
|
swf_assets: alt_style_assets,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def items
|
|
|
|
@items ||= Item.collection_from_pet_type_and_registries(
|
|
|
|
pet_type, @object_info_registry, @object_asset_registry
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def biology_assets
|
|
|
|
@biology_assets ||= begin
|
|
|
|
biology = @custom_pet[:alt_style].present? ?
|
|
|
|
@custom_pet[:original_biology] :
|
|
|
|
@custom_pet[:biology_by_zone]
|
|
|
|
assets_from_biology(biology)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def alt_style_assets
|
|
|
|
raise UnexpectedDataFormat if @custom_pet[:biology_by_zone].empty?
|
|
|
|
assets_from_biology(@custom_pet[:biology_by_zone])
|
|
|
|
end
|
|
|
|
|
|
|
|
def assets_from_biology(biology)
|
|
|
|
raise UnexpectedDataFormat if biology.empty?
|
|
|
|
body_id = @custom_pet[:body_id].to_i
|
|
|
|
biology.values.map { |b| SwfAsset.from_biology_data(body_id, b) }
|
|
|
|
end
|
|
|
|
end
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|