Purchase API Refactoring: Spells [Gold] (#10305)

* convert buySpell operation

* remove purchaseWithSpell - change purchaseType 'special' to 'spells' - fix lint

* fix tests

* rollback 'spells' to 'special'
This commit is contained in:
negue
2018-05-18 17:00:39 +02:00
committed by Matteo Pagliazzi
parent b2edd1d932
commit 3d39718048
7 changed files with 64 additions and 75 deletions

View File

@@ -7,7 +7,7 @@ import {BuyHealthPotionOperation} from './buyHealthPotion';
import {BuyMarketGearOperation} from './buyMarketGear';
import buyMysterySet from './buyMysterySet';
import {BuyQuestWithGoldOperation} from './buyQuest';
import buySpecialSpell from './buySpecialSpell';
import {BuySpellOperation} from './buySpell';
import purchaseOp from './purchase';
import hourglassPurchase from './hourglassPurchase';
import errorMessage from '../../libs/errorMessage';
@@ -70,9 +70,12 @@ module.exports = function buy (user, req = {}, analytics) {
buyRes = buyOp.purchase();
break;
}
case 'special':
buyRes = buySpecialSpell(user, req, analytics);
case 'special': {
const buyOp = new BuySpellOperation(user, req, analytics);
buyRes = buyOp.purchase();
break;
}
default: {
const buyOp = new BuyMarketGearOperation(user, req, analytics);

View File

@@ -25,6 +25,10 @@ export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
user.achievements.quests.taskwoodsTerror3;
}
getItemKey () {
return this.key;
}
getItemValue (item) {
return item.goldValue;
}
@@ -61,13 +65,4 @@ export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
}),
];
}
analyticsData () {
return {
itemKey: this.key,
itemType: 'Market',
acquireMethod: 'Gold',
goldCost: this.getItemValue(this.item.goldValue),
};
}
}

View File

@@ -1,48 +0,0 @@
import i18n from '../../i18n';
import content from '../../content/index';
import get from 'lodash/get';
import pick from 'lodash/pick';
import splitWhitespace from '../../libs/splitWhitespace';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../libs/errors';
import errorMessage from '../../libs/errorMessage';
module.exports = function buySpecialSpell (user, req = {}, analytics) {
let key = get(req, 'params.key');
let quantity = req.quantity || 1;
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
let item = content.special[key];
if (!item) throw new NotFound(errorMessage('spellNotFound', {spellId: key}));
if (user.stats.gp < item.value * quantity) {
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
}
user.stats.gp -= item.value * quantity;
user.items.special[key] += quantity;
if (analytics) {
analytics.track('acquire item', {
uuid: user._id,
itemKey: item.key,
itemType: 'Market',
goldCost: item.goldValue,
quantityPurchased: quantity,
acquireMethod: 'Gold',
category: 'behavior',
headers: req.headers,
});
}
return [
pick(user, splitWhitespace('items stats')),
i18n.t('messageBought', {
itemText: item.text(req.language),
}, req.language),
];
};

View File

@@ -0,0 +1,47 @@
import content from '../../content/index';
import get from 'lodash/get';
import pick from 'lodash/pick';
import splitWhitespace from '../../libs/splitWhitespace';
import {
BadRequest,
NotFound,
} from '../../libs/errors';
import {AbstractGoldItemOperation} from './abstractBuyOperation';
import errorMessage from '../../libs/errorMessage';
export class BuySpellOperation extends AbstractGoldItemOperation {
constructor (user, req, analytics) {
super(user, req, analytics);
}
getItemKey () {
return this.key;
}
multiplePurchaseAllowed () {
return true;
}
extractAndValidateParams (user, req) {
let key = this.key = get(req, 'params.key');
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
let item = content.special[key];
if (!item) throw new NotFound(errorMessage('spellNotFound', {spellId: key}));
this.canUserPurchase(user, item);
}
executeChanges (user, item, req) {
user.items.special[item.key] += this.quantity;
this.subtractCurrency(user, item, this.quantity);
return [
pick(user, splitWhitespace('items stats')),
this.i18n('messageBought', {
itemText: item.text(req.language),
}),
];
}
}

View File

@@ -1,12 +0,0 @@
import buy from './buy';
import get from 'lodash/get';
module.exports = function purchaseWithSpell (user, req = {}, analytics) {
const type = get(req.params, 'type');
if (type === 'spells') {
req.type = 'special';
}
return buy(user, req, analytics);
};