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

Optimize missing-collectible-info #505

Merged
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
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
Loading