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:
parent
2c0d55edd1
commit
0244653cb0
3 changed files with 52 additions and 21 deletions
|
@ -1,27 +1,41 @@
|
||||||
class PetTypesController < ApplicationController
|
class PetTypesController < ApplicationController
|
||||||
def index
|
def index
|
||||||
@species_names = Species.order(:name).map(&:human_name)
|
respond_to do |format|
|
||||||
@color_names = Color.order(:name).map(&:human_name)
|
format.html {
|
||||||
|
@species_names = Species.order(:name).map(&:human_name)
|
||||||
|
@color_names = Color.order(:name).map(&:human_name)
|
||||||
|
|
||||||
if params[:species].present?
|
if params[:species].present?
|
||||||
@selected_species = Species.find_by!(name: params[:species])
|
@selected_species = Species.find_by!(name: params[:species])
|
||||||
@selected_species_name = @selected_species.human_name
|
@selected_species_name = @selected_species.human_name
|
||||||
end
|
end
|
||||||
if params[:color].present?
|
if params[:color].present?
|
||||||
@selected_color = Color.find_by!(name: params[:color])
|
@selected_color = Color.find_by!(name: params[:color])
|
||||||
@selected_color_name = @selected_color.human_name
|
@selected_color_name = @selected_color.human_name
|
||||||
end
|
end
|
||||||
|
|
||||||
@pet_types = PetType.
|
@pet_types = PetType.
|
||||||
includes(:color, :species, :pet_states).
|
includes(:color, :species, :pet_states).
|
||||||
order(created_at: :desc).
|
order(created_at: :desc).
|
||||||
paginate(page: params[:page], per_page: 30)
|
paginate(page: params[:page], per_page: 30)
|
||||||
|
|
||||||
if @selected_species
|
if @selected_species
|
||||||
@pet_types = @pet_types.where(species_id: @selected_species)
|
@pet_types = @pet_types.where(species_id: @selected_species)
|
||||||
end
|
end
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -156,6 +156,5 @@ class PetType < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue