Files
habitica/website/common/script/ops/hatch.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

47 lines
1.3 KiB
JavaScript

import content from '../content/index';
import i18n from '../i18n';
import get from 'lodash/get';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../libs/errors';
import errorMessage from '../libs/errorMessage';
module.exports = function hatch (user, req = {}) {
let egg = get(req, 'params.egg');
let hatchingPotion = get(req, 'params.hatchingPotion');
if (!(egg && hatchingPotion)) {
throw new BadRequest(errorMessage('missingEggHatchingPotion'));
}
if (!(user.items.eggs[egg] > 0 && user.items.hatchingPotions[hatchingPotion] > 0)) {
throw new NotFound(i18n.t('messageMissingEggPotion', req.language));
}
if (content.hatchingPotions[hatchingPotion].premium && !content.dropEggs[egg]) {
throw new BadRequest(i18n.t('messageInvalidEggPotionCombo', req.language));
}
let pet = `${egg}-${hatchingPotion}`;
if (user.items.pets[pet] && user.items.pets[pet] > 0) {
throw new NotAuthorized(i18n.t('messageAlreadyPet', req.language));
}
user.items.pets[pet] = 5;
user.items.eggs[egg]--;
user.items.hatchingPotions[hatchingPotion]--;
if (user.markModified) {
user.markModified('items.pets');
user.markModified('items.eggs');
user.markModified('items.hatchingPotions');
}
return [
user.items,
i18n.t('messageHatched', req.language),
];
};