From e32bace4d6f393f5073c35bea18266ed2d488779 Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 26 Sep 2025 13:16:47 +0100 Subject: [PATCH] Improved dict access inside `SetTraderUnlockedState` + comment and naming improvements --- .../Helpers/TraderHelper.cs | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/TraderHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/TraderHelper.cs index a94e4b88..527b1ea2 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/TraderHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/TraderHelper.cs @@ -227,15 +227,14 @@ public class TraderHelper( /// Alter a traders unlocked status /// /// Trader to alter - /// New status to use + /// New status to apply /// Session id of player public void SetTraderUnlockedState(MongoId traderId, bool status, MongoId sessionId) { var pmcData = profileHelper.GetPmcProfile(sessionId); - var profileTraderData = pmcData.TradersInfo[traderId]; - if (profileTraderData is null) + if (!pmcData.TradersInfo.TryGetValue(traderId, out var profileTraderData)) { - logger.Error($"Unable to set trader {traderId} unlocked state to: {status} as trader cannot be found in profile"); + logger.Error($"Unable to set trader: {traderId} unlocked state to: {status} as trader cannot be found in profile"); return; } @@ -258,7 +257,7 @@ public class TraderHelper( pmcTraderInfo.Standing = AddStandingValuesTogether(pmcTraderInfo.Standing, standingToAdd); if (traderId == Traders.FENCE) - // Must add rep to scav profile to ensure consistency + // Must add rep to scav profile to preserve consistency { fullProfile.CharacterData.ScavData.TradersInfo[traderId].Standing = pmcTraderInfo.Standing; } @@ -288,9 +287,9 @@ public class TraderHelper( { var profile = profileHelper.GetPmcProfile(sessionId); var traders = databaseService.GetTraders(); - foreach (var trader in traders) + foreach (var (traderId, _) in traders) { - LevelUp(trader.Key, profile); + LevelUp(traderId, profile); } } @@ -298,11 +297,11 @@ public class TraderHelper( /// Calculate trader's level based on experience amount and increments level if over threshold. /// Also validates and updates player level if not correct based on XP value. /// - /// Trader to check standing of. + /// Trader to check standing of. /// Profile to update trader in. - public void LevelUp(MongoId traderID, PmcData pmcData) + public void LevelUp(MongoId traderId, PmcData pmcData) { - var loyaltyLevels = databaseService.GetTrader(traderID).Base.LoyaltyLevels; + var loyaltyLevels = databaseService.GetTrader(traderId).Base.LoyaltyLevels; // Level up player pmcData.Info.Level = pmcData.CalculateLevel(databaseService.GetGlobals().Configuration.Exp.Level.ExperienceTable); @@ -311,36 +310,35 @@ public class TraderHelper( var targetLevel = 0; // Round standing to 2 decimal places to address floating point inaccuracies - pmcData.TradersInfo[traderID].Standing = Math.Round(pmcData.TradersInfo[traderID].Standing * 100 ?? 0, 2) / 100; + pmcData.TradersInfo[traderId].Standing = Math.Round(pmcData.TradersInfo[traderId].Standing * 100 ?? 0, 2) / 100; foreach (var loyaltyLevel in loyaltyLevels) { if ( loyaltyLevel.MinLevel <= pmcData.Info.Level - && loyaltyLevel.MinSalesSum <= pmcData.TradersInfo[traderID].SalesSum - && loyaltyLevel.MinStanding <= pmcData.TradersInfo[traderID].Standing + && loyaltyLevel.MinSalesSum <= pmcData.TradersInfo[traderId].SalesSum + && loyaltyLevel.MinStanding <= pmcData.TradersInfo[traderId].Standing && targetLevel < 4 ) - // level reached + // Level reached { targetLevel++; } } // set level - pmcData.TradersInfo[traderID].LoyaltyLevel = targetLevel; + pmcData.TradersInfo[traderId].LoyaltyLevel = targetLevel; } /// /// Get the next update timestamp for a trader. /// - /// Trader to look up update value for. + /// Trader to look up update value for. /// Future timestamp. - public long GetNextUpdateTimestamp(MongoId traderID) + public long GetNextUpdateTimestamp(MongoId traderId) { - var time = timeUtil.GetTimeStamp(); - var updateSeconds = GetTraderUpdateSeconds(traderID) ?? 0; - return time + updateSeconds; + var updateSeconds = GetTraderUpdateSeconds(traderId) ?? 0; + return timeUtil.GetTimeStamp() + updateSeconds; } /// @@ -375,12 +373,18 @@ public class TraderHelper( return randomUtil.GetInt(traderDetails.Seconds.Min, traderDetails.Seconds.Max); } - public TraderLoyaltyLevel GetLoyaltyLevel(MongoId traderID, PmcData pmcData) + /// + /// Get the loyalty level object a profile has with specified trader + /// + /// Trader id to get loyalty level of + /// Profile to look for loyalty data in + /// TraderLoyaltyLevel + public TraderLoyaltyLevel GetLoyaltyLevel(MongoId traderId, PmcData pmcData) { - var traderBase = databaseService.GetTrader(traderID).Base; + var traderBase = databaseService.GetTrader(traderId).Base; int? loyaltyLevel = null; - if (pmcData.TradersInfo.TryGetValue(traderID, out var traderInfo)) + if (pmcData.TradersInfo.TryGetValue(traderId, out var traderInfo)) { loyaltyLevel = traderInfo.LoyaltyLevel; } @@ -457,7 +461,7 @@ public class TraderHelper( } /// - /// EoD and Unheard get a 20% bonus to personal trader limit purchases + /// EoD and Unheard get a 20% bonus (1.2) to personal trader limit purchases /// /// Existing value from trader item /// Profiles game version @@ -466,6 +470,7 @@ public class TraderHelper( { if (GameVersionsWithHigherBuyRestrictions.Contains(gameVersion)) { + // TODO: move value into config return Math.Floor(buyRestrictionMax * 1.2); } @@ -485,7 +490,7 @@ public class TraderHelper( { highestPrice = 1d; // Default price var itemHandbookPrice = handbookHelper.GetTemplatePrice(tpl); - foreach ((var traderKey, var trader) in databaseService.GetTraders()) + foreach (var (_, trader) in databaseService.GetTraders()) { // Get trader and check buy category allows tpl var traderBase = trader.Base; @@ -516,7 +521,7 @@ public class TraderHelper( } /// - /// Accepts a trader id + /// Does the provided ID exist in db as a trader id /// /// Trader id /// True if a Trader exists with given ID