54153ec299
* move to shops/purchase * move files to /buy/ instead of /purchase/ * refactor buy.js - add more itemtypes * revert moving special purchases to buy * only use buyOp from api-routes * fix buying potion client-side * undo import buy instead of purchase * enable potion bulk purchase - use buyGear as fallback (as before) * move quantity purchase inside buyHealthPotion * move quantity purchase inside buyQuest * move quantity purchase inside buySpecialSpell + add analytics * remove unused quantity variable - set req.type on specialKeys * fix `buyKnownKeys` on buy api * test buy-special-spell if not enough gold * more buy ops coverage * fix lint * buyMysterySet: test for window.confirm, buyQuest: check for Masterclassers unlock * fix test & lint * re-create package-lock.json to travis build ? * use global.window instead of method argument * add back canOwn checks * remove buyMysterySet confirm request
55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import i18n from '../../i18n';
|
|
import content from '../../content/index';
|
|
import {
|
|
BadRequest,
|
|
NotAuthorized,
|
|
NotFound,
|
|
} from '../../libs/errors';
|
|
import get from 'lodash/get';
|
|
|
|
// buy a quest with gold
|
|
module.exports = function buyQuest (user, req = {}, analytics) {
|
|
let key = get(req, 'params.key');
|
|
let quantity = req.quantity || 1;
|
|
|
|
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)) {
|
|
throw new NotAuthorized(i18n.t('questNotGoldPurchasable', {key}, req.language));
|
|
}
|
|
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;
|
|
user.items.quests[item.key] += quantity;
|
|
user.stats.gp -= item.goldValue * 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 [
|
|
user.items.quests,
|
|
i18n.t('messageBought', {
|
|
itemText: item.text(req.language),
|
|
}, req.language),
|
|
];
|
|
};
|