mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
* convert buySpell operation * remove purchaseWithSpell - change purchaseType 'special' to 'spells' - fix lint * fix tests * rollback 'spells' to 'special'
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
import content from '../../content/index';
|
|
import get from 'lodash/get';
|
|
import pick from 'lodash/pick';
|
|
import splitWhitespace from '../../libs/splitWhitespace';
|
|
import {
|
|
BadRequest,
|
|
NotFound,
|
|
} from '../../libs/errors';
|
|
import {AbstractGoldItemOperation} from './abstractBuyOperation';
|
|
import errorMessage from '../../libs/errorMessage';
|
|
|
|
export class BuySpellOperation extends AbstractGoldItemOperation {
|
|
constructor (user, req, analytics) {
|
|
super(user, req, analytics);
|
|
}
|
|
|
|
getItemKey () {
|
|
return this.key;
|
|
}
|
|
|
|
multiplePurchaseAllowed () {
|
|
return true;
|
|
}
|
|
|
|
extractAndValidateParams (user, req) {
|
|
let key = this.key = get(req, 'params.key');
|
|
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
|
|
|
|
let item = content.special[key];
|
|
if (!item) throw new NotFound(errorMessage('spellNotFound', {spellId: key}));
|
|
|
|
this.canUserPurchase(user, item);
|
|
}
|
|
|
|
executeChanges (user, item, req) {
|
|
user.items.special[item.key] += this.quantity;
|
|
|
|
this.subtractCurrency(user, item, this.quantity);
|
|
|
|
return [
|
|
pick(user, splitWhitespace('items stats')),
|
|
this.i18n('messageBought', {
|
|
itemText: item.text(req.language),
|
|
}),
|
|
];
|
|
}
|
|
}
|