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
|
|
|
|
2023-11-02 13:50:33 -07:00
|
|
|
# A simple ordering that tries to bring reliable pet states to the front.
|
2023-07-22 14:04:01 -07:00
|
|
|
scope :emotion_order, -> {
|
2023-11-02 13:50:33 -07:00
|
|
|
order(Arel.sql(
|
|
|
|
"(mood_id IS NULL) ASC, mood_id ASC, female DESC, unconverted DESC, " +
|
|
|
|
"glitched ASC, id DESC"
|
|
|
|
))
|
2023-07-22 14:04:01 -07:00
|
|
|
}
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2023-11-02 13:50:33 -07:00
|
|
|
# Filter pet states using the "pose" concept we use in the editor.
|
2023-12-05 18:04:54 -08:00
|
|
|
scope :with_pose, -> pose {
|
2023-11-02 13:50:33 -07:00
|
|
|
case pose
|
|
|
|
when "UNCONVERTED"
|
|
|
|
where(unconverted: true)
|
|
|
|
when "HAPPY_MASC"
|
|
|
|
where(mood_id: 1, female: false)
|
|
|
|
when "HAPPY_FEM"
|
|
|
|
where(mood_id: 1, female: true)
|
|
|
|
when "SAD_MASC"
|
|
|
|
where(mood_id: 2, female: false)
|
|
|
|
when "SAD_FEM"
|
|
|
|
where(mood_id: 2, female: true)
|
|
|
|
when "SICK_MASC"
|
|
|
|
where(mood_id: 4, female: false)
|
|
|
|
when "SICK_FEM"
|
|
|
|
where(mood_id: 4, female: true)
|
|
|
|
when "UNKNOWN"
|
|
|
|
where(mood_id: nil).or(where(female: nil))
|
|
|
|
else
|
|
|
|
raise ArgumentError, "unexpected pose value #{pose}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
def pose
|
|
|
|
if unconverted?
|
|
|
|
"UNCONVERTED"
|
|
|
|
elsif mood_id.nil? || female.nil?
|
|
|
|
"UNKNOWN"
|
|
|
|
elsif mood_id == 1 && !female?
|
|
|
|
"HAPPY_MASC"
|
|
|
|
elsif mood_id == 1 && female?
|
|
|
|
"HAPPY_FEM"
|
|
|
|
elsif mood_id == 2 && !female?
|
|
|
|
"SAD_MASC"
|
|
|
|
elsif mood_id == 2 && female?
|
|
|
|
"SAD_FEM"
|
|
|
|
elsif mood_id == 4 && !female?
|
|
|
|
"SICK_MASC"
|
|
|
|
elsif mood_id == 4 && female?
|
|
|
|
"SICK_FEM"
|
|
|
|
else
|
|
|
|
raise "could not identify pose: moodId=#{mood_id}, female=#{female}, " +
|
|
|
|
"unconverted=#{unconverted}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-27 22:14:00 -07:00
|
|
|
# TODO: More and more, wanting to refactor poses…
|
|
|
|
def pose=(pose)
|
|
|
|
case pose
|
|
|
|
when "UNKNOWN"
|
|
|
|
label_pose nil, nil, unconverted: nil, labeled: false
|
|
|
|
when "HAPPY_MASC"
|
|
|
|
label_pose 1, false
|
|
|
|
when "HAPPY_FEM"
|
|
|
|
label_pose 1, true
|
|
|
|
when "SAD_MASC"
|
|
|
|
label_pose 2, false
|
|
|
|
when "SAD_FEM"
|
|
|
|
label_pose 2, true
|
|
|
|
when "SICK_MASC"
|
|
|
|
label_pose 4, false
|
|
|
|
when "SICK_FEM"
|
|
|
|
label_pose 4, true
|
|
|
|
when "UNCONVERTED"
|
|
|
|
label_pose nil, nil, unconverted: true
|
|
|
|
end
|
|
|
|
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
|
2024-09-26 20:24:31 -07:00
|
|
|
|
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
|
2014-03-31 19:05:28 -07:00
|
|
|
|
2024-09-26 20:24:31 -07:00
|
|
|
def to_param
|
|
|
|
"#{id}-#{pose.split('_').map(&:capitalize).join('-')}"
|
|
|
|
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
|
2010-10-07 07:46:23 -07:00
|
|
|
pet_state
|
|
|
|
end
|
2024-09-27 22:14:00 -07:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# A helper for the `pose=` method.
|
|
|
|
def label_pose(mood_id, female, unconverted: false, labeled: true)
|
|
|
|
self.labeled = labeled
|
|
|
|
self.mood_id = mood_id
|
|
|
|
self.female = female
|
|
|
|
self.unconverted = unconverted
|
|
|
|
end
|
2010-05-20 18:11:08 -07:00
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|