diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 2ad8cafe7..47b3b320e 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -265,57 +265,39 @@ namespace ArchiSteamFarm { return false; } - while (true) { - HashSet confirmations = await BotDatabase.MobileAuthenticator.GetConfirmations().ConfigureAwait(false); - if ((confirmations == null) || (confirmations.Count == 0)) { + HashSet confirmations = await BotDatabase.MobileAuthenticator.GetConfirmations(acceptedType).ConfigureAwait(false); + if ((confirmations == null) || (confirmations.Count == 0)) { + return true; + } + + if ((acceptedSteamID == 0) && ((acceptedTradeIDs == null) || (acceptedTradeIDs.Count == 0))) { + return await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false); + } + + IEnumerable> tasks = confirmations.Select(BotDatabase.MobileAuthenticator.GetConfirmationDetails); + ICollection results; + + switch (Program.GlobalConfig.OptimizationMode) { + case GlobalConfig.EOptimizationMode.MinMemoryUsage: + results = new List(confirmations.Count); + foreach (Task task in tasks) { + results.Add(await task.ConfigureAwait(false)); + } + + break; + default: + results = await Task.WhenAll(tasks).ConfigureAwait(false); + break; + } + + foreach (MobileAuthenticator.Confirmation confirmation in results.Where(details => (details != null) && ((acceptedType != details.Type) || ((acceptedSteamID != 0) && (details.OtherSteamID64 != 0) && (acceptedSteamID != details.OtherSteamID64)) || ((acceptedTradeIDs != null) && (details.TradeOfferID != 0) && !acceptedTradeIDs.Contains(details.TradeOfferID)))).Select(details => details.Confirmation)) { + confirmations.Remove(confirmation); + if (confirmations.Count == 0) { return true; } - - if (acceptedType != Steam.ConfirmationDetails.EType.Unknown) { - if (confirmations.RemoveWhere(confirmation => (confirmation.Type != acceptedType) && (confirmation.Type != Steam.ConfirmationDetails.EType.Other)) > 0) { - if (confirmations.Count == 0) { - return true; - } - } - } - - if ((acceptedSteamID == 0) && ((acceptedTradeIDs == null) || (acceptedTradeIDs.Count == 0))) { - if (!await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false)) { - return false; - } - - continue; - } - - IEnumerable> tasks = confirmations.Select(BotDatabase.MobileAuthenticator.GetConfirmationDetails); - ICollection results; - - switch (Program.GlobalConfig.OptimizationMode) { - case GlobalConfig.EOptimizationMode.MinMemoryUsage: - results = new List(confirmations.Count); - foreach (Task task in tasks) { - results.Add(await task.ConfigureAwait(false)); - } - - break; - default: - results = await Task.WhenAll(tasks).ConfigureAwait(false); - break; - } - - HashSet ignoredConfirmations = new HashSet(results.Where(details => (details != null) && (((acceptedSteamID != 0) && (details.OtherSteamID64 != 0) && (acceptedSteamID != details.OtherSteamID64)) || ((acceptedTradeIDs != null) && (details.TradeOfferID != 0) && !acceptedTradeIDs.Contains(details.TradeOfferID)))).Select(details => details.Confirmation)); - - if (ignoredConfirmations.Count > 0) { - confirmations.ExceptWith(ignoredConfirmations); - if (confirmations.Count == 0) { - return true; - } - } - - if (!await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false)) { - return false; - } } + + return await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false); } internal async Task DeleteAllRelatedFiles() { diff --git a/ArchiSteamFarm/Json/Steam.cs b/ArchiSteamFarm/Json/Steam.cs index c3d60cbce..3e106ccdb 100644 --- a/ArchiSteamFarm/Json/Steam.cs +++ b/ArchiSteamFarm/Json/Steam.cs @@ -23,6 +23,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using ArchiSteamFarm.Localization; using HtmlAgilityPack; using Newtonsoft.Json; using SteamKit2; @@ -180,11 +181,11 @@ namespace ArchiSteamFarm.Json { return _TradeOfferID; } - if ((Type != EType.Trade) || (HtmlDocument == null)) { + if ((Type != EType.Trade) || (DocumentNode == null)) { return 0; } - HtmlNode htmlNode = HtmlDocument.DocumentNode.SelectSingleNode("//div[@class='tradeoffer']"); + HtmlNode htmlNode = DocumentNode.SelectSingleNode("//div[@class='tradeoffer']"); if (htmlNode == null) { ASF.ArchiLogger.LogNullError(nameof(htmlNode)); return 0; @@ -218,23 +219,55 @@ namespace ArchiSteamFarm.Json { } } + internal EType Type { + get { + if (_Type != EType.Unknown) { + return _Type; + } + + if (DocumentNode == null) { + return EType.Unknown; + } + + if (DocumentNode.SelectSingleNode("//div[@class='mobileconf_listing_prices']") != null) { + _Type = EType.Market; + return _Type; + } + + if (DocumentNode.SelectSingleNode("//div[@class='mobileconf_trade_area']") != null) { + _Type = EType.Trade; + return _Type; + } + + _Type = EType.Other; + ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(_Type), _Type)); + return _Type; + } + } + #pragma warning disable 649 [JsonProperty(PropertyName = "html", Required = Required.DisallowNull)] private readonly string HTML; #pragma warning restore 649 - private HtmlDocument HtmlDocument { + private HtmlNode DocumentNode { get { - if (_HtmlDocument != null) { - return _HtmlDocument; + if (_DocumentNode != null) { + return _DocumentNode; } if (string.IsNullOrEmpty(HTML)) { return null; } - _HtmlDocument = WebBrowser.StringToHtmlDocument(HTML); - return _HtmlDocument; + HtmlDocument htmlDocument = WebBrowser.StringToHtmlDocument(HTML); + if (htmlDocument == null) { + ASF.ArchiLogger.LogNullError(nameof(htmlDocument)); + return null; + } + + _DocumentNode = htmlDocument.DocumentNode; + return _DocumentNode; } } @@ -244,11 +277,11 @@ namespace ArchiSteamFarm.Json { return _OtherSteamID3; } - if ((Type != EType.Trade) || (HtmlDocument == null)) { + if ((Type != EType.Trade) || (DocumentNode == null)) { return 0; } - HtmlNode htmlNode = HtmlDocument.DocumentNode.SelectSingleNode("//a/@data-miniprofile"); + HtmlNode htmlNode = DocumentNode.SelectSingleNode("//a/@data-miniprofile"); if (htmlNode == null) { ASF.ArchiLogger.LogNullError(nameof(htmlNode)); return 0; @@ -269,33 +302,6 @@ namespace ArchiSteamFarm.Json { } } - private EType Type { - get { - if (_Type != EType.Unknown) { - return _Type; - } - - if (HtmlDocument == null) { - return EType.Unknown; - } - - HtmlNode testNode = HtmlDocument.DocumentNode.SelectSingleNode("//div[@class='mobileconf_listing_prices']"); - if (testNode != null) { - _Type = EType.Market; - return _Type; - } - - testNode = HtmlDocument.DocumentNode.SelectSingleNode("//div[@class='mobileconf_trade_area']"); - if (testNode != null) { - _Type = EType.Trade; - return _Type; - } - - _Type = EType.Other; - return _Type; - } - } - internal MobileAuthenticator.Confirmation Confirmation { get => _Confirmation; @@ -310,7 +316,7 @@ namespace ArchiSteamFarm.Json { } private MobileAuthenticator.Confirmation _Confirmation; - private HtmlDocument _HtmlDocument; + private HtmlNode _DocumentNode; private uint _OtherSteamID3; private ulong _OtherSteamID64; private ulong _TradeOfferID; diff --git a/ArchiSteamFarm/MobileAuthenticator.cs b/ArchiSteamFarm/MobileAuthenticator.cs index 0c8ff65a9..5db13ab02 100644 --- a/ArchiSteamFarm/MobileAuthenticator.cs +++ b/ArchiSteamFarm/MobileAuthenticator.cs @@ -116,7 +116,7 @@ namespace ArchiSteamFarm { return response?.Success == true ? response : null; } - internal async Task> GetConfirmations() { + internal async Task> GetConfirmations(Steam.ConfirmationDetails.EType acceptedType = Steam.ConfirmationDetails.EType.Unknown) { if (!HasCorrectDeviceID) { Bot.ArchiLogger.LogGenericError(Strings.ErrorMobileAuthenticatorInvalidDeviceID); return null; @@ -187,7 +187,11 @@ namespace ArchiSteamFarm { type = Steam.ConfirmationDetails.EType.Other; } - result.Add(new Confirmation(id, key, type)); + if ((acceptedType != Steam.ConfirmationDetails.EType.Unknown) && (type != acceptedType)) { + continue; + } + + result.Add(new Confirmation(id, key)); } return result; @@ -373,16 +377,14 @@ namespace ArchiSteamFarm { internal sealed class Confirmation { internal readonly ulong ID; internal readonly ulong Key; - internal readonly Steam.ConfirmationDetails.EType Type; - internal Confirmation(ulong id, ulong key, Steam.ConfirmationDetails.EType type) { - if ((id == 0) || (key == 0) || (type == Steam.ConfirmationDetails.EType.Unknown)) { - throw new ArgumentNullException(nameof(id) + " || " + nameof(key) + " || " + nameof(type)); + internal Confirmation(ulong id, ulong key) { + if ((id == 0) || (key == 0)) { + throw new ArgumentNullException(nameof(id) + " || " + nameof(key)); } ID = id; Key = key; - Type = type; } } }