2023-03-03 16:23:46 +01:00
|
|
|
import { inject, injectable } from "tsyringe";
|
2023-07-18 16:44:14 +02:00
|
|
|
import { IPreset } from "../models/eft/common/IGlobals";
|
2023-03-03 16:23:46 +01:00
|
|
|
import { DatabaseServer } from "../servers/DatabaseServer";
|
|
|
|
import { JsonUtil } from "../utils/JsonUtil";
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class PresetHelper
|
|
|
|
{
|
|
|
|
protected lookup: Record<string, string[]> = {};
|
2023-07-18 16:44:14 +02:00
|
|
|
protected defaultPresets: Record<string, IPreset>;
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@inject("JsonUtil") protected jsonUtil: JsonUtil,
|
|
|
|
@inject("DatabaseServer") protected databaseServer: DatabaseServer
|
|
|
|
)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public hydratePresetStore(input: Record<string, string[]>): void
|
|
|
|
{
|
|
|
|
this.lookup = input;
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:44:14 +02:00
|
|
|
public getDefaultPresets(): Record<string, IPreset>
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
if (!this.defaultPresets)
|
|
|
|
{
|
|
|
|
this.defaultPresets = Object.values(this.databaseServer.getTables().globals.ItemPresets)
|
|
|
|
.filter(x => x._encyclopedia !== undefined)
|
|
|
|
.reduce((acc, cur) =>
|
|
|
|
{
|
|
|
|
acc[cur._id] = cur;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.defaultPresets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isPreset(id: string): boolean
|
|
|
|
{
|
|
|
|
return id in this.databaseServer.getTables().globals.ItemPresets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public hasPreset(templateId: string): boolean
|
|
|
|
{
|
|
|
|
return templateId in this.lookup;
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:44:14 +02:00
|
|
|
public getPreset(id: string): IPreset
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
return this.jsonUtil.clone(this.databaseServer.getTables().globals.ItemPresets[id]);
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:44:14 +02:00
|
|
|
public getPresets(templateId: string): IPreset[]
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
if (!this.hasPreset(templateId))
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const presets = [];
|
|
|
|
const ids = this.lookup[templateId];
|
|
|
|
|
|
|
|
for (const id of ids)
|
|
|
|
{
|
|
|
|
presets.push(this.getPreset(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return presets;
|
|
|
|
}
|
|
|
|
|
2023-08-09 12:52:20 +02:00
|
|
|
/**
|
|
|
|
* Get the default preset for passed in weapon id
|
|
|
|
* @param templateId Weapon id to get preset for
|
|
|
|
* @returns Null if no default preset, otherwise IPreset
|
|
|
|
*/
|
2023-07-18 16:44:14 +02:00
|
|
|
public getDefaultPreset(templateId: string): IPreset
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
if (!this.hasPreset(templateId))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allPresets = this.getPresets(templateId);
|
|
|
|
|
|
|
|
for (const preset of allPresets)
|
|
|
|
{
|
|
|
|
if ("_encyclopedia" in preset)
|
|
|
|
{
|
|
|
|
return preset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allPresets[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBaseItemTpl(presetId: string): string
|
|
|
|
{
|
|
|
|
if (this.isPreset(presetId))
|
|
|
|
{
|
|
|
|
const preset = this.getPreset(presetId);
|
|
|
|
|
|
|
|
for (const item of preset._items)
|
|
|
|
{
|
|
|
|
if (preset._parent === item._id)
|
|
|
|
{
|
|
|
|
return item._tpl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|