2019-10-08 14:57:10 +00:00
|
|
|
import get from 'lodash/get';
|
2018-06-28 10:37:21 +00:00
|
|
|
import {
|
|
|
|
|
BadRequest,
|
|
|
|
|
NotAuthorized,
|
|
|
|
|
NotFound,
|
|
|
|
|
} from '../../libs/errors';
|
|
|
|
|
import content from '../../content/index';
|
|
|
|
|
|
2024-03-11 14:59:57 +00:00
|
|
|
import { errorMessage } from '../../libs/errorMessage';
|
2019-10-08 14:57:10 +00:00
|
|
|
import { AbstractGemItemOperation } from './abstractBuyOperation';
|
2018-06-28 10:37:21 +00:00
|
|
|
|
2019-10-09 14:51:17 +00:00
|
|
|
export class BuyQuestWithGemOperation extends AbstractGemItemOperation { // 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
|
2018-06-28 10:37:21 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getItemKey () {
|
|
|
|
|
return this.key;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:08:36 +00:00
|
|
|
getItemValue (item) { // eslint-disable-line class-methods-use-this
|
2018-06-28 10:37:21 +00:00
|
|
|
return item.value / 4;
|
|
|
|
|
}
|
|
|
|
|
|
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 'quest';
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 10:37:21 +00:00
|
|
|
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;
|
2018-06-28 10:37:21 +00:00
|
|
|
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const item = content.quests[key];
|
2018-06-28 10:37:21 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
if (!item) throw new NotFound(errorMessage('questNotFound', { key }));
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
if (item.category === 'gold') {
|
2019-10-08 14:57:10 +00:00
|
|
|
throw new NotAuthorized(this.i18n('questNotGemPurchasable', { key }));
|
2018-06-28 10:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.canUserPurchase(user, item);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 20:24:52 +00:00
|
|
|
canUserPurchase (user, item) {
|
|
|
|
|
if (item && item.prereqQuests) {
|
|
|
|
|
for (const prereq of item.prereqQuests) {
|
|
|
|
|
if (!user.achievements.quests[prereq]) {
|
|
|
|
|
throw new NotAuthorized(this.i18n('mustComplete', { quest: prereq }));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
super.canUserPurchase(user, item);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 21:36:15 +00:00
|
|
|
async executeChanges (user, item, req) {
|
2019-10-09 14:51:17 +00:00
|
|
|
if (
|
|
|
|
|
!user.items.quests[item.key]
|
|
|
|
|
|| user.items.quests[item.key] < 0
|
|
|
|
|
) user.items.quests[item.key] = 0;
|
2019-12-15 19:18:26 +00:00
|
|
|
user.items.quests = {
|
|
|
|
|
...user.items.quests,
|
|
|
|
|
[item.key]: user.items.quests[item.key] + this.quantity,
|
|
|
|
|
};
|
2019-04-01 17:24:18 +00:00
|
|
|
if (user.markModified) user.markModified('items.quests');
|
2018-06-28 10:37:21 +00:00
|
|
|
|
2022-01-31 21:36:15 +00:00
|
|
|
await this.subtractCurrency(user, item, this.quantity);
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
user.items.quests,
|
|
|
|
|
this.i18n('messageBought', {
|
|
|
|
|
itemText: item.text(req.language),
|
|
|
|
|
}),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|