From 567d5f74ba0f3af915a658d15d00bc1932e4268b Mon Sep 17 00:00:00 2001 From: negue Date: Sat, 31 Mar 2018 13:09:16 +0200 Subject: [PATCH] Purchase API Refactoring: Health Potion (#10152) * convert buyHealthPotion * fix health potion tests * fix lint --- package-lock.json | 10 +-- test/common/ops/buy/buyHealthPotion.js | 8 +- website/common/script/ops/buy/buy.js | 9 ++- .../common/script/ops/buy/buyHealthPotion.js | 76 +++++++++---------- 4 files changed, 56 insertions(+), 47 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4a6c6cc7d1..69dc2a79a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -151,7 +151,7 @@ }, "@sinonjs/formatio": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz", "integrity": "sha512-ls6CAMA6/5gG+O/IdsBcblvnd8qcO/l1TYoNeAzp3wcISOxlPXQEus0mLcdwazEkWjaBdaJ3TaxmNgCLWwvWzg==", "dev": true, "requires": { @@ -3548,7 +3548,7 @@ }, "compression": { "version": "1.7.2", - "resolved": "http://registry.npmjs.org/compression/-/compression-1.7.2.tgz", + "resolved": "https://registry.npmjs.org/compression/-/compression-1.7.2.tgz", "integrity": "sha1-qv+81qr4VLROuygDU9WtFlH1mmk=", "requires": { "accepts": "1.3.5", @@ -5601,7 +5601,7 @@ "dependencies": { "onetime": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=" } } @@ -6364,7 +6364,7 @@ }, "event-stream": { "version": "3.3.4", - "resolved": "http://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz", + "resolved": "https://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz", "integrity": "sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=", "requires": { "duplexer": "0.1.1", @@ -15786,7 +15786,7 @@ }, "pinkie-promise": { "version": "2.0.1", - "resolved": "http://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz", + "resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz", "integrity": "sha1-ITXW36ejWMBprJsXh3YogihFD/o=", "requires": { "pinkie": "2.0.4" diff --git a/test/common/ops/buy/buyHealthPotion.js b/test/common/ops/buy/buyHealthPotion.js index 195ccb2f8b..9f04c4d6bb 100644 --- a/test/common/ops/buy/buyHealthPotion.js +++ b/test/common/ops/buy/buyHealthPotion.js @@ -2,7 +2,7 @@ import { generateUser, } from '../../../helpers/common.helper'; -import buyHealthPotion from '../../../../website/common/script/ops/buy/buyHealthPotion'; +import { BuyHealthPotionOperation } from '../../../../website/common/script/ops/buy/buyHealthPotion'; import { NotAuthorized, } from '../../../../website/common/script/libs/errors'; @@ -12,6 +12,12 @@ describe('shared.ops.buyHealthPotion', () => { let user; let analytics = {track () {}}; + function buyHealthPotion (_user, _req, _analytics) { + const buyOp = new BuyHealthPotionOperation(_user, _req, _analytics); + + return buyOp.purchase(); + } + beforeEach(() => { user = generateUser({ items: { diff --git a/website/common/script/ops/buy/buy.js b/website/common/script/ops/buy/buy.js index 51e054f5be..d67c20e0dd 100644 --- a/website/common/script/ops/buy/buy.js +++ b/website/common/script/ops/buy/buy.js @@ -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': diff --git a/website/common/script/ops/buy/buyHealthPotion.js b/website/common/script/ops/buy/buyHealthPotion.js index 81ce8bc184..034a5a17c4 100644 --- a/website/common/script/ops/buy/buyHealthPotion.js +++ b/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; + + 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; + } +}