From 0244653cb0d327e76f844f09100e4f936bfb7d41 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Mon, 7 Oct 2024 17:38:53 -0700 Subject: [PATCH] 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! --- app/controllers/pet_types_controller.rb | 52 ++++++++++++++++--------- app/models/pet_state.rb | 20 +++++++++- app/models/pet_type.rb | 1 - 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/app/controllers/pet_types_controller.rb b/app/controllers/pet_types_controller.rb index b38d37ef..d55f0685 100644 --- a/app/controllers/pet_types_controller.rb +++ b/app/controllers/pet_types_controller.rb @@ -1,27 +1,41 @@ class PetTypesController < ApplicationController def index - @species_names = Species.order(:name).map(&:human_name) - @color_names = Color.order(:name).map(&:human_name) + respond_to do |format| + format.html { + @species_names = Species.order(:name).map(&:human_name) + @color_names = Color.order(:name).map(&:human_name) - if params[:species].present? - @selected_species = Species.find_by!(name: params[:species]) - @selected_species_name = @selected_species.human_name - end - if params[:color].present? - @selected_color = Color.find_by!(name: params[:color]) - @selected_color_name = @selected_color.human_name - end + if params[:species].present? + @selected_species = Species.find_by!(name: params[:species]) + @selected_species_name = @selected_species.human_name + end + if params[:color].present? + @selected_color = Color.find_by!(name: params[:color]) + @selected_color_name = @selected_color.human_name + end - @pet_types = PetType. - includes(:color, :species, :pet_states). - order(created_at: :desc). - paginate(page: params[:page], per_page: 30) + @pet_types = PetType. + includes(:color, :species, :pet_states). + order(created_at: :desc). + paginate(page: params[:page], per_page: 30) - if @selected_species - @pet_types = @pet_types.where(species_id: @selected_species) - end - if @selected_color - @pet_types = @pet_types.where(color_id: @selected_color) + if @selected_species + @pet_types = @pet_types.where(species_id: @selected_species) + end + if @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 diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index 27c27c87..52dffdfd 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -12,7 +12,7 @@ class PetState < ApplicationRecord 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 @@ -209,5 +209,23 @@ class PetState < ApplicationRecord self.female = female self.unconverted = unconverted 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 diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index 784bf9bf..055cca2d 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -156,6 +156,5 @@ class PetType < ApplicationRecord end end end - end