2023-08-02 16:05:02 -07:00
class Item < ApplicationRecord
2011-07-26 15:49:52 -07:00
include PrettyParam
2013-01-11 14:20:06 -08:00
2023-07-22 12:23:14 -07:00
# We use the `type` column to mean something other than what Rails means!
self . inheritance_column = nil
2011-07-26 15:49:52 -07:00
2010-05-19 16:17:33 -07:00
SwfAssetType = 'object'
2013-01-11 14:20:06 -08:00
translates :name , :description , :rarity
2011-05-02 15:07:56 -07:00
2011-07-12 16:37:16 -07:00
has_many :closet_hangers
2013-03-05 18:51:24 -08:00
has_one :contribution , :as = > :contributed , :inverse_of = > :contributed
2012-01-12 15:17:59 -08:00
has_many :parent_swf_asset_relationships , :as = > :parent
has_many :swf_assets , :through = > :parent_swf_asset_relationships
2011-05-02 15:07:56 -07:00
2011-07-22 13:18:15 -07:00
attr_writer :current_body_id , :owned , :wanted
2011-05-02 15:07:56 -07:00
2010-09-08 19:49:39 -07:00
NCRarities = [ 0 , 500 ]
2011-05-02 15:07:56 -07:00
PAINTBRUSH_SET_DESCRIPTION = 'This item is part of a deluxe paint brush set!'
2012-09-29 10:40:55 -07:00
SPECIAL_COLOR_DESCRIPTION_REGEX =
2013-06-23 22:58:17 -07:00
/ This item is only wearable by Neopets painted ([a-zA-Z]+) \ .|WARNING: This [a-zA-Z]+ can be worn by ([a-zA-Z]+) [a-zA-Z]+ ONLY!|If your Neopet is not painted ([a-zA-Z]+), it will not be able to wear this item \ . /
2011-05-02 15:07:56 -07:00
2010-05-15 11:14:17 -07:00
cattr_reader :per_page
@@per_page = 30
2011-05-02 15:07:56 -07:00
2023-07-22 18:13:11 -07:00
scope :alphabetize_by_translations , - > ( locale ) {
locale = locale or I18n . locale
2013-01-11 15:56:33 -08:00
it = Item :: Translation . arel_table
2023-07-22 18:13:11 -07:00
joins ( :translations ) . where ( it [ :locale ] . eq ( 'en' ) ) .
2023-08-02 16:29:31 -07:00
order ( it [ :name ] . asc )
2013-01-11 15:56:33 -08:00
}
2011-05-02 15:07:56 -07:00
2023-07-22 14:04:01 -07:00
scope :newest , - > {
order ( arel_table [ :created_at ] . desc ) if arel_table [ :created_at ]
}
2011-08-04 07:01:44 -07:00
2023-07-22 14:04:01 -07:00
scope :spidered_longest_ago , - > {
order ( [ " (last_spidered IS NULL) DESC " , " last_spidered DESC " ] )
}
2011-05-02 15:07:56 -07:00
2023-07-22 14:04:01 -07:00
scope :sold_in_mall , - > { where ( :sold_in_mall = > true ) }
scope :not_sold_in_mall , - > { where ( :sold_in_mall = > false ) }
2011-05-02 15:07:56 -07:00
2023-07-22 14:04:01 -07:00
scope :sitemap , - > { order ( [ :id ] ) . limit ( 49999 ) }
2011-05-20 17:49:48 -07:00
2023-07-22 14:04:01 -07:00
scope :with_closet_hangers , - > { joins ( :closet_hangers ) }
2011-07-17 14:24:29 -07:00
2023-07-26 11:15:35 -07:00
scope :name_includes , - > ( value , locale = I18n . locale ) {
2023-07-26 11:58:05 -07:00
it = Item :: Translation . arel_table
Item . joins ( :translations ) . where ( it [ :locale ] . eq ( locale ) ) .
2023-07-28 14:45:10 -07:00
where ( it [ :name ] . matches ( '%' + sanitize_sql_like ( value ) + '%' ) )
2023-07-26 11:15:35 -07:00
}
scope :name_excludes , - > ( value , locale = I18n . locale ) {
2023-07-26 11:58:05 -07:00
it = Item :: Translation . arel_table
Item . joins ( :translations ) . where ( it [ :locale ] . eq ( locale ) ) .
2023-07-28 14:45:10 -07:00
where ( it [ :name ] . matches ( '%' + sanitize_sql_like ( value ) + '%' ) . not )
2023-07-26 11:15:35 -07:00
}
2023-07-22 18:13:11 -07:00
scope :is_nc , - > {
i = Item . arel_table
Fix crash when searching for "is:nc"
Huh, Arel can *sometimes* handle just having an attribute stand in as
"X is true" in a condition, but sometimes gets upset about it. I guess
this changed in Rails since we recently wrote this?
Specifically, item search would crash on "is:nc" (but *not* "is:np"),
saying:
```
undefined method `fetch_attribute' for #<struct Arel::Attributes::Attribute relation=#<Arel::Table:0x0000000109a67110 @name="items", @klass=Item(…), @type_caster=#<ActiveRecord::TypeCaster::Map:0x0000000109a66e90 @klass=Item(…)>, @table_alias=nil>, name="is_manually_nc">
```
The traceback was a bit misleading (it happened at the part where we
merge all the scopes together), but that hinted to me that it working
with an attribute in a place where it expected a conditional. So I
converted the attribute in the `is_nc` scope to a conditional, and made
the matching change in `is_np`, and that fixed it! Ok phew!
2023-10-25 12:46:48 -07:00
where ( i [ :rarity_index ] . in ( Item :: NCRarities ) . or ( i [ :is_manually_nc ] . eq ( true ) ) )
2023-07-22 18:13:11 -07:00
}
scope :is_np , - > {
i = Item . arel_table
Fix crash when searching for "is:nc"
Huh, Arel can *sometimes* handle just having an attribute stand in as
"X is true" in a condition, but sometimes gets upset about it. I guess
this changed in Rails since we recently wrote this?
Specifically, item search would crash on "is:nc" (but *not* "is:np"),
saying:
```
undefined method `fetch_attribute' for #<struct Arel::Attributes::Attribute relation=#<Arel::Table:0x0000000109a67110 @name="items", @klass=Item(…), @type_caster=#<ActiveRecord::TypeCaster::Map:0x0000000109a66e90 @klass=Item(…)>, @table_alias=nil>, name="is_manually_nc">
```
The traceback was a bit misleading (it happened at the part where we
merge all the scopes together), but that hinted to me that it working
with an attribute in a place where it expected a conditional. So I
converted the attribute in the `is_nc` scope to a conditional, and made
the matching change in `is_np`, and that fixed it! Ok phew!
2023-10-25 12:46:48 -07:00
where ( i [ :rarity_index ] . in ( Item :: NCRarities ) . or ( i [ :is_manually_nc ] . eq ( true ) ) . not )
2023-07-22 18:13:11 -07:00
}
2023-07-26 11:51:52 -07:00
scope :is_pb , - > {
2023-07-26 11:58:05 -07:00
it = Item :: Translation . arel_table
2023-07-26 12:41:37 -07:00
joins ( :translations ) . where ( it [ :locale ] . eq ( 'en' ) ) .
2023-07-26 11:51:52 -07:00
where ( 'description LIKE ?' ,
2023-07-28 14:45:10 -07:00
'%' + sanitize_sql_like ( PAINTBRUSH_SET_DESCRIPTION ) + '%' )
2023-07-26 11:51:52 -07:00
}
scope :is_not_pb , - > {
2023-07-26 11:58:05 -07:00
it = Item :: Translation . arel_table
2023-07-26 12:41:37 -07:00
joins ( :translations ) . where ( it [ :locale ] . eq ( 'en' ) ) .
2023-07-26 11:51:52 -07:00
where ( 'description NOT LIKE ?' ,
2023-07-28 14:45:10 -07:00
'%' + sanitize_sql_like ( PAINTBRUSH_SET_DESCRIPTION ) + '%' )
2023-07-26 11:51:52 -07:00
}
2023-07-26 12:28:25 -07:00
scope :occupies , - > ( zone_label , locale = I18n . locale ) {
zone_ids = Zone . matching_label ( zone_label , locale ) . map ( & :id )
sa = SwfAsset . arel_table
2023-07-26 12:41:37 -07:00
joins ( :swf_assets ) . where ( sa [ :zone_id ] . in ( zone_ids ) ) . distinct
2023-07-26 12:28:25 -07:00
}
scope :not_occupies , - > ( zone_label , locale = I18n . locale ) {
zone_ids = Zone . matching_label ( zone_label , locale ) . map ( & :id )
i = Item . arel_table
sa = SwfAsset . arel_table
2023-07-28 14:10:13 -07:00
# Querying for "has NO swf_assets matching these zone IDs" is trickier than
# the positive case! To do it, we GROUP_CONCAT the zone_ids together for
# each item, then use FIND_IN_SET to search the result for each zone ID,
# and assert that it must not find a match. (This is uhh, not exactly fast,
# so it helps to have other tighter conditions applied first!)
# TODO: I feel like this could also be solved with a LEFT JOIN, idk if that
# performs any better? In Rails 5+ `left_outer_joins` is built in so!
condition = zone_ids . map { 'FIND_IN_SET(?, GROUP_CONCAT(zone_id)) = 0' } . join ( ' AND ' )
joins ( :swf_assets ) . group ( i [ :id ] ) . having ( condition , * zone_ids ) . distinct
2023-07-26 12:41:37 -07:00
}
scope :restricts , - > ( zone_label , locale = I18n . locale ) {
zone_ids = Zone . matching_label ( zone_label , locale ) . map ( & :id )
2023-07-29 13:11:53 -07:00
condition = zone_ids . map { '(SUBSTR(items.zones_restrict, ?, 1) = "1")' } . join ( ' OR ' )
2023-07-26 12:41:37 -07:00
where ( condition , * zone_ids )
}
scope :not_restricts , - > ( zone_label , locale = I18n . locale ) {
zone_ids = Zone . matching_label ( zone_label , locale ) . map ( & :id )
2023-07-29 13:11:53 -07:00
condition = zone_ids . map { '(SUBSTR(items.zones_restrict, ?, 1) = "1")' } . join ( ' OR ' )
2023-07-26 12:41:37 -07:00
where ( " NOT ( #{ condition } ) " , * zone_ids )
2023-07-26 12:28:25 -07:00
}
2023-07-28 14:45:10 -07:00
scope :fits , - > ( body_id ) {
sa = SwfAsset . arel_table
joins ( :swf_assets ) . where ( sa [ :body_id ] . eq ( body_id ) ) . distinct
}
scope :not_fits , - > ( body_id ) {
i = Item . arel_table
sa = SwfAsset . arel_table
# Querying for "has NO swf_assets matching these body IDs" is trickier than
# the positive case! To do it, we GROUP_CONCAT the body_ids together for
# each item, then use FIND_IN_SET to search the result for the body ID,
# and assert that it must not find a match. (This is uhh, not exactly fast,
# so it helps to have other tighter conditions applied first!)
#
# TODO: I feel like this could also be solved with a LEFT JOIN, idk if that
# performs any better? In Rails 5+ `left_outer_joins` is built in so!
#
# NOTE: The `fits` and `not_fits` counts don't perfectly add up to the
# total number of items, 5 items aren't accounted for? I'm not going to
# bother looking into this, but one thing I notice is items with no assets
# somehow would not match either scope in this impl (but LEFT JOIN would!)
joins ( :swf_assets ) . group ( i [ :id ] ) .
having ( 'FIND_IN_SET(?, GROUP_CONCAT(body_id)) = 0' , body_id ) .
distinct
}
2023-07-22 18:13:11 -07:00
2023-11-03 16:20:02 -07:00
def nc_trade_value
return nil unless nc?
begin
OwlsValueGuide . find_by_name ( name ( :en ) )
rescue OwlsValueGuide :: NotFound = > error
Rails . logger . debug ( " No NC trade value listed for #{ name ( :en ) } ( #{ id } ) " )
return nil
rescue OwlsValueGuide :: NetworkError = > error
Rails . logger . error ( " Couldn't load nc_trade_value: #{ error . full_message } " )
return nil
end
2011-07-12 22:21:48 -07:00
end
2012-10-24 20:09:05 -07:00
# Return an OrderedHash mapping users to the number of times they
# contributed to this item's assets, from most contributions to least.
def contributors_with_counts
# Get contributing users' IDs
swf_asset_ids = swf_assets . select ( SwfAsset . arel_table [ :id ] ) . map ( & :id )
swf_asset_contributions = Contribution . select ( 'user_id' ) .
where ( :contributed_type = > 'SwfAsset' , :contributed_id = > swf_asset_ids )
contributor_ids = swf_asset_contributions . map ( & :user_id )
# Get the users, mapped by ID
contributors_by_id = { }
User . find ( contributor_ids ) . each { | u | contributors_by_id [ u . id ] = u }
# Count each user's contributions
contributor_counts_by_id = Hash . new ( 0 )
contributor_ids . each { | id | contributor_counts_by_id [ id ] += 1 }
# Build an OrderedHash mapping users to counts in descending order
contributors_with_counts = ActiveSupport :: OrderedHash . new
contributor_counts_by_id . sort_by { | k , v | v } . reverse . each do | id , count |
contributor = contributors_by_id [ id ]
contributors_with_counts [ contributor ] = count
end
contributors_with_counts
end
2011-05-02 15:07:56 -07:00
2010-09-08 19:49:39 -07:00
def nc?
2023-11-03 16:27:39 -07:00
is_manually_nc? || NCRarities . include? ( rarity_index )
2010-09-08 19:49:39 -07:00
end
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
def pb?
2013-01-26 07:52:21 -08:00
I18n . with_locale ( :en ) { self . description == PAINTBRUSH_SET_DESCRIPTION }
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
end
2011-05-02 15:07:56 -07:00
2011-07-22 13:18:15 -07:00
def owned?
@owned
end
def wanted?
@wanted
end
2013-01-21 17:34:39 -08:00
def restricted_zones ( options = { } )
2023-11-09 21:35:42 -08:00
options [ :scope ] || = Zone . all
2013-01-21 17:34:39 -08:00
options [ :scope ] . find ( restricted_zone_ids )
2010-09-08 19:49:39 -07:00
end
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
def restricted_zone_ids
2013-01-21 17:34:39 -08:00
unless @restricted_zone_ids
@restricted_zone_ids = [ ]
zones_restrict . split ( / / ) . each_with_index do | switch , id |
@restricted_zone_ids << ( id . to_i + 1 ) if switch == '1'
end
end
@restricted_zone_ids
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
end
def occupied_zone_ids
occupied_zones . map ( & :id )
end
2011-05-02 15:07:56 -07:00
2013-01-21 17:34:39 -08:00
def occupied_zones ( options = { } )
2023-11-09 21:35:42 -08:00
options [ :scope ] || = Zone . all
2010-09-08 19:49:39 -07:00
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
2013-01-21 17:34:39 -08:00
zones = options [ :scope ] . find ( zone_body_ids . keys )
zones_by_id = zones . inject ( { } ) { | h , z | h [ z . id ] = z ; h }
2010-09-08 19:49:39 -07:00
total_body_ids = all_body_ids . size
zone_body_ids . each do | zone_id , body_ids |
2013-01-21 17:34:39 -08:00
zones_by_id [ zone_id ] . sometimes = true if body_ids . size < total_body_ids
2010-09-08 19:49:39 -07:00
end
zones
end
2011-05-02 15:07:56 -07:00
2011-01-27 13:35:46 -08:00
def affected_zones
restricted_zones + occupied_zones
end
2011-05-02 15:07:56 -07:00
def special_color
@special_color || = determine_special_color
end
2013-12-05 13:22:43 -08:00
def special_color_id
special_color . try ( :id )
end
2011-05-02 15:07:56 -07:00
protected
def determine_special_color
2013-01-21 14:01:41 -08:00
I18n . with_locale ( I18n . default_locale ) do
# Rather than go find the special description in all locales, let's just
# run this logic in English.
if description . include? ( PAINTBRUSH_SET_DESCRIPTION )
2013-02-03 11:31:22 -08:00
name_words = name . downcase . split
2013-01-21 14:01:41 -08:00
Color . nonstandard . each do | color |
2013-02-03 11:31:22 -08:00
return color if name_words . include? ( color . name )
2013-01-21 14:01:41 -08:00
end
2011-05-02 15:07:56 -07:00
end
2013-01-21 14:01:41 -08:00
match = description . match ( SPECIAL_COLOR_DESCRIPTION_REGEX )
if match
2013-06-23 22:58:17 -07:00
# Since there are multiple formats in the one regex, there are multiple
# possible color name captures. So, take the first non-nil capture.
color = match . captures . detect ( & :present? )
2013-01-21 14:01:41 -08:00
return Color . find_by_name ( color . downcase )
end
2014-12-16 22:54:55 -08:00
2017-10-21 13:32:01 -07:00
# HACK: this should probably be a flag on the record instead of
# being hardcoded :P
2018-02-25 14:45:10 -08:00
if [ 71893 , 76192 , 76202 , 77367 , 77368 , 77369 , 77370 ] . include? ( id )
2014-12-16 22:54:55 -08:00
return Color . find_by_name ( 'baby' )
end
2017-10-21 13:32:01 -07:00
if [ 76198 ] . include? ( id )
return Color . find_by_name ( 'mutant' )
end
if [ 75372 ] . include? ( id )
return Color . find_by_name ( 'maraquan' )
end
2018-05-09 14:10:20 -07:00
if manual_special_color_id?
return Color . find ( manual_special_color_id )
end
2011-05-02 15:07:56 -07:00
end
end
public
2010-05-15 08:38:45 -07:00
def species_support_ids
2010-11-27 15:41:06 -08:00
@species_support_ids_array || = read_attribute ( 'species_support_ids' ) . split ( ',' ) . map ( & :to_i ) rescue nil
2010-05-15 08:38:45 -07:00
end
2011-05-02 15:07:56 -07:00
2010-05-15 08:38:45 -07:00
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
2013-12-05 13:22:43 -08:00
def supported_species_ids
2013-12-14 15:19:27 -08:00
return Species . select ( [ :id ] ) . map ( & :id ) if modeled_body_ids . include? ( 0 )
2013-01-28 14:54:25 -08:00
2013-12-14 15:19:27 -08:00
pet_types = PetType . where ( :body_id = > modeled_body_ids ) . select ( 'DISTINCT species_id' )
2013-12-03 12:47:33 -08:00
species_ids = pet_types . map ( & :species_id )
2013-01-28 14:54:25 -08:00
# If there are multiple known supported species, it probably supports them
# all. (I've never heard of only a handful of species being supported :P)
2013-12-05 13:22:43 -08:00
species_ids . size > = 2 ? Species . select ( [ :id ] ) . map ( & :id ) : species_ids
2013-01-28 15:54:50 -08:00
end
2013-01-21 12:55:48 -08:00
def support_species? ( species )
species_support_ids . blank? || species_support_ids . include? ( species . id )
end
2011-05-02 15:07:56 -07:00
2013-12-14 15:19:27 -08:00
def modeled_body_ids
@modeled_body_ids || = swf_assets . select ( 'DISTINCT body_id' ) . map ( & :body_id )
end
def modeled_color_ids
# Might be empty if modeled_body_ids is 0. But it's currently not called
# in that scenario, so, whatever.
@modeled_color_ids || = PetType . select ( 'DISTINCT color_id' ) .
where ( body_id : modeled_body_ids ) .
map ( & :color_id )
end
Update missing body ID prediction to handle, say, the Maraquan Mynci.
It turns out that some pets for seemingly nonstandard colors have the
standard body type anyway, and vice-versa. This implies that we should
stop relying on a color's standardness, but, for the time being, we've
just revised the prediction model:
Old model:
* If I see a body_id, I find the corresponding color_ids, and it's wearable
by all pet types with those color_ids.
New model:
* If I see a body_id,
* If it also belongs to a basic pet type, it's a standard body ID.
* It therefore fits all pet types of standard color (if there's
more than one body ID modeled already). (Not really,
because of weird exceptions like Orange Chia. Should that be
standard or not?)
* If it doesn't also belong to a basic pet type, it's a nonstandard
body ID.
* It therefore only belongs to one color, and therefore the item
fits all pet types of the same color.
2014-01-20 13:29:01 -08:00
def basic_body_ids
@basic_body_ids || = begin
basic_color_ids || = Color . select ( [ :id ] ) . basic . map ( & :id )
PetType . select ( 'DISTINCT body_id' ) .
where ( color_id : basic_color_ids ) . map ( & :body_id )
end
2013-12-14 15:19:27 -08:00
end
def predicted_body_ids
@predicted_body_ids || = if modeled_body_ids . include? ( 0 )
# Oh, look, it's already known to fit everybody! Sweet. We're done. (This
# isn't folded into the case below, in case this item somehow got a
# body-specific and non-body-specific asset. In all the cases I've seen
# it, that indicates a glitched item, but this method chooses to reflect
# behavior elsewhere in the app by saying that we can put this item on
# anybody. (Heh. Any body.))
modeled_body_ids
elsif modeled_body_ids . size == 1
# This might just be a species-specific item. Let's be conservative in
# our prediction, though we'll revise it if we see another body ID.
modeled_body_ids
else
# If an item is worn by more than one body, then it must be wearable by
# all bodies of the same color. (To my knowledge, anyway. I'm not aware
# of any exceptions.) So, let's find those bodies by first finding those
# colors.
Update missing body ID prediction to handle, say, the Maraquan Mynci.
It turns out that some pets for seemingly nonstandard colors have the
standard body type anyway, and vice-versa. This implies that we should
stop relying on a color's standardness, but, for the time being, we've
just revised the prediction model:
Old model:
* If I see a body_id, I find the corresponding color_ids, and it's wearable
by all pet types with those color_ids.
New model:
* If I see a body_id,
* If it also belongs to a basic pet type, it's a standard body ID.
* It therefore fits all pet types of standard color (if there's
more than one body ID modeled already). (Not really,
because of weird exceptions like Orange Chia. Should that be
standard or not?)
* If it doesn't also belong to a basic pet type, it's a nonstandard
body ID.
* It therefore only belongs to one color, and therefore the item
fits all pet types of the same color.
2014-01-20 13:29:01 -08:00
basic_modeled_body_ids , nonbasic_modeled_body_ids = modeled_body_ids .
partition { | bi | basic_body_ids . include? ( bi ) }
output = [ ]
if basic_modeled_body_ids . present?
output += basic_body_ids
end
if nonbasic_modeled_body_ids . present?
nonbasic_modeled_color_ids = PetType . select ( 'DISTINCT color_id' ) .
where ( body_id : nonbasic_modeled_body_ids ) .
map ( & :color_id )
output += PetType . select ( 'DISTINCT body_id' ) .
where ( color_id : nonbasic_modeled_color_ids ) .
map ( & :body_id )
end
output
2013-12-14 15:19:27 -08:00
end
end
def predicted_missing_body_ids
@predicted_missing_body_ids || = predicted_body_ids - modeled_body_ids
end
2014-01-01 07:15:58 -08:00
def predicted_missing_standard_body_ids_by_species_id
@predicted_missing_standard_body_ids_by_species_id || =
PetType . select ( 'DISTINCT body_id, species_id' ) .
joins ( :color ) .
where ( body_id : predicted_missing_body_ids ,
colors : { standard : true } ) .
inject ( { } ) { | h , pt | h [ pt . species_id ] = pt . body_id ; h }
2013-12-14 15:19:27 -08:00
end
2023-11-09 21:35:42 -08:00
def predicted_missing_standard_body_ids_by_species ( species_scope = Species . all )
2014-01-01 07:15:58 -08:00
species = species_scope . where ( id : predicted_missing_standard_body_ids_by_species_id . keys )
species_by_id = species . inject ( { } ) { | h , s | h [ s . id ] = s ; h }
predicted_missing_standard_body_ids_by_species_id . inject ( { } ) { | h , ( sid , bid ) |
h [ species_by_id [ sid ] ] = bid ; h }
2013-12-14 15:19:27 -08:00
end
def predicted_missing_nonstandard_body_pet_types
PetType . joins ( :color ) .
Update missing body ID prediction to handle, say, the Maraquan Mynci.
It turns out that some pets for seemingly nonstandard colors have the
standard body type anyway, and vice-versa. This implies that we should
stop relying on a color's standardness, but, for the time being, we've
just revised the prediction model:
Old model:
* If I see a body_id, I find the corresponding color_ids, and it's wearable
by all pet types with those color_ids.
New model:
* If I see a body_id,
* If it also belongs to a basic pet type, it's a standard body ID.
* It therefore fits all pet types of standard color (if there's
more than one body ID modeled already). (Not really,
because of weird exceptions like Orange Chia. Should that be
standard or not?)
* If it doesn't also belong to a basic pet type, it's a nonstandard
body ID.
* It therefore only belongs to one color, and therefore the item
fits all pet types of the same color.
2014-01-20 13:29:01 -08:00
where ( body_id : predicted_missing_body_ids - basic_body_ids ,
2013-12-14 15:19:27 -08:00
colors : { standard : false } )
end
2023-11-09 21:35:42 -08:00
def predicted_missing_nonstandard_body_ids_by_species_by_color ( colors_scope = Color . all , species_scope = Species . all )
2013-12-14 15:19:27 -08:00
pet_types = predicted_missing_nonstandard_body_pet_types
species_by_id = { }
species_scope . find ( pet_types . map ( & :species_id ) ) . each do | species |
species_by_id [ species . id ] = species
end
colors_by_id = { }
colors_scope . find ( pet_types . map ( & :color_id ) ) . each do | color |
colors_by_id [ color . id ] = color
end
2014-01-01 07:15:58 -08:00
body_ids_by_species_by_color = { }
2013-12-14 15:19:27 -08:00
pet_types . each do | pt |
color = colors_by_id [ pt . color_id ]
2014-01-01 07:15:58 -08:00
body_ids_by_species_by_color [ color ] || = { }
body_ids_by_species_by_color [ color ] [ species_by_id [ pt . species_id ] ] = pt . body_id
2013-12-14 15:19:27 -08:00
end
2014-01-01 07:15:58 -08:00
body_ids_by_species_by_color
2013-12-14 15:19:27 -08:00
end
def predicted_fully_modeled?
predicted_missing_body_ids . empty?
end
def predicted_modeled_ratio
modeled_body_ids . size . to_f / predicted_body_ids . size
end
2015-08-05 17:11:08 -07:00
def thumbnail
2017-04-01 10:29:12 -07:00
if thumbnail_url . present?
url = thumbnail_url
else
url = ActionController :: Base . helpers . asset_path (
" broken_item_thumbnail.gif " )
end
@thumbnail || = Image . from_insecure_url ( url )
2015-08-05 17:11:08 -07:00
end
2013-06-27 00:10:55 -07:00
def as_json ( options = { } )
json = {
2010-06-22 10:00:55 -07:00
:description = > description ,
:id = > id ,
:name = > name ,
2015-08-05 17:11:08 -07:00
:thumbnail_url = > thumbnail . secure_url ,
2010-09-08 17:07:09 -07:00
:zones_restrict = > zones_restrict ,
2011-07-12 22:21:48 -07:00
:rarity_index = > rarity_index ,
2012-08-06 18:15:31 -07:00
:nc = > nc?
2010-06-22 10:00:55 -07:00
}
2013-06-27 00:10:55 -07:00
# Set owned and wanted keys, unless explicitly told not to. (For example,
# item proxies don't want us to bother, since they'll override.)
unless options . has_key? ( :include_hanger_status )
options [ :include_hanger_status ] = true
end
if options [ :include_hanger_status ]
json [ :owned ] = owned?
json [ :wanted ] = wanted?
end
json
2010-06-22 10:00:55 -07:00
end
2011-05-02 15:07:56 -07:00
2010-10-10 11:43:01 -07:00
before_create do
2010-11-25 08:10:21 -08:00
self . sold_in_mall || = false
2010-10-09 07:53:58 -07:00
true
2010-10-07 07:46:23 -07:00
end
2011-05-02 15:07:56 -07:00
2010-11-06 15:08:42 -07:00
def handle_assets!
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
2012-01-26 11:51:30 -08:00
2011-07-22 12:08:17 -07:00
# If a relationship used to bind an item and asset for this body type,
# but doesn't in this sample, the two have been unbound. Delete the
# relationship.
2010-10-09 09:22:40 -07:00
ids_to_delete = self . parent_swf_asset_relationships .
2012-01-26 11:51:30 -08:00
select ( rels [ :id ] ) .
2012-01-12 15:17:59 -08:00
joins ( :swf_asset ) .
2010-11-16 14:26:06 -08:00
where ( rels [ :swf_asset_id ] . not_in ( new_swf_asset_ids ) ) .
2010-10-09 09:22:40 -07:00
where ( swf_assets [ :body_id ] . in ( [ @current_body_id , 0 ] ) ) .
2012-01-26 11:51:30 -08:00
map ( & :id )
2010-10-09 09:22:40 -07:00
unless ids_to_delete . empty?
2012-01-26 11:51:30 -08:00
ParentSwfAssetRelationship . where ( :id = > ids_to_delete ) . delete_all
2010-10-09 09:22:40 -07:00
end
2012-01-26 11:51:30 -08:00
2011-10-23 12:09:53 -07:00
@parent_swf_asset_relationships_to_update . each do | rel |
rel . save!
rel . swf_asset . save!
end
2010-10-09 09:22:40 -07:00
end
end
2013-01-22 22:25:09 -08:00
def body_specific?
# If there are species support IDs (it's not empty), the item is
# body-specific. If it's empty, it fits everyone the same.
2017-10-29 15:08:22 -07:00
explicitly_body_specific? || ! species_support_ids . empty?
2013-01-22 22:25:09 -08:00
end
2011-05-02 15:07:56 -07:00
2015-07-27 10:23:46 -07:00
def add_origin_registry_info ( info , locale )
2010-10-07 07:46:23 -07:00
# bear in mind that numbers from registries are floats
2013-12-08 20:59:36 -08:00
species_support_strs = info [ 'species_support' ] || [ ]
self . species_support_ids = species_support_strs . map ( & :to_i )
2015-07-27 10:23:46 -07:00
self . name_translations = { locale = > info [ 'name' ] }
2010-10-07 07:46:23 -07:00
attribute_names . each do | attribute |
2015-07-27 10:23:46 -07:00
next if attribute == 'name'
2010-10-07 07:46:23 -07:00
value = info [ attribute . to_sym ]
if value
value = value . to_i if value . is_a? Float
self [ attribute ] = value
end
end
end
2011-05-02 15:07:56 -07:00
2010-11-06 15:08:42 -07:00
def pending_swf_assets
@parent_swf_asset_relationships_to_update . inject ( [ ] ) do | all_swf_assets , relationship |
all_swf_assets << relationship . swf_asset
end
end
2011-05-02 15:07:56 -07:00
2010-10-09 09:22:40 -07:00
def parent_swf_asset_relationships_to_update = ( rels )
@parent_swf_asset_relationships_to_update = rels
end
2011-05-02 15:07:56 -07:00
2023-11-11 07:54:56 -08:00
Body = Struct . new ( :id , :species )
Appearance = Struct . new ( :body , :swf_assets )
2023-11-11 07:14:48 -08:00
def appearances
all_swf_assets = swf_assets . to_a
# If there are no assets yet, there are no appearances.
return [ ] if all_swf_assets . empty?
# Get all SWF assets, and separate the ones that fit everyone (body_id=0).
swf_assets_by_body_id = all_swf_assets . group_by ( & :body_id )
swf_assets_for_all_bodies = swf_assets_by_body_id . delete ( 0 ) || [ ]
# If there are no body-specific assets, return one appearance for them all.
if swf_assets_by_body_id . empty?
2023-11-11 08:13:07 -08:00
body = Body . new ( 0 , nil )
return [ Appearance . new ( body , swf_assets_for_all_bodies ) ]
2023-11-11 07:14:48 -08:00
end
# Otherwise, create an appearance for each real (nonzero) body ID. We don't
# generally expect body_id = 0 and body_id != 0 to mix, but if they do,
# uhh, let's merge the body_id = 0 ones in?
swf_assets_by_body_id . map do | body_id , body_specific_assets |
swf_assets_for_body = body_specific_assets + swf_assets_for_all_bodies
2023-11-11 07:54:56 -08:00
species = Species . with_body_id ( body_id ) . first!
body = Body . new ( body_id , species )
Appearance . new ( body , swf_assets_for_body )
2023-11-11 07:14:48 -08:00
end
use proxies for item html, too
Some lame benchmarking on my box, dev, cache classes, many items:
No proxies:
Fresh JSON: 175, 90, 90, 93, 82, 88, 158, 150, 85, 167 = 117.8
Cached JSON: (none)
Fresh HTML: 371, 327, 355, 328, 322, 346 = 341.5
Cached HTML: 173, 123, 175, 187, 171, 179 = 168
Proxies:
Fresh JSON: 175, 183, 269, 219, 195, 178 = 203.17
Cached JSON: 88, 70, 89, 162, 80, 77 = 94.3
Fresh HTML: 494, 381, 350, 334, 451, 372 = 397
Cached HTML: 176, 170, 104, 101, 111, 116 = 129.7
So, overhead is significant, but the gains when cached (and that should be
all the time, since we currently have 0 evictions) are definitely worth
it. Worth pushing, and probably putting some future effort into reducing
overhead.
On production (again, lame), items#index was consistently averaging
73-74ms when super healthy, and 82ms when pets#index was being louder
than usual. For reference is all. This will probably perform
significantly worse at first (in JSON, anyway, since HTML is already
mostly cached), so it might be worth briefly warming the cache after
pushing.
2013-06-26 23:39:04 -07:00
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
2023-07-22 13:32:29 -07:00
SwfAsset . object_assets . joins ( :parent_swf_asset_relationships ) .
where ( SwfAsset . arel_table [ :id ] . in ( swf_asset_ids ) ) . select ( [
2012-01-12 15:17:59 -08:00
SwfAsset . arel_table [ :id ] ,
ParentSwfAssetRelationship . arel_table [ :parent_id ]
2023-07-22 13:32:29 -07:00
] ) . each do | row |
2010-11-06 08:52:58 -07:00
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
2011-05-02 15:07:56 -07:00
2023-11-09 21:35:42 -08:00
def self . collection_from_pet_type_and_registries ( pet_type , info_registry , asset_registry , scope = Item . all )
2010-10-07 07:46:23 -07:00
# bear in mind that registries are arrays with many nil elements,
# due to how the parser works
2011-07-22 12:08:17 -07:00
# Collect existing items
2010-10-07 07:46:23 -07:00
items = { }
item_ids = [ ]
2011-01-13 14:22:07 -08:00
info_registry . each do | item_id , info |
2010-10-09 08:06:59 -07:00
if info && info [ :is_compatible ]
2011-01-13 14:22:07 -08:00
item_ids << item_id . to_i
2010-10-07 07:46:23 -07:00
end
end
2011-07-22 12:08:17 -07:00
# Collect existing relationships
2010-10-07 07:46:23 -07:00
existing_relationships_by_item_id_and_swf_asset_id = { }
2023-10-12 18:57:39 -07:00
existing_items = scope . where ( id : item_ids ) .
2023-10-12 21:49:09 -07:00
includes ( :parent_swf_asset_relationships )
2010-10-07 07:46:23 -07:00
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
2011-07-22 12:08:17 -07:00
# Collect existing assets
2010-10-07 07:46:23 -07:00
swf_asset_ids = [ ]
2011-01-13 14:22:07 -08:00
asset_registry . each do | asset_id , asset_data |
swf_asset_ids << asset_id . to_i if asset_data
2010-10-07 07:46:23 -07:00
end
2013-01-28 00:19:37 -08:00
existing_swf_assets = SwfAsset . object_assets . includes ( :zone ) .
2023-10-12 18:57:39 -07:00
where ( remote_id : swf_asset_ids )
2012-01-13 13:56:31 -08:00
existing_swf_assets_by_remote_id = { }
2010-10-07 07:46:23 -07:00
existing_swf_assets . each do | swf_asset |
2012-01-13 13:56:31 -08:00
existing_swf_assets_by_remote_id [ swf_asset . remote_id ] = swf_asset
2010-10-07 07:46:23 -07:00
end
2011-07-22 12:08:17 -07:00
# With each asset in the registry,
2010-10-07 07:46:23 -07:00
relationships_by_item_id = { }
2011-01-13 14:22:07 -08:00
asset_registry . each do | asset_id , asset_data |
2010-10-07 07:46:23 -07:00
if asset_data
2011-07-22 12:08:17 -07:00
# Build and update the item
2010-10-07 07:46:23 -07:00
item_id = asset_data [ :obj_info_id ] . to_i
2011-07-22 12:08:17 -07:00
next unless item_ids . include? ( item_id ) # skip incompatible (Uni Bug)
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
2015-07-27 10:23:46 -07:00
item . add_origin_registry_info info_registry [ item . id . to_s ] , I18n . default_locale
2010-10-09 09:22:40 -07:00
item . current_body_id = pet_type . body_id
2011-07-22 12:08:17 -07:00
# Build and update the SWF
2012-01-13 13:56:31 -08:00
swf_asset_remote_id = asset_data [ :asset_id ] . to_i
swf_asset = existing_swf_assets_by_remote_id [ swf_asset_remote_id ]
2010-10-07 07:46:23 -07:00
unless swf_asset
swf_asset = SwfAsset . new
2012-01-13 13:56:31 -08:00
swf_asset . remote_id = swf_asset_remote_id
2010-10-07 07:46:23 -07:00
end
swf_asset . origin_object_data = asset_data
swf_asset . origin_pet_type = pet_type
2012-10-05 18:56:52 -07:00
swf_asset . item = item
2011-07-22 12:08:17 -07:00
# Build and update the relationship
2012-01-12 20:02:12 -08:00
relationship = existing_relationships_by_item_id_and_swf_asset_id [ item . id ] [ swf_asset . id ] rescue nil
2010-10-07 07:46:23 -07:00
unless relationship
relationship = ParentSwfAssetRelationship . new
2012-01-12 15:17:59 -08:00
relationship . parent = item
2010-10-07 07:46:23 -07:00
end
2012-01-12 15:17:59 -08:00
relationship . swf_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
2011-07-22 12:08:17 -07:00
# Set up the relationships to be updated on item save
2010-10-07 07:46:23 -07:00
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
2011-07-22 12:08:17 -07:00
2010-10-07 07:46:23 -07:00
items . values
end
2010-05-14 15:41:40 -07:00
end