Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ csharp_space_between_square_brackets = false
resharper_wrap_object_and_collection_initializer_style = chop_always
resharper_merge_into_pattern_highlighting = none
resharper_member_can_be_made_static_global_highlighting = none
resharper_possible_multiple_enumeration_highlighting = none

# Ignored warnings
dotnet_diagnostic.CS1591.severity = none
Expand Down
112 changes: 73 additions & 39 deletions Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -897,39 +897,44 @@ public void ReplaceRootItemId(IEnumerable<Item> itemWithChildren, MongoId newId)
// Update all parentIds of items attached to base item to use new id
foreach (var item in itemWithChildren)
{
if (item.ParentId == oldId)
if (item.ParentId != null && item.ParentId == oldId)
{
item.ParentId = newId;
}
}
}

/// <summary>
/// TODO - Write
/// Regenerate all GUIDs with new IDs, except special item types (e.g. quest, sorting table, etc.) This
/// function will not mutate the original items list, but will return a new list with new GUIDs.
/// </summary>
/// <param name="inventory"></param>
/// <param name="insuredItems"></param>
/// <param name="inventory">Inventory to replace Ids in</param>
/// <param name="insuredItems">Insured items that should not have their IDs replaced</param>
public void ReplaceProfileInventoryIds(BotBaseInventory inventory, IEnumerable<InsuredItem>? insuredItems = null)
{
// Blacklist
var itemIdBlacklist = new HashSet<MongoId>();
itemIdBlacklist.UnionWith(
new List<MongoId>
{
inventory.Equipment.Value,
inventory.QuestRaidItems.Value,
inventory.QuestStashItems.Value,
inventory.SortingTable.Value,
inventory.Stash.Value,
inventory.HideoutCustomizationStashId.Value,
inventory.Equipment!.Value,
inventory.QuestRaidItems!.Value,
inventory.QuestStashItems!.Value,
inventory.SortingTable!.Value,
inventory.Stash!.Value,
inventory.HideoutCustomizationStashId!.Value,
}
);
itemIdBlacklist.UnionWith(inventory.HideoutAreaStashes.Values);

if (inventory.HideoutAreaStashes != null)
{
itemIdBlacklist.UnionWith(inventory.HideoutAreaStashes.Values);
}

// Add insured items ids to blacklist
if (insuredItems is not null)
{
itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId.Value));
itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId!.Value));
}

if (inventory.Items is null)
Expand Down Expand Up @@ -967,9 +972,12 @@ public void ReplaceProfileInventoryIds(BotBaseInventory inventory, IEnumerable<I
}

// Update quick-slot id
if (inventory.FastPanel.ContainsKey(originalId))
var fastPanel = inventory.FastPanel;
if (fastPanel.ContainsValue(originalId) && !TryReplaceFastPanelId(fastPanel, originalId, newId))
{
inventory.FastPanel[originalId] = newId;
logger.Error(
$"Original Id: {originalId.ToString()} is contained in the fast panel, but was unable to replace it with new Id: {newId.ToString()}"
);
}
}
}
Expand All @@ -992,21 +1000,24 @@ public IEnumerable<Item> ReplaceIDs(IEnumerable<Item> originalItems, PmcData? pm
itemIdBlacklist.UnionWith(
new List<MongoId>
{
pmcData.Inventory.Equipment.Value,
pmcData.Inventory.QuestRaidItems.Value,
pmcData.Inventory.QuestStashItems.Value,
pmcData.Inventory.SortingTable.Value,
pmcData.Inventory.Stash.Value,
pmcData.Inventory.HideoutCustomizationStashId.Value,
pmcData.Inventory!.Equipment!.Value,
pmcData.Inventory.QuestRaidItems!.Value,
pmcData.Inventory.QuestStashItems!.Value,
pmcData.Inventory.SortingTable!.Value,
pmcData.Inventory.Stash!.Value,
pmcData.Inventory.HideoutCustomizationStashId!.Value,
}
);
itemIdBlacklist.UnionWith(pmcData.Inventory.HideoutAreaStashes.Values);
if (pmcData.Inventory?.HideoutAreaStashes != null)
{
itemIdBlacklist.UnionWith(pmcData.Inventory.HideoutAreaStashes.Values);
}
}

// Add insured items ids to blacklist
if (insuredItems is not null)
{
itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId.Value));
itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId!.Value));
}

foreach (var item in originalItems)
Expand All @@ -1026,7 +1037,7 @@ public IEnumerable<Item> ReplaceIDs(IEnumerable<Item> originalItems, PmcData? pm
item.Id = newId;

// Find all children of item and update their parent ids to match
var childItems = originalItems.Where(x => x.ParentId == originalId);
var childItems = originalItems.Where(x => x.ParentId != null && x.ParentId == originalId);
foreach (var childItem in childItems)
{
childItem.ParentId = newId;
Expand All @@ -1039,16 +1050,37 @@ public IEnumerable<Item> ReplaceIDs(IEnumerable<Item> originalItems, PmcData? pm
}

// Update quick-slot id
// TODO: i dont think the fast panel key is a mongoid, it should be e.g. "Item4"
if (pmcData.Inventory.FastPanel.ContainsKey(originalId))
var fastPanel = pmcData.Inventory.FastPanel;
if (fastPanel.ContainsValue(originalId) && !TryReplaceFastPanelId(fastPanel, originalId, newId))
{
pmcData.Inventory.FastPanel[originalId] = newId;
logger.Error(
$"Original Id: {originalId.ToString()} is contained in the fast panel, but was unable to replace it with new Id: {newId.ToString()}"
);
}
}

return originalItems;
}

/// <summary>
/// Trys to find the original id in FastPanel, if it exists set it to the new value
/// </summary>
/// <param name="fastPanel">Fast panel dictionary to check</param>
/// <param name="originalId">Original id of the item</param>
/// <param name="newId">New Id of the item</param>
/// <returns>True if replaced, otherwise false</returns>
public bool TryReplaceFastPanelId(Dictionary<string, MongoId> fastPanel, MongoId originalId, MongoId newId)
{
var key = fastPanel.FirstOrDefault(kvp => kvp.Value == originalId).Key;
if (key is null)
{
return false;
}

fastPanel[key] = newId;
return true;
}

/// <summary>
/// Mark the passed in list of items as found in raid.
/// Modifies passed in items
Expand Down Expand Up @@ -1122,7 +1154,7 @@ public void SetFoundInRaid(Item item, bool excludeCurrency = true)
if (parentTemplate.Key && parentTemplate.Value?.Properties?.Slots != null)
{
isRequiredSlot =
parentTemplate.Value?.Properties?.Slots?.Any(slot => slot?.Name == item?.SlotId && (slot?.Required ?? false)) ?? false;
parentTemplate.Value?.Properties?.Slots?.Any(slot => slot.Name == item.SlotId && (slot.Required ?? false)) ?? false;
}

return itemTemplate.Key && parentTemplate.Key && !(isNotRaidModdable || isRequiredSlot);
Expand All @@ -1147,7 +1179,7 @@ public void SetFoundInRaid(Item item, bool excludeCurrency = true)

while (currentItem != null && IsAttachmentAttached(currentItem))
{
currentItem = itemsMap.FirstOrDefault(kvp => kvp.Key == currentItem.ParentId).Value;
currentItem = itemsMap.FirstOrDefault(kvp => kvp.Key == currentItem.ParentId!).Value;
if (currentItem == null)
{
return null;
Expand Down Expand Up @@ -1246,10 +1278,10 @@ public bool IsAttachmentAttached(Item item)
// Calculating child ExtraSize
if (itemDbTemplate?.Properties?.ExtraSizeForceAdd ?? false)
{
forcedUp += itemDbTemplate.Properties.ExtraSizeUp.Value;
forcedDown += itemDbTemplate.Properties.ExtraSizeDown.Value;
forcedLeft += itemDbTemplate.Properties.ExtraSizeLeft.Value;
forcedRight += itemDbTemplate.Properties.ExtraSizeRight.Value;
forcedUp += itemDbTemplate.Properties.ExtraSizeUp!.Value;
forcedDown += itemDbTemplate.Properties.ExtraSizeDown!.Value;
forcedLeft += itemDbTemplate.Properties.ExtraSizeLeft!.Value;
forcedRight += itemDbTemplate.Properties.ExtraSizeRight!.Value;
}
else
{
Expand All @@ -1275,10 +1307,10 @@ public bool IsAttachmentAttached(Item item)
/// <returns>Valid caliber for cartridge</returns>
public MongoId? GetRandomCompatibleCaliberTemplateId(TemplateItem item)
{
var cartridges = item?.Properties?.Cartridges?.FirstOrDefault()?.Props?.Filters?.FirstOrDefault()?.Filter;
var cartridges = item.Properties?.Cartridges?.FirstOrDefault()?.Props?.Filters?.FirstOrDefault()?.Filter;
if (cartridges is null)
{
logger.Warning($"Failed to find cartridge for item: {item?.Id} {item?.Name}");
logger.Warning($"Failed to find cartridge for item: {item.Id} {item.Name}");
return null;
}

Expand All @@ -1294,7 +1326,7 @@ public void AddCartridgesToAmmoBox(List<Item> ammoBox, TemplateItem ammoBoxDetai
{
var ammoBoxMaxCartridgeCount = ammoBoxDetails.Properties?.StackSlots?.First().MaxCount;
var cartridgeTpl = ammoBoxDetails.Properties?.StackSlots?.First().Props?.Filters?.First().Filter?.FirstOrDefault();
var cartridgeDetails = GetItem(cartridgeTpl.Value);
var cartridgeDetails = GetItem(cartridgeTpl!.Value);
var cartridgeMaxStackSize = cartridgeDetails.Value?.Properties?.StackMaxSize;

// Exit early if ammo already exists in box
Expand Down Expand Up @@ -1339,7 +1371,7 @@ public void AddSingleStackCartridgesToAmmoBox(List<Item> ammoBox, TemplateItem a
{
var ammoBoxMaxCartridgeCount = ammoBoxDetails.Properties?.StackSlots?.First().MaxCount ?? 0;
var cartridgeTpl = ammoBoxDetails.Properties?.StackSlots?.First().Props?.Filters?.First().Filter?.FirstOrDefault();
ammoBox.Add(CreateCartridges(ammoBox[0].Id, cartridgeTpl.Value, (int)ammoBoxMaxCartridgeCount, 0));
ammoBox.Add(CreateCartridges(ammoBox[0].Id, cartridgeTpl!.Value, (int)ammoBoxMaxCartridgeCount, 0));
}

/// <summary>
Expand Down Expand Up @@ -1470,7 +1502,7 @@ public void FillMagazineWithCartridge(
CreateCartridges(magazineWithChildCartridges[0].Id, cartridgeTpl, cartridgeCountToAdd ?? 0, location)
);

currentStoredCartridgeCount += cartridgeCountToAdd.Value;
currentStoredCartridgeCount += cartridgeCountToAdd!.Value;
location++;
}

Expand Down Expand Up @@ -1547,7 +1579,9 @@ public void FillMagazineWithCartridge(
continue;
}

ammoArray.Add(new ProbabilityObject<MongoId, float?>(ammoDetails.Tpl.Value, (double)ammoDetails.RelativeProbability, null));
ammoArray.Add(
new ProbabilityObject<MongoId, float?>(ammoDetails.Tpl.Value, (double)ammoDetails.RelativeProbability!.Value, null)
);
}

return ammoArray.Draw().FirstOrDefault();
Expand Down Expand Up @@ -1757,7 +1791,7 @@ public List<Item> ReparentItemAndChildren(Item rootItem, List<Item> itemWithChil
if (mod.ParentId != null && (!idMappings.ContainsKey(mod.ParentId) || idMappings?[mod.ParentId] is null))
// Make remapping for items parentId
{
idMappings[mod.ParentId] = new MongoId();
idMappings![mod.ParentId] = new MongoId();
}

mod.Id = idMappings[mod.Id.ToString()];
Expand Down
Loading