Server/project/src/controllers/InsuranceController.ts

686 lines
28 KiB
TypeScript
Raw Normal View History

2023-03-03 15:23:46 +00:00
import { inject, injectable } from "tsyringe";
import { DialogueHelper } from "@spt-aki/helpers/DialogueHelper";
import { ItemHelper } from "@spt-aki/helpers/ItemHelper";
import { ProfileHelper } from "@spt-aki/helpers/ProfileHelper";
import { TraderHelper } from "@spt-aki/helpers/TraderHelper";
import { IPmcData } from "@spt-aki/models/eft/common/IPmcData";
import { Item } from "@spt-aki/models/eft/common/tables/IItem";
import { IGetInsuranceCostRequestData } from "@spt-aki/models/eft/insurance/IGetInsuranceCostRequestData";
import { IGetInsuranceCostResponseData } from "@spt-aki/models/eft/insurance/IGetInsuranceCostResponseData";
import { IInsureRequestData } from "@spt-aki/models/eft/insurance/IInsureRequestData";
import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse";
2023-11-13 11:43:37 -05:00
import { ISystemData, Insurance } from "@spt-aki/models/eft/profile/IAkiProfile";
import { IProcessBuyTradeRequestData } from "@spt-aki/models/eft/trade/IProcessBuyTradeRequestData";
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
import { MessageType } from "@spt-aki/models/enums/MessageType";
import { SkillTypes } from "@spt-aki/models/enums/SkillTypes";
import { IInsuranceConfig } from "@spt-aki/models/spt/config/IInsuranceConfig";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { EventOutputHolder } from "@spt-aki/routers/EventOutputHolder";
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { SaveServer } from "@spt-aki/servers/SaveServer";
import { InsuranceService } from "@spt-aki/services/InsuranceService";
import { MailSendService } from "@spt-aki/services/MailSendService";
import { PaymentService } from "@spt-aki/services/PaymentService";
2023-11-10 16:49:29 -05:00
import { MathUtil } from "@spt-aki/utils/MathUtil";
import { RandomUtil } from "@spt-aki/utils/RandomUtil";
import { TimeUtil } from "@spt-aki/utils/TimeUtil";
2023-03-03 15:23:46 +00:00
@injectable()
export class InsuranceController
{
protected insuranceConfig: IInsuranceConfig;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("RandomUtil") protected randomUtil: RandomUtil,
@inject("MathUtil") protected mathUtil: MathUtil,
2023-03-03 15:23:46 +00:00
@inject("EventOutputHolder") protected eventOutputHolder: EventOutputHolder,
@inject("TimeUtil") protected timeUtil: TimeUtil,
@inject("SaveServer") protected saveServer: SaveServer,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("ItemHelper") protected itemHelper: ItemHelper,
@inject("ProfileHelper") protected profileHelper: ProfileHelper,
@inject("DialogueHelper") protected dialogueHelper: DialogueHelper,
@inject("TraderHelper") protected traderHelper: TraderHelper,
2023-03-03 15:23:46 +00:00
@inject("PaymentService") protected paymentService: PaymentService,
@inject("InsuranceService") protected insuranceService: InsuranceService,
@inject("MailSendService") protected mailSendService: MailSendService,
2023-11-10 16:49:29 -05:00
@inject("ConfigServer") protected configServer: ConfigServer,
2023-03-03 15:23:46 +00:00
)
{
this.insuranceConfig = this.configServer.getConfig(ConfigTypes.INSURANCE);
}
/**
* Process insurance items of all profiles prior to being given back to the player through the mail service.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @returns void
2023-11-10 16:49:29 -05:00
*/
2023-03-03 15:23:46 +00:00
public processReturn(): void
{
// Process each installed profile.
2023-03-03 15:23:46 +00:00
for (const sessionID in this.saveServer.getProfiles())
{
this.processReturnByProfile(sessionID);
}
}
2023-03-03 15:23:46 +00:00
/**
* Process insurance items of a single profile prior to being given back to the player through the mail service.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @returns void
2023-11-10 16:49:29 -05:00
*/
public processReturnByProfile(sessionID: string): void
{
// Filter out items that don't need to be processed yet.
const insuranceDetails = this.filterInsuredItems(sessionID);
2023-03-03 15:23:46 +00:00
// Skip profile if no insured items to process
if (insuranceDetails.length === 0)
{
return;
}
2023-03-03 15:23:46 +00:00
this.processInsuredItems(insuranceDetails, sessionID);
}
2023-03-03 15:23:46 +00:00
/**
* Get all insured items that are ready to be processed in a specific profile.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @param sessionID Session ID of the profile to check.
* @param time The time to check ready status against. Current time by default.
* @returns All insured items that are ready to be processed.
*/
protected filterInsuredItems(sessionID: string, time?: number): Insurance[]
{
// Use the current time by default.
if (!time)
{
time = this.timeUtil.getTimestamp();
}
2023-03-03 15:23:46 +00:00
const profileInsuranceDetails = this.saveServer.getProfile(sessionID).insurance;
this.logger.debug(`Found ${profileInsuranceDetails.length} insurance packages in profile ${sessionID}`);
2023-03-03 15:23:46 +00:00
return profileInsuranceDetails.filter((insured) => time >= insured.scheduledTime);
}
/**
* This method orchestrates the processing of insured items in a profile.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @param insuranceDetails The insured items to process.
* @param sessionID The session ID that should receive the processed items.
* @returns void
*/
protected processInsuredItems(insuranceDetails: Insurance[], sessionID: string): void
{
2023-11-10 16:49:29 -05:00
this.logger.debug(
`Processing ${insuranceDetails.length} insurance packages, which includes a total of ${
this.countAllInsuranceItems(insuranceDetails)
} items, in profile ${sessionID}`,
);
2023-03-03 15:23:46 +00:00
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
// Iterate over each of the insurance packages.
2023-10-31 17:46:14 +00:00
for (const insured of insuranceDetails)
{
// Find items that should be deleted from the insured items.
const itemsToDelete = this.findItemsToDelete(insured);
// Actually remove them.
this.removeItemsFromInsurance(insured, itemsToDelete);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Fix any orphaned items.
this.adoptOrphanedItems(insured);
// Send the mail to the player.
this.sendMail(sessionID, insured);
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
// Remove the fully processed insurance package from the profile.
this.removeInsurancePackageFromProfile(sessionID, insured.messageContent.systemData);
2023-10-31 17:46:14 +00:00
}
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
}
/**
* Count all items in all insurance packages.
* @param insurance
* @returns
*/
protected countAllInsuranceItems(insurance: Insurance[]): number
{
2023-11-10 16:49:29 -05:00
return this.mathUtil.arraySum(insurance.map((ins) => ins.items.length));
}
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
/**
* Remove an insurance package from a profile using the package's system data information.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
* @param sessionID The session ID of the profile to remove the package from.
* @param index The array index of the insurance package to remove.
* @returns void
*/
protected removeInsurancePackageFromProfile(sessionID: string, packageInfo: ISystemData): void
{
const profile = this.saveServer.getProfile(sessionID);
2023-11-10 16:49:29 -05:00
profile.insurance = profile.insurance.filter((insurance) =>
insurance.messageContent.systemData.date !== packageInfo.date
|| insurance.messageContent.systemData.time !== packageInfo.time
|| insurance.messageContent.systemData.location !== packageInfo.location
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
2023-11-10 16:49:29 -05:00
this.logger.debug(
`Removed insurance package with date: ${packageInfo.date}, time: ${packageInfo.time}, and location: ${packageInfo.location} from profile ${sessionID}. Remaining packages: ${profile.insurance.length}`,
);
}
/**
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* Finds the items that should be deleted based on the given Insurance object.
*
* @param insured The insurance object containing the items to evaluate for deletion.
* @returns A Set containing the IDs of items that should be deleted.
*/
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
protected findItemsToDelete(insured: Insurance): Set<string>
{
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
const toDelete = new Set<string>();
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Populate a Map object of items for quick lookup by their ID and use it to populate a Map of main-parent items
// and each of their attachments. For example, a gun mapped to each of its attachments.
const itemsMap = this.populateItemsMap(insured);
const parentAttachmentsMap = this.populateParentAttachmentsMap(insured, itemsMap);
// Check to see if any regular items are present.
2023-11-10 16:49:29 -05:00
const hasRegularItems = Array.from(itemsMap.values()).some((item) =>
!this.itemHelper.isAttachmentAttached(item)
);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Process all items that are not attached, attachments. Those are handled separately, by value.
if (hasRegularItems)
{
this.processRegularItems(insured, toDelete);
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Process attached, attachments, by value, only if there are any.
if (parentAttachmentsMap.size > 0)
{
this.processAttachments(parentAttachmentsMap, itemsMap, insured.traderId, toDelete);
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Log the number of items marked for deletion, if any
if (toDelete.size)
{
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
this.logger.debug(`Marked ${toDelete.size} items for deletion from insurance.`);
}
return toDelete;
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
/**
* Populate a Map object of items for quick lookup by their ID.
*
* @param insured The insurance object containing the items to populate the map with.
* @returns A Map where the keys are the item IDs and the values are the corresponding Item objects.
*/
protected populateItemsMap(insured: Insurance): Map<string, Item>
{
const itemsMap = new Map<string, Item>();
2023-10-31 17:46:14 +00:00
for (const item of insured.items)
{
itemsMap.set(item._id, item);
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
return itemsMap;
}
/**
* Initialize a Map object that holds main-parents to all of their attachments. Note that "main-parent" in this
* context refers to the parent item that an attachment is attached to. For example, a suppressor attached to a gun,
* not the backpack that the gun is located in (the gun's parent).
*
* @param insured - The insurance object containing the items to evaluate.
* @param itemsMap - A Map object for quick item look-up by item ID.
* @returns A Map object containing parent item IDs to arrays of their attachment items.
*/
protected populateParentAttachmentsMap(insured: Insurance, itemsMap: Map<string, Item>): Map<string, Item[]>
{
const mainParentToAttachmentsMap = new Map<string, Item[]>();
for (const insuredItem of insured.items)
{
// Use the parent ID from the item to get the parent item.
2023-11-10 16:49:29 -05:00
const parentItem = insured.items.find((item) => item._id === insuredItem.parentId);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// The parent (not the hideout) could not be found. Skip and warn.
if (!parentItem && insuredItem.parentId !== this.fetchHideoutItemParent(insured.items))
{
2023-11-10 16:49:29 -05:00
this.logger.warning(
`Could not find parent for insured item - ID: ${insuredItem._id}, Template: ${insuredItem._tpl}, Parent ID: ${insuredItem.parentId}`,
);
continue;
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Check if this is an attachment currently attached to its parent.
if (this.itemHelper.isAttachmentAttached(insuredItem))
{
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Filter out items not in the database or not raid moddable.
if (!this.itemHelper.isRaidModdable(insuredItem, parentItem))
2023-03-03 15:23:46 +00:00
{
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
continue;
}
// Get the main parent of this attachment. (e.g., The gun that this suppressor is attached to.)
const mainParent = this.itemHelper.getAttachmentMainParent(insuredItem._id, itemsMap);
if (!mainParent)
{
// Odd. The parent couldn't be found. Skip this attachment and warn.
2023-11-10 16:49:29 -05:00
this.logger.warning(
`Could not find main-parent for insured attachment - ID: ${insuredItem._id}, Template: ${insuredItem._tpl}, Parent ID: ${insuredItem.parentId}`,
);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
continue;
}
// Update (or add to) the main-parent to attachments map.
if (mainParentToAttachmentsMap.has(mainParent._id))
{
mainParentToAttachmentsMap.get(mainParent._id).push(insuredItem);
}
else
{
mainParentToAttachmentsMap.set(mainParent._id, [insuredItem]);
2023-03-03 15:23:46 +00:00
}
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
}
return mainParentToAttachmentsMap;
}
/**
* Process "regular" insurance items. Any insured item that is not an attached, attachment is considered a "regular"
* item. This method iterates over them, preforming item deletion rolls to see if they should be deleted. If so,
* they (and their attached, attachments, if any) are marked for deletion in the toDelete Set.
*
* @param insured The insurance object containing the items to evaluate.
* @param toDelete A Set to keep track of items marked for deletion.
* @returns void
*/
protected processRegularItems(insured: Insurance, toDelete: Set<string>): void
{
for (const insuredItem of insured.items)
{
// Skip if the item is an attachment. These are handled separately.
if (this.itemHelper.isAttachmentAttached(insuredItem))
{
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
continue;
}
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Check if the item has any children
const itemAndChildren = this.itemHelper.findAndReturnChildrenAsItems(insured.items, insuredItem._id);
// Roll for item deletion
const itemRoll = this.rollForDelete(insured.traderId, insuredItem);
if (itemRoll)
{
// Mark the item for deletion
toDelete.add(insuredItem._id);
// Check if the item has any children and mark those for deletion as well, but only if those
// children are currently attached attachments.
2023-11-10 16:49:29 -05:00
const directChildren = insured.items.filter((item) => item.parentId === insuredItem._id);
const allChildrenAreAttachments = directChildren.every((child) =>
this.itemHelper.isAttachmentAttached(child)
);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
if (allChildrenAreAttachments)
{
2023-10-31 17:46:14 +00:00
for (const item of itemAndChildren)
{
toDelete.add(item._id);
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
}
}
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
}
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
/**
* Process parent items and their attachments, updating the toDelete Set accordingly.
*
* @param mainParentToAttachmentsMap A Map object containing parent item IDs to arrays of their attachment items.
* @param itemsMap A Map object for quick item look-up by item ID.
* @param traderId The trader ID from the Insurance object.
* @param toDelete A Set object to keep track of items marked for deletion.
*/
2023-11-10 16:49:29 -05:00
protected processAttachments(
mainParentToAttachmentsMap: Map<string, Item[]>,
itemsMap: Map<string, Item>,
traderId: string,
toDelete: Set<string>,
): void
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
{
2023-11-10 16:49:29 -05:00
for (const [parentId, attachmentItems] of mainParentToAttachmentsMap)
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
{
// Log the parent item's name.
const parentItem = itemsMap.get(parentId);
const parentName = this.itemHelper.getItemName(parentItem._tpl);
this.logger.debug(`Processing attachments for parent item: ${parentName}`);
// Process the attachments for this individual parent item.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
this.processAttachmentByParent(attachmentItems, traderId, toDelete);
2023-10-31 17:46:14 +00:00
}
}
2023-03-03 15:23:46 +00:00
/**
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* Takes an array of attachment items that belong to the same main-parent item, sorts them in descending order by
* their maximum price. For each attachment, a roll is made to determine if a deletion should be made. Once the
* number of deletions has been counted, the attachments are added to the toDelete Set, starting with the most
* valuable attachments first.
*
* @param attachments The array of attachment items to sort, filter, and roll.
* @param traderId The ID of the trader to that has ensured these items.
* @param toDelete The array that accumulates the IDs of the items to be deleted.
* @returns void
*/
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
protected processAttachmentByParent(attachments: Item[], traderId: string, toDelete: Set<string>): void
{
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
const sortedAttachments = this.sortAttachmentsByPrice(attachments);
this.logAttachmentsDetails(sortedAttachments);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
const successfulRolls = this.countSuccessfulRolls(sortedAttachments, traderId);
this.logger.debug(`Number of successful rolls: ${successfulRolls}`);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
this.attachmentDeletionByValue(sortedAttachments, successfulRolls, toDelete);
}
/**
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* Sorts the attachment items by their max price in descending order.
*
* @param attachments The array of attachments items.
* @returns An array of items enriched with their max price and common locale-name.
*/
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
protected sortAttachmentsByPrice(attachments: Item[]): EnrichedItem[]
{
2023-11-10 16:49:29 -05:00
return attachments.map((item) => ({
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
...item,
name: this.itemHelper.getItemName(item._tpl),
2023-11-10 16:49:29 -05:00
maxPrice: this.itemHelper.getItemMaxPrice(item._tpl),
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
})).sort((a, b) => b.maxPrice - a.maxPrice);
}
/**
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* Logs the details of each attachment item.
*
* @param attachments The array of attachment items.
*/
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
protected logAttachmentsDetails(attachments: EnrichedItem[]): void
{
2023-11-10 16:49:29 -05:00
for (const attachment of attachments)
{
2023-10-31 17:46:14 +00:00
this.logger.debug(`Child Item - Name: ${attachment.name}, Max Price: ${attachment.maxPrice}`);
}
}
/**
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* Counts the number of successful rolls for the attachment items.
*
* @param attachments The array of attachment items.
* @param traderId The ID of the trader that has insured these attachments.
* @returns The number of successful rolls.
*/
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
protected countSuccessfulRolls(attachments: Item[], traderId: string): number
{
2023-11-10 16:49:29 -05:00
const rolls = Array.from({length: attachments.length}, () => this.rollForDelete(traderId));
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
return rolls.filter(Boolean).length;
}
2023-03-03 15:23:46 +00:00
/**
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* Marks the most valuable attachments for deletion based on the number of successful rolls made.
*
* @param attachments The array of attachment items.
* @param successfulRolls The number of successful rolls.
* @param toDelete The array that accumulates the IDs of the items to be deleted.
*/
2023-11-10 16:49:29 -05:00
protected attachmentDeletionByValue(
attachments: EnrichedItem[],
successfulRolls: number,
toDelete: Set<string>,
): void
{
2023-11-10 16:49:29 -05:00
const valuableToDelete = attachments.slice(0, successfulRolls).map(({_id}) => _id);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
2023-10-31 17:46:14 +00:00
for (const attachmentsId of valuableToDelete)
{
2023-11-10 16:49:29 -05:00
const valuableChild = attachments.find(({_id}) => _id === attachmentsId);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
if (valuableChild)
{
2023-11-10 16:49:29 -05:00
const {name, maxPrice} = valuableChild;
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
this.logger.debug(`Marked for removal - Child Item: ${name}, Max Price: ${maxPrice}`);
toDelete.add(attachmentsId);
2023-03-03 15:23:46 +00:00
}
2023-10-31 17:46:14 +00:00
}
}
2023-03-03 15:23:46 +00:00
/**
* Remove items from the insured items that should not be returned to the player.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @param insured The insured items to process.
* @param toDelete The items that should be deleted.
* @returns void
*/
InsuranceController Quality and Maintainability (!148) This pull request aims to refactor the InsuranceController class to improve its code quality and maintainability. The changes include restructuring methods, adding detailed comments, and enhancing the overall logic for processing insured items. Specific changes include: - Updating the `findItemstoDelete()` method (and the entire class, really) to keep track of which items are tagged using a Set. This *ensures* that an item can only be attempted to be deleted once, as Sets cannot contain duplicates. - Changing the method in which we remove insurance packages from the profile. We were iterating over them using a `for` in reverse order and then removing them with a splice and the current index. On paper this should work, but funny things were happening... Sometimes. I've created a new method that removes packages from a profile by matching against the package systemData date, time, and location. This should give us a specific insurance package (as we don't have an ID to use). In addition to this we're fetching a new instance of the profile to edit, instead of modifying packages that are being iterated over as they're being iterated over. ----- I was unable to reproduce the issue where insurance packages were not being removed from the profile, but hopefully these simplified approaches lead to less funny business. Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/148 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-12 08:38:22 +00:00
protected removeItemsFromInsurance(insured: Insurance, toDelete: Set<string>): void
{
2023-11-10 16:49:29 -05:00
insured.items = insured.items.filter((item) => !toDelete.has(item._id));
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
/**
* Adopts orphaned items by resetting them as base-level items. Helpful in situations where a parent has been
* deleted from insurance, but any insured items within the parent should remain. This method will remove the
* reference from the children to the parent and set item properties to main-level values.
*
* @param insured Insurance object containing items.
*/
protected adoptOrphanedItems(insured: Insurance): void
{
const hideoutParentId = this.fetchHideoutItemParent(insured.items);
2023-10-31 17:46:14 +00:00
for (const item of insured.items)
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
{
// Check if the item's parent exists in the insured items list.
2023-11-10 16:49:29 -05:00
const parentExists = insured.items.some((parentItem) => parentItem._id === item.parentId);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// If the parent does not exist and the item is not already a 'hideout' item, adopt the orphaned item.
if (!parentExists && item.parentId !== hideoutParentId && item.slotId !== "hideout")
{
item.parentId = hideoutParentId;
item.slotId = "hideout";
delete item.location;
}
2023-10-31 17:46:14 +00:00
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
}
/**
* Fetches the parentId property of an item with a slotId "hideout". Not sure if this is actually dynamic, but this
* method should be a reliable way to fetch it, if it ever does change.
*
* @param items Array of items to search through.
* @returns The parentId of an item with slotId 'hideout'. Empty string if not found.
*/
protected fetchHideoutItemParent(items: Item[]): string
{
2023-11-10 16:49:29 -05:00
const hideoutItem = items.find((item) => item.slotId === "hideout");
const hideoutParentId = hideoutItem ? hideoutItem?.parentId : "";
if (hideoutParentId === "")
{
this.logger.warning("Unable to find an item with slotId 'hideout' in the insured item package.");
}
return hideoutParentId;
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
}
/**
* Handle sending the insurance message to the user that potentially contains the valid insurance items.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @param sessionID The session ID that should receive the insurance message.
* @param insurance The context of insurance to use.
* @returns void
*/
protected sendMail(sessionID: string, insurance: Insurance): void
{
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// After all of the item filtering that we've done, if there are no items remaining, the insurance has
// successfully "failed" to return anything and an appropriate message should be sent to the player.
if (insurance.items.length === 0)
{
2023-11-10 16:49:29 -05:00
const insuranceFailedTemplates =
this.databaseServer.getTables().traders[insurance.traderId].dialogue.insuranceFailed;
insurance.messageContent.templateId = this.randomUtil.getArrayValue(insuranceFailedTemplates);
2023-03-03 15:23:46 +00:00
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Send the insurance message
this.mailSendService.sendLocalisedNpcMessageToPlayer(
sessionID,
this.traderHelper.getTraderById(insurance.traderId),
MessageType.INSURANCE_RETURN,
insurance.messageContent.templateId,
insurance.items,
insurance.messageContent.maxStorageTime,
2023-11-10 16:49:29 -05:00
insurance.messageContent.systemData,
);
2023-03-03 15:23:46 +00:00
}
/**
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* Determines whether a insured item should be removed from the player's inventory based on a random roll and
* trader-specific return chance.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @param traderId The ID of the trader who insured the item.
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
* @param insuredItem Optional. The item to roll for. Only used for logging.
* @returns true if the insured item should be removed from inventory, false otherwise, or null on error.
*/
protected rollForDelete(traderId: string, insuredItem?: Item): boolean | null
{
const trader = this.traderHelper.getTraderById(traderId);
if (!trader)
{
return null;
}
const maxRoll = 9999;
const conversionFactor = 100;
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
const returnChance = this.randomUtil.getInt(0, maxRoll) / conversionFactor;
const traderReturnChance = this.insuranceConfig.returnChancePercent[traderId];
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
const roll = returnChance >= traderReturnChance;
// Log the roll with as much detail as possible.
const itemName = insuredItem ? ` for "${this.itemHelper.getItemName(insuredItem._tpl)}"` : "";
const status = roll ? "Delete" : "Keep";
2023-11-10 16:49:29 -05:00
this.logger.debug(
`Rolling deletion${itemName} with ${trader} - Return ${traderReturnChance}% - Roll: ${returnChance} - Status: ${status}`,
);
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
return roll;
}
2023-03-03 15:23:46 +00:00
/**
* Handle Insure event
2023-03-03 15:23:46 +00:00
* Add insurance to an item
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
2023-03-03 15:23:46 +00:00
* @param pmcData Player profile
* @param body Insurance request
* @param sessionID Session id
* @returns IItemEventRouterResponse object to send to client
*/
public insure(pmcData: IPmcData, body: IInsureRequestData, sessionID: string): IItemEventRouterResponse
{
let output = this.eventOutputHolder.getOutput(sessionID);
const itemsToInsureCount = body.items.length;
2023-03-03 15:23:46 +00:00
const itemsToPay = [];
const inventoryItemsHash = {};
for (const item of pmcData.Inventory.items)
{
inventoryItemsHash[item._id] = item;
}
// get the price of all items
for (const key of body.items)
{
itemsToPay.push({
id: inventoryItemsHash[key]._id,
2023-11-10 16:49:29 -05:00
count: Math.round(this.insuranceService.getPremium(pmcData, inventoryItemsHash[key], body.tid)),
2023-03-03 15:23:46 +00:00
});
}
const options: IProcessBuyTradeRequestData = {
scheme_items: itemsToPay,
tid: body.tid,
Action: "",
type: "",
item_id: "",
count: 0,
2023-11-10 16:49:29 -05:00
scheme_id: 0,
2023-03-03 15:23:46 +00:00
};
// pay for the item insurance
output = this.paymentService.payMoney(pmcData, options, sessionID, output);
if (output.warnings.length > 0)
{
return output;
}
// add items to InsuredItems list once money has been paid
for (const key of body.items)
{
pmcData.InsuredItems.push({tid: body.tid, itemId: inventoryItemsHash[key]._id});
2023-03-03 15:23:46 +00:00
}
this.profileHelper.addSkillPointsToPlayer(pmcData, SkillTypes.CHARISMA, itemsToInsureCount * 0.01);
2023-03-03 15:23:46 +00:00
return output;
}
/**
* Handle client/insurance/items/list/cost
2023-03-03 15:23:46 +00:00
* Calculate insurance cost
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
*
* @param request request object
2023-03-03 15:23:46 +00:00
* @param sessionID session id
* @returns IGetInsuranceCostResponseData object to send to client
*/
public cost(request: IGetInsuranceCostRequestData, sessionID: string): IGetInsuranceCostResponseData
2023-03-03 15:23:46 +00:00
{
const output: IGetInsuranceCostResponseData = {};
const pmcData = this.profileHelper.getPmcProfile(sessionID);
const inventoryItemsHash: Record<string, Item> = {};
for (const item of pmcData.Inventory.items)
{
inventoryItemsHash[item._id] = item;
}
// Loop over each trader in request
for (const trader of request.traders)
2023-03-03 15:23:46 +00:00
{
const items: Record<string, number> = {};
2023-03-03 15:23:46 +00:00
for (const itemId of request.items)
2023-03-03 15:23:46 +00:00
{
// Ensure hash has item in it
if (!inventoryItemsHash[itemId])
{
this.logger.debug(`Item with id: ${itemId} missing from player inventory, skipping`);
continue;
}
2023-11-10 16:49:29 -05:00
items[inventoryItemsHash[itemId]._tpl] = Math.round(
this.insuranceService.getPremium(pmcData, inventoryItemsHash[itemId], trader),
);
2023-03-03 15:23:46 +00:00
}
output[trader] = items;
}
return output;
}
}
Refactor of InsuranceController & New ItemHelper Methods (!151) This commit is my second go-around at refactoring the `InsuranceController`, attempting to improving the code's modularity, maintainability, and efficiency while squashing a few bugs along the way. 1. **InsuranceController.ts** - Removed `ITemplateItem` import, as it is no longer used. - Introduced the `adoptOrphanedItems` method to manage orphaned items in the insurance list. - Since "normal" items are individually rolled for deletion, and can be nested within one another, there are situations where a parent item is deleted, leaving its children orphaned. This method moves those orphaned children from their missing parent into the root of the insurance container. - Overhauled `findItemsToDelete` method to improve its efficiency and readability: - Divided the original monolithic method into smaller, specialized methods like `populateItemsMap`, `populateParentAttachmentsMap`, `processRegularItems`, and `processAttachments`. - Changed the return type to `Set<string>` for better performance. - Introduced `EnrichedItem` interface (a simple extension of the `Item` interface) to add additional item data, like `name` and `maxPrice` to `Item` objects as they're processed throughout the class. This is done in place of repeatedly querying for this data, or complicating return types. - Enhanced logging capabilities to debug the item deletion process. Due to the *current* lack of testing available I've stepped up the amount of debug logging that is done. This will hopefully help us find issues in the future. - Modified the `rollForItemDelete` method, now renamed to `rollForDelete`, to include more detailed logging, return a boolean directly, and changed the `insuredItem` parameter to be optional. - Added new methods for dealing with some of the particulars that arise from item adoption and creating item maps. - Improved inline comments and documentation for better code maintainability. 2. **ItemHelper.ts** - Added the `isRaidModdable` method to check if an item is *actually* modifiable in-raid, which takes into account not just the item, but the item that it's attached to. - Added the `getAttachmentMainParent` method to fetch the main parent item of a given attachment, useful for item hierarchy traversal. For example, if you pass it an item ID of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within other multiple containers. - Added the `isAttachmentAttached` method to check if an item is an attachment that is currently attached to its parent. **Fixes:** - Resolved an issue that caused item attachments from being property grouped together for deletion rolls. This issue prevented valuable attachments from being taken first. - Resolved an issue that caused child items being orphaned when their parent was removed due to an insurance roll. Probable cause of the bug that made the client spaz out and send repeated insurance packages to the profile---Though I'm still unable to reproduce. - Probably more... Co-authored-by: Refringe <brownelltyler@gmail.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/151 Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-committed-by: Refringe <refringe@noreply.dev.sp-tarkov.com>
2023-10-14 09:05:49 +00:00
// Represents an insurance item that has had it's common locale-name and max price added to it.
interface EnrichedItem extends Item
{
name: string;
maxPrice: number;
}