mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
* Re-organize common folder * fix: Correct paths in tests * fix: move new content to proper folder * chore: Move audio folder to assets * Move sprites to sprites assets directory * Move css sprites to assets directory * Split out readmes for common code and sprites * Move images to assets directory * Move destinatin of shared browserified file * remove unused file * move compiled js to client-old * Fix karma tests * fix: Correct paths for sprites
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
import i18n from '../i18n';
|
|
import content from '../content/index';
|
|
import {
|
|
BadRequest,
|
|
NotAuthorized,
|
|
NotFound,
|
|
} from '../libs/errors';
|
|
import _ from 'lodash';
|
|
|
|
// buy a quest with gold
|
|
module.exports = function buyQuest (user, req = {}, analytics) {
|
|
let key = _.get(req, 'params.key');
|
|
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 (!(item.category === 'gold' && item.goldValue)) {
|
|
throw new NotAuthorized(i18n.t('questNotGoldPurchasable', {key}, req.language));
|
|
}
|
|
if (user.stats.gp < item.goldValue) {
|
|
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
|
|
}
|
|
|
|
user.items.quests[item.key] = user.items.quests[item.key] || 0;
|
|
user.items.quests[item.key]++;
|
|
user.stats.gp -= item.goldValue;
|
|
|
|
if (analytics) {
|
|
analytics.track('acquire item', {
|
|
uuid: user._id,
|
|
itemKey: item.key,
|
|
itemType: 'Market',
|
|
goldCost: item.goldValue,
|
|
acquireMethod: 'Gold',
|
|
category: 'behavior',
|
|
headers: req.headers,
|
|
});
|
|
}
|
|
|
|
if (req.v2 === true) {
|
|
return user.items.quests;
|
|
} else {
|
|
return [
|
|
user.items.quests,
|
|
i18n.t('messageBought', {
|
|
itemText: item.text(req.language),
|
|
}, req.language),
|
|
];
|
|
}
|
|
};
|