Skip to content

Commit

Permalink
Optimize missing-collectible-info
Browse files Browse the repository at this point in the history
  • Loading branch information
robojumper committed Oct 10, 2023
1 parent 66e318f commit a02f4d3
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions src/generate-missing-collectible-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getAllDefs, loadLocal } from '@d2api/manifest-node';
import { getAllDefs, getDef, loadLocal } from '@d2api/manifest-node';
import { DestinyInventoryItemDefinition } from 'bungie-api-ts/destiny2';
import stringifyObject from 'stringify-object';
import _categories from '../data/sources/categories.json' assert { type: 'json' };
import {
Expand All @@ -20,7 +21,12 @@ const collectibles = getAllDefs('Collectible');
const hashToMissingCollectibleHash: Record<string, number> = {};

// Every Inventory Item without a collectible hash
const nonCollectibleItems = inventoryItems.filter((item) => !item.collectibleHash);
const nonCollectibleItemsByName: { [name: string]: DestinyInventoryItemDefinition[] } = {};
for (const item of inventoryItems) {
if (item.displayProperties.name && !item.collectibleHash) {
(nonCollectibleItemsByName[item.displayProperties.name] ??= []).push(item);
}
}

// Every Inventory Item with a collectible hash
const collectibleItems = inventoryItems.filter((item) => item.collectibleHash);
Expand All @@ -29,21 +35,20 @@ collectibleItems.forEach((collectibleItem) => {
if (!collectibleItem.displayProperties?.name) {
return;
}
const itemsWithSameName = nonCollectibleItems.filter(
(nonCollectibleItem) =>
collectibleItem.displayProperties.name === nonCollectibleItem.displayProperties.name &&
stringifySortCompare(
collectibleItem.itemCategoryHashes ?? [],
nonCollectibleItem.itemCategoryHashes ?? []
)
);
const itemsWithSameName =
nonCollectibleItemsByName[collectibleItem.displayProperties.name]?.filter(
(nonCollectibleItem) =>
stringifySortCompare(
collectibleItem.itemCategoryHashes ?? [],
nonCollectibleItem.itemCategoryHashes ?? []
)
) ?? [];

itemsWithSameName.forEach((nonCollectibleItem) => {
collectibles.filter((collectible) => {
if (collectibleItem.collectibleHash === collectible.hash && collectible.sourceHash) {
hashToMissingCollectibleHash[nonCollectibleItem.hash] = collectible.sourceHash;
}
});
const collectibleDef = getDef('Collectible', collectibleItem.collectibleHash);
if (collectibleDef?.sourceHash) {
hashToMissingCollectibleHash[nonCollectibleItem.hash] = collectibleDef.sourceHash;
}
});
});

Expand All @@ -69,18 +74,23 @@ Object.entries(categories.sources).forEach(([sourceTag, matchRule]) => {
if (!D2Sources[sourceTag].length && !sourceTag.includes('shatteredthrone')) {
console.log(`no matching sources for: ${matchRule}`);
}
});

Object.entries(hashToMissingCollectibleHash).forEach(([hash, sourceHash]) => {
Object.entries(D2Sources).forEach(([sourceTag, sourceHashes]) => {
if (sourceHashes.includes(Number(sourceHash))) {
newSourceInfo[sourceTag] = newSourceInfo[sourceTag] ?? [];
newSourceInfo[sourceTag].push(Number(hash));
}
newSourceInfo[sourceTag] = uniqAndSortArray(newSourceInfo[sourceTag]);
});
Object.entries(hashToMissingCollectibleHash).forEach(([hash, sourceHash]) => {
Object.entries(D2Sources).forEach(([sourceTag, sourceHashes]) => {
if (sourceHashes.includes(Number(sourceHash))) {
newSourceInfo[sourceTag] = newSourceInfo[sourceTag] ?? [];
newSourceInfo[sourceTag].push(Number(hash));
}
newSourceInfo[sourceTag] = uniqAndSortArray(newSourceInfo[sourceTag]);
});
});

// lastly add aliases and copy info
// lastly add aliases and copy info
Object.keys(categories.sources).forEach((sourceTag) => {
if (sourceTag === 'ignore') {
return;
}
const aliases = categories.sources[sourceTag].alias;
if (aliases) {
aliases.forEach((alias) => (newSourceInfo[alias] = newSourceInfo[sourceTag]));
Expand Down

0 comments on commit a02f4d3

Please sign in to comment.