mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +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:
@@ -1,4 +1,4 @@
|
|||||||
import buySpecialSpell from '../../../../website/common/script/ops/buy/buySpecialSpell';
|
import {BuySpellOperation} from '../../../../website/common/script/ops/buy/buySpell';
|
||||||
import {
|
import {
|
||||||
BadRequest,
|
BadRequest,
|
||||||
NotFound,
|
NotFound,
|
||||||
@@ -15,6 +15,11 @@ describe('shared.ops.buySpecialSpell', () => {
|
|||||||
let user;
|
let user;
|
||||||
let analytics = {track () {}};
|
let analytics = {track () {}};
|
||||||
|
|
||||||
|
function buySpecialSpell (_user, _req, _analytics) {
|
||||||
|
const buyOp = new BuySpellOperation(_user, _req, _analytics);
|
||||||
|
|
||||||
|
return buyOp.purchase();
|
||||||
|
}
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
user = generateUser();
|
user = generateUser();
|
||||||
sinon.stub(analytics, 'track');
|
sinon.stub(analytics, 'track');
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import buyOp from 'common/script/ops/buy/buy';
|
import buyOp from 'common/script/ops/buy/buy';
|
||||||
import content from 'common/script/content/index';
|
import content from 'common/script/content/index';
|
||||||
import purchaseOp from 'common/script/ops/buy/purchaseWithSpell';
|
|
||||||
import hourglassPurchaseOp from 'common/script/ops/buy/hourglassPurchase';
|
import hourglassPurchaseOp from 'common/script/ops/buy/hourglassPurchase';
|
||||||
import sellOp from 'common/script/ops/sell';
|
import sellOp from 'common/script/ops/sell';
|
||||||
import unlockOp from 'common/script/ops/unlock';
|
import unlockOp from 'common/script/ops/unlock';
|
||||||
@@ -91,7 +90,7 @@ async function buyArmoire (store, params) {
|
|||||||
export function purchase (store, params) {
|
export function purchase (store, params) {
|
||||||
const quantity = params.quantity || 1;
|
const quantity = params.quantity || 1;
|
||||||
const user = store.state.user.data;
|
const user = store.state.user.data;
|
||||||
let opResult = purchaseOp(user, {params, quantity});
|
let opResult = buyOp(user, {params, quantity});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
result: opResult,
|
result: opResult,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {BuyHealthPotionOperation} from './buyHealthPotion';
|
|||||||
import {BuyMarketGearOperation} from './buyMarketGear';
|
import {BuyMarketGearOperation} from './buyMarketGear';
|
||||||
import buyMysterySet from './buyMysterySet';
|
import buyMysterySet from './buyMysterySet';
|
||||||
import {BuyQuestWithGoldOperation} from './buyQuest';
|
import {BuyQuestWithGoldOperation} from './buyQuest';
|
||||||
import buySpecialSpell from './buySpecialSpell';
|
import {BuySpellOperation} from './buySpell';
|
||||||
import purchaseOp from './purchase';
|
import purchaseOp from './purchase';
|
||||||
import hourglassPurchase from './hourglassPurchase';
|
import hourglassPurchase from './hourglassPurchase';
|
||||||
import errorMessage from '../../libs/errorMessage';
|
import errorMessage from '../../libs/errorMessage';
|
||||||
@@ -70,9 +70,12 @@ module.exports = function buy (user, req = {}, analytics) {
|
|||||||
buyRes = buyOp.purchase();
|
buyRes = buyOp.purchase();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'special':
|
case 'special': {
|
||||||
buyRes = buySpecialSpell(user, req, analytics);
|
const buyOp = new BuySpellOperation(user, req, analytics);
|
||||||
|
|
||||||
|
buyRes = buyOp.purchase();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
const buyOp = new BuyMarketGearOperation(user, req, analytics);
|
const buyOp = new BuyMarketGearOperation(user, req, analytics);
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
|
|||||||
user.achievements.quests.taskwoodsTerror3;
|
user.achievements.quests.taskwoodsTerror3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getItemKey () {
|
||||||
|
return this.key;
|
||||||
|
}
|
||||||
|
|
||||||
getItemValue (item) {
|
getItemValue (item) {
|
||||||
return item.goldValue;
|
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