2023-03-03 16:23:46 +01:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
2023-10-19 19:21:17 +02:00
|
|
|
import { Money } from "@spt-aki/models/enums/Money";
|
|
|
|
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-04-22 20:33:47 +02:00
|
|
|
class LookupItem<T, I>
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
readonly byId: Map<string, T>;
|
|
|
|
readonly byParent: Map<string, I[]>;
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
this.byId = new Map();
|
|
|
|
this.byParent = new Map();
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LookupCollection
|
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
readonly items: LookupItem<number, string>;
|
|
|
|
readonly categories: LookupItem<string, string>;
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
this.items = new LookupItem<number, string>();
|
|
|
|
this.categories = new LookupItem<string, string>();
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class HandbookHelper
|
|
|
|
{
|
|
|
|
protected lookupCacheGenerated = false;
|
|
|
|
protected handbookPriceCache = new LookupCollection();
|
|
|
|
|
|
|
|
constructor(@inject("DatabaseServer") protected databaseServer: DatabaseServer)
|
|
|
|
{}
|
|
|
|
|
2023-07-24 16:52:55 +02:00
|
|
|
/**
|
|
|
|
* Create an in-memory cache of all items with associated handbook price in handbookPriceCache class
|
|
|
|
*/
|
2023-03-03 16:23:46 +01:00
|
|
|
public hydrateLookup(): void
|
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
const handbookDb = this.databaseServer.getTables().templates.handbook;
|
|
|
|
for (const handbookItem of handbookDb.Items)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
this.handbookPriceCache.items.byId.set(handbookItem.Id, handbookItem.Price);
|
|
|
|
if (!this.handbookPriceCache.items.byParent.has(handbookItem.ParentId))
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
this.handbookPriceCache.items.byParent.set(handbookItem.ParentId, []);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
2023-04-22 20:33:47 +02:00
|
|
|
this.handbookPriceCache.items.byParent
|
|
|
|
.get(handbookItem.ParentId)
|
|
|
|
.push(handbookItem.Id);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
2023-04-22 20:33:47 +02:00
|
|
|
for (const handbookCategory of handbookDb.Categories)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
this.handbookPriceCache.categories.byId.set(handbookCategory.Id, handbookCategory.ParentId || null);
|
2023-03-03 16:23:46 +01:00
|
|
|
if (handbookCategory.ParentId)
|
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
if (!this.handbookPriceCache.categories.byParent.has(handbookCategory.ParentId))
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
this.handbookPriceCache.categories.byParent.set(handbookCategory.ParentId, []);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
2023-04-22 20:33:47 +02:00
|
|
|
this.handbookPriceCache.categories.byParent
|
|
|
|
.get(handbookCategory.ParentId)
|
|
|
|
.push(handbookCategory.Id);
|
2023-03-03 16:23:46 +01: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 20:33:47 +02:00
|
|
|
if (this.handbookPriceCache.items.byId.has(tpl))
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-04-23 12:13:53 +02:00
|
|
|
return this.handbookPriceCache.items.byId.get(tpl);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const handbookItem = this.databaseServer.getTables().templates.handbook.Items.find(x => x.Id === tpl);
|
2023-10-10 13:03:20 +02:00
|
|
|
if (!handbookItem)
|
|
|
|
{
|
|
|
|
this.handbookPriceCache.items.byId.set(tpl, 1);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
return handbookItem.Price;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-24 16:52:55 +02:00
|
|
|
* Get all items in template with the given parent category
|
|
|
|
* @param parentId
|
2023-03-03 16:23:46 +01:00
|
|
|
* @returns string array
|
|
|
|
*/
|
2023-07-24 16:52:55 +02:00
|
|
|
public templatesWithParent(parentId: string): string[]
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-07-24 16:52:55 +02:00
|
|
|
return this.handbookPriceCache.items.byParent.get(parentId) ?? [];
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does category exist in handbook cache
|
|
|
|
* @param category
|
|
|
|
* @returns true if exists in cache
|
|
|
|
*/
|
|
|
|
public isCategory(category: string): boolean
|
|
|
|
{
|
2023-04-22 20:33:47 +02:00
|
|
|
return this.handbookPriceCache.categories.byId.has(category);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:52:55 +02:00
|
|
|
/**
|
|
|
|
* Get all items associated with a categories parent
|
|
|
|
* @param categoryParent
|
|
|
|
* @returns string array
|
|
|
|
*/
|
|
|
|
public childrenCategories(categoryParent: string): string[]
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-07-24 16:52:55 +02:00
|
|
|
return this.handbookPriceCache.categories.byParent.get(categoryParent) ?? [];
|
2023-03-03 16:23:46 +01: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;
|
|
|
|
}
|
|
|
|
}
|