mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
* Add test to prevent death users recovering health * Add check for buying potions with zero health * Validate hp <= 0 to take boss damage into account
54 lines
1.1 KiB
JavaScript
54 lines
1.1 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;
|
|
|
|
if (user.stats.gp < item.value) {
|
|
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;
|
|
if (user.stats.hp > 50) {
|
|
user.stats.hp = 50;
|
|
}
|
|
|
|
user.stats.gp -= item.value;
|
|
|
|
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,
|
|
});
|
|
}
|
|
|
|
return [
|
|
user.stats,
|
|
message,
|
|
];
|
|
};
|