2010-05-15 08:38:45 -07:00
|
|
|
class Item < ActiveRecord::Base
|
2010-05-19 16:17:33 -07:00
|
|
|
SwfAssetType = 'object'
|
2010-05-16 17:45:30 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
has_many :parent_swf_asset_relationships, :foreign_key => 'parent_id',
|
|
|
|
:conditions => {:swf_asset_type => SwfAssetType}
|
2010-10-10 19:18:42 -07:00
|
|
|
has_many :swf_assets, :through => :parent_swf_asset_relationships, :source => :object_asset
|
2010-10-07 07:46:23 -07:00
|
|
|
|
2010-10-09 09:22:40 -07:00
|
|
|
attr_writer :current_body_id
|
|
|
|
|
2010-09-08 19:49:39 -07:00
|
|
|
NCRarities = [0, 500]
|
2010-10-03 17:14:48 -07:00
|
|
|
PaintbrushSetDescription = 'This item is part of a deluxe paint brush set!'
|
2010-09-08 19:49:39 -07:00
|
|
|
|
2010-05-15 08:38:45 -07:00
|
|
|
set_table_name 'objects' # Neo & PHP Impress call them objects, but the class name is a conflict (duh!)
|
|
|
|
set_inheritance_column 'inheritance_type' # PHP Impress used "type" to describe category
|
|
|
|
|
2010-05-15 11:14:17 -07:00
|
|
|
cattr_reader :per_page
|
|
|
|
@@per_page = 30
|
|
|
|
|
2010-05-15 11:01:15 -07:00
|
|
|
scope :alphabetize, order('name ASC')
|
|
|
|
|
2010-06-22 09:42:25 -07:00
|
|
|
scope :join_swf_assets, joins('INNER JOIN parents_swf_assets psa ON psa.swf_asset_type = "object" AND psa.parent_id = objects.id').
|
|
|
|
joins('INNER JOIN swf_assets ON swf_assets.id = psa.swf_asset_id').
|
|
|
|
group('objects.id')
|
2010-06-09 21:51:53 -07:00
|
|
|
|
2010-05-14 15:41:40 -07:00
|
|
|
# Not defining validations, since this app is currently read-only
|
2010-05-15 08:38:45 -07:00
|
|
|
|
2010-09-08 19:49:39 -07:00
|
|
|
def nc?
|
|
|
|
NCRarities.include?(rarity_index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def restricted_zones
|
|
|
|
unless @restricted_zones
|
|
|
|
@restricted_zones = []
|
|
|
|
zones_restrict.split(//).each_with_index do |switch, id|
|
|
|
|
@restricted_zones << Zone.find(id.to_i + 1) if switch == '1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@restricted_zones
|
|
|
|
end
|
|
|
|
|
|
|
|
def occupied_zones
|
|
|
|
all_body_ids = []
|
|
|
|
zone_body_ids = {}
|
|
|
|
selected_assets = swf_assets.select('body_id, zone_id').each do |swf_asset|
|
|
|
|
zone_body_ids[swf_asset.zone_id] ||= []
|
|
|
|
body_ids = zone_body_ids[swf_asset.zone_id]
|
|
|
|
body_ids << swf_asset.body_id unless body_ids.include?(swf_asset.body_id)
|
|
|
|
all_body_ids << swf_asset.body_id unless all_body_ids.include?(swf_asset.body_id)
|
|
|
|
end
|
|
|
|
zones = []
|
|
|
|
total_body_ids = all_body_ids.size
|
|
|
|
zone_body_ids.each do |zone_id, body_ids|
|
|
|
|
zone = Zone.find(zone_id)
|
|
|
|
zone.sometimes = true if body_ids.size < total_body_ids
|
|
|
|
zones << zone
|
|
|
|
end
|
|
|
|
zones
|
|
|
|
end
|
|
|
|
|
2010-05-15 08:38:45 -07:00
|
|
|
def species_support_ids
|
|
|
|
@species_support_ids_array ||= read_attribute('species_support_ids').split(',').map(&:to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def species_support_ids=(replacement)
|
2010-05-16 12:01:38 -07:00
|
|
|
@species_support_ids_array = nil
|
2010-05-15 08:38:45 -07:00
|
|
|
replacement = replacement.join(',') if replacement.is_a?(Array)
|
|
|
|
write_attribute('species_support_ids', replacement)
|
|
|
|
end
|
|
|
|
|
2010-05-16 13:15:21 -07:00
|
|
|
def supported_species
|
2010-05-20 19:09:55 -07:00
|
|
|
@supported_species ||= species_support_ids.empty? ? Species.all : species_support_ids.sort.map { |id| Species.find(id) }
|
2010-05-16 13:15:21 -07:00
|
|
|
end
|
|
|
|
|
2010-05-15 08:38:45 -07:00
|
|
|
def self.search(query)
|
2010-05-15 11:56:21 -07:00
|
|
|
raise ArgumentError, "Please provide a search query" unless query
|
|
|
|
query = query.strip
|
|
|
|
raise ArgumentError, "Search queries should be at least 3 characters" if query.length < 3
|
2010-05-15 08:38:45 -07:00
|
|
|
query_conditions = [Condition.new]
|
|
|
|
in_phrase = false
|
|
|
|
query.each_char do |c|
|
|
|
|
if c == ' ' && !in_phrase
|
|
|
|
query_conditions << Condition.new
|
|
|
|
elsif c == '"'
|
|
|
|
in_phrase = !in_phrase
|
|
|
|
elsif c == ':' && !in_phrase
|
2010-06-22 09:42:25 -07:00
|
|
|
query_conditions.last.to_filter!
|
2010-05-15 09:43:54 -07:00
|
|
|
elsif c == '-' && !in_phrase && query_conditions.last.empty?
|
|
|
|
query_conditions.last.negate!
|
2010-05-15 08:38:45 -07:00
|
|
|
else
|
|
|
|
query_conditions.last << c
|
|
|
|
end
|
|
|
|
end
|
2010-06-22 09:42:25 -07:00
|
|
|
query_conditions.inject(self.scoped) do |scope, condition|
|
2010-05-15 08:38:45 -07:00
|
|
|
condition.narrow(scope)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-22 10:00:55 -07:00
|
|
|
def as_json(options = {})
|
|
|
|
{
|
|
|
|
:description => description,
|
|
|
|
:id => id,
|
|
|
|
:name => name,
|
|
|
|
:thumbnail_url => thumbnail_url,
|
2010-09-08 17:07:09 -07:00
|
|
|
:zones_restrict => zones_restrict,
|
|
|
|
:rarity_index => rarity_index
|
2010-06-22 10:00:55 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-10-10 11:43:01 -07:00
|
|
|
before_create do
|
2010-10-09 07:53:58 -07:00
|
|
|
self.sold_in_mall = false
|
|
|
|
true
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
|
|
|
|
2010-10-10 11:43:01 -07:00
|
|
|
before_save do
|
2010-10-09 09:22:40 -07:00
|
|
|
if @parent_swf_asset_relationships_to_update && @current_body_id
|
|
|
|
new_swf_asset_ids = @parent_swf_asset_relationships_to_update.map(&:swf_asset_id)
|
|
|
|
rels = ParentSwfAssetRelationship.arel_table
|
|
|
|
swf_assets = SwfAsset.arel_table
|
|
|
|
ids_to_delete = self.parent_swf_asset_relationships.
|
|
|
|
select(:id).
|
2010-10-10 19:18:42 -07:00
|
|
|
joins(:object_asset).
|
2010-10-09 09:22:40 -07:00
|
|
|
where(rels[:swf_asset_id].in(new_swf_asset_ids).not).
|
|
|
|
where(swf_assets[:body_id].in([@current_body_id, 0])).
|
|
|
|
map(&:id)
|
|
|
|
unless ids_to_delete.empty?
|
|
|
|
ParentSwfAssetRelationship.
|
|
|
|
where(rels[:parent_id].eq(self.id)).
|
|
|
|
where(rels[:swf_asset_type].eq(SwfAssetType)).
|
|
|
|
where(rels[:swf_asset_id].in(ids_to_delete)).
|
|
|
|
delete_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def origin_registry_info=(info)
|
|
|
|
# bear in mind that numbers from registries are floats
|
|
|
|
self.species_support_ids = info[:species_support].map(&:to_i)
|
|
|
|
attribute_names.each do |attribute|
|
|
|
|
value = info[attribute.to_sym]
|
|
|
|
if value
|
|
|
|
value = value.to_i if value.is_a? Float
|
|
|
|
self[attribute] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-09 09:22:40 -07:00
|
|
|
def parent_swf_asset_relationships_to_update=(rels)
|
|
|
|
self.parent_swf_asset_relationships += rels
|
|
|
|
@parent_swf_asset_relationships_to_update = rels
|
|
|
|
end
|
|
|
|
|
2010-11-06 08:52:58 -07:00
|
|
|
def self.all_by_ids_or_children(ids, swf_assets)
|
|
|
|
swf_asset_ids = []
|
|
|
|
swf_assets_by_id = {}
|
|
|
|
swf_assets_by_parent_id = {}
|
|
|
|
swf_assets.each do |swf_asset|
|
|
|
|
id = swf_asset.id
|
|
|
|
swf_assets_by_id[id] = swf_asset
|
|
|
|
swf_asset_ids << id
|
|
|
|
end
|
|
|
|
SwfAsset.select('id, parent_id').object_assets.joins(:object_asset_relationships).
|
|
|
|
where(SwfAsset.arel_table[:id].in(swf_asset_ids)).each do |row|
|
|
|
|
item_id = row.parent_id.to_i
|
|
|
|
swf_assets_by_parent_id[item_id] ||= []
|
|
|
|
swf_assets_by_parent_id[item_id] << swf_assets_by_id[row.id.to_i]
|
|
|
|
ids << item_id
|
|
|
|
end
|
|
|
|
find(ids).tap do |items|
|
|
|
|
items.each do |item|
|
|
|
|
swf_assets = swf_assets_by_parent_id[item.id]
|
|
|
|
if swf_assets
|
|
|
|
swf_assets.each do |swf_asset|
|
|
|
|
swf_asset.item = item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def self.collection_from_pet_type_and_registries(pet_type, info_registry, asset_registry)
|
|
|
|
# bear in mind that registries are arrays with many nil elements,
|
|
|
|
# due to how the parser works
|
|
|
|
items = {}
|
|
|
|
item_ids = []
|
|
|
|
info_registry.each do |info|
|
2010-10-09 08:06:59 -07:00
|
|
|
if info && info[:is_compatible]
|
2010-10-07 07:46:23 -07:00
|
|
|
item_ids << info[:obj_info_id].to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
existing_relationships_by_item_id_and_swf_asset_id = {}
|
|
|
|
existing_items = Item.find_all_by_id(item_ids, :include => :parent_swf_asset_relationships)
|
|
|
|
existing_items.each do |item|
|
|
|
|
items[item.id] = item
|
|
|
|
relationships_by_swf_asset_id = {}
|
|
|
|
item.parent_swf_asset_relationships.each do |relationship|
|
|
|
|
relationships_by_swf_asset_id[relationship.swf_asset_id] = relationship
|
|
|
|
end
|
|
|
|
existing_relationships_by_item_id_and_swf_asset_id[item.id] =
|
|
|
|
relationships_by_swf_asset_id
|
|
|
|
end
|
|
|
|
swf_asset_ids = []
|
|
|
|
asset_registry.each_with_index do |asset_data, index|
|
|
|
|
swf_asset_ids << index if asset_data
|
|
|
|
end
|
2010-10-09 09:22:40 -07:00
|
|
|
existing_swf_assets = SwfAsset.find_all_by_id swf_asset_ids,
|
|
|
|
:conditions => {:type => SwfAssetType}
|
2010-10-07 07:46:23 -07:00
|
|
|
existing_swf_assets_by_id = {}
|
|
|
|
existing_swf_assets.each do |swf_asset|
|
|
|
|
existing_swf_assets_by_id[swf_asset.id] = swf_asset
|
|
|
|
end
|
|
|
|
relationships_by_item_id = {}
|
|
|
|
asset_registry.each do |asset_data|
|
|
|
|
if asset_data
|
|
|
|
item_id = asset_data[:obj_info_id].to_i
|
2010-10-09 08:06:59 -07:00
|
|
|
next unless item_ids.include?(item_id) # skip incompatible
|
2010-10-07 07:46:23 -07:00
|
|
|
item = items[item_id]
|
|
|
|
unless item
|
|
|
|
item = Item.new
|
|
|
|
item.id = item_id
|
|
|
|
items[item_id] = item
|
|
|
|
end
|
|
|
|
item.origin_registry_info = info_registry[item.id]
|
2010-10-09 09:22:40 -07:00
|
|
|
item.current_body_id = pet_type.body_id
|
2010-10-07 07:46:23 -07:00
|
|
|
swf_asset_id = asset_data[:asset_id].to_i
|
2010-10-09 09:22:40 -07:00
|
|
|
swf_asset = existing_swf_assets_by_id[swf_asset_id]
|
2010-10-07 07:46:23 -07:00
|
|
|
unless swf_asset
|
|
|
|
swf_asset = SwfAsset.new
|
|
|
|
swf_asset.id = swf_asset_id
|
|
|
|
end
|
|
|
|
swf_asset.origin_object_data = asset_data
|
|
|
|
swf_asset.origin_pet_type = pet_type
|
|
|
|
relationship = existing_relationships_by_item_id_and_swf_asset_id[item.id][swf_asset_id] rescue nil
|
|
|
|
unless relationship
|
|
|
|
relationship = ParentSwfAssetRelationship.new
|
|
|
|
relationship.parent_id = item.id
|
|
|
|
relationship.swf_asset_type = SwfAssetType
|
|
|
|
relationship.swf_asset_id = swf_asset.id
|
|
|
|
end
|
2010-10-10 19:18:42 -07:00
|
|
|
relationship.object_asset = swf_asset
|
2010-10-07 07:46:23 -07:00
|
|
|
relationships_by_item_id[item_id] ||= []
|
|
|
|
relationships_by_item_id[item_id] << relationship
|
|
|
|
end
|
|
|
|
end
|
|
|
|
relationships_by_item_id.each do |item_id, relationships|
|
2010-10-09 09:22:40 -07:00
|
|
|
items[item_id].parent_swf_asset_relationships_to_update = relationships
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
|
|
|
items.values
|
|
|
|
end
|
|
|
|
|
2010-05-15 08:38:45 -07:00
|
|
|
private
|
|
|
|
|
2010-06-22 09:42:25 -07:00
|
|
|
SearchFilterScopes = []
|
|
|
|
|
|
|
|
def self.search_filter(name, args={})
|
|
|
|
SearchFilterScopes << name.to_s
|
|
|
|
scope "search_filter_#{name}", lambda { |str, negative|
|
|
|
|
condition = yield(str)
|
|
|
|
condition = condition.not if negative
|
|
|
|
rel = where(condition)
|
|
|
|
rel = rel & args[:scope] if args[:scope]
|
|
|
|
rel
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
search_filter :name do |name|
|
|
|
|
arel_table[:name].matches("%#{name}%")
|
|
|
|
end
|
|
|
|
|
|
|
|
search_filter :description do |description|
|
|
|
|
arel_table[:description].matches("%#{description}%")
|
|
|
|
end
|
|
|
|
|
2010-10-03 17:14:48 -07:00
|
|
|
ADJECTIVE_FILTERS = {
|
|
|
|
'nc' => arel_table[:rarity_index].in(NCRarities),
|
|
|
|
'pb' => arel_table[:description].eq(PaintbrushSetDescription)
|
|
|
|
}
|
|
|
|
search_filter :is do |adjective|
|
|
|
|
filter = ADJECTIVE_FILTERS[adjective]
|
|
|
|
unless filter
|
|
|
|
raise ArgumentError,
|
|
|
|
"We don't know how an item can be \"#{adjective}\". " +
|
|
|
|
"Did you mean is:nc or is:pb?"
|
|
|
|
end
|
|
|
|
filter
|
2010-07-10 09:42:18 -07:00
|
|
|
end
|
|
|
|
|
2010-06-22 09:42:25 -07:00
|
|
|
search_filter :only do |species_name|
|
|
|
|
id = Species.require_by_name(species_name).id
|
|
|
|
arel_table[:species_support_ids].eq(id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
search_filter :species do |species_name|
|
|
|
|
id = Species.require_by_name(species_name).id
|
|
|
|
ids = arel_table[:species_support_ids]
|
|
|
|
ids.eq('').or(ids.matches_any(
|
|
|
|
id,
|
|
|
|
"#{id},%",
|
|
|
|
"%,#{id},%",
|
|
|
|
"%,#{id}"
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
search_filter :type, {:scope => join_swf_assets} do |zone_set_name|
|
|
|
|
zone_set = Zone::ItemZoneSets[zone_set_name]
|
|
|
|
raise ArgumentError, "Type \"#{zone_set_name}\" does not exist" unless zone_set
|
|
|
|
SwfAsset.arel_table[:zone_id].in(zone_set.map(&:id))
|
|
|
|
end
|
|
|
|
|
2010-05-15 08:38:45 -07:00
|
|
|
class Condition < String
|
2010-06-22 09:42:25 -07:00
|
|
|
def to_filter!
|
|
|
|
@filter = self.clone
|
2010-05-15 08:38:45 -07:00
|
|
|
self.replace ''
|
|
|
|
end
|
|
|
|
|
2010-05-15 09:43:54 -07:00
|
|
|
def negate!
|
|
|
|
@negative = true
|
|
|
|
end
|
|
|
|
|
2010-05-15 08:38:45 -07:00
|
|
|
def narrow(scope)
|
2010-06-22 09:42:25 -07:00
|
|
|
if SearchFilterScopes.include?(filter)
|
|
|
|
scope & Item.send("search_filter_#{filter}", self, @negative)
|
2010-05-15 12:00:53 -07:00
|
|
|
else
|
2010-06-22 09:42:25 -07:00
|
|
|
raise ArgumentError, "Filter #{filter} does not exist"
|
2010-05-15 08:38:45 -07:00
|
|
|
end
|
2010-06-22 09:42:25 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter
|
|
|
|
@filter || 'name'
|
2010-05-15 08:38:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2010-06-22 09:42:25 -07:00
|
|
|
@filter ? "#{@filter}:#{super}" : super
|
2010-05-15 08:38:45 -07:00
|
|
|
end
|
|
|
|
end
|
2010-05-14 15:41:40 -07:00
|
|
|
end
|