alter item swf asset routes to allow for page caching

This commit is contained in:
Emi Matchu 2010-06-08 18:26:42 -04:00
parent 6014e87cfd
commit 37372544b0
6 changed files with 34 additions and 23 deletions

View file

@ -4,10 +4,10 @@ class SwfAssetsController < ApplicationController
@swf_assets = Item.find(params[:item_id]).swf_assets
if params[:body_id]
@swf_assets = @swf_assets.fitting_body_id(params[:body_id])
if params[:body_id].is_a? Array
else
@swf_assets = @swf_assets.fitting_standard_body_ids
json = @swf_assets.for_json.all.group_by(&:body_id)
end
end
elsif params[:pet_type_id]
@swf_assets = PetType.find(params[:pet_type_id]).pet_states.first.swf_assets
end

View file

@ -2,4 +2,5 @@ class Color < PetAttribute
fetch_objects!
Basic = %w(blue green red yellow).map { |name| find_by_name(name) }
BasicIds = Basic.map(&:id)
end

View file

@ -3,6 +3,11 @@ class PetType < ActiveRecord::Base
BasicHashes = YAML::load_file(Rails.root.join('config', 'basic_type_hashes.yml'))
StandardBodyIds = PetType.select(arel_table[:body_id]).
where(arel_table[:color_id].in(Color::BasicIds)).
group(arel_table[:species_id]).map(&:body_id)
scope :random_basic_per_species, lambda { |species_ids|
conditions = nil
species_ids.each do |species_id|

View file

@ -6,10 +6,14 @@ class SwfAsset < ActiveRecord::Base
delegate :depth, :to => :zone
scope :for_json, includes(:zone)
scope :fitting_body_id, lambda { |body_ids|
body_ids = [body_ids] unless body_ids.is_a?(Array)
body_ids << 0
where(arel_table[:body_id].in(body_ids))
scope :fitting_body_id, lambda { |body_id|
where(arel_table[:body_id].in([body_id, 0]))
}
BodyIdsFittingStandard = PetType::StandardBodyIds + [0]
scope :fitting_standard_body_ids, lambda {
where(arel_table[:body_id].in(BodyIdsFittingStandard))
}
def local_url

View file

@ -3,6 +3,7 @@ OpenneoImpressItems::Application.routes.draw do |map|
match '/:id' => 'items#show', :as => :item
match '/:item_id/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets
match '/:item_id/bodies/:body_id/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets_for_body_id
match '/pet_types/:pet_type_id/swf_assets.json' => 'swf_assets#index', :as => :pet_type_swf_assets
match '/species/:species_id/color/:color_id/pet_type.json' => 'pet_types#show'

View file

@ -103,19 +103,6 @@ function PetType() {
}
PetType.all = [];
PetType.all.load = function () {
var body_ids = $.map(PetType.all, function (pt) { return pt.body_id });
$.getJSON(Item.current.assets_url_base, {body_id: body_ids}, function (assets_by_body_id) {
$.each(assets_by_body_id, function (i) {
Item.current.assets_by_body_id[parseInt(i)] = this;
});
$.each(PetType.all, function () {
if(Item.current.getAssetsForPetType(this).length == 0) {
this.deactivateWithItem(Item.current);
}
});
});
}
PetType.LOAD_ERROR = new LoadError("$color_article $color $species");
PetType.DASH_REGEX = /-/g;
@ -134,10 +121,9 @@ PetType.createFromLink = function (link) {
function Item(id) {
this.assets_by_body_id = {};
this.assets_url_base = '/' + id + '/swf_assets.json';
this.load = function (pet_type) {
var url = this.assets_url_base + '?body_id=' + pet_type.body_id,
var url = '/' + id + '/bodies/' + pet_type.body_id + '/swf_assets.json',
item = this;
if(this.getAssetsForPetType(pet_type).length) {
pet_type.onUpdate();
@ -148,6 +134,20 @@ function Item(id) {
}
}
this.loadAllStandard = function () {
var item = this;
$.getJSON('/' + id + '/swf_assets.json', function (assets_by_body_id) {
$.each(assets_by_body_id, function (i) {
item.assets_by_body_id[parseInt(i)] = this;
});
$.each(PetType.all, function () {
if(item.getAssetsForPetType(this).length == 0) {
this.deactivateWithItem(item);
}
});
});
}
this.getAssetsForPetType = function (pet_type) {
return this.assets_by_body_id[pet_type.body_id] || this.assets_by_body_id[0] || [];
}
@ -248,6 +248,6 @@ speciesList.each(function () {
});
});
setTimeout(PetType.all.load, 5000);
setTimeout($.proxy(Item.current, 'loadAllStandard'), 5000);
MainWardrobe = { View: { Outfit: Preview } };