mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-15 05:37:22 +01:00
* common: import lodash modules separately * remove test/content from .eslintignore, fix with eslint --fix content/index * lint test/content * lint content/index except for lodash methods * upgrade server/models * upgrade server/middlewares and server/libs * port server/controllers/top-level * port server/controllers/api-v3 * port views and tests * client old port lodash and _(, missing _. * upgrade client-old * port common/script (root level files only) * port common/script/fns * port common/libs * port common/script/ops * port common/script/content and common/script/libs/shops.js * misc fixes * misc fixes * misc fixes * more tests fixes * fix payments test stubbing, down to 2 failing tests * remove more instances of lodash wrapping * fix bug where toObject does not clone object * fix tests * upgrade migration or add lodash 4 note * update shrinkwrap * fix linting * upgrade eslint-config-habitrpg * update shrinkwrap * recompile shrinkwrap
112 lines
2.7 KiB
JavaScript
112 lines
2.7 KiB
JavaScript
import content from '../content/index';
|
|
import i18n from '../i18n';
|
|
import merge from 'lodash/merge';
|
|
import reduce from 'lodash/reduce';
|
|
import each from 'lodash/each';
|
|
import {
|
|
NotAuthorized,
|
|
} from '../libs/errors';
|
|
import randomVal from '../libs/randomVal';
|
|
import predictableRandom from '../fns/predictableRandom';
|
|
|
|
module.exports = function revive (user, req = {}, analytics) {
|
|
if (user.stats.hp > 0) {
|
|
throw new NotAuthorized(i18n.t('cannotRevive', req.language));
|
|
}
|
|
|
|
merge(user.stats, {
|
|
hp: 50,
|
|
exp: 0,
|
|
gp: 0,
|
|
});
|
|
|
|
if (user.stats.lvl > 1) {
|
|
user.stats.lvl--;
|
|
}
|
|
|
|
let lostStat = randomVal(reduce(['str', 'con', 'per', 'int'], function findRandomStat (m, k) {
|
|
if (user.stats[k]) {
|
|
m[k] = k;
|
|
}
|
|
return m;
|
|
}, {}), {
|
|
predictableRandom: predictableRandom(user),
|
|
});
|
|
|
|
if (lostStat) {
|
|
user.stats[lostStat]--;
|
|
}
|
|
|
|
let base = user.items.gear.owned;
|
|
let gearOwned;
|
|
|
|
if (typeof base.toObject === 'function') {
|
|
gearOwned = base.toObject();
|
|
} else {
|
|
gearOwned = user.items.gear.owned;
|
|
}
|
|
|
|
let losableItems = {};
|
|
let userClass = user.stats.class;
|
|
|
|
each(gearOwned, function findLosableItems (value, key) {
|
|
let itm;
|
|
if (value) {
|
|
itm = content.gear.flat[key];
|
|
|
|
if (itm) {
|
|
let itemHasValueOrWarrior0 = itm.value > 0 || key === 'weapon_warrior_0';
|
|
|
|
let itemClassEqualsUserClass = itm.klass === userClass;
|
|
|
|
let itemClassSpecial = itm.klass === 'special';
|
|
let itemNotSpecialOrUserClassIsSpecial = !itm.specialClass || itm.specialClass === userClass;
|
|
let itemIsSpecial = itemNotSpecialOrUserClassIsSpecial && itemClassSpecial;
|
|
|
|
let itemIsArmoire = itm.klass === 'armoire';
|
|
|
|
if (itemHasValueOrWarrior0 && (itemClassEqualsUserClass || itemIsSpecial || itemIsArmoire)) {
|
|
losableItems[key] = key;
|
|
return losableItems[key];
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
let lostItem = randomVal(losableItems, {
|
|
predictableRandom: predictableRandom(user),
|
|
});
|
|
|
|
let message = '';
|
|
let item = content.gear.flat[lostItem];
|
|
|
|
if (item) {
|
|
user.items.gear.owned[lostItem] = false;
|
|
|
|
if (user.items.gear.equipped[item.type] === lostItem) {
|
|
user.items.gear.equipped[item.type] = `${item.type}_base_0`;
|
|
}
|
|
|
|
if (user.items.gear.costume[item.type] === lostItem) {
|
|
user.items.gear.costume[item.type] = `${item.type}_base_0`;
|
|
}
|
|
|
|
message = i18n.t('messageLostItem', { itemText: item.text(req.language)}, req.language);
|
|
}
|
|
|
|
if (analytics) {
|
|
analytics.track('Death', {
|
|
uuid: user._id,
|
|
lostItem,
|
|
gaLabel: lostItem,
|
|
category: 'behavior',
|
|
headers: req.headers,
|
|
});
|
|
}
|
|
|
|
return [
|
|
user.items,
|
|
message,
|
|
];
|
|
};
|