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

View File

@@ -1,6 +1,8 @@
import i18n from '../../i18n'; import i18n from '../../i18n';
import { import {
NotAuthorized, NotImplementedError, NotAuthorized,
NotImplementedError,
BadRequest,
} from '../../libs/errors'; } from '../../libs/errors';
import _merge from 'lodash/merge'; import _merge from 'lodash/merge';
import _get from 'lodash/get'; import _get from 'lodash/get';
@@ -16,7 +18,14 @@ export class AbstractBuyOperation {
this.req = req || {}; this.req = req || {};
this.analytics = analytics; 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 {BuyHealthPotionOperation} from './buyHealthPotion';
import {BuyMarketGearOperation} from './buyMarketGear'; import {BuyMarketGearOperation} from './buyMarketGear';
import buyMysterySet from './buyMysterySet'; import buyMysterySet from './buyMysterySet';
import buyQuest from './buyQuest'; import {BuyQuestWithGoldOperation} from './buyQuest';
import buySpecialSpell from './buySpecialSpell'; import buySpecialSpell from './buySpecialSpell';
import purchaseOp from './purchase'; import purchaseOp from './purchase';
import hourglassPurchase from './hourglassPurchase'; import hourglassPurchase from './hourglassPurchase';
@@ -58,9 +58,12 @@ module.exports = function buy (user, req = {}, analytics) {
case 'mounts': case 'mounts':
buyRes = hourglassPurchase(user, req, analytics); buyRes = hourglassPurchase(user, req, analytics);
break; break;
case 'quest': case 'quest': {
buyRes = buyQuest(user, req, analytics); const buyOp = new BuyQuestWithGoldOperation(user, req, analytics);
buyRes = buyOp.purchase();
break; break;
}
case 'special': case 'special':
buyRes = buySpecialSpell(user, req, analytics); buyRes = buySpecialSpell(user, req, analytics);
break; break;

View File

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