mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-05-22 13:48:46 +00:00
* add another check if previous gear is owned * respect gear purchase order * catch error with miscalculation of equipment number floor * add integration test for proper equipment purchasing order * fix syntax * add 'previousGearNotOwned' string * rewrite logic for different starting levels for wep vs others * separate and add tests for armor and weapon * rename variable for clarification * skip check if itemIndex is NaN * change obscure NaN check for readability * change conditional from checking NaN to Int
This commit is contained in:
parent
bfaa7c0fea
commit
d4bc7c77a9
3 changed files with 35 additions and 2 deletions
|
|
@ -25,12 +25,32 @@ describe('POST /user/buy-gear/:key', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('buys a piece of gear', async () => {
|
||||
it('buys the first level weapon gear', async () => {
|
||||
let key = 'weapon_warrior_0';
|
||||
|
||||
await user.post(`/user/buy-gear/${key}`);
|
||||
await user.sync();
|
||||
|
||||
expect(user.items.gear.owned[key]).to.eql(true);
|
||||
});
|
||||
|
||||
it('buys the first level armor gear', async () => {
|
||||
let key = 'armor_warrior_1';
|
||||
|
||||
await user.post(`/user/buy-gear/${key}`);
|
||||
await user.sync();
|
||||
|
||||
expect(user.items.gear.owned.armor_warrior_1).to.eql(true);
|
||||
expect(user.items.gear.owned[key]).to.eql(true);
|
||||
});
|
||||
|
||||
it('tries to buy subsequent, level gear', async () => {
|
||||
let key = 'armor_warrior_2';
|
||||
|
||||
return expect(user.post(`/user/buy-gear/${key}`))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: 'You need to purchase a lower level gear before this one.',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@
|
|||
|
||||
"messageAlreadyPurchasedGear": "You purchased this gear in the past, but do not currently own it. You can buy it again in the rewards column on the tasks page.",
|
||||
"messageAlreadyOwnGear": "You already own this item. Equip it by going to the equipment page.",
|
||||
"previousGearNotOwned": "You need to purchase a lower level gear before this one.",
|
||||
"messageHealthAlreadyMax": "You already have maximum health.",
|
||||
"messageHealthAlreadyMin": "Oh no! You have already run out of health so it's too late to buy a health potion, but don't worry - you can revive!",
|
||||
|
||||
|
|
|
|||
|
|
@ -35,6 +35,18 @@ module.exports = function buyGear (user, req = {}, analytics) {
|
|||
throw new NotAuthorized(i18n.t('equipmentAlreadyOwned', req.language));
|
||||
}
|
||||
|
||||
let itemIndex = Number(item.index);
|
||||
|
||||
if (Number.isInteger(itemIndex)) {
|
||||
let previousLevelGear = key.replace(/[0-9]/, itemIndex - 1);
|
||||
let hasPreviousLevelGear = user.items.gear.owned[previousLevelGear];
|
||||
let checkIndexToType = itemIndex > (item.type === 'weapon' ? 0 : 1);
|
||||
|
||||
if (checkIndexToType && !hasPreviousLevelGear) {
|
||||
throw new NotAuthorized(i18n.t('previousGearNotOwned', req.language));
|
||||
}
|
||||
}
|
||||
|
||||
if (user.preferences.autoEquip) {
|
||||
user.items.gear.equipped[item.type] = item.key;
|
||||
message = handleTwoHanded(user, item, undefined, req);
|
||||
|
|
|
|||
Loading…
Reference in a new issue