Updated FindAndReturnChildrenByItems to be an extension method
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Extensions;
|
||||
using SPTarkov.Server.Core.Helpers;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||
@@ -311,8 +312,7 @@ public class QuestController(
|
||||
else
|
||||
{
|
||||
// Remove item with children
|
||||
var toRemove = _itemHelper.FindAndReturnChildrenByItems(
|
||||
pmcData.Inventory.Items,
|
||||
var toRemove = pmcData.Inventory.Items.FindAndReturnChildrenByItems(
|
||||
itemHandover.Id
|
||||
);
|
||||
var index = pmcData.Inventory.Items.Count;
|
||||
|
||||
@@ -191,5 +191,37 @@ namespace SPTarkov.Server.Core.Extensions
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursive function that looks at every item from parameter and gets their children's Ids + includes parent item in results
|
||||
/// </summary>
|
||||
/// <param name="items">List of items (item + possible children)</param>
|
||||
/// <param name="baseItemId">Parent item's id</param>
|
||||
/// <returns>list of child item ids</returns>
|
||||
public static List<string> FindAndReturnChildrenByItems(
|
||||
this IEnumerable<Item> items,
|
||||
string baseItemId
|
||||
)
|
||||
{
|
||||
List<string> list = [];
|
||||
|
||||
foreach (var childItem in items)
|
||||
{
|
||||
if (
|
||||
string.Equals(
|
||||
childItem.ParentId,
|
||||
baseItemId,
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
)
|
||||
)
|
||||
{
|
||||
list.AddRange(FindAndReturnChildrenByItems(items, childItem.Id));
|
||||
}
|
||||
}
|
||||
|
||||
list.Add(baseItemId); // Required, push original item id onto array
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Extensions;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||
using SPTarkov.Server.Core.Models.Enums;
|
||||
@@ -182,7 +183,7 @@ public class AssortHelper(
|
||||
assort.LoyalLevelItems.Remove(itemId);
|
||||
|
||||
// The item being removed may have children linked to it, find and remove them too
|
||||
var idsToRemove = _itemHelper.FindAndReturnChildrenByItems(assort.Items, itemId);
|
||||
var idsToRemove = assort.Items.FindAndReturnChildrenByItems(itemId);
|
||||
assort.Items.RemoveAll(item => idsToRemove.Contains(item.Id));
|
||||
|
||||
return assort;
|
||||
|
||||
@@ -1182,7 +1182,7 @@ public class InventoryHelper(
|
||||
HandleCartridges(sourceItems, request);
|
||||
|
||||
// Get all children item has, they need to move with item
|
||||
var idsToMove = _itemHelper.FindAndReturnChildrenByItems(sourceItems, request.Item);
|
||||
var idsToMove = sourceItems.FindAndReturnChildrenByItems(request.Item);
|
||||
foreach (var itemId in idsToMove)
|
||||
{
|
||||
var itemToMove = sourceItems.FirstOrDefault(item => item.Id == itemId);
|
||||
|
||||
@@ -728,29 +728,6 @@ public class ItemHelper(
|
||||
return Math.Sqrt(durability ?? 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursive function that looks at every item from parameter and gets their children's Ids + includes parent item in results
|
||||
/// </summary>
|
||||
/// <param name="items">List of items (item + possible children)</param>
|
||||
/// <param name="baseItemId">Parent item's id</param>
|
||||
/// <returns>list of child item ids</returns>
|
||||
public List<string> FindAndReturnChildrenByItems(IEnumerable<Item> items, string baseItemId)
|
||||
{
|
||||
List<string> list = [];
|
||||
|
||||
foreach (var childItem in items)
|
||||
{
|
||||
if (string.Equals(childItem.ParentId, baseItemId, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
list.AddRange(FindAndReturnChildrenByItems(items, childItem.Id));
|
||||
}
|
||||
}
|
||||
|
||||
list.Add(baseItemId); // Required, push original item id onto array
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A variant of FindAndReturnChildren where the output is list of item objects instead of their ids.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Frozen;
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Extensions;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||
using SPTarkov.Server.Core.Models.Eft.Profile;
|
||||
@@ -378,8 +379,7 @@ public class ProfileHelper(
|
||||
if (secureContainer is not null)
|
||||
{
|
||||
// Find and remove container + children
|
||||
var childItemsInSecureContainer = _itemHelper.FindAndReturnChildrenByItems(
|
||||
items,
|
||||
var childItemsInSecureContainer = items.FindAndReturnChildrenByItems(
|
||||
secureContainer.Id
|
||||
);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Extensions;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||
|
||||
namespace SPTarkov.Server.Core.Helpers;
|
||||
@@ -21,10 +22,7 @@ public class SecureContainerHelper(ItemHelper _itemHelper)
|
||||
return [];
|
||||
}
|
||||
|
||||
var itemsInSecureContainer = _itemHelper.FindAndReturnChildrenByItems(
|
||||
items,
|
||||
secureContainer.Id
|
||||
);
|
||||
var itemsInSecureContainer = items.FindAndReturnChildrenByItems(secureContainer.Id);
|
||||
|
||||
// Return all items returned and exclude the secure container item itself
|
||||
return itemsInSecureContainer.Where(x => x != secureContainer.Id).ToList();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using SPTarkov.Common.Extensions;
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Extensions;
|
||||
using SPTarkov.Server.Core.Helpers;
|
||||
using SPTarkov.Server.Core.Models.Common;
|
||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||
@@ -1497,9 +1498,7 @@ public class FenceService(
|
||||
}
|
||||
|
||||
// Remove item and its sub-items to prevent orphans
|
||||
toDelete.UnionWith(
|
||||
itemHelper.FindAndReturnChildrenByItems(itemAndMods, itemMod.Id)
|
||||
);
|
||||
toDelete.UnionWith(itemAndMods.FindAndReturnChildrenByItems(itemMod.Id));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user