Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add items with daily limits into dailyCooldowns #29

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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,
})
})
})
Loading