From 22107f51c0163685b48bd3473e1fdae9bca1fc2e Mon Sep 17 00:00:00 2001 From: Ecmelt Date: Fri, 15 Nov 2024 11:40:58 +0000 Subject: [PATCH] Add items with daily limits into dailyCooldowns --- src/helpers/dailyCooldowns.ts | 42 ++++++++ src/static/dailyLimitedItems.ts | 7 ++ tests/helpers/dailyCooldowns.spec.ts | 153 +++++++++++++++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 src/static/dailyLimitedItems.ts diff --git a/src/helpers/dailyCooldowns.ts b/src/helpers/dailyCooldowns.ts index 7df6c53..c630797 100755 --- a/src/helpers/dailyCooldowns.ts +++ b/src/helpers/dailyCooldowns.ts @@ -1,9 +1,12 @@ import { DAILY_COOLDOWNS } from '../static/dailyCooldowns' +import { DAILY_LIMITED_ITEMS, DailyLimitedItem } from '../static/dailyLimitedItems' import { RecipeTreeWithCraftFlags } from '../types' const dailyCooldownIds = DAILY_COOLDOWNS.filter((x) => x.craftInterval === 'daily').map((x) => x.id) +const dailyLimitedItems = DAILY_LIMITED_ITEMS export type DailyCooldownsBreakdown = Record +export type MatchingItemWithoutLimit = Omit // Get a list of daily cooldowns used in the recipe export function dailyCooldowns( @@ -18,6 +21,45 @@ export function dailyCooldowns( breakdown[tree.id] = (breakdown[tree.id] || 0) + tree.usedQuantity } + const matchingItem = dailyLimitedItems.find((item) => item.id === tree.id) + if ( + matchingItem && + matchesItemProperties( + tree, + (({ dailyLimit, ...matchingItemWithoutLimit }) => matchingItemWithoutLimit)(matchingItem) + ) + ) { + breakdown[tree.id] = (breakdown[tree.id] || 0) + tree.usedQuantity + } + tree.components.map((component) => dailyCooldowns(component, breakdown)) return breakdown } + +export function matchesItemProperties( + tree: RecipeTreeWithCraftFlags, + matchingItemWithoutLimit: MatchingItemWithoutLimit +): boolean { + return Object.entries(matchingItemWithoutLimit).every(([key, value]) => { + if (!(key in tree)) { + return false + } + + const treeValue = tree[key as keyof RecipeTreeWithCraftFlags] as unknown + + if (typeof value === 'object' && !Array.isArray(value) && typeof treeValue === 'object') { + return matchesItemProperties(treeValue as RecipeTreeWithCraftFlags, value) + } + + if (Array.isArray(value)) { + if (!Array.isArray(treeValue)) { + return false + } + return value.every((nestedObject, index) => + matchesItemProperties(treeValue[index], nestedObject as MatchingItemWithoutLimit) + ) + } + + return treeValue === value + }) +} diff --git a/src/static/dailyLimitedItems.ts b/src/static/dailyLimitedItems.ts new file mode 100644 index 0000000..a6a0808 --- /dev/null +++ b/src/static/dailyLimitedItems.ts @@ -0,0 +1,7 @@ +export type DailyLimitedItem = { id: number; dailyLimit: number } & Partial<{ + components: { id: number; type: string; quantity: number }[] +}> + +export const DAILY_LIMITED_ITEMS: DailyLimitedItem[] = [ + { id: 92272, components: [{ id: 2, type: 'Currency', quantity: 2668 }], dailyLimit: 10 }, // Eternal Ice Shard +] diff --git a/tests/helpers/dailyCooldowns.spec.ts b/tests/helpers/dailyCooldowns.spec.ts index 9b05630..0f5e466 100755 --- a/tests/helpers/dailyCooldowns.spec.ts +++ b/tests/helpers/dailyCooldowns.spec.ts @@ -20,6 +20,44 @@ describe('helpers > dailyCooldowns', () => { craftResultPrice: 1, multipleRecipeCount: 1, components: [ + { + craft: true, + craftDecisionPrice: 66700, + id: 92272, + totalQuantity: 25, + usedQuantity: 25, + quantity: 25, + type: 'Recipe', + output: 1, + min_rating: null, + disciplines: ['Merchant'], + buyPriceEach: false, + buyPrice: false, + decisionPrice: 66700, + craftResultPrice: 0, + prerequisites: [], + multipleRecipeCount: 2, + components: [ + { + craft: false, + craftDecisionPrice: 66700, + id: 2, + totalQuantity: 66700, + usedQuantity: 66700, + quantity: 2668, + type: 'Currency', + output: 1, + min_rating: null, + disciplines: [], + buyPriceEach: false, + buyPrice: false, + decisionPrice: 66700, + craftResultPrice: false, + prerequisites: [], + multipleRecipeCount: 1, + }, + ], + }, { craft: false, craftDecisionPrice: 1, @@ -127,6 +165,44 @@ describe('helpers > dailyCooldowns', () => { prerequisites: [], multipleRecipeCount: 1, }, + { + craft: true, + craftDecisionPrice: 66700, + id: 92272, + totalQuantity: 25, + usedQuantity: 25, + quantity: 25, + type: 'Recipe', + output: 1, + min_rating: null, + disciplines: ['Merchant'], + buyPriceEach: false, + buyPrice: false, + decisionPrice: 66700, + craftResultPrice: 0, + prerequisites: [], + multipleRecipeCount: 2, + components: [ + { + craft: false, + craftDecisionPrice: 66700, + id: 2, + totalQuantity: 66700, + usedQuantity: 66700, + quantity: 2668, + type: 'Currency', + output: 1, + min_rating: null, + disciplines: [], + buyPriceEach: false, + buyPrice: false, + decisionPrice: 66700, + craftResultPrice: false, + prerequisites: [], + multipleRecipeCount: 1, + }, + ], + }, ], prerequisites: [], }, @@ -170,6 +246,44 @@ describe('helpers > dailyCooldowns', () => { prerequisites: [], multipleRecipeCount: 1, }, + { + craft: false, + craftDecisionPrice: 0, + id: 92272, + totalQuantity: 25, + usedQuantity: 0, + quantity: 25, + type: 'Recipe', + output: 1, + min_rating: null, + disciplines: ['Merchant'], + buyPriceEach: false, + buyPrice: false, + decisionPrice: false, + craftResultPrice: false, + prerequisites: [], + multipleRecipeCount: 2, + components: [ + { + craft: false, + craftDecisionPrice: 0, + id: 2, + totalQuantity: 0, + usedQuantity: 0, + quantity: 2668, + type: 'Currency', + output: 1, + min_rating: null, + disciplines: [], + buyPriceEach: false, + buyPrice: false, + decisionPrice: 0, + craftResultPrice: false, + prerequisites: [], + multipleRecipeCount: 1, + }, + ], + }, ], prerequisites: [], multipleRecipeCount: 1, @@ -226,6 +340,44 @@ describe('helpers > dailyCooldowns', () => { prerequisites: [], multipleRecipeCount: 1, }, + { + craft: true, + craftDecisionPrice: 66700, + id: 92272, + totalQuantity: 25, + usedQuantity: 25, + quantity: 25, + type: 'Recipe', + output: 1, + min_rating: null, + disciplines: ['Merchant'], + buyPriceEach: false, + buyPrice: false, + decisionPrice: 66700, + craftResultPrice: 0, + prerequisites: [], + multipleRecipeCount: 2, + components: [ + { + craft: false, + craftDecisionPrice: 66700, + id: 2, + totalQuantity: 66700, + usedQuantity: 66700, + quantity: 2750, + type: 'Currency', + output: 1, + min_rating: null, + disciplines: [], + buyPriceEach: false, + buyPrice: false, + decisionPrice: 66700, + craftResultPrice: false, + prerequisites: [], + multipleRecipeCount: 1, + }, + ], + }, ], prerequisites: [], }, @@ -275,6 +427,7 @@ describe('helpers > dailyCooldowns', () => { expect(dailyCooldowns(tree)).toEqual({ 46740: 3, 66913: 4, + 92272: 50, }) }) })