Server/project/src/helpers/RagfairHelper.ts

204 lines
6.2 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { HandbookHelper } from "@spt/helpers/HandbookHelper";
import { ItemHelper } from "@spt/helpers/ItemHelper";
import { TraderAssortHelper } from "@spt/helpers/TraderAssortHelper";
import { UtilityHelper } from "@spt/helpers/UtilityHelper";
import { Item } from "@spt/models/eft/common/tables/IItem";
import { ITraderAssort } from "@spt/models/eft/common/tables/ITrader";
import { ISearchRequestData } from "@spt/models/eft/ragfair/ISearchRequestData";
import { ConfigTypes } from "@spt/models/enums/ConfigTypes";
import { Money } from "@spt/models/enums/Money";
import { IRagfairConfig } from "@spt/models/spt/config/IRagfairConfig";
import { ILogger } from "@spt/models/spt/utils/ILogger";
import { ConfigServer } from "@spt/servers/ConfigServer";
import { DatabaseService } from "@spt/services/DatabaseService";
import { RagfairLinkedItemService } from "@spt/services/RagfairLinkedItemService";
import { ICloner } from "@spt/utils/cloners/ICloner";
2023-03-03 16:23:46 +01:00
@injectable()
export class RagfairHelper
{
protected ragfairConfig: IRagfairConfig;
constructor(
@inject("PrimaryLogger") protected logger: ILogger,
2023-03-03 16:23:46 +01:00
@inject("TraderAssortHelper") protected traderAssortHelper: TraderAssortHelper,
@inject("DatabaseService") protected databaseService: DatabaseService,
2023-03-03 16:23:46 +01:00
@inject("HandbookHelper") protected handbookHelper: HandbookHelper,
@inject("ItemHelper") protected itemHelper: ItemHelper,
@inject("RagfairLinkedItemService") protected ragfairLinkedItemService: RagfairLinkedItemService,
@inject("UtilityHelper") protected utilityHelper: UtilityHelper,
2023-11-13 17:07:59 +01:00
@inject("ConfigServer") protected configServer: ConfigServer,
@inject("PrimaryCloner") protected cloner: ICloner,
2023-03-03 16:23:46 +01:00
)
{
this.ragfairConfig = this.configServer.getConfig(ConfigTypes.RAGFAIR);
}
/**
2023-11-13 17:07:59 +01:00
* Gets currency TAG from TPL
* @param {string} currency
* @returns string
*/
2023-03-03 16:23:46 +01:00
public getCurrencyTag(currency: string): string
{
switch (currency)
{
case Money.EUROS:
2023-03-03 16:23:46 +01:00
return "EUR";
case Money.DOLLARS:
2023-03-03 16:23:46 +01:00
return "USD";
case Money.ROUBLES:
2023-03-03 16:23:46 +01:00
return "RUB";
case Money.GP:
return "GP";
2023-03-03 16:23:46 +01:00
default:
return "";
}
}
public filterCategories(sessionID: string, request: ISearchRequestData): string[]
2023-03-03 16:23:46 +01:00
{
let result: string[] = [];
// Case: weapon builds
if (request.buildCount)
2023-03-03 16:23:46 +01:00
{
return Object.keys(request.buildItems);
2023-03-03 16:23:46 +01:00
}
// Case: search
if (request.linkedSearchId)
2023-03-03 16:23:46 +01:00
{
const data = this.ragfairLinkedItemService.getLinkedItems(request.linkedSearchId);
result = !data ? [] : [...data];
2023-03-03 16:23:46 +01:00
}
// Case: category
if (request.handbookId)
2023-03-03 16:23:46 +01:00
{
const handbook = this.getCategoryList(request.handbookId);
2023-03-03 16:23:46 +01:00
if (result.length)
{
result = this.utilityHelper.arrayIntersect(result, handbook);
}
else
{
result = handbook;
}
}
return result;
}
public getDisplayableAssorts(sessionID: string): Record<string, ITraderAssort>
{
const result: Record<string, ITraderAssort> = {};
for (const traderID in this.databaseService.getTraders())
2023-03-03 16:23:46 +01:00
{
if (this.ragfairConfig.traders[traderID])
{
result[traderID] = this.traderAssortHelper.getAssort(sessionID, traderID, true);
}
}
return result;
}
protected getCategoryList(handbookId: string): string[]
{
let result: string[] = [];
// if its "mods" great-parent category, do double recursive loop
if (handbookId === "5b5f71a686f77447ed5636ab")
{
for (const categ of this.handbookHelper.childrenCategories(handbookId))
{
for (const subcateg of this.handbookHelper.childrenCategories(categ))
{
result = [...result, ...this.handbookHelper.templatesWithParent(subcateg)];
}
}
return result;
}
// item is in any other category
if (this.handbookHelper.isCategory(handbookId))
{
// list all item of the category
result = this.handbookHelper.templatesWithParent(handbookId);
for (const categ of this.handbookHelper.childrenCategories(handbookId))
{
result = [...result, ...this.handbookHelper.templatesWithParent(categ)];
}
return result;
}
// its a specific item searched
result.push(handbookId);
return result;
}
/**
* Merges Root Items
* Ragfair allows abnormally large stacks.
*/
public mergeStackable(items: Item[]): Item[]
{
const list = [];
let rootItem = undefined;
2023-03-03 16:23:46 +01:00
for (let item of items)
{
item = this.itemHelper.fixItemStackCount(item);
const isChild = items.some((it) => it._id === item.parentId);
2023-03-03 16:23:46 +01:00
if (!isChild)
{
if (!rootItem)
{
rootItem = this.cloner.clone(item);
2023-03-03 16:23:46 +01:00
rootItem.upd.OriginalStackObjectsCount = rootItem.upd.StackObjectsCount;
}
else
{
rootItem.upd.StackObjectsCount += item.upd.StackObjectsCount;
list.push(item);
}
}
else
{
list.push(item);
}
}
return [...[rootItem], ...list];
}
/**
* Return the symbol for a currency
* e.g. 5449016a4bdc2d6f028b456f return
* @param currencyTpl currency to get symbol for
* @returns symbol of currency
*/
2023-03-03 16:23:46 +01:00
public getCurrencySymbol(currencyTpl: string): string
{
switch (currencyTpl)
{
case Money.EUROS:
return "€";
case Money.DOLLARS:
return "$";
default: // Money.ROUBLES
2023-03-03 16:23:46 +01:00
return "₽";
}
}
}