Purchase API Refactoring: Health Potion (#10152)
* convert buyHealthPotion * fix health potion tests * fix lint
This commit is contained in:
@@ -3,7 +3,7 @@ import get from 'lodash/get';
|
||||
import {
|
||||
BadRequest,
|
||||
} from '../../libs/errors';
|
||||
import buyHealthPotion from './buyHealthPotion';
|
||||
import {BuyHealthPotionOperation} from './buyHealthPotion';
|
||||
import buyArmoire from './buyArmoire';
|
||||
import {BuyMarketGearOperation} from './buyMarketGear';
|
||||
import buyMysterySet from './buyMysterySet';
|
||||
@@ -36,9 +36,12 @@ module.exports = function buy (user, req = {}, analytics) {
|
||||
case 'mystery':
|
||||
buyRes = buyMysterySet(user, req, analytics);
|
||||
break;
|
||||
case 'potion':
|
||||
buyRes = buyHealthPotion(user, req, analytics);
|
||||
case 'potion': {
|
||||
const buyOp = new BuyHealthPotionOperation(user, req, analytics);
|
||||
|
||||
buyRes = buyOp.purchase();
|
||||
break;
|
||||
}
|
||||
case 'eggs':
|
||||
case 'hatchingPotions':
|
||||
case 'food':
|
||||
|
||||
@@ -1,55 +1,55 @@
|
||||
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;
|
||||
import { AbstractGoldItemOperation} from './abstractBuyOperation';
|
||||
|
||||
if (user.stats.gp < item.value * quantity) {
|
||||
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
|
||||
export class BuyHealthPotionOperation extends AbstractGoldItemOperation {
|
||||
constructor (user, req, analytics) {
|
||||
super(user, req, analytics);
|
||||
}
|
||||
|
||||
if (item.canOwn && !item.canOwn(user)) {
|
||||
throw new NotAuthorized(i18n.t('cannotBuyItem', req.language));
|
||||
multiplePurchaseAllowed () {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (user.stats.hp >= 50) {
|
||||
throw new NotAuthorized(i18n.t('messageHealthAlreadyMax', req.language));
|
||||
extractAndValidateParams (user) {
|
||||
let item = content.potion;
|
||||
let userHp = user.stats.hp;
|
||||
|
||||
super.canUserPurchase(user, item);
|
||||
|
||||
if (userHp >= 50) {
|
||||
throw new NotAuthorized(this.i18n('messageHealthAlreadyMax'));
|
||||
}
|
||||
|
||||
if (userHp <= 0) {
|
||||
throw new NotAuthorized(this.i18n('messageHealthAlreadyMin'));
|
||||
}
|
||||
}
|
||||
|
||||
if (user.stats.hp <= 0) {
|
||||
throw new NotAuthorized(i18n.t('messageHealthAlreadyMin', req.language));
|
||||
}
|
||||
executeChanges (user, item) {
|
||||
user.stats.hp += 15 * this.quantity;
|
||||
if (user.stats.hp > 50) {
|
||||
user.stats.hp = 50;
|
||||
}
|
||||
|
||||
user.stats.hp += 15 * quantity;
|
||||
if (user.stats.hp > 50) {
|
||||
user.stats.hp = 50;
|
||||
}
|
||||
this.substractCurrency(user, item, this.quantity);
|
||||
|
||||
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,
|
||||
let message = this.i18n('messageBought', {
|
||||
itemText: this.item.text(this.req.language),
|
||||
});
|
||||
|
||||
return [
|
||||
this.user.stats,
|
||||
message,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
user.stats,
|
||||
message,
|
||||
];
|
||||
};
|
||||
analyticsData () {
|
||||
let data = super.analyticsData();
|
||||
data.itemKey = 'Potion';
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user