Add /rainbow-pool.json for all species, colors, and poses

This clocks in a bit bigger than what Impress 2020 does in terms of
binary encoding (with gzip it's at 11K instead of 4K), but I'm okay
with that for the simplicity win.

Gonna try to swap this in for where we're still using Impress 2020 for
the species/color picker in the outfit editor!
This commit is contained in:
Emi Matchu 2024-10-07 17:38:53 -07:00
parent 2c0d55edd1
commit 0244653cb0
3 changed files with 52 additions and 21 deletions

View file

@ -1,5 +1,7 @@
class PetTypesController < ApplicationController class PetTypesController < ApplicationController
def index def index
respond_to do |format|
format.html {
@species_names = Species.order(:name).map(&:human_name) @species_names = Species.order(:name).map(&:human_name)
@color_names = Color.order(:name).map(&:human_name) @color_names = Color.order(:name).map(&:human_name)
@ -23,6 +25,18 @@ class PetTypesController < ApplicationController
if @selected_color if @selected_color
@pet_types = @pet_types.where(color_id: @selected_color) @pet_types = @pet_types.where(color_id: @selected_color)
end end
}
format.json {
if stale?(etag: PetState.last_updated_key)
render json: {
species: Species.order(:name).all,
colors: Color.order(:name).all,
supported_poses: PetState.all_supported_poses,
}
end
}
end
end end
def show def show

View file

@ -12,7 +12,7 @@ class PetState < ApplicationRecord
belongs_to :pet_type belongs_to :pet_type
delegate :color, to: :pet_type delegate :species_id, :species, :color_id, :color, to: :pet_type
alias_method :swf_asset_ids_from_association, :swf_asset_ids alias_method :swf_asset_ids_from_association, :swf_asset_ids
@ -209,5 +209,23 @@ class PetState < ApplicationRecord
self.female = female self.female = female
self.unconverted = unconverted self.unconverted = unconverted
end end
def self.last_updated_key
PetState.maximum(:id)
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
end end

View file

@ -156,6 +156,5 @@ class PetType < ApplicationRecord
end end
end end
end end
end end