Files
habitica/website/common/script/ops/buy/buyHealthPotion.js
T
negue 54153ec299 Refactor Purchase API - Part 1 (#9714)
* move to shops/purchase

* move files to /buy/ instead of /purchase/

* refactor buy.js - add more itemtypes

* revert moving special purchases to buy

* only use buyOp from api-routes

* fix buying potion client-side

* undo import buy instead of purchase

* enable potion bulk purchase - use buyGear as fallback (as before)

* move quantity purchase inside buyHealthPotion

* move quantity purchase inside buyQuest

* move quantity purchase inside buySpecialSpell + add analytics

* remove unused quantity variable - set req.type on specialKeys

* fix `buyKnownKeys` on buy api

* test buy-special-spell if not enough gold

* more buy ops coverage

* fix lint

* buyMysterySet: test for window.confirm, buyQuest: check for Masterclassers unlock

* fix test & lint

* re-create package-lock.json to travis build ?

* use global.window instead of method argument

* add back canOwn checks

* remove buyMysterySet confirm request
2018-02-10 11:14:40 +01:00

56 lines
1.3 KiB
JavaScript

import content from '../../content/index';
import i18n from '../../i18n';
import {
NotAuthorized,
} from '../../libs/errors';
module.exports = function buyHealthPotion (user, req = {}, analytics) {
let item = content.potion;
let quantity = req.quantity || 1;
if (user.stats.gp < item.value * quantity) {
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
}
if (item.canOwn && !item.canOwn(user)) {
throw new NotAuthorized(i18n.t('cannotBuyItem', req.language));
}
if (user.stats.hp >= 50) {
throw new NotAuthorized(i18n.t('messageHealthAlreadyMax', req.language));
}
if (user.stats.hp <= 0) {
throw new NotAuthorized(i18n.t('messageHealthAlreadyMin', req.language));
}
user.stats.hp += 15 * quantity;
if (user.stats.hp > 50) {
user.stats.hp = 50;
}
user.stats.gp -= item.value * quantity;
let message = i18n.t('messageBought', {
itemText: item.text(req.language),
}, req.language);
if (analytics) {
analytics.track('acquire item', {
uuid: user._id,
itemKey: 'Potion',
acquireMethod: 'Gold',
goldCost: item.value,
category: 'behavior',
headers: req.headers,
quantityPurchased: quantity,
});
}
return [
user.stats,
message,
];
};