mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 14:17:22 +01:00
Preventing users from buying already gifted subscriber items (#7734)
* Adding the unopened mystery items to the call to get not obtained subscriber items. closes #7712 * refactoring according to pr * Refactoring according to pr. moved time-travelers to it's own file and added new tests.
This commit is contained in:
@@ -28,7 +28,10 @@ describe('Inventory Controller', function() {
|
||||
},
|
||||
preferences: {
|
||||
suppressModals: {}
|
||||
}
|
||||
},
|
||||
purchased: {
|
||||
plan: {}
|
||||
},
|
||||
});
|
||||
|
||||
Shared.wrap(user);
|
||||
|
||||
29
test/content/time-travelers.test.js
Normal file
29
test/content/time-travelers.test.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import _ from 'lodash';
|
||||
import {
|
||||
generateUser,
|
||||
} from '../helpers/common.helper';
|
||||
|
||||
import timeTravelers from '../../website/common/script/content/time-travelers'
|
||||
|
||||
describe('time-travelers store', () => {
|
||||
let user;
|
||||
beforeEach(() => {
|
||||
user = generateUser();
|
||||
});
|
||||
|
||||
it('removes owned sets from the time travelers store', () => {
|
||||
user.items.gear.owned['head_mystery_201602'] = true;
|
||||
expect(timeTravelers.timeTravelerStore(user)['201602']).to.not.exist;
|
||||
expect(timeTravelers.timeTravelerStore(user)['201603']).to.exist;
|
||||
});
|
||||
|
||||
it('removes unopened mystery item sets from the time travelers store', () => {
|
||||
user.purchased = {
|
||||
plan: {
|
||||
mysteryItems: ['head_mystery_201602'],
|
||||
},
|
||||
};
|
||||
expect(timeTravelers.timeTravelerStore(user)['201602']).to.not.exist;
|
||||
expect(timeTravelers.timeTravelerStore(user)['201603']).to.exist;
|
||||
});
|
||||
});
|
||||
@@ -371,9 +371,16 @@ habitrpg.controller("InventoryCtrl",
|
||||
$scope.hasAllTimeTravelerItemsOfType('mounts'));
|
||||
};
|
||||
|
||||
$scope.shouldShowTimeTravelerItem = function(category, item) {
|
||||
if (category.identifier === 'pets' || category.identifier === 'mounts') {
|
||||
return !user.items[category.identifier][item.key];
|
||||
}
|
||||
return !user.items.gear.owned[item.key] && user.purchased.plan.mysteryItems.indexOf(item.key) === -1;
|
||||
};
|
||||
|
||||
$scope.hasAllTimeTravelerItemsOfType = function(type) {
|
||||
if (type === 'mystery') {
|
||||
var itemsLeftInTimeTravelerStore = Content.timeTravelerStore(user.items.gear.owned);
|
||||
var itemsLeftInTimeTravelerStore = Content.timeTravelerStore(user);
|
||||
var keys = Object.keys(itemsLeftInTimeTravelerStore);
|
||||
|
||||
return keys.length === 0;
|
||||
|
||||
@@ -14,8 +14,6 @@ let api = module.exports;
|
||||
|
||||
import achievements from './achievements';
|
||||
|
||||
import mysterySets from './mystery-sets';
|
||||
|
||||
import eggs from './eggs';
|
||||
import hatchingPotions from './hatching-potions';
|
||||
import stable from './stable';
|
||||
@@ -25,39 +23,19 @@ import appearances from './appearance';
|
||||
import backgrounds from './appearance/backgrounds.js'
|
||||
import spells from './spells';
|
||||
import faq from './faq';
|
||||
import timeTravelers from './time-travelers';
|
||||
|
||||
import loginIncentives from './loginIncentives';
|
||||
|
||||
api.achievements = achievements;
|
||||
|
||||
api.mystery = mysterySets;
|
||||
|
||||
api.itemList = ITEM_LIST;
|
||||
|
||||
api.gear = gear;
|
||||
api.spells = spells;
|
||||
|
||||
/*
|
||||
Time Traveler Store, mystery sets need their items mapped in
|
||||
*/
|
||||
|
||||
_.each(api.mystery, function(v, k) {
|
||||
return v.items = _.where(api.gear.flat, {
|
||||
mystery: k
|
||||
});
|
||||
});
|
||||
|
||||
api.timeTravelerStore = function(owned) {
|
||||
var ownedKeys;
|
||||
ownedKeys = _.keys((typeof owned.toObject === "function" ? owned.toObject() : void 0) || owned);
|
||||
return _.reduce(api.mystery, function(m, v, k) {
|
||||
if (k === 'wondercon' || ~ownedKeys.indexOf(v.items[0].key)) {
|
||||
return m;
|
||||
}
|
||||
m[k] = v;
|
||||
return m;
|
||||
}, {});
|
||||
};
|
||||
|
||||
api.mystery = timeTravelers.mystery;
|
||||
api.timeTravelerStore = timeTravelers.timeTravelerStore;
|
||||
|
||||
/*
|
||||
---------------------------------------------------------------
|
||||
|
||||
31
website/common/script/content/time-travelers.js
Normal file
31
website/common/script/content/time-travelers.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import _ from 'lodash';
|
||||
import mysterySets from './mystery-sets';
|
||||
import gear from './gear';
|
||||
|
||||
let mystery = mysterySets;
|
||||
|
||||
_.each(mystery, (v, k) => {
|
||||
return v.items = _.where(gear.flat, {
|
||||
mystery: k,
|
||||
});
|
||||
});
|
||||
|
||||
let timeTravelerStore = (user) => {
|
||||
let ownedKeys;
|
||||
let owned = user.items.gear.owned;
|
||||
let unopenedGifts = user.purchased.plan.mysteryItems;
|
||||
ownedKeys = _.keys((typeof owned.toObject === 'function' ? owned.toObject() : undefined) || owned);
|
||||
ownedKeys = _.union(ownedKeys, unopenedGifts);
|
||||
return _.reduce(mystery, (m, v, k) => {
|
||||
if (k === 'wondercon' || ownedKeys.indexOf(v.items[0].key) !== -1) {
|
||||
return m;
|
||||
}
|
||||
m[k] = v;
|
||||
return m;
|
||||
}, {});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
timeTravelerStore,
|
||||
mystery,
|
||||
};
|
||||
@@ -180,7 +180,7 @@ shops.getTimeTravelersCategories = function getTimeTravelersCategories (user, la
|
||||
}
|
||||
}
|
||||
|
||||
let sets = content.timeTravelerStore(user.items.gear.owned);
|
||||
let sets = content.timeTravelerStore(user);
|
||||
for (let setKey in sets) {
|
||||
if (sets.hasOwnProperty(setKey)) {
|
||||
let set = sets[setKey];
|
||||
|
||||
@@ -15,7 +15,7 @@ module.exports = function buyMysterySet (user, req = {}, analytics) {
|
||||
throw new NotAuthorized(i18n.t('notEnoughHourglasses', req.language));
|
||||
}
|
||||
|
||||
let ref = content.timeTravelerStore(user.items.gear.owned);
|
||||
let ref = content.timeTravelerStore(user);
|
||||
let mysterySet = ref ? ref[key] : undefined;
|
||||
|
||||
if (!mysterySet) {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
.col-md-12
|
||||
li.customize-menu.inventory-gear
|
||||
menu.pets-menu(label='{{::category.text}}', ng-repeat='category in timeTravelersCategories')
|
||||
div(ng-repeat='item in category.items', ng-if='category.identifier === "pets" || category.identifier === "mounts" ? !user.items[category.identifier][item.key] : !user.items.gear.owned[item.key]')
|
||||
div(ng-repeat='item in category.items', ng-if='shouldShowTimeTravelerItem(category, item)')
|
||||
button.customize-option(class='{{item.class ? item.class : "shop_"+item.key}}',
|
||||
popover='{{item.notes}}', popover-title='{{item.text}}',
|
||||
popover-trigger='mouseenter', popover-placement='right',
|
||||
|
||||
Reference in New Issue
Block a user