mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +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
127 lines
3.2 KiB
JavaScript
127 lines
3.2 KiB
JavaScript
import content from '../content/index';
|
|
import i18n from '../i18n';
|
|
import get from 'lodash/get';
|
|
import pick from 'lodash/pick';
|
|
import splitWhitespace from '../libs/splitWhitespace';
|
|
import planGemLimits from '../libs/planGemLimits';
|
|
import {
|
|
NotFound,
|
|
NotAuthorized,
|
|
BadRequest,
|
|
} from '../libs/errors';
|
|
|
|
module.exports = function purchase (user, req = {}, analytics) {
|
|
let type = get(req.params, 'type');
|
|
let key = get(req.params, 'key');
|
|
let item;
|
|
let price;
|
|
|
|
if (!type) {
|
|
throw new BadRequest(i18n.t('typeRequired', req.language));
|
|
}
|
|
|
|
if (!key) {
|
|
throw new BadRequest(i18n.t('keyRequired', req.language));
|
|
}
|
|
|
|
if (type === 'gems' && key === 'gem') {
|
|
let convRate = planGemLimits.convRate;
|
|
let convCap = planGemLimits.convCap;
|
|
convCap += user.purchased.plan.consecutive.gemCapExtra;
|
|
|
|
if (!user.purchased || !user.purchased.plan || !user.purchased.plan.customerId) {
|
|
throw new NotAuthorized(i18n.t('mustSubscribeToPurchaseGems', req.language));
|
|
}
|
|
|
|
if (user.stats.gp < convRate) {
|
|
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
|
|
}
|
|
|
|
if (user.purchased.plan.gemsBought >= convCap) {
|
|
throw new NotAuthorized(i18n.t('reachedGoldToGemCap', {convCap}, req.language));
|
|
}
|
|
|
|
user.balance += 0.25;
|
|
user.purchased.plan.gemsBought++;
|
|
user.stats.gp -= convRate;
|
|
|
|
if (analytics) {
|
|
analytics.track('purchase gems', {
|
|
uuid: user._id,
|
|
itemKey: key,
|
|
acquireMethod: 'Gold',
|
|
goldCost: convRate,
|
|
category: 'behavior',
|
|
headers: req.headers,
|
|
});
|
|
}
|
|
|
|
return [
|
|
pick(user, splitWhitespace('stats balance')),
|
|
i18n.t('plusOneGem', req.language),
|
|
];
|
|
}
|
|
|
|
let acceptedTypes = ['eggs', 'hatchingPotions', 'food', 'quests', 'gear'];
|
|
if (acceptedTypes.indexOf(type) === -1) {
|
|
throw new NotFound(i18n.t('notAccteptedType', req.language));
|
|
}
|
|
|
|
if (type === 'gear') {
|
|
item = content.gear.flat[key];
|
|
|
|
if (!item) {
|
|
throw new NotFound(i18n.t('contentKeyNotFound', {type}, req.language));
|
|
}
|
|
|
|
if (user.items.gear.owned[key]) {
|
|
throw new NotAuthorized(i18n.t('alreadyHave', req.language));
|
|
}
|
|
|
|
price = (item.twoHanded || item.gearSet === 'animal' ? 2 : 1) / 4;
|
|
} else {
|
|
item = content[type][key];
|
|
|
|
if (!item) {
|
|
throw new NotFound(i18n.t('contentKeyNotFound', {type}, req.language));
|
|
}
|
|
|
|
price = item.value / 4;
|
|
}
|
|
|
|
if (!item.canBuy(user)) {
|
|
throw new NotAuthorized(i18n.t('messageNotAvailable', req.language));
|
|
}
|
|
|
|
if (!user.balance || user.balance < price) {
|
|
throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
|
|
}
|
|
|
|
user.balance -= price;
|
|
|
|
if (type === 'gear') {
|
|
user.items.gear.owned[key] = true;
|
|
} else {
|
|
if (!user.items[type][key] || user.items[type][key] < 0) {
|
|
user.items[type][key] = 0;
|
|
}
|
|
user.items[type][key]++;
|
|
}
|
|
|
|
if (analytics) {
|
|
analytics.track('acquire item', {
|
|
uuid: user._id,
|
|
itemKey: key,
|
|
itemType: 'Market',
|
|
acquireMethod: 'Gems',
|
|
gemCost: price * 4,
|
|
category: 'behavior',
|
|
headers: req.headers,
|
|
});
|
|
}
|
|
|
|
return [
|
|
pick(user, splitWhitespace('items balance')),
|
|
];
|
|
};
|