habitica-self-host/website/common/script/ops/buy/buyMarketGear.js

94 lines
3 KiB
JavaScript
Raw Normal View History

import get from 'lodash/get';
import pick from 'lodash/pick';
2019-10-08 14:57:10 +00:00
import content from '../../content/index';
import splitWhitespace from '../../libs/splitWhitespace';
Onboarding guide and initial achievements refactoring (#11536) * add achievements to user * add placeholder strings * add to achievements to common script * add onboarding achievements category * add notifications * more notifications * award achievements * wip notification panel * add achievements icons and copy * do not count onboarding tasks for the created task achievement * add notes * sprites, fixes and completion status and reward * add onboarding panel * add toggle * fix toggle size * fix tests * fix typo * add notification * start adding modal * fix remove button positionin, timeout, progress bar * modal + fixes * disable broken social links from level up modal * change toggle icon color on hover * add border bottom to onboarding guide panel * add collapse animation * expanded onboarding on first open * onboarding: flip toggle colors * onboarding: show progress bar all the time * onboarding: fix panel closing on click * onboarding modal: add close icon and fix padding * wip: add migration for existing users * fix titles in guide * fix achievements copy * do not award completed task achievement when direction is down * start implementing new achievements * start migrating client * remove social links from achievements modals * prevent skipping tutorial + fix achievement notification * sync fixes * start redesign achievement modal * misc fixes to achievements, polish generic achievement modal and hatched pet modal * add special badge for onboarding * fix badge condition * modals fixes * hatched pet modal: add close icon * fix badge typo * fix justin button * new scrolling behavior for dropdowns * fix strings capitalization * add common tests * add api unit tests * add date check * achievements modal polishing * typos * add toggle for achievements categories * typo * fix test * fix edit avatar modal cannot be closed * finish migration and correct launch date * fix migration * migration fixes * fix tests
2019-12-16 16:20:47 +00:00
import { checkOnboardingStatus } from '../../libs/onboarding';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../libs/errors';
import handleTwoHanded from '../../fns/handleTwoHanded';
import ultimateGear from '../../fns/ultimateGear';
2019-10-08 14:57:10 +00:00
import { removePinnedGearAddPossibleNewOnes } from '../pinnedGearUtils';
import { AbstractGoldItemOperation } from './abstractBuyOperation';
import errorMessage from '../../libs/errorMessage';
2019-10-09 14:51:17 +00:00
export class BuyMarketGearOperation extends AbstractGoldItemOperation { // eslint-disable-line import/prefer-default-export, max-len
2019-10-09 18:08:36 +00:00
multiplePurchaseAllowed () { // eslint-disable-line class-methods-use-this
return false;
}
2019-10-08 14:57:10 +00:00
canUserPurchase (user, item) {
super.canUserPurchase(user, item);
const checkKlass = item.klass && !['special', 'armoire', user.stats.class].includes(item.klass);
const checkSpecialClass = item.klass === 'special' && item.specialClass && item.specialClass !== user.stats.class;
// check for different class gear
if (checkKlass || checkSpecialClass) {
throw new NotAuthorized(this.i18n('cannotBuyItem'));
}
}
extractAndValidateParams (user, req) {
2019-10-09 14:51:17 +00:00
this.key = get(req, 'params.key');
2019-10-10 20:15:58 +00:00
const { key } = this;
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
2019-10-08 14:57:10 +00:00
const item = content.gear.flat[key];
if (!item) throw new NotFound(errorMessage('itemNotFound', { key }));
this.canUserPurchase(user, item);
if (user.items.gear.owned[item.key]) {
throw new NotAuthorized(this.i18n('equipmentAlreadyOwned'));
}
2019-10-08 14:57:10 +00:00
const itemIndex = Number(item.index);
if (Number.isInteger(itemIndex) && content.classes.includes(item.klass)) {
2019-10-08 14:57:10 +00:00
const previousLevelGear = key.replace(/[0-9]/, itemIndex - 1);
const hasPreviousLevelGear = user.items.gear.owned[previousLevelGear];
2019-10-09 18:08:36 +00:00
const checkIndexToType = itemIndex > (item.type === 'weapon' || (item.type === 'shield' && item.klass === 'rogue') ? 0 : 1);
if (checkIndexToType && !hasPreviousLevelGear) {
throw new NotAuthorized(this.i18n('previousGearNotOwned'));
}
}
}
executeChanges (user, item, req) {
let message;
if (user.preferences.autoEquip) {
user.items.gear.equipped[item.type] = item.key;
message = handleTwoHanded(user, item, undefined, req);
}
Onboarding guide and initial achievements refactoring (#11536) * add achievements to user * add placeholder strings * add to achievements to common script * add onboarding achievements category * add notifications * more notifications * award achievements * wip notification panel * add achievements icons and copy * do not count onboarding tasks for the created task achievement * add notes * sprites, fixes and completion status and reward * add onboarding panel * add toggle * fix toggle size * fix tests * fix typo * add notification * start adding modal * fix remove button positionin, timeout, progress bar * modal + fixes * disable broken social links from level up modal * change toggle icon color on hover * add border bottom to onboarding guide panel * add collapse animation * expanded onboarding on first open * onboarding: flip toggle colors * onboarding: show progress bar all the time * onboarding: fix panel closing on click * onboarding modal: add close icon and fix padding * wip: add migration for existing users * fix titles in guide * fix achievements copy * do not award completed task achievement when direction is down * start implementing new achievements * start migrating client * remove social links from achievements modals * prevent skipping tutorial + fix achievement notification * sync fixes * start redesign achievement modal * misc fixes to achievements, polish generic achievement modal and hatched pet modal * add special badge for onboarding * fix badge condition * modals fixes * hatched pet modal: add close icon * fix badge typo * fix justin button * new scrolling behavior for dropdowns * fix strings capitalization * add common tests * add api unit tests * add date check * achievements modal polishing * typos * add toggle for achievements categories * typo * fix test * fix edit avatar modal cannot be closed * finish migration and correct launch date * fix migration * migration fixes * fix tests
2019-12-16 16:20:47 +00:00
if (!user.achievements.purchasedEquipment && user.addAchievement) {
user.addAchievement('purchasedEquipment');
checkOnboardingStatus(user);
}
removePinnedGearAddPossibleNewOnes(user, `gear.flat.${item.key}`, item.key);
if (item.last) ultimateGear(user);
this.subtractCurrency(user, item);
if (!message) {
message = this.i18n('messageBought', {
itemText: item.text(req.language),
});
}
return [
pick(user, splitWhitespace('items achievements stats flags pinnedItems')),
message,
];
}
}