mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-04-14 11:46:23 +00:00
correctly memoize conent api
This commit is contained in:
parent
e0f6f79c5b
commit
877fe48225
6 changed files with 584 additions and 343 deletions
154
test/content/index.test.js
Normal file
154
test/content/index.test.js
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
import content from '../../website/common/script/content';
|
||||
|
||||
describe('content index', () => {
|
||||
let clock;
|
||||
|
||||
afterEach(() => {
|
||||
if (clock) {
|
||||
clock.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it('Releases eggs when appropriate without needing restarting', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-06-20'));
|
||||
const mayEggs = content.eggs;
|
||||
expect(mayEggs.Chameleon).to.not.exist;
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date('2024-07-20'));
|
||||
const juneEggs = content.eggs;
|
||||
expect(juneEggs.Chameleon).to.exist;
|
||||
expect(Object.keys(mayEggs).length, '').to.equal(Object.keys(juneEggs).length - 1);
|
||||
});
|
||||
|
||||
it('Releases hatching potions when appropriate without needing restarting', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-05-20'));
|
||||
const mayHatchingPotions = content.hatchingPotions;
|
||||
expect(mayHatchingPotions.Koi).to.not.exist;
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date('2024-06-20'));
|
||||
const juneHatchingPotions = content.hatchingPotions;
|
||||
expect(juneHatchingPotions.Koi).to.exist;
|
||||
expect(Object.keys(mayHatchingPotions).length, '').to.equal(Object.keys(juneHatchingPotions).length - 1);
|
||||
});
|
||||
|
||||
it('Releases armoire gear when appropriate without needing restarting', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-06-20'));
|
||||
const juneGear = content.gear.flat;
|
||||
expect(juneGear.armor_armoire_corsairsCoatAndCape).to.not.exist;
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date('2024-07-10'));
|
||||
const julyGear = content.gear.flat;
|
||||
expect(julyGear.armor_armoire_corsairsCoatAndCape).to.exist;
|
||||
expect(Object.keys(juneGear).length, '').to.equal(Object.keys(julyGear).length - 3);
|
||||
});
|
||||
|
||||
it('Releases pets gear when appropriate without needing restarting', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-06-20'));
|
||||
const junePets = content.petInfo;
|
||||
expect(junePets['Chameleon-Base']).to.not.exist;
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date('2024-07-10'));
|
||||
const julyPets = content.petInfo;
|
||||
expect(julyPets['Chameleon-Base']).to.exist;
|
||||
expect(Object.keys(junePets).length, '').to.equal(Object.keys(julyPets).length - 10);
|
||||
});
|
||||
|
||||
it('Releases mounts gear when appropriate without needing restarting', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-06-20'));
|
||||
const juneMounts = content.mountInfo;
|
||||
expect(juneMounts['Chameleon-Base']).to.not.exist;
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date('2024-07-10'));
|
||||
const julyMounts = content.mountInfo;
|
||||
expect(julyMounts['Chameleon-Base']).to.exist;
|
||||
expect(Object.keys(juneMounts).length, '').to.equal(Object.keys(julyMounts).length - 10);
|
||||
});
|
||||
|
||||
it('marks regular food as buyable and droppable without any events', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-06-20'));
|
||||
const { food } = content;
|
||||
Object.keys(food).forEach(key => {
|
||||
if (key === 'Saddle') {
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.be.true;
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.be.false;
|
||||
return;
|
||||
}
|
||||
let expected = true;
|
||||
if (key.startsWith('Cake_')) {
|
||||
expected = false;
|
||||
} else if (key.startsWith('Candy_')) {
|
||||
expected = false;
|
||||
} else if (key.startsWith('Pie_')) {
|
||||
expected = false;
|
||||
}
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.equal(expected);
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('marks candy as buyable and droppable during habitoween', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-10-31'));
|
||||
const { food } = content;
|
||||
Object.keys(food).forEach(key => {
|
||||
if (key === 'Saddle') {
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.be.true;
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.be.false;
|
||||
return;
|
||||
}
|
||||
let expected = false;
|
||||
if (key.startsWith('Cake_')) {
|
||||
expected = false;
|
||||
} else if (key.startsWith('Candy_')) {
|
||||
expected = true;
|
||||
} else if (key.startsWith('Pie_')) {
|
||||
expected = false;
|
||||
}
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.equal(expected);
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('marks cake as buyable and droppable during birthday', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-01-31'));
|
||||
const { food } = content;
|
||||
Object.keys(food).forEach(key => {
|
||||
if (key === 'Saddle') {
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.be.true;
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.be.false;
|
||||
return;
|
||||
}
|
||||
let expected = false;
|
||||
if (key.startsWith('Cake_')) {
|
||||
expected = true;
|
||||
} else if (key.startsWith('Candy_')) {
|
||||
expected = false;
|
||||
} else if (key.startsWith('Pie_')) {
|
||||
expected = false;
|
||||
}
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.equal(expected);
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('marks pie as buyable and droppable during pi day', () => {
|
||||
clock = sinon.useFakeTimers(new Date('2024-03-14'));
|
||||
const { food } = content;
|
||||
Object.keys(food).forEach(key => {
|
||||
if (key === 'Saddle') {
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.be.true;
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.be.false;
|
||||
return;
|
||||
}
|
||||
let expected = false;
|
||||
if (key.startsWith('Cake_')) {
|
||||
expected = false;
|
||||
} else if (key.startsWith('Candy_')) {
|
||||
expected = false;
|
||||
} else if (key.startsWith('Pie_')) {
|
||||
expected = true;
|
||||
}
|
||||
expect(food[key].canBuy(), `${key} canBuy`).to.equal(expected);
|
||||
expect(food[key].canDrop, `${key} canDrop`).to.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -6,7 +6,7 @@ import {
|
|||
} from '../helpers/content.helper';
|
||||
import t from '../../website/common/script/content/translation';
|
||||
|
||||
import * as stable from '../../website/common/script/content/stable';
|
||||
import stable from '../../website/common/script/content/stable';
|
||||
import eggs from '../../website/common/script/content/eggs';
|
||||
import potions from '../../website/common/script/content/hatching-potions';
|
||||
|
||||
|
|
|
|||
|
|
@ -320,7 +320,7 @@
|
|||
<script>
|
||||
import each from 'lodash/each';
|
||||
import * as quests from '@/../../common/script/content/quests';
|
||||
import { mountInfo, petInfo } from '@/../../common/script/content/stable';
|
||||
import stable from '@/../../common/script/content/stable';
|
||||
import content from '@/../../common/script/content';
|
||||
import gear from '@/../../common/script/content/gear';
|
||||
import styleHelper from '@/mixins/styleHelper';
|
||||
|
|
@ -330,6 +330,8 @@ import userLink from '../userLink';
|
|||
import PurchaseHistoryTable from '../ui/purchaseHistoryTable.vue';
|
||||
import { userStateMixin } from '../../mixins/userState';
|
||||
|
||||
const { mountInfo, petInfo } = stable;
|
||||
|
||||
export default {
|
||||
components: {
|
||||
userLink,
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@
|
|||
</style>
|
||||
|
||||
<script>
|
||||
import { mountInfo } from '@/../../common/script/content/stable';
|
||||
import stable from '@/../../common/script/content/stable';
|
||||
import markdownDirective from '@/directives/markdown';
|
||||
|
||||
export default {
|
||||
|
|
@ -105,7 +105,7 @@ export default {
|
|||
},
|
||||
methods: {
|
||||
openDialog (mountKey) {
|
||||
this.mount = mountInfo[mountKey];
|
||||
this.mount = stable.mountInfo[mountKey];
|
||||
this.$root.$emit('bv::show::modal', 'mount-raised-modal');
|
||||
},
|
||||
close () {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ import achievements from './achievements';
|
|||
|
||||
import eggs from './eggs';
|
||||
import hatchingPotions from './hatching-potions';
|
||||
import * as stable from './stable';
|
||||
import stable from './stable';
|
||||
import gear from './gear';
|
||||
import { quests, questsByLevel, userCanOwnQuestCategories } from './quests';
|
||||
|
||||
|
|
@ -38,6 +38,7 @@ import { REPEATING_EVENTS, getRepeatingEvents } from './constants/events';
|
|||
import loginIncentives from './loginIncentives';
|
||||
|
||||
import officialPinnedItems from './officialPinnedItems';
|
||||
import memoize from '../fns/datedMemoize';
|
||||
|
||||
const api = {};
|
||||
|
||||
|
|
@ -165,9 +166,15 @@ api.cardTypes = {
|
|||
|
||||
api.special = api.spells.special;
|
||||
|
||||
api.dropEggs = eggs.drops;
|
||||
api.questEggs = eggs.quests;
|
||||
api.eggs = eggs.all;
|
||||
Object.defineProperty(api, 'dropEggs', {
|
||||
get () { return eggs.drops; },
|
||||
});
|
||||
Object.defineProperty(api, 'questEggs', {
|
||||
get () { return eggs.quests; },
|
||||
});
|
||||
Object.defineProperty(api, 'eggs', {
|
||||
get () { return eggs.all; },
|
||||
});
|
||||
|
||||
api.timeTravelStable = {
|
||||
pets: {
|
||||
|
|
@ -186,25 +193,56 @@ api.timeTravelStable = {
|
|||
},
|
||||
};
|
||||
|
||||
api.dropHatchingPotions = hatchingPotions.drops;
|
||||
api.premiumHatchingPotions = hatchingPotions.premium;
|
||||
api.wackyHatchingPotions = hatchingPotions.wacky;
|
||||
api.hatchingPotions = hatchingPotions.all;
|
||||
Object.defineProperty(api, 'dropHatchingPotions', {
|
||||
get () { return hatchingPotions.drops; },
|
||||
});
|
||||
Object.defineProperty(api, 'premiumHatchingPotions', {
|
||||
get () { return hatchingPotions.premium; },
|
||||
});
|
||||
Object.defineProperty(api, 'wackyHatchingPotions', {
|
||||
get () { return hatchingPotions.wacky; },
|
||||
});
|
||||
Object.defineProperty(api, 'hatchingPotions', {
|
||||
get () { return hatchingPotions.all; },
|
||||
});
|
||||
|
||||
api.pets = stable.dropPets;
|
||||
api.premiumPets = stable.premiumPets;
|
||||
api.questPets = stable.questPets;
|
||||
api.specialPets = stable.specialPets;
|
||||
api.wackyPets = stable.wackyPets;
|
||||
api.petInfo = stable.petInfo;
|
||||
Object.defineProperty(api, 'dropPets', {
|
||||
get () { return stable.dropPets; },
|
||||
});
|
||||
Object.defineProperty(api, 'premiumPets', {
|
||||
get () { return stable.premiumPets; },
|
||||
});
|
||||
Object.defineProperty(api, 'questPets', {
|
||||
get () { return stable.questPets; },
|
||||
});
|
||||
Object.defineProperty(api, 'specialPets', {
|
||||
get () { return stable.specialPets; },
|
||||
});
|
||||
Object.defineProperty(api, 'wackyPets', {
|
||||
get () { return stable.wackyPets; },
|
||||
});
|
||||
Object.defineProperty(api, 'petInfo', {
|
||||
get () { return stable.petInfo; },
|
||||
});
|
||||
|
||||
api.mounts = stable.dropMounts;
|
||||
api.questMounts = stable.questMounts;
|
||||
api.premiumMounts = stable.premiumMounts;
|
||||
api.specialMounts = stable.specialMounts;
|
||||
api.mountInfo = stable.mountInfo;
|
||||
Object.defineProperty(api, 'dropMounts', {
|
||||
get () { return stable.dropMounts; },
|
||||
});
|
||||
Object.defineProperty(api, 'premiumMounts', {
|
||||
get () { return stable.premiumMounts; },
|
||||
});
|
||||
Object.defineProperty(api, 'questMounts', {
|
||||
get () { return stable.questMounts; },
|
||||
});
|
||||
Object.defineProperty(api, 'specialMounts', {
|
||||
get () { return stable.specialMounts; },
|
||||
});
|
||||
Object.defineProperty(api, 'mountInfo', {
|
||||
get () { return stable.mountInfo; },
|
||||
});
|
||||
|
||||
api.food = {
|
||||
function buildFood() {
|
||||
const food = {
|
||||
Meat: {
|
||||
text: t('foodMeat'),
|
||||
textA: t('foodMeatA'),
|
||||
|
|
@ -270,6 +308,7 @@ api.food = {
|
|||
text: t('foodSaddleText'),
|
||||
value: 5,
|
||||
notes: t('foodSaddleNotes'),
|
||||
canBuy: () => true,
|
||||
canDrop: false,
|
||||
},
|
||||
/* eslint-disable camelcase */
|
||||
|
|
@ -454,15 +493,15 @@ api.food = {
|
|||
target: 'Red',
|
||||
},
|
||||
/* eslint-enable camelcase */
|
||||
};
|
||||
};
|
||||
|
||||
let FOOD_SEASON = 'Normal';
|
||||
getRepeatingEvents(moment()).forEach(event => {
|
||||
let FOOD_SEASON = 'Normal';
|
||||
getRepeatingEvents(moment()).forEach(event => {
|
||||
if (event.foodSeason) {
|
||||
FOOD_SEASON = event.foodSeason;
|
||||
}
|
||||
});
|
||||
each(api.food, (food, key) => {
|
||||
});
|
||||
each(food, (foodItem, key) => {
|
||||
let foodType = 'Normal';
|
||||
if (key.startsWith('Cake_')) {
|
||||
foodType = 'Cake';
|
||||
|
|
@ -471,13 +510,22 @@ each(api.food, (food, key) => {
|
|||
} else if (key.startsWith('Pie_')) {
|
||||
foodType = 'Pie';
|
||||
}
|
||||
defaults(food, {
|
||||
defaults(foodItem, {
|
||||
value: 1,
|
||||
key,
|
||||
notes: t('foodNotes'),
|
||||
canBuy: () => FOOD_SEASON === foodType,
|
||||
canDrop: FOOD_SEASON === foodType,
|
||||
});
|
||||
});
|
||||
|
||||
return food;
|
||||
}
|
||||
|
||||
const memoizedBuildFood = memoize(buildFood);
|
||||
|
||||
Object.defineProperty(api, 'food', {
|
||||
get () { return memoizedBuildFood(); },
|
||||
});
|
||||
|
||||
api.appearances = appearances;
|
||||
|
|
|
|||
|
|
@ -4,11 +4,9 @@ import { EVENTS } from './constants/events';
|
|||
import allEggs from './eggs';
|
||||
import allPotions from './hatching-potions';
|
||||
import t from './translation';
|
||||
import memoize from '../fns/datedMemoize';
|
||||
|
||||
const petInfo = {};
|
||||
const mountInfo = {};
|
||||
|
||||
function constructSet (type, eggs, potions, hasMounts = true) {
|
||||
function constructSet (type, eggs, potions, petInfo, mountInfo, hasMounts = true) {
|
||||
const pets = {};
|
||||
const mounts = {};
|
||||
|
||||
|
|
@ -123,11 +121,6 @@ const canFindSpecial = {
|
|||
},
|
||||
};
|
||||
|
||||
const [dropPets, dropMounts] = constructSet('drop', allEggs.drops, allPotions.drops);
|
||||
const [premiumPets, premiumMounts] = constructSet('premium', allEggs.drops, allPotions.premium);
|
||||
const [questPets, questMounts] = constructSet('quest', allEggs.quests, allPotions.drops);
|
||||
const wackyPets = constructSet('wacky', allEggs.drops, allPotions.wacky, false);
|
||||
|
||||
const specialPets = {
|
||||
'Wolf-Veteran': 'veteranWolf',
|
||||
'Wolf-Cerberus': 'cerberusPup',
|
||||
|
|
@ -178,16 +171,25 @@ const specialMounts = {
|
|||
'JackOLantern-RoyalPurple': 'royalPurpleJackolantern',
|
||||
};
|
||||
|
||||
each(specialPets, (translationString, key) => {
|
||||
function buildInfo () {
|
||||
const petInfo = {};
|
||||
const mountInfo = {};
|
||||
|
||||
const [dropPets, dropMounts] = constructSet('drop', allEggs.drops, allPotions.drops, petInfo, mountInfo);
|
||||
const [premiumPets, premiumMounts] = constructSet('premium', allEggs.drops, allPotions.premium, petInfo, mountInfo);
|
||||
const [questPets, questMounts] = constructSet('quest', allEggs.quests, allPotions.drops, petInfo, mountInfo);
|
||||
const wackyPets = constructSet('wacky', allEggs.drops, allPotions.wacky, petInfo, mountInfo, false);
|
||||
|
||||
each(specialPets, (translationString, key) => {
|
||||
petInfo[key] = {
|
||||
key,
|
||||
type: 'special',
|
||||
text: t(translationString),
|
||||
canFind: canFindSpecial.pets[key],
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
Object.assign(petInfo['Gryphatrice-Jubilant'], {
|
||||
Object.assign(petInfo['Gryphatrice-Jubilant'], {
|
||||
canBuy () {
|
||||
return moment().isBetween(EVENTS.birthday10.start, EVENTS.birthday10.end);
|
||||
},
|
||||
|
|
@ -195,18 +197,18 @@ Object.assign(petInfo['Gryphatrice-Jubilant'], {
|
|||
event: 'birthday10',
|
||||
value: 60,
|
||||
purchaseType: 'pets',
|
||||
});
|
||||
});
|
||||
|
||||
each(specialMounts, (translationString, key) => {
|
||||
each(specialMounts, (translationString, key) => {
|
||||
mountInfo[key] = {
|
||||
key,
|
||||
type: 'special',
|
||||
text: t(translationString),
|
||||
canFind: canFindSpecial.mounts[key],
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
export {
|
||||
return {
|
||||
dropPets,
|
||||
premiumPets,
|
||||
questPets,
|
||||
|
|
@ -218,4 +220,39 @@ export {
|
|||
specialMounts,
|
||||
petInfo,
|
||||
mountInfo,
|
||||
};
|
||||
}
|
||||
|
||||
const memoizedBuildInfo = memoize(buildInfo);
|
||||
|
||||
export default {
|
||||
get dropPets () {
|
||||
return memoizedBuildInfo().dropPets;
|
||||
},
|
||||
get premiumPets () {
|
||||
return memoizedBuildInfo().premiumPets;
|
||||
},
|
||||
get questPets () {
|
||||
return memoizedBuildInfo().questPets;
|
||||
},
|
||||
get wackyPets () {
|
||||
return memoizedBuildInfo().wackyPets;
|
||||
},
|
||||
get dropMounts () {
|
||||
return memoizedBuildInfo().dropMounts;
|
||||
},
|
||||
get questMounts () {
|
||||
return memoizedBuildInfo().questMounts;
|
||||
},
|
||||
get premiumMounts () {
|
||||
return memoizedBuildInfo().premiumMounts;
|
||||
},
|
||||
get petInfo () {
|
||||
return memoizedBuildInfo().petInfo;
|
||||
},
|
||||
get mountInfo () {
|
||||
return memoizedBuildInfo().mountInfo;
|
||||
},
|
||||
specialPets,
|
||||
specialMounts,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue