Resolve ItemBaseClassService cache error when iterating all items (!281)

The ItemBaseClassCache is only generated for items with a _type of 'Items', so before trying to look up an item in itemHasBaseClass, make sure the item also has a _type of 'Item'
Move `allDbItems` to a class variable since we need it for looking up the _type of the given template Id

This shouldn't require any changes to mods

Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/281
Co-authored-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
Co-committed-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
This commit is contained in:
DrakiaXYZ 2024-04-08 17:40:04 +00:00 committed by chomp
parent 2404e7a5ab
commit 8e91842d4c

View File

@ -13,6 +13,7 @@ import { LocalisationService } from "@spt-aki/services/LocalisationService";
export class ItemBaseClassService
{
protected itemBaseClassesCache: Record<string, string[]> = {};
protected items: Record<string, ITemplateItem>;
protected cacheGenerated = false;
constructor(
@ -31,15 +32,15 @@ export class ItemBaseClassService
// Clear existing cache
this.itemBaseClassesCache = {};
const allDbItems = this.databaseServer.getTables().templates.items;
if (!allDbItems)
this.items = this.databaseServer.getTables().templates.items;
if (!this.items)
{
this.logger.warning(this.localisationService.getText("baseclass-missing_db_no_cache"));
return;
}
const filteredDbItems = Object.values(allDbItems).filter((x) => x._type === "Item");
const filteredDbItems = Object.values(this.items).filter((x) => x._type === "Item");
for (const item of filteredDbItems)
{
const itemIdToUpdate = item._id;
@ -48,7 +49,7 @@ export class ItemBaseClassService
this.itemBaseClassesCache[item._id] = [];
}
this.addBaseItems(itemIdToUpdate, item, allDbItems);
this.addBaseItems(itemIdToUpdate, item);
}
this.cacheGenerated = true;
@ -58,16 +59,15 @@ export class ItemBaseClassService
* Helper method, recursivly iterate through items parent items, finding and adding ids to dictionary
* @param itemIdToUpdate item tpl to store base ids against in dictionary
* @param item item being checked
* @param allDbItems all items in db
*/
protected addBaseItems(itemIdToUpdate: string, item: ITemplateItem, allDbItems: Record<string, ITemplateItem>): void
protected addBaseItems(itemIdToUpdate: string, item: ITemplateItem): void
{
this.itemBaseClassesCache[itemIdToUpdate].push(item._parent);
const parent = allDbItems[item._parent];
const parent = this.items[item._parent];
if (parent._parent !== "")
{
this.addBaseItems(itemIdToUpdate, parent, allDbItems);
this.addBaseItems(itemIdToUpdate, parent);
}
}
@ -91,8 +91,8 @@ export class ItemBaseClassService
return false;
}
// Edge case - this is the 'root' item that all other items inherit from
if (itemTpl === BaseClasses.ITEM)
// Edge case, the cache is only generated for items with `_type === "Item"`, so return false for any other type
if (this.items[itemTpl]?._type !== "Item")
{
return false;
}