mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
convert buyQuest (gold) to the purchase refactoring / check quantity to be a number (#10244)
This commit is contained in:
@@ -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');
|
||||||
|
|||||||
@@ -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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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));
|
|
||||||
|
|
||||||
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.category === 'gold' && item.goldValue)) {
|
multiplePurchaseAllowed () {
|
||||||
throw new NotAuthorized(i18n.t('questNotGoldPurchasable', {key}, req.language));
|
return true;
|
||||||
}
|
|
||||||
if (user.stats.gp < item.goldValue * quantity) {
|
|
||||||
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user.items.quests[item.key] = user.items.quests[item.key] || 0;
|
userAbleToStartMasterClasser (user) {
|
||||||
user.items.quests[item.key] += quantity;
|
return user.achievements.quests.dilatoryDistress3 &&
|
||||||
user.stats.gp -= item.goldValue * quantity;
|
user.achievements.quests.mayhemMistiflying3 &&
|
||||||
|
user.achievements.quests.stoikalmCalamity3 &&
|
||||||
|
user.achievements.quests.taskwoodsTerror3;
|
||||||
|
}
|
||||||
|
|
||||||
if (analytics) {
|
getItemValue (item) {
|
||||||
analytics.track('acquire item', {
|
return item.goldValue;
|
||||||
uuid: user._id,
|
}
|
||||||
itemKey: item.key,
|
|
||||||
|
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(this.i18n('questNotFound', {key}));
|
||||||
|
|
||||||
|
if (!(item.category === 'gold' && item.goldValue)) {
|
||||||
|
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] += this.quantity;
|
||||||
|
|
||||||
|
this.subtractCurrency(user, item, this.quantity);
|
||||||
|
|
||||||
|
return [
|
||||||
|
user.items.quests,
|
||||||
|
this.i18n('messageBought', {
|
||||||
|
itemText: item.text(req.language),
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
analyticsData () {
|
||||||
|
return {
|
||||||
|
itemKey: this.key,
|
||||||
itemType: 'Market',
|
itemType: 'Market',
|
||||||
goldCost: item.goldValue,
|
|
||||||
quantityPurchased: quantity,
|
|
||||||
acquireMethod: 'Gold',
|
acquireMethod: 'Gold',
|
||||||
category: 'behavior',
|
goldCost: this.getItemValue(this.item.goldValue),
|
||||||
headers: req.headers,
|
};
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return [
|
|
||||||
user.items.quests,
|
|
||||||
i18n.t('messageBought', {
|
|
||||||
itemText: item.text(req.language),
|
|
||||||
}, req.language),
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user