forked from OpenNeo/impress
instead of crashing in mall spider when pet not found, delete pet and find a new one
This commit is contained in:
parent
49c3859a00
commit
3d05bbeeed
2 changed files with 35 additions and 20 deletions
|
@ -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 = Pet.load(pet_name)
|
pet_valid = nil
|
||||||
if pet.pet_type == pet_type
|
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)
|
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
|
||||||
|
|
|
@ -4,16 +4,16 @@ class Pet < ActiveRecord::Base
|
||||||
PET_VIEWER_METHOD = 'getViewerData'
|
PET_VIEWER_METHOD = 'getViewerData'
|
||||||
PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.'
|
PET_NOT_FOUND_REMOTE_ERROR = 'PHP: Unable to retrieve records from the database.'
|
||||||
WARDROBE_PATH = '/wardrobe'
|
WARDROBE_PATH = '/wardrobe'
|
||||||
|
|
||||||
belongs_to :pet_type
|
belongs_to :pet_type
|
||||||
|
|
||||||
attr_reader :items, :pet_state
|
attr_reader :items, :pet_state
|
||||||
attr_accessor :contributor
|
attr_accessor :contributor
|
||||||
|
|
||||||
scope :with_pet_type_color_ids, lambda { |color_ids|
|
scope :with_pet_type_color_ids, lambda { |color_ids|
|
||||||
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
|
joins(:pet_type).where(PetType.arel_table[:id].in(color_ids))
|
||||||
}
|
}
|
||||||
|
|
||||||
def load!
|
def load!
|
||||||
require 'ostruct'
|
require 'ostruct'
|
||||||
begin
|
begin
|
||||||
|
@ -41,7 +41,7 @@ class Pet < ActiveRecord::Base
|
||||||
contents.object_info_registry, contents.object_asset_registry)
|
contents.object_info_registry, contents.object_asset_registry)
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def wardrobe_query
|
def wardrobe_query
|
||||||
{
|
{
|
||||||
:name => self.name,
|
:name => self.name,
|
||||||
|
@ -51,7 +51,7 @@ class Pet < ActiveRecord::Base
|
||||||
:objects => self.items.map(&:id)
|
:objects => self.items.map(&:id)
|
||||||
}.to_query
|
}.to_query
|
||||||
end
|
end
|
||||||
|
|
||||||
def contributables
|
def contributables
|
||||||
contributables = [pet_type, @pet_state]
|
contributables = [pet_type, @pet_state]
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
|
@ -60,28 +60,30 @@ class Pet < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
contributables
|
contributables
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
||||||
item.handle_assets!
|
@items.each do |item|
|
||||||
item.save!
|
item.handle_assets!
|
||||||
|
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)
|
||||||
pet.load!
|
pet.load!
|
||||||
pet
|
pet
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.amf_service
|
def self.amf_service
|
||||||
@amf_service ||= gateway.service AMF_SERVICE_NAME
|
@amf_service ||= gateway.service AMF_SERVICE_NAME
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.gateway
|
def self.gateway
|
||||||
unless @gateway
|
unless @gateway
|
||||||
require 'rocketamf/remote_gateway'
|
require 'rocketamf/remote_gateway'
|
||||||
|
@ -89,7 +91,8 @@ class Pet < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
@gateway
|
@gateway
|
||||||
end
|
end
|
||||||
|
|
||||||
class PetNotFound < Exception;end
|
class PetNotFound < Exception;end
|
||||||
class DownloadError < Exception;end
|
class DownloadError < Exception;end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue