From c2a4877989c32f5124eb21080bb9081def2615d2 Mon Sep 17 00:00:00 2001 From: Chomp Date: Tue, 5 Aug 2025 16:30:53 +0100 Subject: [PATCH] Cleaned up `mergeItems()` logic --- .../SPT_Data/database/locales/server/en.json | 2 +- .../Controllers/InventoryController.cs | 49 ++++++++++--------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Libraries/SPTarkov.Server.Assets/SPT_Data/database/locales/server/en.json b/Libraries/SPTarkov.Server.Assets/SPT_Data/database/locales/server/en.json index 4b48f202..952741a1 100644 --- a/Libraries/SPTarkov.Server.Assets/SPT_Data/database/locales/server/en.json +++ b/Libraries/SPTarkov.Server.Assets/SPT_Data/database/locales/server/en.json @@ -73,7 +73,7 @@ "database-no_location_found_with_id": "No location found with an Id of: %s in database", "database-no_trader_found_with_id": "Unable to find trader: %s in database", "dialog-chatbot_id_already_exists": "Chat bot: %s being registered already exists, unable to register bot", - "dialog-missing_item_template": "Unable to find item tpl {{tpl}} in db, cannot send message of type {{type}}, skipping", + "dialog-missing_item_template": "Unable to find item tpl: {{tpl}} in db, cannot send message of type: {{type}}, skipping", "dialogue-unable_to_find_dialogs_in_profile": "No dialog object in profile: {{sessionId}}", "dialogue-list_from_client_empty": "No dialog object sent from client: {{sessionId}}", "dialogue-unable_to_find_in_profile": "No dialog in profile: {{sessionId}} found with id: {{dialogueId}}", diff --git a/Libraries/SPTarkov.Server.Core/Controllers/InventoryController.cs b/Libraries/SPTarkov.Server.Core/Controllers/InventoryController.cs index 7d4431ba..028256ce 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/InventoryController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/InventoryController.cs @@ -816,7 +816,7 @@ public class InventoryController( /// Client response public void MergeItem(PmcData pmcData, InventoryMergeRequestData request, MongoId sessionID, ItemEventRouterResponse output) { - // Changes made to result apply to character inventory + // Get source and destination inventories, they are not always the same (e.g. moving post-raid scav item stack into pmc inventory stack) var inventoryItems = inventoryHelper.GetOwnerInventoryItems(request, request.Item, sessionID); // Get source item (can be from player or trader or mail) @@ -843,43 +843,48 @@ public class InventoryController( return; } - if (destinationItem.Upd?.StackObjectsCount is null) - // No stackcount on destination, add one - { - destinationItem.Upd = new Upd { StackObjectsCount = 1 }; - } + EnsureItemHasValidStackCount(destinationItem); + EnsureItemHasValidStackCount(sourceItem); - if (sourceItem.Upd is null) - { - sourceItem.Upd = new Upd { StackObjectsCount = 1 }; - } - else if (sourceItem.Upd.StackObjectsCount is null) - // Items pulled out of raid can have no stack count if the stack should be 1 - { - sourceItem.Upd.StackObjectsCount = 1; - } - - // Remove FiR status from destination stack when source stack has no FiR but destination does + // Merging non Found in Raid (SpawnedInSession) items with FiR item causing result to be not Found in Raid if (!sourceItem.Upd.SpawnedInSession.GetValueOrDefault(false) && destinationItem.Upd.SpawnedInSession.GetValueOrDefault(false)) { destinationItem.Upd.SpawnedInSession = false; } - destinationItem.Upd.StackObjectsCount += sourceItem.Upd.StackObjectsCount; // Add source stackcount to destination + // Add source stackcount to destination + destinationItem.Upd.StackObjectsCount += sourceItem.Upd.StackObjectsCount; + + // Update output to inform client of source stack being deleted output.ProfileChanges[sessionID].Items.DeletedItems.Add(new DeletedItem { Id = sourceItem.Id }); // Inform client source item being deleted - var indexOfItemToRemove = inventoryItems.From.FindIndex(x => x.Id == sourceItem.Id); - if (indexOfItemToRemove == -1) + // Remove source item from server profile now it has been merged into destination item + if (!inventoryItems.From.Remove(sourceItem)) { var errorMessage = $"Unable to find item: {sourceItem.Id} to remove from sender inventory"; logger.Error(errorMessage); httpResponseUtil.AppendErrorToOutput(output, errorMessage); + } + } - return; + /// + /// Ensure an item has a upd object with a stack count of 1 + /// + /// Item to check + protected void EnsureItemHasValidStackCount(Item item) + { + if (item.Upd is null) + { + item.AddUpd(); + item.Upd.StackObjectsCount = 1; } - inventoryItems.From.RemoveAt(indexOfItemToRemove); // Remove source item from 'from' inventory + if (item.Upd.StackObjectsCount is null or 0) + { + // Items pulled out of raid can have no stack count, default to 1 + item.Upd.StackObjectsCount = 1; + } } ///