Files
habitica/website/common/script/ops/releaseMounts.js
Matteo Pagliazzi 0b8ce63c76 WIP: Improve User model performances (#10832)
* wip: define items as mixed objects

* add default owned gear

* mark modified

* more mark modified

* more mark modified

* more mark modified

* more mark modified

* fix common tests

* fix common tests

* update mongoose

* add itemsUtils

* use new util function in hall controller

* add tests for items utils

* update website/server to mark all items as modified

* start updating common code

* update login incentives

* update unlock

* remove changes to package-lock.json

* remove changes to package.json
2019-04-01 19:24:18 +02:00

57 lines
1.4 KiB
JavaScript

import content from '../content/index';
import {mountMasterProgress} from '../count';
import i18n from '../i18n';
import {
NotAuthorized,
} from '../libs/errors';
module.exports = function releaseMounts (user, req = {}, analytics) {
if (user.balance < 1) {
throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
}
if (mountMasterProgress(user.items.mounts) !== 90) {
throw new NotAuthorized(i18n.t('notEnoughMounts', req.language));
}
user.balance -= 1;
let giveMountMasterAchievement = true;
let mountInfo = content.mountInfo[user.items.currentMount];
if (mountInfo && mountInfo.type === 'drop') {
user.items.currentMount = '';
}
for (let mount in content.pets) {
if (user.items.mounts[mount] === null || user.items.mounts[mount] === undefined) {
giveMountMasterAchievement = false;
}
user.items.mounts[mount] = null;
}
if (user.markModified) user.markModified('items.mounts');
if (giveMountMasterAchievement) {
if (!user.achievements.mountMasterCount) {
user.achievements.mountMasterCount = 0;
}
user.achievements.mountMasterCount++;
}
if (analytics) {
analytics.track('release mounts', {
uuid: user._id,
acquireMethod: 'Gems',
gemCost: 4,
category: 'behavior',
headers: req.headers,
});
}
return [
user.items.mounts,
i18n.t('mountsReleased'),
];
};