From 665166507eb19b31def1aa0e716d7655cde6bd0c Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Thu, 5 Dec 2013 18:19:39 -0700 Subject: [PATCH] classes: move item-store sorting from web to shard (so we can share with mobile) --- dist/habitrpg-shared.js | 36 ++++++++++++++++++++++++++---------- script/items.coffee | 26 +++++++++++++++++--------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/dist/habitrpg-shared.js b/dist/habitrpg-shared.js index 3893038957..32e438f8a2 100644 --- a/dist/habitrpg-shared.js +++ b/dist/habitrpg-shared.js @@ -11259,25 +11259,41 @@ try { module.exports.updateStore = function(user) { var changes; - changes = {}; + changes = []; _.each(['weapon', 'armor', 'shield', 'head'], function(type) { var i; i = module.exports.getItem(user, type).index; - return changes[type] = items.gear.flat["" + type + "_" + user.stats["class"] + "_" + (+i + 1)] || { + return changes.push(items.gear.flat["" + type + "_" + user.stats["class"] + "_" + (+i + 1)] || { hide: true - }; + }); }); - _.defaults(changes, _.reduce(_.where(items.gear.flat, { + _.defaults(changes, _.transform(_.where(items.gear.flat, { klass: 'special' }), function(m, v) { if ((typeof v.canOwn === "function" ? v.canOwn(user) : void 0) && !user.items.gear.owned[v.key]) { - m[v.key] = v; + return m.push(v); } - return m; - }, {})); - changes.potion = items.potion; - changes.reroll = items.reroll; - return changes; + })); + changes.push(items.potion); + changes.push(items.reroll); + return _.sortBy(changes, function(item) { + switch (item.type) { + case 'weapon': + return 1; + case 'armor': + return 2; + case 'head': + return 3; + case 'shield': + return 4; + case 'potion': + return 5; + case 'reroll': + return 6; + default: + return 7; + } + }); }; /* diff --git a/script/items.coffee b/script/items.coffee index 0f9881b9de..b88a908ab6 100644 --- a/script/items.coffee +++ b/script/items.coffee @@ -452,20 +452,28 @@ module.exports.buyItem = (user, item) -> update store ### module.exports.updateStore = (user) -> - changes = {} + changes = [] _.each ['weapon', 'armor', 'shield', 'head'], (type) -> i = module.exports.getItem(user, type).index - changes[type] = items.gear.flat["#{type}_#{user.stats.class}_#{+i + 1}"] or {hide:true} + changes.push items.gear.flat["#{type}_#{user.stats.class}_#{+i + 1}"] or {hide:true} # Add special items (contrib gear, backer gear, etc) - _.defaults changes, _.reduce(_.where(items.gear.flat, {klass:'special'}), (m,v) -> - m[v.key] = v if v.canOwn?(user) && !user.items.gear.owned[v.key] - m - , {}) + _.defaults changes, _.transform _.where(items.gear.flat, {klass:'special'}), (m,v) -> + m.push v if v.canOwn?(user) && !user.items.gear.owned[v.key] - changes.potion = items.potion - changes.reroll = items.reroll - changes + changes.push items.potion + changes.push items.reroll + + # Return sorted store (array) + _.sortBy changes, (item) -> + switch item.type + when 'weapon' then 1 + when 'armor' then 2 + when 'head' then 3 + when 'shield' then 4 + when 'potion' then 5 + when 'reroll' then 6 + else 7 ### Gets an item, and caps max to the last item in its array