From d2267847a8b7b3c21dfd041a3807ca4daaf7b37b Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 27 Jun 2025 23:21:44 +0100 Subject: [PATCH] Deconstructed dictionaries to improve readability --- .../Controllers/AchievementController.cs | 2 +- .../Controllers/CustomizationController.cs | 8 +++---- .../Controllers/DialogueController.cs | 22 ++++++++++++----- .../Controllers/GameController.cs | 12 +++++----- .../Controllers/HealthController.cs | 8 +++---- .../Controllers/HideoutController.cs | 24 ++++++++++--------- .../Controllers/InsuranceController.cs | 4 ++-- .../Controllers/LauncherController.cs | 15 ++++++------ .../Controllers/LauncherV2Controller.cs | 24 ++++++++----------- .../Controllers/LocationController.cs | 6 ++--- .../Controllers/TraderController.cs | 8 +++---- .../Generators/BotInventoryGenerator.cs | 15 +++++------- 12 files changed, 76 insertions(+), 72 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Controllers/AchievementController.cs b/Libraries/SPTarkov.Server.Core/Controllers/AchievementController.cs index 7d8f0449..dc6da8d1 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/AchievementController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/AchievementController.cs @@ -51,7 +51,7 @@ public class AchievementController( ) { var profilesHaveAchievement = 0; - foreach (var (profileId, profile) in profiles) + foreach (var (_, profile) in profiles) { if (profile.CharacterData?.PmcData?.Achievements is null) { diff --git a/Libraries/SPTarkov.Server.Core/Controllers/CustomizationController.cs b/Libraries/SPTarkov.Server.Core/Controllers/CustomizationController.cs index 543d00ac..8c32a0aa 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/CustomizationController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/CustomizationController.cs @@ -209,14 +209,14 @@ public class CustomizationController( var traders = _databaseService.GetTraders(); var result = new List(); - foreach (var trader in traders) + foreach (var (traderId, trader) in traders) { if ( - trader.Value.Base?.CustomizationSeller is not null - && trader.Value.Base.CustomizationSeller.Value + trader.Base?.CustomizationSeller is not null + && trader.Base.CustomizationSeller.Value ) { - result.AddRange(GetTraderSuits(trader.Key, sessionId)); + result.AddRange(GetTraderSuits(traderId, sessionId)); } } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/DialogueController.cs b/Libraries/SPTarkov.Server.Core/Controllers/DialogueController.cs index 891c4a5e..31aa1a69 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/DialogueController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/DialogueController.cs @@ -127,10 +127,9 @@ public class DialogueController( public virtual List GenerateDialogueList(string sessionId) { var data = new List(); - foreach (var dialogueId in _dialogueHelper.GetDialogsForProfile(sessionId)) + foreach (var (_, dialog) in _dialogueHelper.GetDialogsForProfile(sessionId)) { - var dialogueInfo = GetDialogueInfo(dialogueId.Key, sessionId); - + var dialogueInfo = GetDialogueInfo(dialog, sessionId); if (dialogueInfo is null) { continue; @@ -153,6 +152,17 @@ public class DialogueController( var dialogs = _dialogueHelper.GetDialogsForProfile(sessionId); var dialogue = dialogs!.GetValueOrDefault(dialogueId); + return GetDialogueInfo(dialogue, sessionId); + } + + /// + /// Get the content of a dialogue + /// + /// Dialog + /// Session Id + /// DialogueInfo + public virtual DialogueInfo? GetDialogueInfo(Dialogue dialogue, string sessionId) + { if (!dialogue.Messages.Any()) { return null; @@ -160,7 +170,7 @@ public class DialogueController( var result = new DialogueInfo { - Id = dialogueId, + Id = dialogue.Id, Type = dialogue?.Type ?? MessageType.NpcTraderMessage, Message = _dialogueHelper.GetMessagePreview(dialogue), New = dialogue?.New, @@ -555,9 +565,9 @@ public class DialogueController( /// Session id protected void RemoveExpiredItemsFromMessages(string sessionId) { - foreach (var dialogueId in _dialogueHelper.GetDialogsForProfile(sessionId)) + foreach (var (dialogId, _) in _dialogueHelper.GetDialogsForProfile(sessionId)) { - RemoveExpiredItemsFromMessage(sessionId, dialogueId.Key); + RemoveExpiredItemsFromMessage(sessionId, dialogId); } } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs b/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs index 0d82dac0..607b9882 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs @@ -412,26 +412,26 @@ public class GameController( } // Look for effects - foreach (var effectKvP in bodyPart.Effects) + foreach (var (effectId, effect) in bodyPart.Effects) { // remove effects below 1, .e.g. bleeds at -1 - if (effectKvP.Value.Time < 1) + if (effect.Time < 1) { // More than 30 minutes has passed if (diffSeconds > 1800) { - bodyPart.Effects.Remove(effectKvP.Key); + bodyPart.Effects.Remove(effectId); } continue; } // Decrement effect time value by difference between current time and time health was last updated - effectKvP.Value.Time -= diffSeconds; - if (effectKvP.Value.Time < 1) + effect.Time -= diffSeconds; + if (effect.Time < 1) // Effect time was sub 1, set floor it can be { - effectKvP.Value.Time = 1; + effect.Time = 1; } } } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/HealthController.cs b/Libraries/SPTarkov.Server.Core/Controllers/HealthController.cs index 8c63c141..9345f45a 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/HealthController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/HealthController.cs @@ -103,11 +103,11 @@ public class HealthController( if (itemRemovesEffects && bodyPartToHeal.Effects is not null) { // Can remove effects and limb has effects to remove - foreach (var effectKvP in bodyPartToHeal.Effects) + foreach (var (effectId, _) in bodyPartToHeal.Effects) { // Check enum has effectType - if (!Enum.TryParse(effectKvP.Key, out var effect)) - // Enum doesnt contain this key + if (!Enum.TryParse(effectId, out var effect)) + // Enum doesn't contain this key { continue; } @@ -126,7 +126,7 @@ public class HealthController( // Adjust limb heal amount based on if it's fixing an effect (request.count is TOTAL cost of hp resource on heal item, NOT amount to heal limb) amountToHealLimb -= (int)(matchingEffectFromHealingItem.Cost ?? 0); - bodyPartToHeal.Effects.Remove(effectKvP.Key); + bodyPartToHeal.Effects.Remove(effectId); } } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/HideoutController.cs b/Libraries/SPTarkov.Server.Core/Controllers/HideoutController.cs index 58cccceb..1139d1d0 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/HideoutController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/HideoutController.cs @@ -965,20 +965,22 @@ public class HideoutController( // Validate that we have a matching production var productionDict = pmcData.Hideout.Production; string? prodId = null; - foreach (var production in productionDict) + foreach (var (productionId, production) in productionDict) { // Skip undefined production objects - if (production.Value is null) + if (production is null) + { + continue; + } + + if (production.RecipeId != request.RecipeId) { continue; } // Production or ScavCase - if (production.Value.RecipeId == request.RecipeId) - { - prodId = production.Key; // Set to objects key - break; - } + prodId = productionId; // Set to objects key + break; } // If we're unable to find the production, send an error to the client @@ -1794,17 +1796,17 @@ public class HideoutController( /// public void Update() { - foreach (var sessionID in _saveServer.GetProfiles()) + foreach (var (sessionId, profile) in _saveServer.GetProfiles()) { if ( - sessionID.Value.CharacterData.PmcData.Hideout is not null + profile.CharacterData.PmcData.Hideout is not null && _profileActivityService.ActiveWithinLastMinutes( - sessionID.Key, + sessionId, _hideoutConfig.UpdateProfileHideoutWhenActiveWithinMinutes ) ) { - _hideoutHelper.UpdatePlayerHideout(sessionID.Key); + _hideoutHelper.UpdatePlayerHideout(sessionId); } } } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/InsuranceController.cs b/Libraries/SPTarkov.Server.Core/Controllers/InsuranceController.cs index f1483ae4..4ab43697 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/InsuranceController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/InsuranceController.cs @@ -497,10 +497,10 @@ public class InsuranceController( _mathUtil, _cloner ); - foreach (var attachmentTpl in weightedAttachmentByPrice) + foreach (var (itemTpl, price) in weightedAttachmentByPrice) { attachmentsProbabilityArray.Add( - new ProbabilityObject(attachmentTpl.Key, attachmentTpl.Value, null) + new ProbabilityObject(itemTpl, price, null) ); } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs b/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs index 9cf545df..f83d4fd3 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs @@ -90,12 +90,12 @@ public class LauncherController( /// public string? Login(LoginRequestData? info) { - foreach (var kvp in _saveServer.GetProfiles()) + foreach (var (sessionId, profile) in _saveServer.GetProfiles()) { - var account = _saveServer.GetProfile(kvp.Key).ProfileInfo; + var account = profile.ProfileInfo; if (info?.Username == account?.Username) { - return kvp.Key; + return sessionId; } } @@ -108,9 +108,9 @@ public class LauncherController( /// public async Task Register(RegisterData info) { - foreach (var kvp in _saveServer.GetProfiles()) + foreach (var (_, profile) in _saveServer.GetProfiles()) { - if (info.Username == _saveServer.GetProfile(kvp.Key).ProfileInfo?.Username) + if (info.Username == profile.ProfileInfo?.Username) { return ""; } @@ -281,13 +281,12 @@ public class LauncherController( // Find the highest versioned mod and add to results array var result = new List(); - foreach (var modName in modsGroupedByName) + foreach (var (modName, modDatas) in modsGroupedByName) { - var modDatas = modsGroupedByName[modName.Key]; var modVersions = modDatas.Select(x => x.Version); // var highestVersion = MaxSatisfying(modVersions, "*"); ?? TODO: Node used SemVer here - var chosenVersion = modDatas.FirstOrDefault(x => x.Name == modName.Key); // && x.Version == highestVersion + var chosenVersion = modDatas.FirstOrDefault(x => x.Name == modName); // && x.Version == highestVersion if (chosenVersion is null) { continue; diff --git a/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs b/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs index 2e4353da..03548481 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs @@ -76,9 +76,9 @@ public class LauncherV2Controller( /// public async Task Register(RegisterData info) { - foreach (var session in _saveServer.GetProfiles()) + foreach (var (_, profile) in _saveServer.GetProfiles()) { - if (info.Username == _saveServer.GetProfile(session.Key).ProfileInfo!.Username) + if (info.Username == profile.ProfileInfo!.Username) { return false; } @@ -150,14 +150,10 @@ public class LauncherV2Controller( /// public Dictionary LoadedMods() { - var result = new Dictionary(); - - foreach (var sptMod in _loadedMods) - { - result.Add(sptMod.ModMetadata.Name, sptMod.ModMetadata); - } - - return result; + return _loadedMods.ToDictionary( + sptMod => sptMod.ModMetadata.Name, + sptMod => sptMod.ModMetadata + ); } /// @@ -205,14 +201,14 @@ public class LauncherV2Controller( protected string? GetSessionId(LoginRequestData info) { - foreach (var profile in _saveServer.GetProfiles()) + foreach (var (sessionId, profile) in _saveServer.GetProfiles()) { if ( - info.Username == profile.Value.ProfileInfo!.Username - && info.Password == profile.Value.ProfileInfo.Password + info.Username == profile.ProfileInfo!.Username + && info.Password == profile.ProfileInfo.Password ) { - return profile.Key; + return sessionId; } } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/LocationController.cs b/Libraries/SPTarkov.Server.Core/Controllers/LocationController.cs index 141ad860..b8129b91 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/LocationController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/LocationController.cs @@ -31,14 +31,14 @@ public class LocationController( // keyed by _id location property var locationResult = new Dictionary(); - foreach (var kvp in maps) + foreach (var (locationId, location) in maps) { - var mapBase = kvp.Value.Base; + var mapBase = location.Base; if (mapBase == null) { if (_logger.IsLogEnabled(LogLevel.Debug)) { - _logger.Debug($"Map: {kvp} has no base json file, skipping generation"); + _logger.Debug($"Map: {locationId} has no base json file, skipping generation"); } continue; diff --git a/Libraries/SPTarkov.Server.Core/Controllers/TraderController.cs b/Libraries/SPTarkov.Server.Core/Controllers/TraderController.cs index 6b2e0374..945027c6 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/TraderController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/TraderController.cs @@ -141,15 +141,15 @@ public class TraderController( { var traders = new List(); var pmcData = profileHelper.GetPmcProfile(sessionId); - foreach (var (traderId, _) in databaseService.GetTables().Traders) + foreach (var (traderId, trader) in databaseService.GetTables().Traders) { - var traderToAdd = traderHelper.GetTrader(traderId, sessionId); - if (traderToAdd is null) + traderHelper.GetTrader(traderId, sessionId); + if (trader.Base is null) { logger.Warning($"No trader with id: {traderId} found, skipping"); continue; } - traders.Add(traderToAdd); + traders.Add(trader.Base); if (pmcData?.Info != null) { diff --git a/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs index 390c6bef..0cc570ee 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs @@ -648,27 +648,24 @@ public class BotInventoryGenerator( ) { var modPool = _botEquipmentModPoolService.GetModsForGearSlot(itemTpl); - foreach (var modSlot in modPool) + foreach (var (modSlot, modsForSlot) in modPool) { // Get blacklist - if (!equipmentBlacklist.TryGetValue(modSlot.Key, out var blacklistedMods)) + if (!equipmentBlacklist.TryGetValue(modSlot, out var blacklistedMods)) { blacklistedMods = []; } - ; // Get mods not on blacklist - var filteredMods = modPool[modSlot.Key] - .Where(slotName => !blacklistedMods.Contains(slotName)); + var filteredMods = modsForSlot.Where(slotName => !blacklistedMods.Contains(slotName)); if (!filteredMods.Any()) { - _logger.Warning( - $"Filtering {modSlot.Key} pool resulting in 0 items, skipping filter" - ); + _logger.Warning($"Filtering {modSlot} pool resulting in 0 items, skipping filter"); continue; } - modPool[modSlot.Key] = filteredMods.ToHashSet(); + modsForSlot.Clear(); + modsForSlot.UnionWith(filteredMods); } return modPool.ToDictionary();