convert buyQuest (gold) to the purchase refactoring / check quantity to be a number (#10244)

This commit is contained in:
negue
2018-04-13 15:14:51 +02:00
committed by Matteo Pagliazzi
parent 8f1d241e83
commit 1109ae308d
4 changed files with 80 additions and 46 deletions

View File

@@ -1,7 +1,7 @@
import {
generateUser,
} from '../../../helpers/common.helper';
import buyQuest from '../../../../website/common/script/ops/buy/buyQuest';
import {BuyQuestWithGoldOperation} from '../../../../website/common/script/ops/buy/buyQuest';
import {
BadRequest,
NotAuthorized,
@@ -13,6 +13,12 @@ describe('shared.ops.buyQuest', () => {
let user;
let analytics = {track () {}};
function buyQuest (_user, _req, _analytics) {
const buyOp = new BuyQuestWithGoldOperation(_user, _req, _analytics);
return buyOp.purchase();
}
beforeEach(() => {
user = generateUser();
sinon.stub(analytics, 'track');

View File

@@ -1,6 +1,8 @@
import i18n from '../../i18n';
import {
NotAuthorized, NotImplementedError,
NotAuthorized,
NotImplementedError,
BadRequest,
} from '../../libs/errors';
import _merge from 'lodash/merge';
import _get from 'lodash/get';
@@ -16,7 +18,14 @@ export class AbstractBuyOperation {
this.req = req || {};
this.analytics = analytics;
this.quantity = _get(req, 'quantity', 1);
this.quantity = 1;
if (this.multiplePurchaseAllowed()) {
let quantity = _get(req, 'quantity');
this.quantity = quantity ? Number(quantity) : 1;
if (isNaN(this.quantity)) throw new BadRequest(this.i18n('invalidQuantity'));
}
}
/**

View File

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

View File

@@ -1,56 +1,72 @@
import i18n from '../../i18n';
import content from '../../content/index';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../libs/errors';
import content from '../../content/index';
import get from 'lodash/get';
// buy a quest with gold
module.exports = function buyQuest (user, req = {}, analytics) {
let key = get(req, 'params.key');
import {AbstractGoldItemOperation} from './abstractBuyOperation';
let quantity = req.quantity ? Number(req.quantity) : 1;
if (isNaN(quantity)) throw new BadRequest(i18n.t('invalidQuantity', req.language));
export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
constructor (user, req, analytics) {
super(user, req, analytics);
}
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));
multiplePurchaseAllowed () {
return true;
}
userAbleToStartMasterClasser (user) {
return user.achievements.quests.dilatoryDistress3 &&
user.achievements.quests.mayhemMistiflying3 &&
user.achievements.quests.stoikalmCalamity3 &&
user.achievements.quests.taskwoodsTerror3;
}
getItemValue (item) {
return item.goldValue;
}
extractAndValidateParams (user, req) {
let key = this.key = get(req, 'params.key');
if (!key) throw new BadRequest(this.i18n('missingKeyParam'));
if (key === 'lostMasterclasser1' && !this.userAbleToStartMasterClasser(user)) {
throw new NotAuthorized(this.i18n('questUnlockLostMasterclasser'));
}
let item = content.quests[key];
if (!item) throw new NotFound(i18n.t('questNotFound', {key}, req.language));
if (key === 'lostMasterclasser1' && !(user.achievements.quests.dilatoryDistress3 && user.achievements.quests.mayhemMistiflying3 && user.achievements.quests.stoikalmCalamity3 && user.achievements.quests.taskwoodsTerror3)) {
throw new NotAuthorized(i18n.t('questUnlockLostMasterclasser', req.language));
}
if (!item) throw new NotFound(this.i18n('questNotFound', {key}));
if (!(item.category === 'gold' && item.goldValue)) {
throw new NotAuthorized(i18n.t('questNotGoldPurchasable', {key}, req.language));
}
if (user.stats.gp < item.goldValue * quantity) {
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
throw new NotAuthorized(this.i18n('questNotGoldPurchasable', {key}));
}
this.canUserPurchase(user, item);
}
executeChanges (user, item, req) {
user.items.quests[item.key] = user.items.quests[item.key] || 0;
user.items.quests[item.key] += quantity;
user.stats.gp -= item.goldValue * quantity;
user.items.quests[item.key] += this.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,
});
}
this.subtractCurrency(user, item, this.quantity);
return [
user.items.quests,
i18n.t('messageBought', {
this.i18n('messageBought', {
itemText: item.text(req.language),
}, req.language),
}),
];
};
}
analyticsData () {
return {
itemKey: this.key,
itemType: 'Market',
acquireMethod: 'Gold',
goldCost: this.getItemValue(this.item.goldValue),
};
}
}