Updated FindAndReturnChildrenByItems to be an extension method
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using SPTarkov.DI.Annotations;
|
using SPTarkov.DI.Annotations;
|
||||||
|
using SPTarkov.Server.Core.Extensions;
|
||||||
using SPTarkov.Server.Core.Helpers;
|
using SPTarkov.Server.Core.Helpers;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||||
@@ -311,8 +312,7 @@ public class QuestController(
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Remove item with children
|
// Remove item with children
|
||||||
var toRemove = _itemHelper.FindAndReturnChildrenByItems(
|
var toRemove = pmcData.Inventory.Items.FindAndReturnChildrenByItems(
|
||||||
pmcData.Inventory.Items,
|
|
||||||
itemHandover.Id
|
itemHandover.Id
|
||||||
);
|
);
|
||||||
var index = pmcData.Inventory.Items.Count;
|
var index = pmcData.Inventory.Items.Count;
|
||||||
|
|||||||
@@ -191,5 +191,37 @@ namespace SPTarkov.Server.Core.Extensions
|
|||||||
|
|
||||||
return items;
|
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.DI.Annotations;
|
||||||
|
using SPTarkov.Server.Core.Extensions;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||||
using SPTarkov.Server.Core.Models.Enums;
|
using SPTarkov.Server.Core.Models.Enums;
|
||||||
@@ -182,7 +183,7 @@ public class AssortHelper(
|
|||||||
assort.LoyalLevelItems.Remove(itemId);
|
assort.LoyalLevelItems.Remove(itemId);
|
||||||
|
|
||||||
// The item being removed may have children linked to it, find and remove them too
|
// 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));
|
assort.Items.RemoveAll(item => idsToRemove.Contains(item.Id));
|
||||||
|
|
||||||
return assort;
|
return assort;
|
||||||
|
|||||||
@@ -1182,7 +1182,7 @@ public class InventoryHelper(
|
|||||||
HandleCartridges(sourceItems, request);
|
HandleCartridges(sourceItems, request);
|
||||||
|
|
||||||
// Get all children item has, they need to move with item
|
// 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)
|
foreach (var itemId in idsToMove)
|
||||||
{
|
{
|
||||||
var itemToMove = sourceItems.FirstOrDefault(item => item.Id == itemId);
|
var itemToMove = sourceItems.FirstOrDefault(item => item.Id == itemId);
|
||||||
|
|||||||
@@ -728,29 +728,6 @@ public class ItemHelper(
|
|||||||
return Math.Sqrt(durability ?? 0);
|
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>
|
/// <summary>
|
||||||
/// A variant of FindAndReturnChildren where the output is list of item objects instead of their ids.
|
/// A variant of FindAndReturnChildren where the output is list of item objects instead of their ids.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Frozen;
|
using System.Collections.Frozen;
|
||||||
using SPTarkov.DI.Annotations;
|
using SPTarkov.DI.Annotations;
|
||||||
|
using SPTarkov.Server.Core.Extensions;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Profile;
|
using SPTarkov.Server.Core.Models.Eft.Profile;
|
||||||
@@ -378,8 +379,7 @@ public class ProfileHelper(
|
|||||||
if (secureContainer is not null)
|
if (secureContainer is not null)
|
||||||
{
|
{
|
||||||
// Find and remove container + children
|
// Find and remove container + children
|
||||||
var childItemsInSecureContainer = _itemHelper.FindAndReturnChildrenByItems(
|
var childItemsInSecureContainer = items.FindAndReturnChildrenByItems(
|
||||||
items,
|
|
||||||
secureContainer.Id
|
secureContainer.Id
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using SPTarkov.DI.Annotations;
|
using SPTarkov.DI.Annotations;
|
||||||
|
using SPTarkov.Server.Core.Extensions;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
|
||||||
|
|
||||||
namespace SPTarkov.Server.Core.Helpers;
|
namespace SPTarkov.Server.Core.Helpers;
|
||||||
@@ -21,10 +22,7 @@ public class SecureContainerHelper(ItemHelper _itemHelper)
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var itemsInSecureContainer = _itemHelper.FindAndReturnChildrenByItems(
|
var itemsInSecureContainer = items.FindAndReturnChildrenByItems(secureContainer.Id);
|
||||||
items,
|
|
||||||
secureContainer.Id
|
|
||||||
);
|
|
||||||
|
|
||||||
// Return all items returned and exclude the secure container item itself
|
// Return all items returned and exclude the secure container item itself
|
||||||
return itemsInSecureContainer.Where(x => x != secureContainer.Id).ToList();
|
return itemsInSecureContainer.Where(x => x != secureContainer.Id).ToList();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using SPTarkov.Common.Extensions;
|
using SPTarkov.Common.Extensions;
|
||||||
using SPTarkov.DI.Annotations;
|
using SPTarkov.DI.Annotations;
|
||||||
|
using SPTarkov.Server.Core.Extensions;
|
||||||
using SPTarkov.Server.Core.Helpers;
|
using SPTarkov.Server.Core.Helpers;
|
||||||
using SPTarkov.Server.Core.Models.Common;
|
using SPTarkov.Server.Core.Models.Common;
|
||||||
using SPTarkov.Server.Core.Models.Eft.Common;
|
using SPTarkov.Server.Core.Models.Eft.Common;
|
||||||
@@ -1497,9 +1498,7 @@ public class FenceService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove item and its sub-items to prevent orphans
|
// Remove item and its sub-items to prevent orphans
|
||||||
toDelete.UnionWith(
|
toDelete.UnionWith(itemAndMods.FindAndReturnChildrenByItems(itemMod.Id));
|
||||||
itemHelper.FindAndReturnChildrenByItems(itemAndMods, itemMod.Id)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user