instead of crashing in mall spider when pet not found, delete pet and find a new one

This commit is contained in:
Emi Matchu 2011-05-21 22:32:01 -04:00
parent 49c3859a00
commit 3d05bbeeed
2 changed files with 35 additions and 20 deletions

View file

@ -453,8 +453,22 @@ class Item < ActiveRecord::Base
def load_for_pet_type(item, pet_type, banned_pet_ids=[]) def load_for_pet_type(item, pet_type, banned_pet_ids=[])
pet_id = pet_type.pet_id pet_id = pet_type.pet_id
pet_name = pet_type.pet_name pet_name = pet_type.pet_name
pet_valid = nil
begin
pet = Pet.load(pet_name) pet = Pet.load(pet_name)
if pet.pet_type == pet_type if pet.pet_type_id == pet_type.id
pet_valid = true
else
pet_valid = false
puts " - Pet #{pet_name} is pet type \##{pet.pet_type_id}, not \##{pet_type.id}; saving it and loading new pet"
pet.save!
end
rescue Pet::PetNotFound
pet_valid = false
puts " - Pet #{pet_name} no longer exists; destroying and loading new pet"
Pet.find_by_name(pet_name).destroy
end
if pet_valid
swf_assets = load_for_pet_name(item, pet_type, pet_name) swf_assets = load_for_pet_name(item, pet_type, pet_name)
if swf_assets if swf_assets
puts " - Modeled with #{pet_name}, saved assets (#{swf_assets.map(&:id).join(', ')})" puts " - Modeled with #{pet_name}, saved assets (#{swf_assets.map(&:id).join(', ')})"
@ -463,8 +477,6 @@ class Item < ActiveRecord::Base
end end
return swf_assets return swf_assets
else else
puts " - Pet #{pet_name} is pet type \##{pet.pet_type_id}, not \##{pet_type.id}; saving it and loading new pet"
pet.save
banned_pet_ids << pet_id banned_pet_ids << pet_id
new_pet = pet_type.pets.select([:id, :name]).where(Pet.arel_table[:id].not_in(banned_pet_ids)).first new_pet = pet_type.pets.select([:id, :name]).where(Pet.arel_table[:id].not_in(banned_pet_ids)).first
if new_pet if new_pet

View file

@ -63,12 +63,14 @@ class Pet < ActiveRecord::Base
before_validation do before_validation do
pet_type.save! pet_type.save!
@pet_state.save! @pet_state.save! if @pet_state
items.each do |item| if @items
@items.each do |item|
item.handle_assets! item.handle_assets!
item.save! item.save!
end end
end end
end
def self.load(name) def self.load(name)
pet = Pet.find_or_initialize_by_name(name) pet = Pet.find_or_initialize_by_name(name)
@ -93,3 +95,4 @@ class Pet < ActiveRecord::Base
class PetNotFound < Exception;end class PetNotFound < Exception;end
class DownloadError < Exception;end class DownloadError < Exception;end
end end