Updated UpdateFenceAssorts() to add a upd object when missing

Updated various methods to use library functionality

Improved string compare inside `RandomiseArmorInsertsDurabilities()`

Comment improvements
This commit is contained in:
Chomp
2025-05-21 12:02:12 +01:00
parent d88db583c1
commit bdde7d7454
@@ -404,7 +404,7 @@ public class FenceService(
{
var firstItem = itemWithChildren.FirstOrDefault(x => x != null);
logger.Error(
$"Unable to process fence assort as root item is missing, {firstItem?.Template}, skipping"
$"Unable to process fence assort as root item is missing: {firstItem?.Template}, skipping"
);
continue;
}
@@ -425,24 +425,22 @@ public class FenceService(
// Guard against a missing stack count
if (existingRootItem.Upd?.StackObjectsCount == null)
{
existingRootItem.Upd ??= new Upd();
existingRootItem.Upd.StackObjectsCount = 1;
}
// Merge new items count into existing, dont add new loyalty/barter data as it already exists
// Merge new items count into existing, don't add new loyalty/barter data as it already exists
existingRootItem.Upd.StackObjectsCount += newRootItem?.Upd?.StackObjectsCount ?? 1;
continue;
}
}
// if the Upd doesnt exist just initialize it
if (newRootItem.Upd == null)
// if the Upd doesn't exist just initialize it
newRootItem.Upd ??= new Upd
{
newRootItem.Upd = new Upd
{
StackObjectsCount = 1
};
}
StackObjectsCount = 1
};
// New assort to be added to existing assorts
existingFenceAssorts.Items.AddRange(itemWithChildren);
@@ -583,7 +581,7 @@ public class FenceService(
/// <summary>
/// Get the count of items fence offers
/// </summary>
/// <returns> Number </returns>
/// <returns> Count of fence offers </returns>
public int GetOfferCount()
{
if ((fenceAssort?.Items?.Count ?? 0) == 0)
@@ -627,9 +625,9 @@ public class FenceService(
protected TraderAssort ConvertIntoFenceAssort(CreateFenceAssortsResult intermediaryAssorts)
{
var result = CreateFenceAssortSkeleton();
foreach (var itemWithChilden in intermediaryAssorts.SptItems)
foreach (var itemWithChildren in intermediaryAssorts.SptItems)
{
result.Items.AddRange(itemWithChilden);
result.Items.AddRange(itemWithChildren);
}
result.BarterScheme = intermediaryAssorts.BarterScheme;
@@ -726,8 +724,8 @@ public class FenceService(
}
if (itemCounts.WeaponPreset > 0 || itemCounts.EquipmentPreset > 0)
// Add presets
{
AddPresetsToAssort(
itemCounts.WeaponPreset,
itemCounts.EquipmentPreset,
@@ -833,7 +831,7 @@ public class FenceService(
RandomiseItemUpdProperties(itemDbDetails, rootItemBeingAdded);
}
// Skip items already in the assort if it exists in the prevent duplicate list
// Skip items already in the assort if it exists in the 'prevent duplicate' list
var existingItemThatMatches = GetMatchingItem(rootItemBeingAdded, itemDbDetails, assorts.SptItems);
var shouldBeStacked = ItemShouldBeForceStacked(existingItemThatMatches, itemDbDetails);
if (shouldBeStacked && existingItemThatMatches != null)
@@ -845,7 +843,7 @@ public class FenceService(
continue;
}
// Add mods to armors so they dont show as red in the trade screen
// Add mods to armors so they don't show as red in the trade screen
if (itemHelper.ItemRequiresSoftInserts(rootItemBeingAdded.Template))
{
RandomiseArmorModDurability(desiredAssortItemAndChildrenClone, itemDbDetails);
@@ -912,7 +910,7 @@ public class FenceService(
) &&
(itemDbDetails.Properties.Slots?.Count ?? 0) > 0;
// Only one match and its not medical or armored gear
// Only one match and it's not medical or armored gear
if (matchingItems.Count == 1 && !(isMedical || isGearAndHasSlots))
{
return matchingItems[0];
@@ -1132,7 +1130,7 @@ public class FenceService(
itemHelper.FindAndReturnChildrenAsItems(baseFenceAssort.Items, randomPresetRoot.Id)
);
// Need to add mods to armors so they dont show as red in the trade screen
// Need to add mods to armors so they don't show as red in the trade screen
if (itemHelper.ItemRequiresSoftInserts(randomPresetRoot.Template))
{
RandomiseArmorModDurability(presetWithChildrenClone, rootItemDb);
@@ -1158,7 +1156,7 @@ public class FenceService(
itemHelper.ReparentItemAndChildren(presetWithChildrenClone[0], presetWithChildrenClone);
itemHelper.RemapRootItemId(presetWithChildrenClone);
// Remapping IDs causes parentid to be altered
// Remapping IDs causes parentId to be altered
presetWithChildrenClone[0].ParentId = "hideout";
assorts.SptItems.Add(presetWithChildrenClone);
@@ -1240,14 +1238,12 @@ public class FenceService(
itemHelper.AddUpdObjectToItem(modItemToAdjust);
if (modItemToAdjust.Upd.Repairable == null)
// Ensure property isn't null
modItemToAdjust.Upd.Repairable ??= new UpdRepairable
{
modItemToAdjust.Upd.Repairable = new UpdRepairable
{
Durability = modItemDbDetails.Properties.MaxDurability,
MaxDurability = modItemDbDetails.Properties.MaxDurability
};
}
Durability = modItemDbDetails.Properties.MaxDurability,
MaxDurability = modItemDbDetails.Properties.MaxDurability
};
modItemToAdjust.Upd.Repairable.Durability = durabilityValues.Durability;
modItemToAdjust.Upd.Repairable.MaxDurability = durabilityValues.MaxDurability;
@@ -1293,7 +1289,7 @@ public class FenceService(
if (!randomUtil.GetChance100(plateExistsChance))
{
// Remove plate from armor
armorItemAndMods = armorItemAndMods.Where(item => item.SlotId.ToLower() != plateSlot.Name.ToLower())
armorItemAndMods = armorItemAndMods.Where(item => !string.Equals(item.SlotId, plateSlot.Name, StringComparison.CurrentCultureIgnoreCase))
.ToList();
continue;
@@ -1611,9 +1607,7 @@ public class FenceService(
public FenceLevel GetFenceInfo(PmcData pmcData)
{
var fenceSettings = databaseService.GetGlobals().Configuration.FenceSettings;
var pmcFenceInfo = pmcData.TradersInfo[fenceSettings.FenceIdentifier];
if (pmcFenceInfo == null)
if (!pmcData.TradersInfo.TryGetValue(fenceSettings.FenceIdentifier, out var pmcFenceInfo))
{
return fenceSettings.Levels[0];
}
@@ -1621,7 +1615,7 @@ public class FenceService(
var fenceLevels = fenceSettings.Levels.Keys;
var minLevel = fenceLevels.Min();
var maxLevel = fenceLevels.Max();
var pmcFenceLevel = Math.Floor((double) pmcFenceInfo.Standing);
var pmcFenceLevel = Math.Floor(pmcFenceInfo.Standing.Value);
if (pmcFenceLevel < minLevel)
{