From 556f3fdac05dc57a531143c418c6077030863f8f Mon Sep 17 00:00:00 2001 From: Archi Date: Wed, 28 Feb 2024 21:40:54 +0100 Subject: [PATCH] Misc --- .../RemoteCommunication.cs | 21 ++++++++----------- .../ApiAuthenticationMiddleware.cs | 6 +++--- ArchiSteamFarm/Steam/Bot.cs | 2 +- ArchiSteamFarm/Steam/Exchange/Trading.cs | 12 +++++------ ArchiSteamFarm/Steam/Interaction/Commands.cs | 10 ++++----- 5 files changed, 23 insertions(+), 28 deletions(-) diff --git a/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs b/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs index 027b34f7598de..a02fa115fbdf4 100644 --- a/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs +++ b/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs @@ -383,7 +383,7 @@ internal async Task OnPersonaState(string? nickname = null, string? avatarHash = (uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (asset.RealAppID, asset.Type, asset.Rarity); if (state.TryGetValue(key, out Dictionary? set)) { - set[asset.ClassID] = set.TryGetValue(asset.ClassID, out uint amount) ? amount + asset.Amount : asset.Amount; + set[asset.ClassID] = set.GetValueOrDefault(asset.ClassID) + asset.Amount; } else { state[key] = new Dictionary { { asset.ClassID, asset.Amount } }; } @@ -977,7 +977,7 @@ private async void MatchActively(object? state = null) { (uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (asset.RealAppID, asset.Type, asset.Rarity); if (setsState.TryGetValue(key, out Dictionary? set)) { - set[asset.ClassID] = set.TryGetValue(asset.ClassID, out uint amount) ? amount + asset.Amount : asset.Amount; + set[asset.ClassID] = set.GetValueOrDefault(asset.ClassID) + asset.Amount; } else { setsState[key] = new Dictionary { { asset.ClassID, asset.Amount } }; } @@ -1331,15 +1331,16 @@ private async Task MatchActively(IReadOnlyCollection listedUse continue; } - foreach ((ulong theirItem, uint theirTradableAmount) in theirTradableItems.OrderBy(item => ourFullSet.TryGetValue(item.Key, out uint ourAmountOfTheirItem) ? ourAmountOfTheirItem : 0)) { + foreach ((ulong theirItem, uint theirTradableAmount) in theirTradableItems.OrderBy(item => ourFullSet.GetValueOrDefault(item.Key))) { if (ourFullSet.TryGetValue(theirItem, out uint ourAmountOfTheirItem) && (ourFullAmount <= ourAmountOfTheirItem + 1)) { continue; } if (!listedUser.MatchEverything) { // We have a potential match, let's check fairness for them - fairClassIDsToGive.TryGetValue(ourItem, out uint fairGivenAmount); - fairClassIDsToReceive.TryGetValue(theirItem, out uint fairReceivedAmount); + uint fairGivenAmount = fairClassIDsToGive.GetValueOrDefault(ourItem); + uint fairReceivedAmount = fairClassIDsToReceive.GetValueOrDefault(theirItem); + fairClassIDsToGive[ourItem] = ++fairGivenAmount; fairClassIDsToReceive[theirItem] = ++fairReceivedAmount; @@ -1373,11 +1374,11 @@ private async Task MatchActively(IReadOnlyCollection listedUse skippedSetsThisTrade.Add(set); // Update our state based on given items - classIDsToGive[ourItem] = classIDsToGive.TryGetValue(ourItem, out uint ourGivenAmount) ? ourGivenAmount + 1 : 1; + classIDsToGive[ourItem] = classIDsToGive.GetValueOrDefault(ourItem) + 1; ourFullSet[ourItem] = ourFullAmount - 1; // We don't need to remove anything here because we can guarantee that ourItem.Value is at least 2 // Update our state based on received items - classIDsToReceive[theirItem] = classIDsToReceive.TryGetValue(theirItem, out uint ourReceivedAmount) ? ourReceivedAmount + 1 : 1; + classIDsToReceive[theirItem] = classIDsToReceive.GetValueOrDefault(theirItem) + 1; ourFullSet[theirItem] = ourAmountOfTheirItem + 1; if (ourTradableAmount > 1) { @@ -1515,11 +1516,7 @@ private async Task MatchActively(IReadOnlyCollection listedUse throw new InvalidOperationException(nameof(fullAmounts)); } - if (!fullAmounts.TryGetValue(itemToReceive.ClassID, out uint fullAmount)) { - fullAmount = 0; - } - - fullAmounts[itemToReceive.ClassID] = itemToReceive.Amount + fullAmount; + fullAmounts[itemToReceive.ClassID] = fullAmounts.GetValueOrDefault(itemToReceive.ClassID) + itemToReceive.Amount; } skippedSetsThisUser.UnionWith(skippedSetsThisTrade); diff --git a/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs b/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs index 9e6af20f6409b..5d7c65e7d3f53 100644 --- a/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs +++ b/ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs @@ -164,14 +164,14 @@ internal static bool UnbanIP(IPAddress ipAddress) { } try { - bool hasFailedAuthorizations = FailedAuthorizations.TryGetValue(clientIP, out attempts); + attempts = FailedAuthorizations.GetValueOrDefault(clientIP); - if (hasFailedAuthorizations && (attempts >= MaxFailedAuthorizationAttempts)) { + if (attempts >= MaxFailedAuthorizationAttempts) { return (HttpStatusCode.Forbidden, false); } if (!authorized) { - FailedAuthorizations[clientIP] = hasFailedAuthorizations ? ++attempts : (byte) 1; + FailedAuthorizations[clientIP] = ++attempts; } } finally { AuthorizationTasks.TryRemove(clientIP, out _); diff --git a/ArchiSteamFarm/Steam/Bot.cs b/ArchiSteamFarm/Steam/Bot.cs index bced1b6059d47..4c92ea1bec91e 100644 --- a/ArchiSteamFarm/Steam/Bot.cs +++ b/ArchiSteamFarm/Steam/Bot.cs @@ -1189,7 +1189,7 @@ internal static string FormatBotResponse(string response, string botName) { return (optimisticDiscovery ? appID : 0, DateTime.MinValue, true); } - SteamApps.PICSRequest request = new(appID, tokenCallback.AppTokens.TryGetValue(appID, out ulong accessToken) ? accessToken : 0); + SteamApps.PICSRequest request = new(appID, tokenCallback.AppTokens.GetValueOrDefault(appID)); AsyncJobMultiple.ResultSet? productInfoResultSet = null; diff --git a/ArchiSteamFarm/Steam/Exchange/Trading.cs b/ArchiSteamFarm/Steam/Exchange/Trading.cs index e5c133c1de276..bbe5e974bad00 100644 --- a/ArchiSteamFarm/Steam/Exchange/Trading.cs +++ b/ArchiSteamFarm/Steam/Exchange/Trading.cs @@ -82,14 +82,14 @@ public static bool IsFairExchange(IReadOnlyCollection itemsToGive, IReadO foreach (Asset item in itemsToGive) { (uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity); - itemsToGiveAmounts[key] = itemsToGiveAmounts.TryGetValue(key, out uint amount) ? amount + item.Amount : item.Amount; + itemsToGiveAmounts[key] = itemsToGiveAmounts.GetValueOrDefault(key) + item.Amount; } Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity), uint> itemsToReceiveAmounts = new(); foreach (Asset item in itemsToReceive) { (uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity); - itemsToReceiveAmounts[key] = itemsToReceiveAmounts.TryGetValue(key, out uint amount) ? amount + item.Amount : item.Amount; + itemsToReceiveAmounts[key] = itemsToReceiveAmounts.GetValueOrDefault(key) + item.Amount; } // Ensure that amount of items to give is at least amount of items to receive (per all fairness factors) @@ -210,7 +210,7 @@ internal static (Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rar (uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity); if (fullState.TryGetValue(key, out Dictionary? fullSet)) { - fullSet[item.ClassID] = fullSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount; + fullSet[item.ClassID] = fullSet.GetValueOrDefault(item.ClassID) + item.Amount; } else { fullState[key] = new Dictionary { { item.ClassID, item.Amount } }; } @@ -220,7 +220,7 @@ internal static (Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rar } if (tradableState.TryGetValue(key, out Dictionary? tradableSet)) { - tradableSet[item.ClassID] = tradableSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount; + tradableSet[item.ClassID] = tradableSet.GetValueOrDefault(item.ClassID) + item.Amount; } else { tradableState[key] = new Dictionary { { item.ClassID, item.Amount } }; } @@ -240,7 +240,7 @@ internal static (Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rar (uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity); if (tradableState.TryGetValue(key, out Dictionary? tradableSet)) { - tradableSet[item.ClassID] = tradableSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount; + tradableSet[item.ClassID] = tradableSet.GetValueOrDefault(item.ClassID) + item.Amount; } else { tradableState[key] = new Dictionary { { item.ClassID, item.Amount } }; } @@ -391,7 +391,7 @@ internal async Task OnNewTrade() { (uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity); if (state.TryGetValue(key, out Dictionary? set)) { - set[item.ClassID] = set.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount; + set[item.ClassID] = set.GetValueOrDefault(item.ClassID) + item.Amount; } else { state[key] = new Dictionary { { item.ClassID, item.Amount } }; } diff --git a/ArchiSteamFarm/Steam/Interaction/Commands.cs b/ArchiSteamFarm/Steam/Interaction/Commands.cs index a2ffba16c0682..63adf803bcb9b 100644 --- a/ArchiSteamFarm/Steam/Interaction/Commands.cs +++ b/ArchiSteamFarm/Steam/Interaction/Commands.cs @@ -1906,14 +1906,12 @@ internal void OnNewLicenseList() { return null; } - Dictionary ownedGamesStats = new(StringComparer.Ordinal); + Dictionary ownedGamesStats = new(StringComparer.Ordinal); foreach ((string gameID, string gameName) in validResults.Where(static validResult => validResult.OwnedGames.Count > 0).SelectMany(static validResult => validResult.OwnedGames)) { - if (ownedGamesStats.TryGetValue(gameID, out (ushort Count, string GameName) ownedGameStats)) { - ownedGameStats.Count++; - } else { - ownedGameStats.Count = 1; - } + (ushort Count, string? GameName) ownedGameStats = ownedGamesStats.GetValueOrDefault(gameID); + + ownedGameStats.Count++; if (!string.IsNullOrEmpty(gameName)) { ownedGameStats.GameName = gameName;