mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-14 16:02:14 +00:00
Purchase API Refactoring: Health Potion (#10152)
* convert buyHealthPotion * fix health potion tests * fix lint
This commit is contained in:
parent
338781f57b
commit
567d5f74ba
4 changed files with 56 additions and 47 deletions
10
package-lock.json
generated
10
package-lock.json
generated
|
|
@ -151,7 +151,7 @@
|
|||
},
|
||||
"@sinonjs/formatio": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz",
|
||||
"resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz",
|
||||
"integrity": "sha512-ls6CAMA6/5gG+O/IdsBcblvnd8qcO/l1TYoNeAzp3wcISOxlPXQEus0mLcdwazEkWjaBdaJ3TaxmNgCLWwvWzg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
|
|
@ -3548,7 +3548,7 @@
|
|||
},
|
||||
"compression": {
|
||||
"version": "1.7.2",
|
||||
"resolved": "http://registry.npmjs.org/compression/-/compression-1.7.2.tgz",
|
||||
"resolved": "https://registry.npmjs.org/compression/-/compression-1.7.2.tgz",
|
||||
"integrity": "sha1-qv+81qr4VLROuygDU9WtFlH1mmk=",
|
||||
"requires": {
|
||||
"accepts": "1.3.5",
|
||||
|
|
@ -5601,7 +5601,7 @@
|
|||
"dependencies": {
|
||||
"onetime": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "http://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz",
|
||||
"resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz",
|
||||
"integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k="
|
||||
}
|
||||
}
|
||||
|
|
@ -6364,7 +6364,7 @@
|
|||
},
|
||||
"event-stream": {
|
||||
"version": "3.3.4",
|
||||
"resolved": "http://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz",
|
||||
"resolved": "https://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz",
|
||||
"integrity": "sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=",
|
||||
"requires": {
|
||||
"duplexer": "0.1.1",
|
||||
|
|
@ -15786,7 +15786,7 @@
|
|||
},
|
||||
"pinkie-promise": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "http://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz",
|
||||
"resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz",
|
||||
"integrity": "sha1-ITXW36ejWMBprJsXh3YogihFD/o=",
|
||||
"requires": {
|
||||
"pinkie": "2.0.4"
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
import {
|
||||
generateUser,
|
||||
} from '../../../helpers/common.helper';
|
||||
import buyHealthPotion from '../../../../website/common/script/ops/buy/buyHealthPotion';
|
||||
import { BuyHealthPotionOperation } from '../../../../website/common/script/ops/buy/buyHealthPotion';
|
||||
import {
|
||||
NotAuthorized,
|
||||
} from '../../../../website/common/script/libs/errors';
|
||||
|
|
@ -12,6 +12,12 @@ describe('shared.ops.buyHealthPotion', () => {
|
|||
let user;
|
||||
let analytics = {track () {}};
|
||||
|
||||
function buyHealthPotion (_user, _req, _analytics) {
|
||||
const buyOp = new BuyHealthPotionOperation(_user, _req, _analytics);
|
||||
|
||||
return buyOp.purchase();
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
user = generateUser({
|
||||
items: {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import get from 'lodash/get';
|
|||
import {
|
||||
BadRequest,
|
||||
} from '../../libs/errors';
|
||||
import buyHealthPotion from './buyHealthPotion';
|
||||
import {BuyHealthPotionOperation} from './buyHealthPotion';
|
||||
import buyArmoire from './buyArmoire';
|
||||
import {BuyMarketGearOperation} from './buyMarketGear';
|
||||
import buyMysterySet from './buyMysterySet';
|
||||
|
|
@ -36,9 +36,12 @@ module.exports = function buy (user, req = {}, analytics) {
|
|||
case 'mystery':
|
||||
buyRes = buyMysterySet(user, req, analytics);
|
||||
break;
|
||||
case 'potion':
|
||||
buyRes = buyHealthPotion(user, req, analytics);
|
||||
case 'potion': {
|
||||
const buyOp = new BuyHealthPotionOperation(user, req, analytics);
|
||||
|
||||
buyRes = buyOp.purchase();
|
||||
break;
|
||||
}
|
||||
case 'eggs':
|
||||
case 'hatchingPotions':
|
||||
case 'food':
|
||||
|
|
|
|||
|
|
@ -1,55 +1,55 @@
|
|||
import content from '../../content/index';
|
||||
import i18n from '../../i18n';
|
||||
import {
|
||||
NotAuthorized,
|
||||
} from '../../libs/errors';
|
||||
|
||||
module.exports = function buyHealthPotion (user, req = {}, analytics) {
|
||||
let item = content.potion;
|
||||
let quantity = req.quantity || 1;
|
||||
import { AbstractGoldItemOperation} from './abstractBuyOperation';
|
||||
|
||||
if (user.stats.gp < item.value * quantity) {
|
||||
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
|
||||
export class BuyHealthPotionOperation extends AbstractGoldItemOperation {
|
||||
constructor (user, req, analytics) {
|
||||
super(user, req, analytics);
|
||||
}
|
||||
|
||||
if (item.canOwn && !item.canOwn(user)) {
|
||||
throw new NotAuthorized(i18n.t('cannotBuyItem', req.language));
|
||||
multiplePurchaseAllowed () {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (user.stats.hp >= 50) {
|
||||
throw new NotAuthorized(i18n.t('messageHealthAlreadyMax', req.language));
|
||||
extractAndValidateParams (user) {
|
||||
let item = content.potion;
|
||||
let userHp = user.stats.hp;
|
||||
|
||||
super.canUserPurchase(user, item);
|
||||
|
||||
if (userHp >= 50) {
|
||||
throw new NotAuthorized(this.i18n('messageHealthAlreadyMax'));
|
||||
}
|
||||
|
||||
if (userHp <= 0) {
|
||||
throw new NotAuthorized(this.i18n('messageHealthAlreadyMin'));
|
||||
}
|
||||
}
|
||||
|
||||
if (user.stats.hp <= 0) {
|
||||
throw new NotAuthorized(i18n.t('messageHealthAlreadyMin', req.language));
|
||||
}
|
||||
executeChanges (user, item) {
|
||||
user.stats.hp += 15 * this.quantity;
|
||||
if (user.stats.hp > 50) {
|
||||
user.stats.hp = 50;
|
||||
}
|
||||
|
||||
user.stats.hp += 15 * quantity;
|
||||
if (user.stats.hp > 50) {
|
||||
user.stats.hp = 50;
|
||||
}
|
||||
this.substractCurrency(user, item, this.quantity);
|
||||
|
||||
user.stats.gp -= item.value * quantity;
|
||||
|
||||
let message = i18n.t('messageBought', {
|
||||
itemText: item.text(req.language),
|
||||
}, req.language);
|
||||
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('acquire item', {
|
||||
uuid: user._id,
|
||||
itemKey: 'Potion',
|
||||
acquireMethod: 'Gold',
|
||||
goldCost: item.value,
|
||||
category: 'behavior',
|
||||
headers: req.headers,
|
||||
quantityPurchased: quantity,
|
||||
let message = this.i18n('messageBought', {
|
||||
itemText: this.item.text(this.req.language),
|
||||
});
|
||||
|
||||
return [
|
||||
this.user.stats,
|
||||
message,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
user.stats,
|
||||
message,
|
||||
];
|
||||
};
|
||||
analyticsData () {
|
||||
let data = super.analyticsData();
|
||||
data.itemKey = 'Potion';
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue