mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
port disable classes and change class ops
This commit is contained in:
@@ -2,58 +2,70 @@ import i18n from '../i18n';
|
||||
import _ from 'lodash';
|
||||
import splitWhitespace from '../libs/splitWhitespace';
|
||||
import { capByLevel } from '../statHelpers';
|
||||
import {
|
||||
NotAuthorized,
|
||||
} from '../libs/errors';
|
||||
|
||||
module.exports = function changeClass (user, req = {}, analytics) {
|
||||
let klass = _.get(req, 'query.class');
|
||||
|
||||
module.exports = function(user, req, cb, analytics) {
|
||||
var analyticsData, klass, ref;
|
||||
klass = (ref = req.query) != null ? ref["class"] : void 0;
|
||||
if (klass === 'warrior' || klass === 'rogue' || klass === 'wizard' || klass === 'healer') {
|
||||
analyticsData = {
|
||||
uuid: user._id,
|
||||
"class": klass,
|
||||
acquireMethod: 'Gems',
|
||||
gemCost: 3,
|
||||
category: 'behavior'
|
||||
};
|
||||
if (analytics != null) {
|
||||
analytics.track('change class', analyticsData);
|
||||
}
|
||||
user.stats["class"] = klass;
|
||||
user.stats.class = klass;
|
||||
user.flags.classSelected = true;
|
||||
_.each(["weapon", "armor", "shield", "head"], function(type) {
|
||||
var foundKey;
|
||||
foundKey = false;
|
||||
_.findLast(user.items.gear.owned, function(v, k) {
|
||||
if (~k.indexOf(type + "_" + klass) && v === true) {
|
||||
return foundKey = k;
|
||||
|
||||
_.each(['weapon', 'armor', 'shield', 'head'], (type) => {
|
||||
let foundKey = false;
|
||||
_.findLast(user.items.gear.owned, (val, key) => {
|
||||
if (key.indexOf(`${type}_${klass}`) !== -1 && val === true) {
|
||||
foundKey = key;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
user.items.gear.equipped[type] = foundKey ? foundKey : type === "weapon" ? "weapon_" + klass + "_0" : type === "shield" && klass === "rogue" ? "shield_rogue_0" : type + "_base_0";
|
||||
if (type === "weapon" || (type === "shield" && klass === "rogue")) {
|
||||
user.items.gear.owned[type + "_" + klass + "_0"] = true;
|
||||
|
||||
if (!foundKey) {
|
||||
if (type === 'weapon') {
|
||||
foundKey = `weapon_${klass}_0`;
|
||||
} else if (type === 'shield' && klass === 'rogue') {
|
||||
foundKey = 'shield_rogue_0';
|
||||
} else {
|
||||
foundKey = `${type}_base_0`;
|
||||
}
|
||||
}
|
||||
|
||||
user.items.gear.equipped[type] = foundKey;
|
||||
|
||||
if (type === 'weapon' || (type === 'shield' && klass === 'rogue')) { // eslint-disable-line no-extra-parens
|
||||
user.items.gear.owned[`${type}_${klass}_0`] = true;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('change class', {
|
||||
uuid: user._id,
|
||||
class: klass,
|
||||
acquireMethod: 'Gems',
|
||||
gemCost: 3,
|
||||
category: 'behavior',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (user.preferences.disableClasses) {
|
||||
user.preferences.disableClasses = false;
|
||||
user.preferences.autoAllocate = false;
|
||||
} else {
|
||||
if (!(user.balance >= .75)) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 401,
|
||||
message: i18n.t('notEnoughGems', req.language)
|
||||
}) : void 0;
|
||||
}
|
||||
user.balance -= .75;
|
||||
if (user.balance < 0.75) throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
|
||||
user.balance -= 0.75;
|
||||
}
|
||||
_.merge(user.stats, {
|
||||
str: 0,
|
||||
con: 0,
|
||||
per: 0,
|
||||
int: 0,
|
||||
points: capByLevel(user.stats.lvl)
|
||||
});
|
||||
|
||||
user.stats.str = 0;
|
||||
user.stats.con = 0;
|
||||
user.stats.per = 0;
|
||||
user.stats.int = 0;
|
||||
user.stats.points = capByLevel(user.stats.lvl);
|
||||
user.flags.classSelected = false;
|
||||
}
|
||||
return typeof cb === "function" ? cb(null, _.pick(user, splitWhitespace('stats flags items preferences'))) : void 0;
|
||||
|
||||
return {
|
||||
data: _.pick(user, splitWhitespace('stats flags items preferences')),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user