Skip to content

Commit

Permalink
Add items with daily limits into dailyCooldowns
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecmelt committed Nov 15, 2024
1 parent ebcafc5 commit 22107f5
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/helpers/dailyCooldowns.ts
Original file line number Diff line number Diff line change
@@ -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<string, number>
export type MatchingItemWithoutLimit = Omit<DailyLimitedItem, 'dailyLimit'>

// Get a list of daily cooldowns used in the recipe
export function dailyCooldowns(
Expand All @@ -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)

Check warning on line 29 in src/helpers/dailyCooldowns.ts

View workflow job for this annotation

GitHub Actions / Test & build

'dailyLimit' is defined but never used
)
) {
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
})
}
7 changes: 7 additions & 0 deletions src/static/dailyLimitedItems.ts
Original file line number Diff line number Diff line change
@@ -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
]
153 changes: 153 additions & 0 deletions tests/helpers/dailyCooldowns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: [],
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: [],
},
Expand Down Expand Up @@ -275,6 +427,7 @@ describe('helpers > dailyCooldowns', () => {
expect(dailyCooldowns(tree)).toEqual({
46740: 3,
66913: 4,
92272: 50,
})
})
})

0 comments on commit 22107f5

Please sign in to comment.