2023-03-03 15:23:46 +00:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
2023-12-05 20:41:43 +00:00
|
|
|
import { Category } from "@spt-aki/models/eft/common/tables/IHandbookBase";
|
2024-02-05 19:52:46 +00:00
|
|
|
import { Item } from "@spt-aki/models/eft/common/tables/IItem";
|
2024-02-06 12:16:26 +00:00
|
|
|
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
|
2023-10-19 17:21:17 +00:00
|
|
|
import { Money } from "@spt-aki/models/enums/Money";
|
2024-02-06 12:16:26 +00:00
|
|
|
import { IItemConfig } from "@spt-aki/models/spt/config/IItemConfig";
|
|
|
|
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
|
2023-10-19 17:21:17 +00:00
|
|
|
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
|
2023-11-20 13:47:47 +00:00
|
|
|
import { JsonUtil } from "@spt-aki/utils/JsonUtil";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2023-04-22 19:33:47 +01:00
|
|
|
class LookupItem<T, I>
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
readonly byId: Map<string, T>;
|
|
|
|
readonly byParent: Map<string, I[]>;
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
this.byId = new Map();
|
|
|
|
this.byParent = new Map();
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LookupCollection
|
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
readonly items: LookupItem<number, string>;
|
|
|
|
readonly categories: LookupItem<string, string>;
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
this.items = new LookupItem<number, string>();
|
|
|
|
this.categories = new LookupItem<string, string>();
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class HandbookHelper
|
2023-11-16 21:42:06 +00:00
|
|
|
{
|
2024-02-06 12:16:26 +00:00
|
|
|
protected itemConfig: IItemConfig;
|
2023-03-03 15:23:46 +00:00
|
|
|
protected lookupCacheGenerated = false;
|
|
|
|
protected handbookPriceCache = new LookupCollection();
|
|
|
|
|
2023-11-20 13:47:47 +00:00
|
|
|
constructor(
|
|
|
|
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
|
2024-02-02 13:54:07 -05:00
|
|
|
@inject("JsonUtil") protected jsonUtil: JsonUtil,
|
2024-02-06 12:16:26 +00:00
|
|
|
@inject("ConfigServer") protected configServer: ConfigServer,
|
2023-11-20 13:47:47 +00:00
|
|
|
)
|
2024-02-06 12:16:26 +00:00
|
|
|
{
|
|
|
|
this.itemConfig = this.configServer.getConfig(ConfigTypes.ITEM);
|
|
|
|
}
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2023-07-24 15:52:55 +01:00
|
|
|
/**
|
|
|
|
* Create an in-memory cache of all items with associated handbook price in handbookPriceCache class
|
|
|
|
*/
|
2023-03-03 15:23:46 +00:00
|
|
|
public hydrateLookup(): void
|
|
|
|
{
|
2024-02-06 12:52:56 +00:00
|
|
|
// Add handbook overrides found in items.json config into db
|
|
|
|
for (const itemTpl in this.itemConfig.handbookPriceOverride)
|
|
|
|
{
|
|
|
|
let itemToUpdate = this.databaseServer.getTables().templates.handbook.Items.find((item) =>
|
|
|
|
item.Id === itemTpl
|
|
|
|
);
|
|
|
|
if (!itemToUpdate)
|
|
|
|
{
|
|
|
|
this.databaseServer.getTables().templates.handbook.Items.push({
|
|
|
|
Id: itemTpl,
|
|
|
|
ParentId: this.databaseServer.getTables().templates.items[itemTpl]._parent,
|
|
|
|
Price: this.itemConfig.handbookPriceOverride[itemTpl],
|
|
|
|
});
|
|
|
|
itemToUpdate = this.databaseServer.getTables().templates.handbook.Items.find((item) =>
|
|
|
|
item.Id === itemTpl
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
itemToUpdate.Price = this.itemConfig.handbookPriceOverride[itemTpl];
|
|
|
|
}
|
|
|
|
|
|
|
|
const handbookDbClone = this.jsonUtil.clone(this.databaseServer.getTables().templates.handbook);
|
|
|
|
for (const handbookItem of handbookDbClone.Items)
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
this.handbookPriceCache.items.byId.set(handbookItem.Id, handbookItem.Price);
|
|
|
|
if (!this.handbookPriceCache.items.byParent.has(handbookItem.ParentId))
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
this.handbookPriceCache.items.byParent.set(handbookItem.ParentId, []);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
2023-11-16 21:42:06 +00:00
|
|
|
this.handbookPriceCache.items.byParent.get(handbookItem.ParentId).push(handbookItem.Id);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 12:52:56 +00:00
|
|
|
for (const handbookCategory of handbookDbClone.Categories)
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
this.handbookPriceCache.categories.byId.set(handbookCategory.Id, handbookCategory.ParentId || null);
|
2023-03-03 15:23:46 +00:00
|
|
|
if (handbookCategory.ParentId)
|
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
if (!this.handbookPriceCache.categories.byParent.has(handbookCategory.ParentId))
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
this.handbookPriceCache.categories.byParent.set(handbookCategory.ParentId, []);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
2023-11-16 21:42:06 +00:00
|
|
|
this.handbookPriceCache.categories.byParent.get(handbookCategory.ParentId).push(handbookCategory.Id);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get price from internal cache, if cache empty look up price directly in handbook (expensive)
|
|
|
|
* If no values found, return 1
|
|
|
|
* @param tpl item tpl to look up price for
|
|
|
|
* @returns price in roubles
|
|
|
|
*/
|
|
|
|
public getTemplatePrice(tpl: string): number
|
|
|
|
{
|
|
|
|
if (!this.lookupCacheGenerated)
|
|
|
|
{
|
|
|
|
this.hydrateLookup();
|
|
|
|
this.lookupCacheGenerated = true;
|
|
|
|
}
|
|
|
|
|
2023-04-22 19:33:47 +01:00
|
|
|
if (this.handbookPriceCache.items.byId.has(tpl))
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-04-23 11:13:53 +01:00
|
|
|
return this.handbookPriceCache.items.byId.get(tpl);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 21:42:06 +00:00
|
|
|
const handbookItem = this.databaseServer.getTables().templates.handbook.Items.find((x) => x.Id === tpl);
|
2023-10-10 11:03:20 +00:00
|
|
|
if (!handbookItem)
|
|
|
|
{
|
2023-11-07 21:21:34 +00:00
|
|
|
const newValue = 0;
|
|
|
|
this.handbookPriceCache.items.byId.set(tpl, newValue);
|
2023-10-10 11:03:20 +00:00
|
|
|
|
2023-11-07 21:21:34 +00:00
|
|
|
return newValue;
|
2023-10-10 11:03:20 +00:00
|
|
|
}
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2023-10-10 11:03:20 +00:00
|
|
|
return handbookItem.Price;
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 19:52:46 +00:00
|
|
|
public getTemplatePriceForItems(items: Item[]): number
|
|
|
|
{
|
|
|
|
let total = 0;
|
|
|
|
for (const item of items)
|
|
|
|
{
|
|
|
|
total += this.getTemplatePrice(item._tpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:23:46 +00:00
|
|
|
/**
|
2023-07-24 15:52:55 +01:00
|
|
|
* Get all items in template with the given parent category
|
2023-11-16 21:42:06 +00:00
|
|
|
* @param parentId
|
2023-03-03 15:23:46 +00:00
|
|
|
* @returns string array
|
|
|
|
*/
|
2023-07-24 15:52:55 +01:00
|
|
|
public templatesWithParent(parentId: string): string[]
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-07-24 15:52:55 +01:00
|
|
|
return this.handbookPriceCache.items.byParent.get(parentId) ?? [];
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does category exist in handbook cache
|
2023-11-16 21:42:06 +00:00
|
|
|
* @param category
|
2023-03-03 15:23:46 +00:00
|
|
|
* @returns true if exists in cache
|
|
|
|
*/
|
|
|
|
public isCategory(category: string): boolean
|
|
|
|
{
|
2023-04-22 19:33:47 +01:00
|
|
|
return this.handbookPriceCache.categories.byId.has(category);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 15:52:55 +01:00
|
|
|
/**
|
|
|
|
* Get all items associated with a categories parent
|
2023-11-16 21:42:06 +00:00
|
|
|
* @param categoryParent
|
2023-07-24 15:52:55 +01:00
|
|
|
* @returns string array
|
|
|
|
*/
|
|
|
|
public childrenCategories(categoryParent: string): string[]
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-07-24 15:52:55 +01:00
|
|
|
return this.handbookPriceCache.categories.byParent.get(categoryParent) ?? [];
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert non-roubles into roubles
|
|
|
|
* @param nonRoubleCurrencyCount Currency count to convert
|
|
|
|
* @param currencyTypeFrom What current currency is
|
|
|
|
* @returns Count in roubles
|
|
|
|
*/
|
|
|
|
public inRUB(nonRoubleCurrencyCount: number, currencyTypeFrom: string): number
|
|
|
|
{
|
|
|
|
if (currencyTypeFrom === Money.ROUBLES)
|
|
|
|
{
|
|
|
|
return nonRoubleCurrencyCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.round(nonRoubleCurrencyCount * (this.getTemplatePrice(currencyTypeFrom) || 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert roubles into another currency
|
|
|
|
* @param roubleCurrencyCount roubles to convert
|
|
|
|
* @param currencyTypeTo Currency to convert roubles into
|
|
|
|
* @returns currency count in desired type
|
|
|
|
*/
|
|
|
|
public fromRUB(roubleCurrencyCount: number, currencyTypeTo: string): number
|
|
|
|
{
|
|
|
|
if (currencyTypeTo === Money.ROUBLES)
|
|
|
|
{
|
|
|
|
return roubleCurrencyCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get price of currency from handbook
|
|
|
|
const price = this.getTemplatePrice(currencyTypeTo);
|
|
|
|
return price ? Math.round(roubleCurrencyCount / price) : 0;
|
|
|
|
}
|
2023-12-05 20:41:43 +00:00
|
|
|
|
|
|
|
public getCategoryById(handbookId: string): Category
|
|
|
|
{
|
2024-02-02 13:54:07 -05:00
|
|
|
return this.databaseServer.getTables().templates.handbook.Categories.find((x) => x.Id === handbookId);
|
2023-12-05 20:41:43 +00:00
|
|
|
}
|
2023-11-16 21:42:06 +00:00
|
|
|
}
|