bulk preloading item assets after 5 seconds, to be able to show reds more quickly

This commit is contained in:
Emi Matchu 2010-06-07 16:33:43 -04:00
parent 240a070281
commit 5e5032d671
3 changed files with 46 additions and 20 deletions

View file

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

View file

@ -6,7 +6,11 @@ class SwfAsset < ActiveRecord::Base
delegate :depth, :to => :zone delegate :depth, :to => :zone
scope :for_json, includes(:zone) scope :for_json, includes(:zone)
scope :fitting_body_id, lambda { |body_id| where(arel_table[:body_id].in([body_id, 0])) } 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))
}
def local_url def local_url
uri = URI.parse(url) uri = URI.parse(url)

View file

@ -77,16 +77,20 @@ function PetType() {
} }
} }
this.onUpdate = function () {
if(pet_type == PetType.current) Preview.update()
}
function loadAssets() { function loadAssets() {
function onComplete() { if(pet_type == PetType.current) Preview.update(); } if(loaded_assets) {
if(!loaded_assets) { pet_type.onUpdate();
} else {
$.getJSON('/pet_types/' + pet_type.id + '/swf_assets.json', function (assets) { $.getJSON('/pet_types/' + pet_type.id + '/swf_assets.json', function (assets) {
pet_type.assets = assets; pet_type.assets = assets;
loaded_assets = true; loaded_assets = true;
onComplete(); pet_type.onUpdate();
}); });
} }
onComplete();
} }
function showDeactivationMsg() { function showDeactivationMsg() {
@ -95,6 +99,15 @@ function PetType() {
} }
PetType.all = []; 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(PetType.all, function () {
var assets = assets_by_body_id[this.body_id] || [];
Item.current.setAssetsForPetType(assets, this);
});
});
}
PetType.LOAD_ERROR = new LoadError("$color_article $color $species"); PetType.LOAD_ERROR = new LoadError("$color_article $color $species");
PetType.DASH_REGEX = /-/g; PetType.DASH_REGEX = /-/g;
@ -111,25 +124,18 @@ PetType.createFromLink = function (link) {
return pet_type; return pet_type;
} }
function Item() { function Item(id) {
this.assets_by_body_id = {}; this.assets_by_body_id = {};
this.assets_url_base = '/' + id + '/swf_assets.json';
this.load = function (pet_type) { this.load = function (pet_type) {
var url = '/' + this.id + '/swf_assets.json?body_id=' + pet_type.body_id, var url = this.assets_url_base + '?body_id=' + pet_type.body_id,
item = this; item = this;
function onComplete() { if(pet_type == PetType.current) Preview.update() }
if(this.getAssetsForPetType(pet_type).length) { if(this.getAssetsForPetType(pet_type).length) {
onComplete(); pet_type.onUpdate();
} else { } else {
$.getJSON(url, function (data) { $.getJSON(url, function (data) {
if(data.length) { item.setAssetsForPetType(data, pet_type);
item.assets_by_body_id[pet_type.body_id] = data;
onComplete();
} else {
pet_type.deactivate(Item.LOAD_ERROR, {
item: item.name
});
}
}); });
} }
} }
@ -141,13 +147,23 @@ function Item() {
this.setAsCurrent = function () { this.setAsCurrent = function () {
Item.current = this; Item.current = this;
} }
this.setAssetsForPetType = function (assets, pet_type) {
if(assets.length) {
this.assets_by_body_id[pet_type.body_id] = assets;
pet_type.onUpdate();
} else {
pet_type.deactivate(Item.LOAD_ERROR, {
item: this.name
});
}
}
} }
Item.LOAD_ERROR = new LoadError("$species_article $species wear a $item"); Item.LOAD_ERROR = new LoadError("$species_article $species wear a $item");
Item.createFromLocation = function () { Item.createFromLocation = function () {
var item = new Item(); var item = new Item(parseInt(document.location.pathname.substr(1)));
item.id = parseInt(document.location.pathname.substr(1));
return item; return item;
} }
@ -217,4 +233,6 @@ speciesList.each(function () {
}); });
}); });
setTimeout(PetType.all.load, 5000);
MainWardrobe = { View: { Outfit: Preview } }; MainWardrobe = { View: { Outfit: Preview } };