diff --git a/app/models/pet.rb b/app/models/pet.rb index e03b0273..a4299a32 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -37,20 +37,35 @@ class Pet < ApplicationRecord raise UnexpectedDataFormat unless pet_data[:color_id] raise UnexpectedDataFormat unless pet_data[:body_id] + has_alt_style = pet_data[:alt_style].present? + self.pet_type = PetType.find_or_initialize_by( species_id: pet_data[:species_id].to_i, color_id: pet_data[:color_id].to_i ) - self.pet_type.body_id = pet_data[:body_id] self.pet_type.origin_pet = self - pet_state_biology = pet_data[:alt_style] ? - pet_data[:original_biology] : pet_data[:biology_by_zone] + # With an alt style, `body_id` in the biology data refers to the body ID of + # the *alt* style, not the usual pet type. (We have `original_biology` for + # *some* of the pet type's situation, but not it's body ID!) + # + # So, in the alt style case, don't update `body_id` - but if this is our + # first time seeing this pet type and it doesn't *have* a `body_id` yet, + # let's not be creating it without one. We'll need to model it without the + # alt style first. (I don't bother with a clear error message though 😅) + self.pet_type.body_id = pet_data[:body_id] unless has_alt_style + if self.pet_type.body_id.nil? + raise UnexpectedDataFormat, + "can't process alt style on first occurrence of pet type" + end + + pet_state_biology = has_alt_style ? pet_data[:original_biology] : + pet_data[:biology_by_zone] raise UnexpectedDataFormat if pet_state_biology.empty? pet_state_biology[0] = nil # remove effects if present @pet_state = self.pet_type.add_pet_state_from_biology! pet_state_biology - if pet_data[:alt_style] + if has_alt_style raise UnexpectedDataFormat unless pet_data[:alt_color] raise UnexpectedDataFormat if pet_data[:biology_by_zone].empty?