Server/project/src/helpers/PresetHelper.ts
DrakiaXYZ 15257ea263 Fix daily reward budget not using preset prices for some items (!256)
- Switch item cost calculation to use a new helper that checks for a preset first, to more accurately keep track of the budget
- Subtract weapon price from reward budget
- Decrement reward counter for skipped ammo
- Add a new helper for filtering the reward pool by budget, so it's consistent in the two places it's done
- Add a metric boatload of debug output for daily generation

Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/256
Co-authored-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
Co-committed-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
2024-03-11 00:03:41 +00:00

181 lines
5.0 KiB
TypeScript

import { inject, injectable } from "tsyringe";
import { IPreset } from "@spt-aki/models/eft/common/IGlobals";
import { BaseClasses } from "@spt-aki/models/enums/BaseClasses";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { JsonUtil } from "@spt-aki/utils/JsonUtil";
import { ItemHelper } from "./ItemHelper";
@injectable()
export class PresetHelper
{
protected lookup: Record<string, string[]> = {};
protected defaultEquipmentPresets: Record<string, IPreset>;
protected defaultWeaponPresets: Record<string, IPreset>;
constructor(
@inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("ItemHelper") protected itemHelper: ItemHelper,
)
{}
public hydratePresetStore(input: Record<string, string[]>): void
{
this.lookup = input;
}
/**
* Get default weapon and equipment presets
* @returns Dictionary
*/
public getDefaultPresets(): Record<string, IPreset>
{
const weapons = this.getDefaultWeaponPresets();
const equipment = this.getDefaultEquipmentPresets();
return Object.assign({}, weapons, equipment);
}
/**
* Get default weapon presets
* @returns Dictionary
*/
public getDefaultWeaponPresets(): Record<string, IPreset>
{
if (!this.defaultWeaponPresets)
{
this.defaultWeaponPresets = Object.values(this.databaseServer.getTables().globals.ItemPresets).filter((
preset,
) => preset._encyclopedia !== undefined
&& this.itemHelper.isOfBaseclass(preset._encyclopedia, BaseClasses.WEAPON)
).reduce((acc, cur) =>
{
acc[cur._id] = cur;
return acc;
}, {});
}
return this.defaultWeaponPresets;
}
/**
* Get default equipment presets
* @returns Dictionary
*/
public getDefaultEquipmentPresets(): Record<string, IPreset>
{
if (!this.defaultEquipmentPresets)
{
this.defaultEquipmentPresets = Object.values(this.databaseServer.getTables().globals.ItemPresets).filter((
preset,
) => preset._encyclopedia !== undefined && this.itemHelper.armorItemCanHoldMods(preset._encyclopedia))
.reduce((acc, cur) =>
{
acc[cur._id] = cur;
return acc;
}, {});
}
return this.defaultEquipmentPresets;
}
public isPreset(id: string): boolean
{
return id in this.databaseServer.getTables().globals.ItemPresets;
}
public hasPreset(templateId: string): boolean
{
return templateId in this.lookup;
}
public getPreset(id: string): IPreset
{
return this.jsonUtil.clone(this.databaseServer.getTables().globals.ItemPresets[id]);
}
public getAllPresets(): IPreset[]
{
return this.jsonUtil.clone(Object.values(this.databaseServer.getTables().globals.ItemPresets));
}
public getPresets(templateId: string): IPreset[]
{
if (!this.hasPreset(templateId))
{
return [];
}
const presets = [];
const ids = this.lookup[templateId];
for (const id of ids)
{
presets.push(this.getPreset(id));
}
return presets;
}
/**
* Get the default preset for passed in item id
* @param templateId Item id to get preset for
* @returns Null if no default preset, otherwise IPreset
*/
public getDefaultPreset(templateId: string): IPreset
{
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 "";
}
/**
* Return the price of the preset for the given item tpl, or for the tpl itself if no preset exists
* @param tpl The item template to get the price of
* @returns The price of the given item preset, or base item if no preset exists
*/
public getDefaultPresetOrItemPrice(tpl: string): number
{
// Get default preset if it exists
const defaultPreset = this.getDefaultPreset(tpl);
// Bundle up tpls we want price for
const tpls = defaultPreset ? defaultPreset._items.map((item) => item._tpl) : [tpl];
// Get price of tpls
return this.itemHelper.getItemAndChildrenPrice(tpls);
}
}