diff --git a/test/api/v3/integration/user/buy/POST-user_buy_gear.test.js b/test/api/v3/integration/user/buy/POST-user_buy_gear.test.js index 6056e2fbdf..d3992ed1fe 100644 --- a/test/api/v3/integration/user/buy/POST-user_buy_gear.test.js +++ b/test/api/v3/integration/user/buy/POST-user_buy_gear.test.js @@ -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.', + }); }); }); diff --git a/website/common/locales/en/messages.json b/website/common/locales/en/messages.json index 64eb2cd9a4..9877ab39f2 100644 --- a/website/common/locales/en/messages.json +++ b/website/common/locales/en/messages.json @@ -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!", diff --git a/website/common/script/ops/buyGear.js b/website/common/script/ops/buyGear.js index 91c35ca3e7..7ef868b1e1 100644 --- a/website/common/script/ops/buyGear.js +++ b/website/common/script/ops/buyGear.js @@ -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);