diff --git a/Libraries/Core/Callbacks/SaveCallbacks.cs b/Libraries/Core/Callbacks/SaveCallbacks.cs index dba662dd..5957b62c 100644 --- a/Libraries/Core/Callbacks/SaveCallbacks.cs +++ b/Libraries/Core/Callbacks/SaveCallbacks.cs @@ -1,4 +1,4 @@ -using SptCommon.Annotations; +using SptCommon.Annotations; using Core.DI; using Core.Models.Spt.Config; using Core.Servers; @@ -23,9 +23,9 @@ public class SaveCallbacks( _saveServer.Load(); } - public Task OnUpdate(long SecondsSinceLastRun) + public Task OnUpdate(long secondsSinceLastRun) { - if (SecondsSinceLastRun > _coreConfig.ProfileSaveIntervalInSeconds) + if (secondsSinceLastRun > _coreConfig.ProfileSaveIntervalInSeconds) { _saveServer.Save(); return Task.FromResult(true); diff --git a/Libraries/Core/Helpers/InventoryHelper.cs b/Libraries/Core/Helpers/InventoryHelper.cs index 671483dc..3066f8b6 100644 --- a/Libraries/Core/Helpers/InventoryHelper.cs +++ b/Libraries/Core/Helpers/InventoryHelper.cs @@ -3,6 +3,7 @@ using Core.Models.Eft.Common; using Core.Models.Eft.Common.Tables; using Core.Models.Eft.Inventory; using Core.Models.Eft.ItemEvent; +using Core.Models.Eft.Profile; using Core.Models.Enums; using Core.Models.Spt.Config; using Core.Models.Spt.Inventory; @@ -10,6 +11,7 @@ using Core.Models.Utils; using Core.Servers; using Core.Services; using Core.Utils; +using Core.Utils.Cloners; using SptCommon.Annotations; namespace Core.Helpers; @@ -17,19 +19,18 @@ namespace Core.Helpers; [Injectable] public class InventoryHelper( ISptLogger _logger, - HashUtil hashUtil, - HttpResponseUtil httpResponseUtil, - FenceService fenceService, - DialogueHelper dialogueHelper, - ContainerHelper containerHelper, - DatabaseServer databaseServer, - PaymentHelper paymentHelper, - TraderAssortHelper traderAssortHelper, - ProfileHelper _profileHelper, + HashUtil _hashUtil, + HttpResponseUtil _httpResponseUtil, + FenceService _fenceService, DialogueHelper _dialogueHelper, ContainerHelper _containerHelper, + DatabaseServer _databaseServer, + PaymentHelper _paymentHelper, + TraderAssortHelper _traderAssortHelper, + ProfileHelper _profileHelper, ItemHelper _itemHelper, - LocalisationService _localisationService + LocalisationService _localisationService, + ICloner _cloner ) { /// @@ -45,7 +46,32 @@ public class InventoryHelper( PmcData pmcData, ItemEventRouterResponse output) { - throw new NotImplementedException(); + // Check all items fit into inventory before adding + if (!CanPlaceItemsInInventory(sessionId, request.ItemsWithModsToAdd)) + { + // No space, exit + _httpResponseUtil.AppendErrorToOutput( + output, + _localisationService.GetText("inventory-no_stash_space"), + BackendErrorCodes.NotEnoughSpace); + + return; + } + + foreach (var itemToAdd in request.ItemsWithModsToAdd) { + var addItemRequest = new AddItemDirectRequest{ + ItemWithModsToAdd = itemToAdd, + FoundInRaid = request.FoundInRaid, + UseSortingTable = request.UseSortingTable, + Callback = request.Callback }; + + // Add to player inventory + AddItemToStash(sessionId, addItemRequest, pmcData, output); + if (output.Warnings.Count > 0) + { + return; + } + } } /// @@ -61,7 +87,61 @@ public class InventoryHelper( PmcData pmcData, ItemEventRouterResponse output) { - throw new NotImplementedException(); + var itemWithModsToAddClone = _cloner.Clone(request.ItemWithModsToAdd); + + // Get stash layouts ready for use + var stashFS2D = GetStashSlotMap(pmcData, sessionId); + if (stashFS2D is null) + { + _logger.Error("Unable to get stash map for players: { sessionId} stash"); + + return; + } + var sortingTableFS2D = GetSortingTableSlotMap(pmcData); + + // Find empty slot in stash for item being added - adds 'location' + parentid + slotId properties to root item + PlaceItemInInventory( + stashFS2D, + sortingTableFS2D, + itemWithModsToAddClone, + pmcData.Inventory, + request.UseSortingTable.GetValueOrDefault(false), + output); + if (output.Warnings.Count > 0) + { + // Failed to place, error out + return; + } + + // Apply/remove FiR to item + mods + SetFindInRaidStatusForItem(itemWithModsToAddClone, request.FoundInRaid.GetValueOrDefault(false)); + + // Remove trader properties from root item + RemoveTraderRagfairRelatedUpdProperties(itemWithModsToAddClone[0].Upd); + + // Run callback + try + { + if (request.Callback is not null) + { + request.Callback(itemWithModsToAddClone.FirstOrDefault().Upd.StackObjectsCount.Value); + } + } + catch (Exception ex) + { + // Callback failed + var message = ex.Message; + + _httpResponseUtil.AppendErrorToOutput(output, message); + + return; + } + + // Add item + mods to output and profile inventory + output.ProfileChanges[sessionId].Items.NewItems.AddRange(itemWithModsToAddClone.Select(x => x.ConvertToProduct())); + pmcData.Inventory.Items.AddRange(itemWithModsToAddClone); + + _logger.Debug($"Added ${ itemWithModsToAddClone[0].Upd?.StackObjectsCount ?? 1} item: ${itemWithModsToAddClone[0].Template } with: ${ itemWithModsToAddClone.Count - 1} mods to inventory"); } /// @@ -210,8 +290,8 @@ public class InventoryHelper( /// Should sorting table to be used if main stash has no space /// Output to send back to client protected void PlaceItemInInventory( - List> stashFS2D, - List> sortingTableFS2D, + int[][] stashFS2D, + int[][] sortingTableFS2D, List itemWithChildren, BotBaseInventory playerInventory, bool useSortingTable, diff --git a/Libraries/Core/Models/Eft/Inventory/AddItemDirectRequest.cs b/Libraries/Core/Models/Eft/Inventory/AddItemDirectRequest.cs index ae6937ee..2804d64d 100644 --- a/Libraries/Core/Models/Eft/Inventory/AddItemDirectRequest.cs +++ b/Libraries/Core/Models/Eft/Inventory/AddItemDirectRequest.cs @@ -1,4 +1,4 @@ -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; using Core.Models.Eft.Common.Tables; namespace Core.Models.Eft.Inventory; @@ -15,7 +15,7 @@ public record AddItemDirectRequest public bool? FoundInRaid { get; set; } [JsonPropertyName("callback")] - public Action? Callback { get; set; } + public Action? Callback { get; set; } [JsonPropertyName("useSortingTable")] public bool? UseSortingTable { get; set; } diff --git a/Libraries/Core/Models/Eft/Inventory/AddItemsDirectRequest.cs b/Libraries/Core/Models/Eft/Inventory/AddItemsDirectRequest.cs index 3b865d5b..49329f4c 100644 --- a/Libraries/Core/Models/Eft/Inventory/AddItemsDirectRequest.cs +++ b/Libraries/Core/Models/Eft/Inventory/AddItemsDirectRequest.cs @@ -1,4 +1,4 @@ -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; using Core.Models.Eft.Common.Tables; namespace Core.Models.Eft.Inventory; @@ -14,7 +14,7 @@ public record AddItemsDirectRequest /// Runs after EACH item with children is added [JsonPropertyName("callback")] - public Action? Callback { get; set; } + public Action? Callback { get; set; } /// Should sorting table be used when no space found in stash [JsonPropertyName("useSortingTable")]