contrib: move equipped() helper to items.coffee, use a more robust

method of determining which item is available in the store, and which
items i can legally equip. It's a bit wild, I think we should find a
different approach
This commit is contained in:
Tyler Renelle 2013-11-07 13:11:35 -08:00
parent 54229ee406
commit 761887da18
3 changed files with 185 additions and 175 deletions

View file

@ -9171,80 +9171,7 @@ var process=require("__browserify_process");(function() {
loc = (typeof window !== "undefined" && window !== null ? window.location.host : void 0) || (typeof process !== "undefined" && process !== null ? (_ref = process.env) != null ? _ref.BASE_URL : void 0 : void 0) || '';
return encodeURIComponent("http://" + loc + "/v1/users/" + uid + "/calendar.ics?apiToken=" + apiToken);
},
/*
User's currently equiped item
*/
equipped: function(type, item, preferences, backerTier) {
var armorSet, gender;
if (item == null) {
item = 0;
}
if (preferences == null) {
preferences = {
gender: 'm',
armorSet: 'v1'
};
}
if (backerTier == null) {
backerTier = 0;
}
gender = preferences.gender, armorSet = preferences.armorSet;
item = ~~item;
backerTier = ~~backerTier;
switch (type) {
case 'armor':
if (item > 5) {
if (backerTier >= 45) {
return 'armor_6';
}
item = 5;
}
if (gender === 'f') {
if (item === 0) {
return "f_armor_" + item + "_" + armorSet;
} else {
return "f_armor_" + item;
}
} else {
return "m_armor_" + item;
}
break;
case 'head':
if (item > 5) {
if (backerTier >= 45) {
return 'head_6';
}
item = 5;
}
if (gender === 'f') {
if (item > 1) {
return "f_head_" + item + "_" + armorSet;
} else {
return "f_head_" + item;
}
} else {
return "m_head_" + item;
}
break;
case 'shield':
if (item > 5) {
if (backerTier >= 45) {
return 'shield_6';
}
item = 5;
}
return "" + preferences.gender + "_shield_" + item;
case 'weapon':
if (item > 6) {
if (backerTier >= 70) {
return 'weapon_7';
}
item = 6;
}
return "" + preferences.gender + "_weapon_" + item;
}
},
equipped: items.equipped,
/*
Gold amount from their money
*/
@ -9484,7 +9411,7 @@ try {
} catch(e) {}
},{"./algos.coffee":5,"./helpers.coffee":6,"./items.coffee":8,"lodash":2,"moment":3}],8:[function(require,module,exports){
(function() {
var items, _;
var items, reversed, _;
_ = require('lodash');
@ -9545,7 +9472,22 @@ try {
classes: 'weapon_7',
notes: 'Increases experience gain by 21%.',
strength: 21,
value: 150
value: 150,
canOwn: (function(u) {
var _ref;
return +((_ref = u.backer) != null ? _ref.tier : void 0) >= 70;
})
}, {
index: 8,
text: "Crystal Blade",
classes: 'weapon_8',
notes: 'Increases experience gain by 24%.',
strength: 24,
value: 170,
canOwn: (function(u) {
var _ref;
return +((_ref = u.contributor) != null ? _ref.level : void 0) >= 4;
})
}
],
armor: [
@ -9597,7 +9539,22 @@ try {
classes: 'armor_6',
notes: 'Decreases HP loss by 12%.',
defense: 12,
value: 150
value: 150,
canOwn: (function(u) {
var _ref;
return +((_ref = u.backer) != null ? _ref.tier : void 0) >= 45;
})
}, {
index: 7,
text: "Crystal Armor",
classes: 'armor_7',
notes: 'Decreases HP loss by 14%.',
defense: 14,
value: 170,
canOwn: (function(u) {
var _ref;
return +((_ref = u.contributor) != null ? _ref.level : void 0) >= 2;
})
}
],
head: [
@ -9649,7 +9606,22 @@ try {
classes: 'head_6',
notes: 'Decreases HP loss by 7%.',
defense: 7,
value: 100
value: 100,
canOwn: (function(u) {
var _ref;
return +((_ref = u.backer) != null ? _ref.tier : void 0) >= 45;
})
}, {
index: 7,
text: "Crystal Helm",
classes: 'head_7',
notes: 'Decreases HP loss by 8%.',
defense: 8,
value: 120,
canOwn: (function(u) {
var _ref;
return +((_ref = u.contributor) != null ? _ref.level : void 0) >= 3;
})
}
],
shield: [
@ -9701,7 +9673,22 @@ try {
classes: 'shield_6',
notes: 'Decreases HP loss by 9%.',
defense: 9,
value: 120
value: 120,
canOwn: (function(u) {
var _ref;
return +((_ref = u.backer) != null ? _ref.tier : void 0) >= 45;
})
}, {
index: 7,
text: "Crystal Shield",
classes: 'shield_7',
notes: 'Decreases HP loss by 10%.',
defense: 10,
value: 150,
canOwn: (function(u) {
var _ref;
return +((_ref = u.contributor) != null ? _ref.level : void 0) >= 5;
})
}
],
potion: {
@ -9812,9 +9799,17 @@ try {
]
};
_.each(['weapon', 'armor', 'head', 'shield'], function(key) {
return _.each(items[key], function(item) {
return item.type = key;
reversed = {};
_.each(['weapon', 'armor', 'head', 'shield'], function(type) {
reversed[type] = items[type].slice().reverse();
return _.each(items[type], function(item) {
return _.defaults(item, {
type: type,
canOwn: function() {
return true;
}
});
});
});
@ -9826,15 +9821,11 @@ try {
return hatchingPotion.notes = "Pour this on an egg, and it will hatch as a " + hatchingPotion.text + " pet.";
});
module.exports.buyItem = function(user, type, options) {
module.exports.buyItem = function(user, type) {
var nextItem;
if (options == null) {
options = {};
}
_.defaults(options, {
paths: {}
});
nextItem = type === 'potion' ? items.potion : module.exports.getItem(type, (~~user.items[type] || 0) + 1);
nextItem = type === 'potion' ? items.potion : _.find(items[type].slice(~~user.items[type] + 1), (function(i) {
return i.canOwn(user);
}));
if (+user.stats.gp < +nextItem.value) {
return false;
}
@ -9843,16 +9834,13 @@ try {
if (user.stats.hp > 50) {
user.stats.hp = 50;
}
options.paths['stats.hp'] = true;
} else {
user.items[type] = ~~nextItem.index;
if (user.items.weapon >= 6 && user.items.armor >= 5 && user.items.head >= 5 && user.items.shield >= 5) {
if (+user.items.weapon >= 6 && +user.items.armor >= 5 && +user.items.head >= 5 && +user.items.shield >= 5) {
user.achievements.ultimateGear = true;
}
options.paths["items." + type] = true;
}
user.stats.gp -= +nextItem.value;
options.paths['stats.gp'] = true;
return true;
};
@ -9865,19 +9853,11 @@ try {
var changes;
changes = {};
_.each(['weapon', 'armor', 'shield', 'head'], function(type) {
var i, showNext, _ref, _ref1, _ref2;
i = ~~(((_ref = user.items) != null ? _ref[type] : void 0) || 0) + 1;
showNext = true;
if (i === items[type].length - 1) {
if ((type === 'armor' || type === 'shield' || type === 'head')) {
showNext = ((_ref1 = user.backer) != null ? _ref1.tier : void 0) && user.backer.tier >= 45;
} else {
showNext = ((_ref2 = user.backer) != null ? _ref2.tier : void 0) && user.backer.tier >= 70;
}
} else if (i === items[type].length) {
showNext = false;
}
return changes[type] = showNext ? items[type][i] : {
var curr, _ref;
curr = ((_ref = user.items) != null ? _ref[type] : void 0) || 0;
return changes[type] = _.find(items[type].slice(curr + 1), (function(item) {
return item.canOwn(user);
})) || {
hide: true
};
});
@ -9887,7 +9867,7 @@ try {
};
/*
Gets an itme, and caps max to the last item in its array
Gets an item, and caps max to the last item in its array
*/
@ -9896,10 +9876,59 @@ try {
if (index == null) {
index = 0;
}
i = ~~index > items[type].length - 1 ? items[type].length - 1 : ~~index;
i = ~~index > items[type].length - 1 ? 0 : ~~index;
return items[type][i];
};
/*
User's currently equiped item
TODO this function is ugly, find a more elegant solution
*/
module.exports.equipped = function(type, item, pref, backer, contributor) {
var lastStandardItem, _ref;
if (item == null) {
item = 0;
}
if (pref == null) {
pref = {
gender: 'm',
armorSet: 'v1'
};
}
if (backer == null) {
backer = {};
}
if (contributor == null) {
contributor = {};
}
lastStandardItem = items[type].length - 3;
if ((item > lastStandardItem) && !((_ref = items[type][item]) != null ? _ref.canOwn({
backer: backer,
contributor: contributor
}) : void 0)) {
item = _.find(reversed[type], (function(i) {
return i.canOwn({
backer: backer,
contributor: contributor
});
})).index;
}
if (item > lastStandardItem) {
return "" + type + "_" + item;
}
if ((type === 'armor' || type === 'head') && pref.gender === 'f') {
if (item === 0 && type === 'armor') {
return "f_armor_" + item + "_" + pref.armorSet;
}
if (item > 1 && type === 'head') {
return "f_head_" + item + "_" + pref.armorSet;
}
}
return "" + pref.gender + "_" + type + "_" + item;
};
}).call(this);

View file

@ -210,44 +210,7 @@ module.exports =
loc = window?.location.host or process?.env?.BASE_URL or ''
encodeURIComponent "http://#{loc}/v1/users/#{uid}/calendar.ics?apiToken=#{apiToken}"
###
User's currently equiped item
###
equipped: (type, item=0, preferences={gender:'m', armorSet:'v1'}, backerTier=0) ->
{gender, armorSet} = preferences
item = ~~item
backerTier = ~~backerTier
switch type
when'armor'
if item > 5
return 'armor_6' if backerTier >= 45
item = 5 # set them back if they're trying to cheat
if gender is 'f'
return if (item is 0) then "f_armor_#{item}_#{armorSet}" else "f_armor_#{item}"
else
return "m_armor_#{item}"
when 'head'
if item > 5
return 'head_6' if backerTier >= 45
item = 5
if gender is 'f'
return if (item > 1) then "f_head_#{item}_#{armorSet}" else "f_head_#{item}"
else
return "m_head_#{item}"
when 'shield'
if item > 5
return 'shield_6' if backerTier >= 45
item = 5
return "#{preferences.gender}_shield_#{item}"
when 'weapon'
if item > 6
return 'weapon_7' if backerTier >= 70
item = 6
return "#{preferences.gender}_weapon_#{item}"
equipped: items.equipped
###
Gold amount from their money

View file

@ -9,7 +9,8 @@ items = module.exports.items =
{index: 4, text: "Blue Sword", classes:'weapon_4', notes:'Increases experience gain by 12%.', strength: 12, value:65}
{index: 5, text: "Red Sword", classes:'weapon_5', notes:'Increases experience gain by 15%.', strength: 15, value:90}
{index: 6, text: "Golden Sword", classes:'weapon_6', notes:'Increases experience gain by 18%.', strength: 18, value:120}
{index: 7, text: "Dark Souls Blade", classes:'weapon_7', notes:'Increases experience gain by 21%.', strength: 21, value:150}
{index: 7, text: "Dark Souls Blade", classes:'weapon_7', notes:'Increases experience gain by 21%.', strength: 21, value:150, canOwn: ((u)-> +u.backer?.tier >= 70)}
{index: 8, text: "Crystal Blade", classes:'weapon_8', notes:'Increases experience gain by 24%.', strength: 24, value:170, canOwn: ((u)-> +u.contributor?.level >= 4)}
]
armor: [
{index: 0, text: "Cloth Armor", classes: 'armor_0', notes:'Training armor.', defense: 0, value:0}
@ -18,7 +19,8 @@ items = module.exports.items =
{index: 3, text: "Plate Mail", classes: 'armor_3', notes:'Decreases HP loss by 7%.', defense: 7, value:65}
{index: 4, text: "Red Armor", classes: 'armor_4', notes:'Decreases HP loss by 8%.', defense: 8, value:90}
{index: 5, text: "Golden Armor", classes: 'armor_5', notes:'Decreases HP loss by 10%.', defense: 10, value:120}
{index: 6, text: "Shade Armor", classes: 'armor_6', notes:'Decreases HP loss by 12%.', defense: 12, value:150}
{index: 6, text: "Shade Armor", classes: 'armor_6', notes:'Decreases HP loss by 12%.', defense: 12, value:150, canOwn: ((u)-> +u.backer?.tier >= 45)}
{index: 7, text: "Crystal Armor", classes: 'armor_7', notes:'Decreases HP loss by 14%.', defense: 14, value:170, canOwn: ((u)-> +u.contributor?.level >= 2)}
]
head: [
{index: 0, text: "No Helm", classes: 'head_0', notes:'Training helm.', defense: 0, value:0}
@ -27,7 +29,8 @@ items = module.exports.items =
{index: 3, text: "Plate Helm", classes: 'head_3', notes:'Decreases HP loss by 4%.', defense: 4, value:45}
{index: 4, text: "Red Helm", classes: 'head_4', notes:'Decreases HP loss by 5%.', defense: 5, value:60}
{index: 5, text: "Golden Helm", classes: 'head_5', notes:'Decreases HP loss by 6%.', defense: 6, value:80}
{index: 6, text: "Shade Helm", classes: 'head_6', notes:'Decreases HP loss by 7%.', defense: 7, value:100}
{index: 6, text: "Shade Helm", classes: 'head_6', notes:'Decreases HP loss by 7%.', defense: 7, value:100, canOwn: ((u)-> +u.backer?.tier >= 45)}
{index: 7, text: "Crystal Helm", classes: 'head_7', notes:'Decreases HP loss by 8%.', defense: 8, value:120, canOwn: ((u)-> +u.contributor?.level >= 3)}
]
shield: [
{index: 0, text: "No Shield", classes: 'shield_0', notes:'No Shield.', defense: 0, value:0}
@ -36,7 +39,8 @@ items = module.exports.items =
{index: 3, text: "Reinforced Shield", classes: 'shield_3', notes:'Decreases HP loss by 5%.', defense: 5, value:55}
{index: 4, text: "Red Shield", classes: 'shield_4', notes:'Decreases HP loss by 7%.', defense: 7, value:70}
{index: 5, text: "Golden Shield", classes: 'shield_5', notes:'Decreases HP loss by 8%.', defense: 8, value:90}
{index: 6, text: "Tormented Skull", classes: 'shield_6', notes:'Decreases HP loss by 9%.', defense: 9, value:120}
{index: 6, text: "Tormented Skull", classes: 'shield_6', notes:'Decreases HP loss by 9%.', defense: 9, value:120, canOwn: ((u)-> +u.backer?.tier >= 45)}
{index: 7, text: "Crystal Shield", classes: 'shield_7', notes:'Decreases HP loss by 10%.', defense: 10, value:150, canOwn: ((u)-> +u.contributor?.level >= 5)}
]
potion: {type: 'potion', text: "Potion", notes: "Recover 15 HP (Instant Use)", value: 25, classes: 'potion'}
reroll: {type: 'reroll', text: "Re-Roll", classes: 'reroll', notes: "Resets your task values back to 0 (yellow). Useful when everything's red and it's hard to stay alive.", value:0 }
@ -67,32 +71,32 @@ items = module.exports.items =
{text: 'Golden', name: 'Golden', notes: 'Turns your animal into a Golden pet.', value: 5}
]
# add "type" to each item, so we can reference that as "weapon" or "armor" in the html
_.each ['weapon', 'armor', 'head', 'shield'], (key) ->
_.each items[key], (item) -> item.type = key
# we somtimes want item arrays above in reverse order, for backward lookups (you'll see later in the code)
reversed = {}
_.each ['weapon', 'armor', 'head', 'shield'], (type) ->
reversed[type] = items[type].slice().reverse()
# add "type" to each item, so we can reference that as "weapon" or "armor" in the html
# Also add canOwn(), which we use when comparing if user is a backer or contributor - but defaulted to `return true`
_.each items[type], (item) -> _.defaults(item, {type, canOwn: ->true})
_.each items.pets, (pet) -> pet.notes = 'Find a hatching potion to pour on this egg, and it will hatch into a loyal pet.'
_.each items.hatchingPotions, (hatchingPotion) -> hatchingPotion.notes = "Pour this on an egg, and it will hatch as a #{hatchingPotion.text} pet."
module.exports.buyItem = (user, type, options={}) ->
_.defaults options, {paths: {}}
module.exports.buyItem = (user, type) ->
nextItem =
if type is 'potion' then items.potion
else module.exports.getItem type, ((~~user.items[type] or 0) + 1)
if type is 'potion' then items.potion
else _.find items[type].slice(~~user.items[type] + 1), ((i) -> i.canOwn user)
return false if +user.stats.gp < +nextItem.value
if nextItem.type is 'potion'
user.stats.hp += 15;
user.stats.hp = 50 if user.stats.hp > 50
options.paths['stats.hp'] = true
else
user.items[type] = ~~nextItem.index
if user.items.weapon >= 6 and user.items.armor >= 5 and user.items.head >= 5 and user.items.shield >= 5
if +user.items.weapon >= 6 and +user.items.armor >= 5 and +user.items.head >= 5 and +user.items.shield >= 5
user.achievements.ultimateGear = true;
options.paths["items.#{type}"] = true
user.stats.gp -= +nextItem.value
options.paths['stats.gp'] = true
true
###
@ -101,23 +105,37 @@ module.exports.buyItem = (user, type, options={}) ->
module.exports.updateStore = (user) ->
changes = {}
_.each ['weapon', 'armor', 'shield', 'head'], (type) ->
i = ~~(user.items?[type] or 0) + 1
showNext = true
if i is items[type].length - 1
if (type in ['armor', 'shield', 'head'])
showNext = user.backer?.tier and user.backer.tier >= 45 # backer armor
else
showNext = user.backer?.tier and user.backer.tier >= 70 # backer weapon
else if i is items[type].length
showNext = false
changes[type] = if showNext then items[type][i] else {hide:true}
# Find the first item that fits the "next" bill, from between current item and end of the list
curr = user.items?[type] or 0
changes[type] = _.find(items[type].slice(curr+1), ((item) -> item.canOwn user)) or {hide:true}
changes.potion = items.potion
changes.reroll = items.reroll
changes
###
Gets an itme, and caps max to the last item in its array
Gets an item, and caps max to the last item in its array
###
module.exports.getItem = (type, index=0) ->
i = if (~~index > items[type].length - 1) then items[type].length - 1 else ~~index
# if they set their gear manually to something over what they can own, just set to 0 to avoid errors
i = if (~~index > items[type].length - 1) then 0 else ~~index
items[type][i]
###
User's currently equiped item
TODO this function is ugly, find a more elegant solution
###
module.exports.equipped = (type, item=0, pref={gender:'m', armorSet:'v1'}, backer={}, contributor={}) ->
lastStandardItem = items[type].length - 3 # -1 for 0-based index, -2 for backer + contrib
if (item > lastStandardItem) and !items[type][item]?.canOwn({backer,contributor})
# They entered a # above what's legal in the restore dialog, find first legit item. (we should do checking on the restore-save instead)
item = _.find(reversed[type], ((i) -> i.canOwn({backer, contributor}))).index
# backer / contrib (they don't have gender)
return "#{type}_#{item}" if (item > lastStandardItem)
# Females have some special thing going on for their armor / helms
if (type in ['armor', 'head'] and pref.gender is 'f')
return "f_armor_#{item}_#{pref.armorSet}" if (item is 0 and type is 'armor')
return "f_head_#{item}_#{pref.armorSet}" if (item > 1 and type is 'head')
return "#{pref.gender}_#{type}_#{item}"