2023-08-02 16:05:02 -07:00
|
|
|
class PetState < ApplicationRecord
|
2010-05-20 18:11:08 -07:00
|
|
|
SwfAssetType = 'biology'
|
2012-01-12 15:17:59 -08:00
|
|
|
|
2013-03-05 18:51:24 -08:00
|
|
|
has_many :contributions, :as => :contributed,
|
|
|
|
:inverse_of => :contributed # in case of duplicates being merged
|
2010-11-15 18:15:33 -08:00
|
|
|
has_many :outfits
|
2012-02-21 11:25:11 -08:00
|
|
|
has_many :parent_swf_asset_relationships, :as => :parent,
|
2012-01-13 14:02:14 -08:00
|
|
|
:autosave => false
|
2012-01-12 15:17:59 -08:00
|
|
|
has_many :swf_assets, :through => :parent_swf_asset_relationships
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
belongs_to :pet_type
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2014-03-28 13:15:04 -07:00
|
|
|
delegate :color, to: :pet_type
|
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
alias_method :swf_asset_ids_from_association, :swf_asset_ids
|
2012-01-13 14:11:44 -08:00
|
|
|
|
|
|
|
attr_writer :parent_swf_asset_relationships_to_update
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2013-04-27 08:21:51 -07:00
|
|
|
# Our ideal order is: happy, sad, sick, UC, any+effects, glitched, with male
|
|
|
|
# before female within those groups for consistency. We therefore order as
|
|
|
|
# follows, listed in order of priority:
|
|
|
|
# * Send glitched states to the back
|
2012-06-20 13:10:53 -07:00
|
|
|
# * Bring known happy states to the front (we don't want to sort by mood_id
|
|
|
|
# DESC first because then labeled sad will appear before unlabeled happy)
|
|
|
|
# * Send states with effect assets to the back
|
|
|
|
# * Bring state with more assets forward (that is, send UC near the back)
|
|
|
|
# * Bring males forward
|
|
|
|
# * Bring states with a lower asset ID sum forward (the idea being that
|
|
|
|
# sad/female states are usually created after a happy/male base, but that's
|
|
|
|
# becoming increasingly untrue over time - this is a very last resort)
|
|
|
|
#
|
|
|
|
# Maybe someday, when most states are labeled, we can depend exclusively on
|
|
|
|
# their labels - or at least use more than is-happy and is-female. For now,
|
|
|
|
# though, this strikes a good balance of bringing default to the front for
|
|
|
|
# many pet types (the highest priority!) and otherwise doing decent sorting.
|
2011-06-21 17:42:41 -07:00
|
|
|
bio_effect_zone_id = 4
|
2023-07-22 14:04:01 -07:00
|
|
|
scope :emotion_order, -> {
|
|
|
|
joins(:parent_swf_asset_relationships).
|
2012-03-15 15:01:21 -07:00
|
|
|
joins("LEFT JOIN swf_assets effect_assets ON effect_assets.id = parents_swf_assets.swf_asset_id AND effect_assets.zone_id = #{bio_effect_zone_id}").
|
2011-06-21 19:56:42 -07:00
|
|
|
group("pet_states.id").
|
2023-08-02 17:57:53 -07:00
|
|
|
order(Arel.sql("glitched ASC, (mood_id = 1) DESC, COUNT(effect_assets.remote_id) ASC, COUNT(parents_swf_assets.swf_asset_id) DESC, female ASC, SUM(parents_swf_assets.swf_asset_id) ASC"))
|
2023-07-22 14:04:01 -07:00
|
|
|
}
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2013-01-31 17:46:34 -08:00
|
|
|
def as_json(options={})
|
2013-12-08 20:59:36 -08:00
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
gender_mood_description: gender_mood_description,
|
2014-03-31 19:05:28 -07:00
|
|
|
swf_asset_ids: swf_asset_ids_array,
|
|
|
|
artist_name: artist_name,
|
|
|
|
artist_url: artist_url
|
2013-12-08 20:59:36 -08:00
|
|
|
}
|
2013-01-31 17:46:34 -08:00
|
|
|
end
|
|
|
|
|
2010-11-15 18:15:33 -08:00
|
|
|
def reassign_children_to!(main_pet_state)
|
|
|
|
self.contributions.each do |contribution|
|
|
|
|
contribution.contributed = main_pet_state
|
|
|
|
contribution.save
|
|
|
|
end
|
|
|
|
self.outfits.each do |outfit|
|
|
|
|
outfit.pet_state = main_pet_state
|
|
|
|
outfit.save
|
|
|
|
end
|
|
|
|
ParentSwfAssetRelationship.where(ParentSwfAssetRelationship.arel_table[:parent_id].eq(self.id)).delete_all
|
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2010-11-15 18:15:33 -08:00
|
|
|
def reassign_duplicates!
|
|
|
|
raise "This may only be applied to pet states that represent many duplicate entries" unless duplicate_ids
|
|
|
|
pet_states = duplicate_ids.split(',').map do |id|
|
|
|
|
PetState.find(id.to_i)
|
|
|
|
end
|
|
|
|
main_pet_state = pet_states.shift
|
|
|
|
pet_states.each do |pet_state|
|
|
|
|
pet_state.reassign_children_to!(main_pet_state)
|
|
|
|
pet_state.destroy
|
|
|
|
end
|
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2010-11-15 18:15:33 -08:00
|
|
|
def sort_swf_asset_ids!
|
2013-12-08 20:59:36 -08:00
|
|
|
self.swf_asset_ids = swf_asset_ids_array.sort.join(',')
|
2010-11-15 18:15:33 -08:00
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def swf_asset_ids
|
|
|
|
self['swf_asset_ids']
|
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2013-12-08 20:59:36 -08:00
|
|
|
def swf_asset_ids_array
|
|
|
|
swf_asset_ids.split(',').map(&:to_i)
|
|
|
|
end
|
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def swf_asset_ids=(ids)
|
|
|
|
self['swf_asset_ids'] = ids
|
|
|
|
end
|
2011-10-31 14:22:24 -07:00
|
|
|
|
|
|
|
def handle_assets!
|
2012-01-13 14:11:44 -08:00
|
|
|
@parent_swf_asset_relationships_to_update.each do |rel|
|
2011-10-31 14:22:24 -07:00
|
|
|
rel.swf_asset.save!
|
2012-01-13 14:02:14 -08:00
|
|
|
rel.save!
|
2011-10-31 14:22:24 -07:00
|
|
|
end
|
|
|
|
end
|
2012-05-23 17:00:38 -07:00
|
|
|
|
|
|
|
def mood
|
2023-08-02 14:58:16 -07:00
|
|
|
Mood.find(self.mood_id)
|
2012-05-23 17:00:38 -07:00
|
|
|
end
|
2013-01-31 17:11:15 -08:00
|
|
|
|
|
|
|
def gender_name
|
|
|
|
if female?
|
|
|
|
I18n.translate("pet_states.description.gender.female")
|
|
|
|
else
|
|
|
|
I18n.translate("pet_states.description.gender.male")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mood_name
|
|
|
|
I18n.translate("pet_states.description.mood.#{mood.name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def gender_mood_description
|
2013-04-27 08:21:51 -07:00
|
|
|
if glitched?
|
|
|
|
I18n.translate('pet_states.description.glitched')
|
|
|
|
elsif unconverted?
|
2013-01-31 17:46:34 -08:00
|
|
|
I18n.translate('pet_states.description.unconverted')
|
|
|
|
elsif labeled?
|
2013-01-31 17:11:15 -08:00
|
|
|
I18n.translate('pet_states.description.main', :gender => gender_name,
|
|
|
|
:mood => mood_name)
|
|
|
|
else
|
|
|
|
I18n.translate('pet_states.description.unlabeled')
|
|
|
|
end
|
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2013-11-30 17:33:48 -08:00
|
|
|
def replace_with(other)
|
|
|
|
PetState.transaction do
|
|
|
|
count = outfits.count
|
|
|
|
outfits.find_each { |outfit|
|
|
|
|
outfit.pet_state = other
|
|
|
|
outfit.save!
|
|
|
|
}
|
|
|
|
destroy
|
|
|
|
end
|
|
|
|
count
|
|
|
|
end
|
|
|
|
|
2014-03-31 19:05:28 -07:00
|
|
|
def artist_name
|
|
|
|
artist_neopets_username || I18n.translate("pet_states.default_artist_name")
|
|
|
|
end
|
|
|
|
|
|
|
|
def artist_url
|
|
|
|
if artist_neopets_username
|
|
|
|
"http://www.neopets.com/userlookup.phtml?user=#{artist_neopets_username}"
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
def self.from_pet_type_and_biology_info(pet_type, info)
|
|
|
|
swf_asset_ids = []
|
2011-01-13 14:22:07 -08:00
|
|
|
info.each do |zone_id, asset_info|
|
2013-05-23 18:48:19 -07:00
|
|
|
if zone_id.present? && asset_info
|
2010-10-07 07:46:23 -07:00
|
|
|
swf_asset_ids << asset_info[:part_id].to_i
|
|
|
|
end
|
|
|
|
end
|
2010-11-15 18:15:33 -08:00
|
|
|
swf_asset_ids_str = swf_asset_ids.sort.join(',')
|
2010-10-07 07:46:23 -07:00
|
|
|
if pet_type.new_record?
|
|
|
|
pet_state = self.new :swf_asset_ids => swf_asset_ids_str
|
|
|
|
else
|
2023-10-12 18:05:01 -07:00
|
|
|
pet_state = self.find_or_initialize_by(
|
|
|
|
pet_type_id: pet_type.id,
|
|
|
|
swf_asset_ids: swf_asset_ids_str
|
|
|
|
)
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
2013-01-28 00:19:37 -08:00
|
|
|
existing_swf_assets = SwfAsset.biology_assets.includes(:zone).
|
2023-10-12 18:57:39 -07:00
|
|
|
where(remote_id: swf_asset_ids)
|
2010-10-07 07:46:23 -07:00
|
|
|
existing_swf_assets_by_id = {}
|
|
|
|
existing_swf_assets.each do |swf_asset|
|
2012-01-12 19:47:17 -08:00
|
|
|
existing_swf_assets_by_id[swf_asset.remote_id] = swf_asset
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
|
|
|
existing_relationships_by_swf_asset_id = {}
|
|
|
|
unless pet_state.new_record?
|
|
|
|
pet_state.parent_swf_asset_relationships.each do |relationship|
|
|
|
|
existing_relationships_by_swf_asset_id[relationship.swf_asset_id] = relationship
|
|
|
|
end
|
|
|
|
end
|
|
|
|
pet_state.pet_type = pet_type # save the second case from having to look it up by ID
|
|
|
|
relationships = []
|
2011-01-13 14:22:07 -08:00
|
|
|
info.each do |zone_id, asset_info|
|
2013-05-23 18:48:19 -07:00
|
|
|
if zone_id.present? && asset_info
|
2010-10-07 07:46:23 -07:00
|
|
|
swf_asset_id = asset_info[:part_id].to_i
|
|
|
|
swf_asset = existing_swf_assets_by_id[swf_asset_id]
|
|
|
|
unless swf_asset
|
|
|
|
swf_asset = SwfAsset.new
|
2012-01-12 15:17:59 -08:00
|
|
|
swf_asset.remote_id = swf_asset_id
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
|
|
|
swf_asset.origin_biology_data = asset_info
|
|
|
|
swf_asset.origin_pet_type = pet_type
|
2012-01-12 20:02:12 -08:00
|
|
|
relationship = existing_relationships_by_swf_asset_id[swf_asset.id]
|
2010-10-07 07:46:23 -07:00
|
|
|
unless relationship
|
|
|
|
relationship ||= ParentSwfAssetRelationship.new
|
2012-01-12 15:17:59 -08:00
|
|
|
relationship.parent = pet_state
|
2010-10-07 07:46:23 -07:00
|
|
|
relationship.swf_asset_id = swf_asset.id
|
|
|
|
end
|
2012-01-12 15:17:59 -08:00
|
|
|
relationship.swf_asset = swf_asset
|
2010-10-07 07:46:23 -07:00
|
|
|
relationships << relationship
|
|
|
|
end
|
|
|
|
end
|
2012-01-13 14:11:44 -08:00
|
|
|
pet_state.parent_swf_asset_relationships_to_update = relationships
|
2012-05-23 17:00:38 -07:00
|
|
|
pet_state.unconverted = (relationships.size == 1)
|
2010-10-07 07:46:23 -07:00
|
|
|
pet_state
|
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2010-11-15 18:15:33 -08:00
|
|
|
def self.repair_all!
|
|
|
|
self.transaction do
|
|
|
|
self.all.each do |pet_state|
|
|
|
|
pet_state.sort_swf_asset_ids!
|
|
|
|
pet_state.save
|
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2010-11-15 18:15:33 -08:00
|
|
|
self.
|
|
|
|
select('pet_states.pet_type_id, pet_states.swf_asset_ids, GROUP_CONCAT(DISTINCT pet_states.id) AS duplicate_ids').
|
|
|
|
joins('INNER JOIN pet_states ps2 ON pet_states.pet_type_id = ps2.pet_type_id AND pet_states.swf_asset_ids = ps2.swf_asset_ids').
|
|
|
|
group('pet_states.pet_type_id, pet_states.swf_asset_ids').
|
|
|
|
having('count(*) > 1').
|
|
|
|
all.
|
|
|
|
each do |pet_state|
|
|
|
|
pet_state.reassign_duplicates!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-08-02 14:58:16 -07:00
|
|
|
|
|
|
|
# Copied from https://github.com/matchu/neopets/blob/5d13a720b616ba57fbbd54541f3e5daf02b3fedc/lib/neopets/pet/mood.rb
|
|
|
|
class Mood
|
|
|
|
attr_reader :id, :name
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
@id = options[:id]
|
|
|
|
@name = options[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find(id)
|
|
|
|
self.all_by_id[id.to_i]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all
|
|
|
|
@all ||= [
|
|
|
|
Mood.new(:id => 1, :name => :happy),
|
|
|
|
Mood.new(:id => 2, :name => :sad),
|
|
|
|
Mood.new(:id => 4, :name => :sick)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all_by_id
|
|
|
|
@all_by_id ||= self.all.inject({}) { |h, m| h[m.id] = m; h }
|
|
|
|
end
|
|
|
|
end
|
2010-05-20 18:11:08 -07:00
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|