1
0
Fork 1
impress/app/models/pet.rb
Emi Matchu 5f31e38428 Reapply "Extract modeling ViewerData class into new Pet::ModelingSnapshot file"
This change was modified a bit after cherry-picking, to no longer
include the broken changes to item modeling in 9eaee4a.

(cherry picked from commit 90407403ba)
2024-11-10 11:43:54 -08:00

65 lines
1.5 KiB
Ruby

class Pet < ApplicationRecord
belongs_to :pet_type
attr_reader :items, :pet_state, :alt_style
def load!(timeout: nil)
raise ModelingDisabled unless Rails.configuration.modeling_enabled
viewer_data_hash = Neopets::CustomPets.fetch_viewer_data(name, timeout:)
use_modeling_snapshot(ModelingSnapshot.new(viewer_data_hash))
end
def use_modeling_snapshot(snapshot)
self.pet_type = snapshot.pet_type
@pet_state = snapshot.pet_state
@alt_style = snapshot.alt_style
@items = snapshot.items
end
def wardrobe_query
{
name: self.name,
color: self.pet_type.color_id,
species: self.pet_type.species_id,
pose: self.pet_state.pose,
state: self.pet_state.id,
objects: self.items.map(&:id),
style: self.alt_style ? self.alt_style.id : nil,
}.to_query
end
def contributables
contributables = [pet_type, @pet_state, @alt_style].filter(&:present?)
items.each do |item|
contributables << item
contributables += item.pending_swf_assets
end
contributables
end
before_validation do
pet_type.save!
@pet_state.save! if @pet_state
if @items
@items.each do |item|
item.save! if item.changed?
item.handle_assets!
end
end
if @alt_style
@alt_style.save!
end
end
def self.load(name, **options)
pet = Pet.find_or_initialize_by(name: name)
pet.load!(**options)
pet
end
class UnexpectedDataFormat < RuntimeError;end
class ModelingDisabled < RuntimeError;end
end