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=[])
pet_id = pet_type.pet_id
pet_name = pet_type.pet_name
pet = Pet.load(pet_name)
if pet.pet_type == pet_type
pet_valid = nil
begin
pet = Pet.load(pet_name)
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)
if swf_assets
puts " - Modeled with #{pet_name}, saved assets (#{swf_assets.map(&:id).join(', ')})"
@ -463,8 +477,6 @@ class Item < ActiveRecord::Base
end
return swf_assets
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
new_pet = pet_type.pets.select([:id, :name]).where(Pet.arel_table[:id].not_in(banned_pet_ids)).first
if new_pet

View file

@ -4,16 +4,16 @@ class Pet < ActiveRecord::Base
PET_VIEWER_METHOD = 'getViewerData'
PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.'
WARDROBE_PATH = '/wardrobe'
belongs_to :pet_type
attr_reader :items, :pet_state
attr_accessor :contributor
scope :with_pet_type_color_ids, lambda { |color_ids|
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
}
def load!
require 'ostruct'
begin
@ -41,7 +41,7 @@ class Pet < ActiveRecord::Base
contents.object_info_registry, contents.object_asset_registry)
true
end
def wardrobe_query
{
:name => self.name,
@ -51,7 +51,7 @@ class Pet < ActiveRecord::Base
:objects => self.items.map(&:id)
}.to_query
end
def contributables
contributables = [pet_type, @pet_state]
items.each do |item|
@ -60,28 +60,30 @@ class Pet < ActiveRecord::Base
end
contributables
end
before_validation do
pet_type.save!
@pet_state.save!
items.each do |item|
item.handle_assets!
item.save!
@pet_state.save! if @pet_state
if @items
@items.each do |item|
item.handle_assets!
item.save!
end
end
end
def self.load(name)
pet = Pet.find_or_initialize_by_name(name)
pet.load!
pet
end
private
def self.amf_service
@amf_service ||= gateway.service AMF_SERVICE_NAME
end
def self.gateway
unless @gateway
require 'rocketamf/remote_gateway'
@ -89,7 +91,8 @@ class Pet < ActiveRecord::Base
end
@gateway
end
class PetNotFound < Exception;end
class DownloadError < Exception;end
end