From 90387707d8b165df9e5c1ff14ce9acf0de3cec3e Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 22 Sep 2015 20:53:51 -0500 Subject: [PATCH] Move flattening function to gear module --- common/script/content/index.coffee | 27 ++----------- common/script/src/content/classes.js | 3 ++ common/script/src/content/gear/index.js | 51 ++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 common/script/src/content/classes.js diff --git a/common/script/content/index.coffee b/common/script/content/index.coffee index 7cd5797d72..76b835d3f6 100644 --- a/common/script/content/index.coffee +++ b/common/script/content/index.coffee @@ -12,8 +12,7 @@ t = require('../../dist/scripts/content/helpers').translator --------------------------------------------------------------- ### -classes = ['warrior', 'rogue', 'healer', 'wizard'] -gearTypes = [ 'weapon', 'armor', 'head', 'shield', 'body', 'back', 'headAccessory', 'eyewear'] +classes = require('../../dist/scripts/content/classes') events = require('../../dist/scripts/content/events') @@ -29,28 +28,8 @@ gear = require('../../dist/scripts/content/gear/index') ### api.gear = tree: gear - flat: {} + flat: gear.flat -_.each gearTypes, (type) -> - _.each classes.concat(['base', 'special', 'mystery', 'armoire']), (klass) -> - # add "type" to each item, so we can reference that as "weapon" or "armor" in the html - _.each gear[type][klass], (item, i) -> - key = "#{type}_#{klass}_#{i}" - _.defaults item, {type, key, klass, index: i, str:0, int:0, per:0, con:0} - - if item.event - #? indicates null/undefined. true means they own currently, false means they once owned - and false is what we're - # after (they can buy back if they bought it during the event's timeframe) - _canOwn = item.canOwn or (->true) - item.canOwn = (u)-> - _canOwn(u) and - (u.items.gear.owned[key]? or (moment().isAfter(item.event.start) and moment().isBefore(item.event.end))) and - (if item.specialClass then (u.stats.class is item.specialClass) else true) - - if item.mystery - item.canOwn = (u)-> u.items.gear.owned[key]? - - api.gear.flat[key] = item ### Time Traveler Store, mystery sets need their items mapped in @@ -101,7 +80,7 @@ api.classes = classes --------------------------------------------------------------- ### -api.gearTypes = gearTypes +api.gearTypes = gear.gearTypes api.spells = require('../../dist/scripts/content/spells/index') diff --git a/common/script/src/content/classes.js b/common/script/src/content/classes.js new file mode 100644 index 0000000000..7966effb1c --- /dev/null +++ b/common/script/src/content/classes.js @@ -0,0 +1,3 @@ +let classes = ['warrior', 'rogue', 'healer', 'wizard']; + +export default classes; diff --git a/common/script/src/content/gear/index.js b/common/script/src/content/gear/index.js index 697edc8681..81d30b5105 100644 --- a/common/script/src/content/gear/index.js +++ b/common/script/src/content/gear/index.js @@ -1,5 +1,6 @@ import {translator as t} from '../helpers'; -import events from '../events'; +import {each, defaults} from 'lodash'; +import classes from '../classes'; import weapon from './weapon'; import armor from './armor'; @@ -10,6 +11,8 @@ import body from './body'; import headAccessory from './head-accessory'; import eyewear from './eyewear'; +const GEAR_TYPES = [ 'weapon', 'armor', 'head', 'shield', 'body', 'back', 'headAccessory', 'eyewear'] + let gear = { weapon: weapon, armor: armor, @@ -21,4 +24,48 @@ let gear = { eyewear: eyewear, }; -export default gear; +let flat = {}; + + +// The gear is exported as a tree (defined above), and a flat list (eg, {weapon_healer_1: .., shield_special_0: ...}) since +// they are needed in different forms at different points in the app + +each(GEAR_TYPES, function(type) { + let classTypes = classes.concat(['base', 'special', 'mystery', 'armoire']); + + each(classTypes, function(klass) { + each(gear[type][klass], function(item, i) { + let key = type + "_" + klass + "_" + i; + defaults(item, { + type: type, + key: key, + klass: klass, + index: i, + str: 0, + int: 0, + per: 0, + con: 0 + }); + if (item.event) { + let _canOwn = item.canOwn || (function() { + return true; + }); + item.canOwn = function(u) { + return _canOwn(u) && ((u.items.gear.owned[key] != null) || (moment().isAfter(item.event.start) && moment().isBefore(item.event.end))) && (item.specialClass ? u.stats["class"] === item.specialClass : true); + }; + } + if (item.mystery) { + item.canOwn = function(u) { + return u.items.gear.owned[key] != null; + }; + } + flat[key] = item; + }); + }); +}); + +export default { + tree: gear, + flat: flat, + gearTypes: GEAR_TYPES +};