Skip to content

Commit

Permalink
Change ItemService.GetItemAndChildren to return List<ItemInstance> in…
Browse files Browse the repository at this point in the history
…stead of List<MongoId>
  • Loading branch information
nexus4880 committed Oct 31, 2024
1 parent ae41c05 commit fa53534
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
8 changes: 2 additions & 6 deletions Fuyu.Backend.BSG/Services/InventoryService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Fuyu.Backend.BSG.DTO.Profiles;
using Fuyu.Backend.EFT.DTO.Items;
using Fuyu.Common.Hashing;
Expand All @@ -11,13 +10,10 @@ public static class InventoryService
public static List<ItemInstance> RemoveItem(InventoryInfo inventory, ItemInstance item)
{
var itemsToRemove = ItemService.GetItemAndChildren(inventory.items, item);
// TODO: Change this later, I would rather itemsToRemove be List<ItemInstance>
// -- nexus4880, 2024-10-26
var itemInstances = inventory.items.Where(i => itemsToRemove.Contains(i._id)).ToList();

inventory.items.RemoveAll(i => itemsToRemove.Contains(i._id));
inventory.items.RemoveAll(i => itemsToRemove.Contains(i));

return itemInstances;
return itemsToRemove;
}

// NOTE:
Expand Down
14 changes: 7 additions & 7 deletions Fuyu.Backend.BSG/Services/ItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@ public static void RegenerateItemIds(List<ItemInstance> items)
RegenerateItemIds(items, mapping);
}

public static List<MongoId> GetItemAndChildren(List<ItemInstance> items, ItemInstance item)
public static List<ItemInstance> GetItemAndChildren(List<ItemInstance> items, ItemInstance item)
{
return GetItemAndChildren(items, item._id);
}

public static List<MongoId> GetItemAndChildren(List<ItemInstance> items, MongoId id)
public static List<ItemInstance> GetItemAndChildren(List<ItemInstance> items, MongoId id)
{
List<MongoId> itemsToRemove = [id];
List<MongoId> idsToReturn = [id];
bool foundNewItem = true;
while (foundNewItem)
{
foundNewItem = false;
var found = items.Where(
i => !itemsToRemove.Contains(i._id)
i => !idsToReturn.Contains(i._id)
&& i.parentId.HasValue
&& itemsToRemove.Contains(i.parentId.Value));
&& idsToReturn.Contains(i.parentId.Value));

if (found.Any())
{
foundNewItem = true;
itemsToRemove.AddRange(found.Select(i => i._id));
idsToReturn.AddRange(found.Select(i => i._id));
}
}

return itemsToRemove;
return items.Where(i => idsToReturn.Contains(i._id)).ToList();
}
}
}

0 comments on commit fa53534

Please sign in to comment.