Skip to content

Commit

Permalink
convert buyHealthPotion
Browse files Browse the repository at this point in the history
  • Loading branch information
negue committed Mar 19, 2018
1 parent 480a839 commit e4d916d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions website/common/script/ops/buy/buy.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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':
Expand Down
72 changes: 36 additions & 36 deletions website/common/script/ops/buy/buyHealthPotion.js
@@ -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;

if (user.stats.hp <= 0) {
throw new NotAuthorized(i18n.t('messageHealthAlreadyMin', req.language));
}
super.canUserPurchase(user, item);

user.stats.hp += 15 * quantity;
if (user.stats.hp > 50) {
user.stats.hp = 50;
}
if (userHp >= 50) {
throw new NotAuthorized(this.i18n('messageHealthAlreadyMax'));
}

user.stats.gp -= item.value * quantity;
if (userHp <= 0) {
throw new NotAuthorized(this.i18n('messageHealthAlreadyMin'));
}
}

let message = i18n.t('messageBought', {
itemText: item.text(req.language),
}, req.language);
executeChanges (user, item) {
user.stats.hp += 15 * this.quantity;
if (user.stats.hp > 50) {
user.stats.hp = 50;
}

this.substractCurrency(user, item, this.quantity);

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;
}
}

0 comments on commit e4d916d

Please sign in to comment.