Add logging for alt style changes
There's something fishy going on with alt style IDs perhaps being reused? I want logs to be able to potentially track this down later on…
This commit is contained in:
parent
ec8d0fdbdc
commit
3dca3fe05a
2 changed files with 79 additions and 0 deletions
|
|
@ -52,12 +52,42 @@ class Pet::ModelingSnapshot
|
|||
|
||||
id = @custom_pet[:alt_style].to_i
|
||||
AltStyle.find_or_initialize_by(id:).tap do |alt_style|
|
||||
pet_name = @custom_pet[:name]
|
||||
|
||||
# Capture old asset IDs before assignment
|
||||
old_asset_ids = alt_style.swf_assets.map(&:remote_id).sort
|
||||
|
||||
# Assign new attributes and assets
|
||||
new_asset_ids = alt_style_assets.map(&:remote_id).sort
|
||||
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,
|
||||
)
|
||||
|
||||
# Log the modeling event using Rails' change tracking
|
||||
if alt_style.new_record?
|
||||
Rails.logger.info "[Alt Style Modeling] Created alt style " \
|
||||
"ID=#{id} for pet=#{pet_name}: " \
|
||||
"species_id=#{alt_style.species_id}, " \
|
||||
"color_id=#{alt_style.color_id}, " \
|
||||
"body_id=#{alt_style.body_id}, " \
|
||||
"asset_ids=#{new_asset_ids.inspect}"
|
||||
elsif alt_style.changes.any? || old_asset_ids != new_asset_ids
|
||||
changes = []
|
||||
changes << "species_id: #{alt_style.species_id_was} -> #{alt_style.species_id}" if alt_style.species_id_changed?
|
||||
changes << "color_id: #{alt_style.color_id_was} -> #{alt_style.color_id}" if alt_style.color_id_changed?
|
||||
changes << "body_id: #{alt_style.body_id_was} -> #{alt_style.body_id}" if alt_style.body_id_changed?
|
||||
changes << "asset_ids: #{old_asset_ids.inspect} -> #{new_asset_ids.inspect}" if old_asset_ids != new_asset_ids
|
||||
|
||||
Rails.logger.warn "[Alt Style Modeling] Updated alt style " \
|
||||
"ID=#{id} for pet=#{pet_name}. " \
|
||||
"CHANGED: #{changes.join(', ')}"
|
||||
else
|
||||
Rails.logger.info "[Alt Style Modeling] Loaded alt style " \
|
||||
"ID=#{id} for pet=#{pet_name} (no changes)"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -551,6 +551,17 @@ RSpec.describe Pet, type: :model do
|
|||
it("has no thumbnail yet") { expect(alt_style.thumbnail_url?).to be false }
|
||||
it("is saved when saving the pet") { pet.save!; should be_persisted }
|
||||
|
||||
it "logs creation of new alt style" do
|
||||
expect(Rails.logger).to receive(:info).with(
|
||||
a_string_matching(/\[Alt Style Modeling\] Created alt style ID=87458 for pet=Majal_Kita/)
|
||||
.and(matching(/species_id=20/))
|
||||
.and(matching(/color_id=62/))
|
||||
.and(matching(/body_id=378/))
|
||||
.and(matching(/asset_ids=\[56223\]/))
|
||||
)
|
||||
pet.alt_style
|
||||
end
|
||||
|
||||
describe "its assets" do
|
||||
subject(:assets) { alt_style.swf_assets }
|
||||
let(:asset_ids) { assets.map(&:remote_id) }
|
||||
|
|
@ -588,6 +599,13 @@ RSpec.describe Pet, type: :model do
|
|||
new_pet.save!; expect(alt_style.previous_changes).to be_empty
|
||||
end
|
||||
|
||||
it "logs re-modeling without changes" do
|
||||
expect(Rails.logger).to receive(:info).with(
|
||||
a_string_matching(/\[Alt Style Modeling\] Loaded alt style ID=87458 for pet=Majal_Kita \(no changes\)/)
|
||||
)
|
||||
new_pet.alt_style
|
||||
end
|
||||
|
||||
describe "its assets" do
|
||||
subject(:assets) { alt_style.swf_assets }
|
||||
|
||||
|
|
@ -599,6 +617,37 @@ RSpec.describe Pet, type: :model do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when an alt style with the same ID but different attributes already exists" do
|
||||
before do
|
||||
Pet.load("Blue_Jetsam").save!
|
||||
# Create an alt style with ID 87458 but different attributes
|
||||
# (simulating Neopets reusing an ID)
|
||||
wrong_color = Color.find_by_name!("Blue")
|
||||
wrong_species = Species.find_by_name!("Acara")
|
||||
AltStyle.create!(
|
||||
id: 87458,
|
||||
color_id: wrong_color.id,
|
||||
species_id: wrong_species.id,
|
||||
body_id: 999,
|
||||
thumbnail_url: "http://example.com/wrong.png"
|
||||
)
|
||||
end
|
||||
|
||||
subject(:pet) { Pet.load("Majal_Kita") }
|
||||
|
||||
it "logs a warning about updating existing alt style" do
|
||||
expect(Rails.logger).to receive(:warn).with(
|
||||
a_string_matching(/\[Alt Style Modeling\] Updated alt style ID=87458 for pet=Majal_Kita/)
|
||||
.and(matching(/CHANGED:/))
|
||||
.and(matching(/species_id: 1 -> 20/))
|
||||
.and(matching(/color_id: 8 -> 62/))
|
||||
.and(matching(/body_id: 999 -> 378/))
|
||||
.and(matching(/asset_ids: \[\] -> \[56223\]/))
|
||||
)
|
||||
pet.alt_style
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue