Extract biology processing from AltStyle into Pet

I'm trying to pull more of the modeling code out of the individual
classes, and into this encapsulated preprocessing, so it's a lot more
in-one-place!
This commit is contained in:
Emi Matchu 2024-11-03 11:46:29 -08:00
parent c03e7446e3
commit 52ca41dbff
2 changed files with 13 additions and 16 deletions

View file

@ -74,15 +74,6 @@ class AltStyle < ApplicationRecord
Item.appearances_for(items, self, ...) Item.appearances_for(items, self, ...)
end end
def biology=(biology)
# TODO: This is very similar to what `PetState` does, but like… much much
# more compact? Idk if I'm missing something, or if I was just that much
# more clueless back when I wrote it, lol 😅
self.swf_assets = biology.values.map do |asset_data|
SwfAsset.from_biology_data(self.body_id, asset_data)
end
end
# At time of writing, most batches of Alt Styles thumbnails used a simple # At time of writing, most batches of Alt Styles thumbnails used a simple
# pattern for the item thumbnail URL, but that's not always the case anymore. # pattern for the item thumbnail URL, but that's not always the case anymore.
# For now, let's keep using this format as the default value when creating a # For now, let's keep using this format as the default value when creating a

View file

@ -110,9 +110,7 @@ class Pet < ApplicationRecord
def alt_style def alt_style
@alt_style ||= begin @alt_style ||= begin
return nil unless @custom_pet[:alt_style] return nil unless @custom_pet[:alt_style]
raise UnexpectedDataFormat unless @custom_pet[:alt_color] raise UnexpectedDataFormat unless @custom_pet[:alt_color]
raise UnexpectedDataFormat if @custom_pet[:biology_by_zone].empty?
id = @custom_pet[:alt_style].to_i id = @custom_pet[:alt_style].to_i
AltStyle.find_or_initialize_by(id:).tap do |alt_style| AltStyle.find_or_initialize_by(id:).tap do |alt_style|
@ -120,7 +118,7 @@ class Pet < ApplicationRecord
color_id: @custom_pet[:alt_color].to_i, color_id: @custom_pet[:alt_color].to_i,
species_id: @custom_pet[:species_id].to_i, species_id: @custom_pet[:species_id].to_i,
body_id: @custom_pet[:body_id].to_i, body_id: @custom_pet[:body_id].to_i,
biology: @custom_pet[:biology_by_zone], swf_assets: alt_style_assets,
) )
end end
end end
@ -139,12 +137,20 @@ class Pet < ApplicationRecord
biology = @custom_pet[:alt_style].present? ? biology = @custom_pet[:alt_style].present? ?
@custom_pet[:original_biology] : @custom_pet[:original_biology] :
@custom_pet[:biology_by_zone] @custom_pet[:biology_by_zone]
raise UnexpectedDataFormat if biology.empty? 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 body_id = @custom_pet[:body_id].to_i
biology.values.map { |b| SwfAsset.from_biology_data(body_id, b) } biology.values.map { |b| SwfAsset.from_biology_data(body_id, b) }
end end
end end
end
end end