0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 09:50:43 -05:00
server/project/src/services/ItemBaseClassService.ts
DrakiaXYZ 8e91842d4c 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: SPT-AKI/Server#281
Co-authored-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
Co-committed-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
2024-04-08 17:40:04 +00:00

137 lines
4.3 KiB
TypeScript

import { inject, injectable } from "tsyringe";
import { ITemplateItem } from "@spt-aki/models/eft/common/tables/ITemplateItem";
import { BaseClasses } from "@spt-aki/models/enums/BaseClasses";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
/**
* Cache the baseids for each item in the tiems db inside a dictionary
*/
@injectable()
export class ItemBaseClassService
{
protected itemBaseClassesCache: Record<string, string[]> = {};
protected items: Record<string, ITemplateItem>;
protected cacheGenerated = false;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
)
{}
/**
* Create cache and store inside ItemBaseClassService
* Store a dict of an items tpl to the base classes it and its parents have
*/
public hydrateItemBaseClassCache(): void
{
// Clear existing cache
this.itemBaseClassesCache = {};
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(this.items).filter((x) => x._type === "Item");
for (const item of filteredDbItems)
{
const itemIdToUpdate = item._id;
if (!this.itemBaseClassesCache[item._id])
{
this.itemBaseClassesCache[item._id] = [];
}
this.addBaseItems(itemIdToUpdate, item);
}
this.cacheGenerated = true;
}
/**
* 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
*/
protected addBaseItems(itemIdToUpdate: string, item: ITemplateItem): void
{
this.itemBaseClassesCache[itemIdToUpdate].push(item._parent);
const parent = this.items[item._parent];
if (parent._parent !== "")
{
this.addBaseItems(itemIdToUpdate, parent);
}
}
/**
* Does item tpl inherit from the requested base class
* @param itemTpl item to check base classes of
* @param baseClass base class to check for
* @returns true if item inherits from base class passed in
*/
public itemHasBaseClass(itemTpl: string, baseClasses: string[]): boolean
{
if (!this.cacheGenerated)
{
this.hydrateItemBaseClassCache();
}
if (typeof itemTpl === "undefined")
{
this.logger.warning("Unable to check itemTpl base class as its undefined");
return false;
}
// 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;
}
// No item in cache
if (!this.itemBaseClassesCache[itemTpl])
{
// Hydrate again
this.logger.debug(this.localisationService.getText("baseclass-item_not_found", itemTpl));
this.hydrateItemBaseClassCache();
// Check for item again, throw exception if not found
if (!this.itemBaseClassesCache[itemTpl])
{
throw new Error(this.localisationService.getText("baseclass-item_not_found_failed", itemTpl));
}
}
return this.itemBaseClassesCache[itemTpl].some((x) => baseClasses.includes(x));
}
/**
* Get base classes item inherits from
* @param itemTpl item to get base classes for
* @returns array of base classes
*/
public getItemBaseClasses(itemTpl: string): string[]
{
if (!this.cacheGenerated)
{
this.hydrateItemBaseClassCache();
}
if (!this.itemBaseClassesCache[itemTpl])
{
return [];
}
return this.itemBaseClassesCache[itemTpl];
}
}