Check previous gear owned before purchasing next level gear (fixes #9071) (#9466)

* 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:
Julius Jung 2017-12-05 14:58:12 -05:00 committed by Sabe Jones
parent bfaa7c0fea
commit d4bc7c77a9
3 changed files with 35 additions and 2 deletions

View file

@ -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.',
});
});
});

View file

@ -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!",

View file

@ -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);