mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
Merge branch 'Alys-v3-buy-health-potion' into api-v3
This commit is contained in:
@@ -114,7 +114,7 @@ import sleep from './ops/sleep';
|
|||||||
import allocate from './ops/allocate';
|
import allocate from './ops/allocate';
|
||||||
import buy from './ops/buy';
|
import buy from './ops/buy';
|
||||||
import buyGear from './ops/buyGear';
|
import buyGear from './ops/buyGear';
|
||||||
import buyPotion from './ops/buyPotion';
|
import buyHealthPotion from './ops/buyHealthPotion';
|
||||||
import buyArmoire from './ops/buyArmoire';
|
import buyArmoire from './ops/buyArmoire';
|
||||||
import buyMysterySet from './ops/buyMysterySet';
|
import buyMysterySet from './ops/buyMysterySet';
|
||||||
import buyQuest from './ops/buyQuest';
|
import buyQuest from './ops/buyQuest';
|
||||||
@@ -155,7 +155,7 @@ api.ops = {
|
|||||||
allocate,
|
allocate,
|
||||||
buy,
|
buy,
|
||||||
buyGear,
|
buyGear,
|
||||||
buyPotion,
|
buyHealthPotion,
|
||||||
buyArmoire,
|
buyArmoire,
|
||||||
buyMysterySet,
|
buyMysterySet,
|
||||||
buySpecialSpell,
|
buySpecialSpell,
|
||||||
@@ -274,7 +274,7 @@ api.wrap = function wrapUser (user, main = true) {
|
|||||||
releaseMounts: _.partial(importedOps.releaseMounts, user),
|
releaseMounts: _.partial(importedOps.releaseMounts, user),
|
||||||
releaseBoth: _.partial(importedOps.releaseBoth, user),
|
releaseBoth: _.partial(importedOps.releaseBoth, user),
|
||||||
buy: _.partial(importedOps.buy, user),
|
buy: _.partial(importedOps.buy, user),
|
||||||
buyPotion: _.partial(importedOps.buyPotion, user),
|
buyHealthPotion: _.partial(importedOps.buyHealthPotion, user),
|
||||||
buyArmoire: _.partial(importedOps.buyArmoire, user),
|
buyArmoire: _.partial(importedOps.buyArmoire, user),
|
||||||
buyGear: _.partial(importedOps.buyGear, user),
|
buyGear: _.partial(importedOps.buyGear, user),
|
||||||
buyQuest: _.partial(importedOps.buyQuest, user),
|
buyQuest: _.partial(importedOps.buyQuest, user),
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import _ from 'lodash';
|
|||||||
import {
|
import {
|
||||||
BadRequest,
|
BadRequest,
|
||||||
} from '../libs/errors';
|
} from '../libs/errors';
|
||||||
import buyPotion from './buyPotion';
|
import buyHealthPotion from './buyHealthPotion';
|
||||||
import buyArmoire from './buyArmoire';
|
import buyArmoire from './buyArmoire';
|
||||||
import buyGear from './buyGear';
|
import buyGear from './buyGear';
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ module.exports = function buy (user, req = {}, analytics) {
|
|||||||
|
|
||||||
let buyRes;
|
let buyRes;
|
||||||
if (key === 'potion') {
|
if (key === 'potion') {
|
||||||
buyRes = buyPotion(user, req, analytics);
|
buyRes = buyHealthPotion(user, req, analytics);
|
||||||
} else if (key === 'armoire') {
|
} else if (key === 'armoire') {
|
||||||
buyRes = buyArmoire(user, req, analytics);
|
buyRes = buyArmoire(user, req, analytics);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
NotAuthorized,
|
NotAuthorized,
|
||||||
} from '../libs/errors';
|
} from '../libs/errors';
|
||||||
|
|
||||||
module.exports = function buyPotion (user, req = {}, analytics) {
|
module.exports = function buyHealthPotion (user, req = {}, analytics) {
|
||||||
let item = content.potion;
|
let item = content.potion;
|
||||||
|
|
||||||
if (user.stats.gp < item.value) {
|
if (user.stats.gp < item.value) {
|
||||||
@@ -31,7 +31,7 @@ import releaseMounts from './releaseMounts';
|
|||||||
import releaseBoth from './releaseBoth';
|
import releaseBoth from './releaseBoth';
|
||||||
import buy from './buy';
|
import buy from './buy';
|
||||||
import buyGear from './buyGear';
|
import buyGear from './buyGear';
|
||||||
import buyPotion from './buyPotion';
|
import buyHealthPotion from './buyHealthPotion';
|
||||||
import buyArmoire from './buyArmoire';
|
import buyArmoire from './buyArmoire';
|
||||||
import buyQuest from './buyQuest';
|
import buyQuest from './buyQuest';
|
||||||
import buyMysterySet from './buyMysterySet';
|
import buyMysterySet from './buyMysterySet';
|
||||||
@@ -83,7 +83,7 @@ module.exports = {
|
|||||||
releaseBoth,
|
releaseBoth,
|
||||||
buy,
|
buy,
|
||||||
buyGear,
|
buyGear,
|
||||||
buyPotion,
|
buyHealthPotion,
|
||||||
buyArmoire,
|
buyArmoire,
|
||||||
buyQuest,
|
buyQuest,
|
||||||
buyMysterySet,
|
buyMysterySet,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ describe('POST /user/buy-armoire', () => {
|
|||||||
// More tests in common code unit tests
|
// More tests in common code unit tests
|
||||||
|
|
||||||
it('returns an error if user does not have enough gold', async () => {
|
it('returns an error if user does not have enough gold', async () => {
|
||||||
await expect(user.post('/user/buy-potion'))
|
await expect(user.post('/user/buy-health-potion'))
|
||||||
.to.eventually.be.rejected.and.eql({
|
.to.eventually.be.rejected.and.eql({
|
||||||
code: 401,
|
code: 401,
|
||||||
error: 'NotAuthorized',
|
error: 'NotAuthorized',
|
||||||
@@ -32,7 +32,7 @@ describe('POST /user/buy-armoire', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let potion = content.potion;
|
let potion = content.potion;
|
||||||
let res = await user.post('/user/buy-potion');
|
let res = await user.post('/user/buy-health-potion');
|
||||||
await user.sync();
|
await user.sync();
|
||||||
|
|
||||||
expect(user.stats.hp).to.equal(50);
|
expect(user.stats.hp).to.equal(50);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import shared from '../../../../../common/script';
|
|||||||
|
|
||||||
let content = shared.content;
|
let content = shared.content;
|
||||||
|
|
||||||
describe('POST /user/buy-potion', () => {
|
describe('POST /user/buy-health-potion', () => {
|
||||||
let user;
|
let user;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@@ -18,7 +18,7 @@ describe('POST /user/buy-potion', () => {
|
|||||||
// More tests in common code unit tests
|
// More tests in common code unit tests
|
||||||
|
|
||||||
it('returns an error if user does not have enough gold', async () => {
|
it('returns an error if user does not have enough gold', async () => {
|
||||||
await expect(user.post('/user/buy-potion'))
|
await expect(user.post('/user/buy-health-potion'))
|
||||||
.to.eventually.be.rejected.and.eql({
|
.to.eventually.be.rejected.and.eql({
|
||||||
code: 401,
|
code: 401,
|
||||||
error: 'NotAuthorized',
|
error: 'NotAuthorized',
|
||||||
@@ -32,7 +32,7 @@ describe('POST /user/buy-potion', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let potion = content.potion;
|
let potion = content.potion;
|
||||||
let res = await user.post('/user/buy-potion');
|
let res = await user.post('/user/buy-health-potion');
|
||||||
await user.sync();
|
await user.sync();
|
||||||
|
|
||||||
expect(user.stats.hp).to.equal(50);
|
expect(user.stats.hp).to.equal(50);
|
||||||
@@ -2,13 +2,13 @@
|
|||||||
import {
|
import {
|
||||||
generateUser,
|
generateUser,
|
||||||
} from '../../helpers/common.helper';
|
} from '../../helpers/common.helper';
|
||||||
import buyPotion from '../../../common/script/ops/buyPotion';
|
import buyHealthPotion from '../../../common/script/ops/buyHealthPotion';
|
||||||
import {
|
import {
|
||||||
NotAuthorized,
|
NotAuthorized,
|
||||||
} from '../../../common/script/libs/errors';
|
} from '../../../common/script/libs/errors';
|
||||||
import i18n from '../../../common/script/i18n';
|
import i18n from '../../../common/script/i18n';
|
||||||
|
|
||||||
describe('shared.ops.buyPotion', () => {
|
describe('shared.ops.buyHealthPotion', () => {
|
||||||
let user;
|
let user;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -30,19 +30,19 @@ describe('shared.ops.buyPotion', () => {
|
|||||||
context('Potion', () => {
|
context('Potion', () => {
|
||||||
it('recovers 15 hp', () => {
|
it('recovers 15 hp', () => {
|
||||||
user.stats.hp = 30;
|
user.stats.hp = 30;
|
||||||
buyPotion(user);
|
buyHealthPotion(user);
|
||||||
expect(user.stats.hp).to.eql(45);
|
expect(user.stats.hp).to.eql(45);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not increase hp above 50', () => {
|
it('does not increase hp above 50', () => {
|
||||||
user.stats.hp = 45;
|
user.stats.hp = 45;
|
||||||
buyPotion(user);
|
buyHealthPotion(user);
|
||||||
expect(user.stats.hp).to.eql(50);
|
expect(user.stats.hp).to.eql(50);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('deducts 25 gp', () => {
|
it('deducts 25 gp', () => {
|
||||||
user.stats.hp = 45;
|
user.stats.hp = 45;
|
||||||
buyPotion(user);
|
buyHealthPotion(user);
|
||||||
|
|
||||||
expect(user.stats.gp).to.eql(175);
|
expect(user.stats.gp).to.eql(175);
|
||||||
});
|
});
|
||||||
@@ -51,7 +51,7 @@ describe('shared.ops.buyPotion', () => {
|
|||||||
user.stats.hp = 45;
|
user.stats.hp = 45;
|
||||||
user.stats.gp = 5;
|
user.stats.gp = 5;
|
||||||
try {
|
try {
|
||||||
buyPotion(user);
|
buyHealthPotion(user);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err).to.be.an.instanceof(NotAuthorized);
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||||||
@@ -578,25 +578,23 @@ api.buyArmoire = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {post} /user/buy-potion Buy a potion.
|
* @api {post} /user/buy-health-potion Buy a health potion
|
||||||
* @apiVersion 3.0.0
|
* @apiVersion 3.0.0
|
||||||
* @apiName UserBuyPotion
|
* @apiName UserBuyPotion
|
||||||
* @apiGroup User
|
* @apiGroup User
|
||||||
*
|
*
|
||||||
* @apiParam {string} key The item to buy.
|
|
||||||
*
|
|
||||||
* @apiSuccess {Object} data user.stats
|
* @apiSuccess {Object} data user.stats
|
||||||
* @apiSuccess {string} message Success message
|
* @apiSuccess {string} message Success message
|
||||||
*/
|
*/
|
||||||
api.buyPotion = {
|
api.buyHealthPotion = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
middlewares: [authWithHeaders()],
|
middlewares: [authWithHeaders()],
|
||||||
url: '/user/buy-potion',
|
url: '/user/buy-health-potion',
|
||||||
async handler (req, res) {
|
async handler (req, res) {
|
||||||
let user = res.locals.user;
|
let user = res.locals.user;
|
||||||
let buyPotionResponse = common.ops.buyPotion(user, req, res.analytics);
|
let buyHealthPotionResponse = common.ops.buyHealthPotion(user, req, res.analytics);
|
||||||
await user.save();
|
await user.save();
|
||||||
res.respond(200, ...buyPotionResponse);
|
res.respond(200, ...buyHealthPotionResponse);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user