Server/project/src/helpers/RepairHelper.ts
Refringe 4ac12ef70a Formatting/Linting Changes (!168)
These are the formatting & linting configuration changes from the `3.8.0` branch and the changes that they make to the overall project.

The majority of these changes are from running two commands:

`npm run lint:fix`
`npm run style:fix`

This has already been run on the `3.8.0` branch and this PR should make `master` play nicer when it comes to merges going forward.

There are now four VSCode plugins recommended for server development. They've been added to the workspace file and a user should get a UI notification when the workspace is opened if they're not installed.

The four plugins are:
https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
https://marketplace.visualstudio.com/items?itemName=dprint.dprint
https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
https://marketplace.visualstudio.com/items?itemName=biomejs.biome

Once installed they should just work within the workspace.

Also, be sure to `npm i` to get the new dprint application.

Co-authored-by: Refringe <brownelltyler@gmail.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/168
2023-11-16 21:42:06 +00:00

168 lines
6.8 KiB
TypeScript

import { inject, injectable } from "tsyringe";
import { Item } from "@spt-aki/models/eft/common/tables/IItem";
import { ITemplateItem, Props } from "@spt-aki/models/eft/common/tables/ITemplateItem";
import { BaseClasses } from "@spt-aki/models/enums/BaseClasses";
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
import { IRepairConfig } from "@spt-aki/models/spt/config/IRepairConfig";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { JsonUtil } from "@spt-aki/utils/JsonUtil";
import { RandomUtil } from "@spt-aki/utils/RandomUtil";
@injectable()
export class RepairHelper
{
protected repairConfig: IRepairConfig;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("RandomUtil") protected randomUtil: RandomUtil,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("ConfigServer") protected configServer: ConfigServer,
)
{
this.repairConfig = this.configServer.getConfig(ConfigTypes.REPAIR);
}
/**
* Alter an items durability after a repair by trader/repair kit
* @param itemToRepair item to update durability details
* @param itemToRepairDetails db details of item to repair
* @param isArmor Is item being repaired a piece of armor
* @param amountToRepair how many unit of durability to repair
* @param useRepairKit Is item being repaired with a repair kit
* @param traderQualityMultipler Trader quality value from traders base json
* @param applyMaxDurabilityDegradation should item have max durability reduced
*/
public updateItemDurability(
itemToRepair: Item,
itemToRepairDetails: ITemplateItem,
isArmor: boolean,
amountToRepair: number,
useRepairKit: boolean,
traderQualityMultipler: number,
applyMaxDurabilityDegradation = true,
): void
{
this.logger.debug(`Adding ${amountToRepair} to ${itemToRepairDetails._name} using kit: ${useRepairKit}`);
const itemMaxDurability = this.jsonUtil.clone(itemToRepair.upd.Repairable.MaxDurability);
const itemCurrentDurability = this.jsonUtil.clone(itemToRepair.upd.Repairable.Durability);
const itemCurrentMaxDurability = this.jsonUtil.clone(itemToRepair.upd.Repairable.MaxDurability);
let newCurrentDurability = itemCurrentDurability + amountToRepair;
let newCurrentMaxDurability = itemCurrentMaxDurability + amountToRepair;
// Ensure new max isnt above items max
if (newCurrentMaxDurability > itemMaxDurability)
{
newCurrentMaxDurability = itemMaxDurability;
}
// Ensure new current isnt above items max
if (newCurrentDurability > itemMaxDurability)
{
newCurrentDurability = itemMaxDurability;
}
// Construct object to return
itemToRepair.upd.Repairable = { Durability: newCurrentDurability, MaxDurability: newCurrentMaxDurability };
// when modders set the repair coefficient to 0 it means that they dont want to lose durability on items
// the code below generates a random degradation on the weapon durability
if (applyMaxDurabilityDegradation)
{
const randomisedWearAmount = isArmor
? this.getRandomisedArmorRepairDegradationValue(
itemToRepairDetails._props.ArmorMaterial,
useRepairKit,
itemCurrentMaxDurability,
traderQualityMultipler,
)
: this.getRandomisedWeaponRepairDegradationValue(
itemToRepairDetails._props,
useRepairKit,
itemCurrentMaxDurability,
traderQualityMultipler,
);
// Apply wear to durability
itemToRepair.upd.Repairable.MaxDurability -= randomisedWearAmount;
// After adjusting max durability with degradation, ensure current dura isnt above max
if (itemToRepair.upd.Repairable.Durability > itemToRepair.upd.Repairable.MaxDurability)
{
itemToRepair.upd.Repairable.Durability = itemToRepair.upd.Repairable.MaxDurability;
}
}
// Repair mask cracks
if (itemToRepair.upd.FaceShield && itemToRepair.upd.FaceShield.Hits > 0)
{
itemToRepair.upd.FaceShield.Hits = 0;
}
}
protected getRandomisedArmorRepairDegradationValue(
armorMaterial: string,
isRepairKit: boolean,
armorMax: number,
traderQualityMultipler: number,
): number
{
const armorMaterialSettings = this.databaseServer.getTables().globals.config.ArmorMaterials[armorMaterial];
const minMultiplier = isRepairKit
? armorMaterialSettings.MinRepairKitDegradation
: armorMaterialSettings.MinRepairDegradation;
const maxMultiplier = isRepairKit
? armorMaterialSettings.MaxRepairKitDegradation
: armorMaterialSettings.MaxRepairDegradation;
const duraLossPercent = this.randomUtil.getFloat(minMultiplier, maxMultiplier);
const duraLossMultipliedByTraderMultiplier = (duraLossPercent * armorMax) * traderQualityMultipler;
return Number(duraLossMultipliedByTraderMultiplier.toFixed(2));
}
protected getRandomisedWeaponRepairDegradationValue(
itemProps: Props,
isRepairKit: boolean,
weaponMax: number,
traderQualityMultipler: number,
): number
{
const minRepairDeg = isRepairKit ? itemProps.MinRepairKitDegradation : itemProps.MinRepairDegradation;
let maxRepairDeg = isRepairKit ? itemProps.MaxRepairKitDegradation : itemProps.MaxRepairDegradation;
// WORKAROUND: Some items are always 0 when repairkit is true
if (maxRepairDeg === 0)
{
maxRepairDeg = itemProps.MaxRepairDegradation;
}
const duraLossPercent = this.randomUtil.getFloat(minRepairDeg, maxRepairDeg);
const duraLossMultipliedByTraderMultiplier = (duraLossPercent * weaponMax) * traderQualityMultipler;
return Number(duraLossMultipliedByTraderMultiplier.toFixed(2));
}
/**
* Is the supplied tpl a weapon
* @param tpl tplId to check is a weapon
* @returns true if tpl is a weapon
*/
public isWeaponTemplate(tpl: string): boolean
{
const itemTemplates = this.databaseServer.getTables().templates.items;
const baseItem = itemTemplates[tpl];
const baseNode = itemTemplates[baseItem._parent];
const parentNode = itemTemplates[baseNode._parent];
return parentNode._id === BaseClasses.WEAPON;
}
}