2023-08-02 16:05:02 -07:00
|
|
|
class PetState < ApplicationRecord
|
2010-05-20 18:11:08 -07:00
|
|
|
SwfAssetType = 'biology'
|
2024-10-04 19:24:40 -07:00
|
|
|
|
|
|
|
MAIN_POSES = %w(HAPPY_FEM HAPPY_MASC SAD_FEM SAD_MASC SICK_FEM SICK_MASC)
|
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
|
2024-11-10 11:36:23 -08:00
|
|
|
has_many :parent_swf_asset_relationships, :as => :parent
|
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
|
|
|
|
2024-11-03 12:16:27 -08:00
|
|
|
serialize :swf_asset_ids, coder: Serializers::IntegerSet, type: Array
|
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
belongs_to :pet_type
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2024-10-07 17:38:53 -07:00
|
|
|
delegate :species_id, :species, :color_id, :color, to: :pet_type
|
2014-03-28 13:15:04 -07:00
|
|
|
|
2010-10-07 07:46:23 -07:00
|
|
|
alias_method :swf_asset_ids_from_association, :swf_asset_ids
|
2011-06-21 17:42:41 -07:00
|
|
|
|
2024-12-01 11:13:21 -08:00
|
|
|
scope :glitched, -> { where(glitched: true) }
|
|
|
|
scope :needs_labeling, -> { unlabeled.where(glitched: false) }
|
|
|
|
scope :unlabeled, -> { with_pose("UNKNOWN") }
|
|
|
|
scope :usable, -> { where(labeled: true, glitched: false) }
|
|
|
|
|
2024-12-01 10:09:26 -08:00
|
|
|
scope :newest, -> { order(created_at: :desc) }
|
|
|
|
scope :newest_pet_type, -> { joins(:pet_type).merge(PetType.newest) }
|
2024-12-08 10:08:39 -08:00
|
|
|
scope :created_before, ->(time) { where(arel_table[:created_at].lt(time)) }
|
2024-12-01 10:09:26 -08: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
|
|
|
|
|
2024-09-26 20:24:31 -07:00
|
|
|
def to_param
|
|
|
|
"#{id}-#{pose.split('_').map(&:capitalize).join('-')}"
|
|
|
|
end
|
|
|
|
|
2024-11-03 12:16:27 -08:00
|
|
|
# Because our column is named `swf_asset_ids`, we need to ensure writes to
|
|
|
|
# it go to the attribute, and not the thing ActiveRecord does of finding the
|
|
|
|
# relevant `swf_assets`.
|
|
|
|
# TODO: Consider renaming the column to `cached_swf_asset_ids`?
|
|
|
|
def swf_asset_ids=(new_swf_asset_ids)
|
|
|
|
write_attribute(:swf_asset_ids, new_swf_asset_ids)
|
|
|
|
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
|
2024-10-07 17:38:53 -07:00
|
|
|
|
|
|
|
def self.last_updated_key
|
2024-10-07 17:56:42 -07:00
|
|
|
PetState.maximum(:updated_at)
|
2024-10-07 17:38:53 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.all_supported_poses
|
|
|
|
Rails.cache.fetch("PetState.all_supported_poses #{last_updated_key}") do
|
|
|
|
{}.tap do |h|
|
|
|
|
includes(:pet_type).find_each do |pet_state|
|
|
|
|
h[pet_state.species_id] ||= {}
|
|
|
|
h[pet_state.species_id][pet_state.color_id] ||= []
|
|
|
|
h[pet_state.species_id][pet_state.color_id] << pet_state.pose
|
|
|
|
end
|
|
|
|
|
|
|
|
h.values.map(&:values).flatten(1).each(&:uniq!).each(&:sort!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-12-01 11:13:21 -08:00
|
|
|
|
2024-12-08 10:08:39 -08:00
|
|
|
def self.next_unlabeled_appearance(after_id: nil)
|
2024-12-01 11:13:21 -08:00
|
|
|
# Rather than just getting the newest unlabeled pet state, prioritize the
|
|
|
|
# newest *pet type*. This better matches the user's perception of what the
|
|
|
|
# newest state is, because the Rainbow Pool UI is grouped by pet type!
|
2024-12-08 10:08:39 -08:00
|
|
|
pet_states = needs_labeling.newest_pet_type.newest
|
|
|
|
|
|
|
|
# If `after_id` is given, convert it from a PetState ID to creation
|
|
|
|
# timestamps, and find the next record prior to those timestamps. This
|
|
|
|
# enables skipping past records the user doesn't want to label.
|
|
|
|
if after_id
|
|
|
|
begin
|
|
|
|
after_pet_state = PetState.find(after_id)
|
|
|
|
before_pt_created_at = after_pet_state.pet_type.created_at
|
|
|
|
before_ps_created_at = after_pet_state.created_at
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
Rails.logger.warn "PetState.next_unlabeled_appearance: Could not " +
|
|
|
|
"find pet state ##{after_id}"
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Because we sort by `newest_pet_type` first, then breaks ties by
|
|
|
|
# `newest`, our filter needs to operate the same way. Kudos to:
|
|
|
|
# https://brunoscheufler.com/blog/2022-01-01-paginating-large-ordered-datasets-with-cursor-based-pagination
|
|
|
|
pet_states.merge!(
|
|
|
|
PetType.created_before(before_pt_created_at).or(
|
|
|
|
PetType.created_at(before_pt_created_at).and(
|
|
|
|
PetState.created_before(before_ps_created_at)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
pet_states.first
|
2024-12-01 11:13:21 -08:00
|
|
|
end
|
2010-05-20 18:11:08 -07:00
|
|
|
end
|
2011-06-21 17:42:41 -07:00
|
|
|
|