2018-05-18 15:00:39 +00:00
|
|
|
import get from 'lodash/get';
|
|
|
|
|
import pick from 'lodash/pick';
|
2019-10-08 14:57:10 +00:00
|
|
|
import content from '../../content/index';
|
2018-05-18 15:00:39 +00:00
|
|
|
import splitWhitespace from '../../libs/splitWhitespace';
|
|
|
|
|
import {
|
|
|
|
|
BadRequest,
|
|
|
|
|
NotFound,
|
|
|
|
|
} from '../../libs/errors';
|
2019-10-08 14:57:10 +00:00
|
|
|
import { AbstractGoldItemOperation } from './abstractBuyOperation';
|
2024-03-11 14:59:57 +00:00
|
|
|
import { errorMessage } from '../../libs/errorMessage';
|
2024-05-16 18:09:34 +00:00
|
|
|
import { NotAuthorized } from '../../../../server/libs/errors';
|
|
|
|
|
import { getScheduleMatchingGroup } from '../../content/constants/schedule';
|
2018-05-18 15:00:39 +00:00
|
|
|
|
2019-10-09 14:51:17 +00:00
|
|
|
export class BuySpellOperation extends AbstractGoldItemOperation { // eslint-disable-line import/prefer-default-export, max-len
|
2018-05-18 15:00:39 +00:00
|
|
|
getItemKey () {
|
|
|
|
|
return this.key;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:08:36 +00:00
|
|
|
getItemType () { // eslint-disable-line class-methods-use-this
|
2018-08-20 19:13:22 +00:00
|
|
|
return 'spell';
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:08:36 +00:00
|
|
|
multiplePurchaseAllowed () { // eslint-disable-line class-methods-use-this
|
2018-05-18 15:00:39 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 18:09:34 +00:00
|
|
|
canUserPurchase (user, item) {
|
|
|
|
|
super.canUserPurchase(user, item);
|
|
|
|
|
|
|
|
|
|
if (item.limited) {
|
|
|
|
|
let matcherGroup;
|
|
|
|
|
if (content.cardTypes[item.key]) {
|
|
|
|
|
matcherGroup = 'card';
|
|
|
|
|
} else {
|
|
|
|
|
matcherGroup = 'seasonalSpells';
|
|
|
|
|
}
|
|
|
|
|
const matcher = getScheduleMatchingGroup(matcherGroup);
|
|
|
|
|
if (!matcher.match(item.key)) {
|
|
|
|
|
throw new NotAuthorized(this.i18n('cannotBuyItem'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-18 15:00:39 +00:00
|
|
|
extractAndValidateParams (user, req) {
|
2019-10-09 14:51:17 +00:00
|
|
|
this.key = get(req, 'params.key');
|
|
|
|
|
const { key } = this;
|
2018-05-18 15:00:39 +00:00
|
|
|
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const item = content.special[key];
|
|
|
|
|
if (!item) throw new NotFound(errorMessage('spellNotFound', { spellId: key }));
|
2018-05-18 15:00:39 +00:00
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
}),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|