Cleaned up mergeItems() logic
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user