Cleaned up mergeItems() logic

This commit is contained in:
Chomp
2025-08-05 16:30:53 +01:00
parent 02b80888ec
commit c2a4877989
2 changed files with 28 additions and 23 deletions
@@ -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}}",
@@ -816,7 +816,7 @@ public class InventoryController(
/// <param name="output">Client response</param>
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;
/// <summary>
/// Ensure an item has a upd object with a stack count of 1
/// </summary>
/// <param name="item">Item to check</param>
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;
}
}
/// <summary>