mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-15 21:57:22 +01:00
Purchase API Refactoring: Spells [Gold] (#10305)
* convert buySpell operation * remove purchaseWithSpell - change purchaseType 'special' to 'spells' - fix lint * fix tests * rollback 'spells' to 'special'
This commit is contained in:
@@ -7,7 +7,7 @@ import {BuyHealthPotionOperation} from './buyHealthPotion';
|
||||
import {BuyMarketGearOperation} from './buyMarketGear';
|
||||
import buyMysterySet from './buyMysterySet';
|
||||
import {BuyQuestWithGoldOperation} from './buyQuest';
|
||||
import buySpecialSpell from './buySpecialSpell';
|
||||
import {BuySpellOperation} from './buySpell';
|
||||
import purchaseOp from './purchase';
|
||||
import hourglassPurchase from './hourglassPurchase';
|
||||
import errorMessage from '../../libs/errorMessage';
|
||||
@@ -70,9 +70,12 @@ module.exports = function buy (user, req = {}, analytics) {
|
||||
buyRes = buyOp.purchase();
|
||||
break;
|
||||
}
|
||||
case 'special':
|
||||
buyRes = buySpecialSpell(user, req, analytics);
|
||||
case 'special': {
|
||||
const buyOp = new BuySpellOperation(user, req, analytics);
|
||||
|
||||
buyRes = buyOp.purchase();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const buyOp = new BuyMarketGearOperation(user, req, analytics);
|
||||
|
||||
|
||||
@@ -25,6 +25,10 @@ export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
|
||||
user.achievements.quests.taskwoodsTerror3;
|
||||
}
|
||||
|
||||
getItemKey () {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
getItemValue (item) {
|
||||
return item.goldValue;
|
||||
}
|
||||
@@ -61,13 +65,4 @@ export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
analyticsData () {
|
||||
return {
|
||||
itemKey: this.key,
|
||||
itemType: 'Market',
|
||||
acquireMethod: 'Gold',
|
||||
goldCost: this.getItemValue(this.item.goldValue),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import i18n from '../../i18n';
|
||||
import content from '../../content/index';
|
||||
import get from 'lodash/get';
|
||||
import pick from 'lodash/pick';
|
||||
import splitWhitespace from '../../libs/splitWhitespace';
|
||||
import {
|
||||
BadRequest,
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
} from '../../libs/errors';
|
||||
import errorMessage from '../../libs/errorMessage';
|
||||
|
||||
module.exports = function buySpecialSpell (user, req = {}, analytics) {
|
||||
let key = get(req, 'params.key');
|
||||
let quantity = req.quantity || 1;
|
||||
|
||||
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
|
||||
|
||||
let item = content.special[key];
|
||||
if (!item) throw new NotFound(errorMessage('spellNotFound', {spellId: key}));
|
||||
|
||||
if (user.stats.gp < item.value * quantity) {
|
||||
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
|
||||
}
|
||||
user.stats.gp -= item.value * quantity;
|
||||
|
||||
user.items.special[key] += quantity;
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('acquire item', {
|
||||
uuid: user._id,
|
||||
itemKey: item.key,
|
||||
itemType: 'Market',
|
||||
goldCost: item.goldValue,
|
||||
quantityPurchased: quantity,
|
||||
acquireMethod: 'Gold',
|
||||
category: 'behavior',
|
||||
headers: req.headers,
|
||||
});
|
||||
}
|
||||
|
||||
return [
|
||||
pick(user, splitWhitespace('items stats')),
|
||||
i18n.t('messageBought', {
|
||||
itemText: item.text(req.language),
|
||||
}, req.language),
|
||||
];
|
||||
};
|
||||
47
website/common/script/ops/buy/buySpell.js
Normal file
47
website/common/script/ops/buy/buySpell.js
Normal file
@@ -0,0 +1,47 @@
|
||||
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),
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import buy from './buy';
|
||||
import get from 'lodash/get';
|
||||
|
||||
module.exports = function purchaseWithSpell (user, req = {}, analytics) {
|
||||
const type = get(req.params, 'type');
|
||||
|
||||
if (type === 'spells') {
|
||||
req.type = 'special';
|
||||
}
|
||||
|
||||
return buy(user, req, analytics);
|
||||
};
|
||||
Reference in New Issue
Block a user