From 4c0dc570fe08df8ea34ca99aa39c232b01f84335 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 12 Dec 2018 21:17:12 +0100 Subject: [PATCH] Improve confirmations code --- ArchiSteamFarm/Actions.cs | 16 ++++++++++------ ArchiSteamFarm/Json/Steam.cs | 20 -------------------- ArchiSteamFarm/Statistics.cs | 2 +- ArchiSteamFarm/Trading.cs | 2 +- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/ArchiSteamFarm/Actions.cs b/ArchiSteamFarm/Actions.cs index 963f2b671..98e7f9e8b 100644 --- a/ArchiSteamFarm/Actions.cs +++ b/ArchiSteamFarm/Actions.cs @@ -55,7 +55,7 @@ namespace ArchiSteamFarm { CardsFarmerResumeTimer?.Dispose(); } - internal async Task AcceptConfirmations(bool accept, Steam.ConfirmationDetails.EType acceptedType = Steam.ConfirmationDetails.EType.Unknown, ulong acceptedSteamID = 0, ISet acceptedTradeOfferIDs = null, bool waitIfNeeded = false) { + internal async Task AcceptConfirmations(bool accept, Steam.ConfirmationDetails.EType acceptedType = Steam.ConfirmationDetails.EType.Unknown, IReadOnlyCollection acceptedTradeOfferIDs = null, bool waitIfNeeded = false) { if (!Bot.HasMobileAuthenticator) { return false; } @@ -70,13 +70,14 @@ namespace ArchiSteamFarm { continue; } - if ((acceptedSteamID == 0) && ((acceptedTradeOfferIDs == null) || (acceptedTradeOfferIDs.Count == 0))) { + // If we can skip asking for details, we can handle confirmations right away + if ((acceptedTradeOfferIDs == null) || (acceptedTradeOfferIDs.Count == 0)) { return await Bot.BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false); } IList results = await Utilities.InParallel(confirmations.Select(Bot.BotDatabase.MobileAuthenticator.GetConfirmationDetails)).ConfigureAwait(false); - foreach (MobileAuthenticator.Confirmation confirmation in results.Where(details => (details != null) && ((acceptedType != details.Type) || ((acceptedSteamID != 0) && (details.OtherSteamID64 != 0) && (acceptedSteamID != details.OtherSteamID64)) || ((acceptedTradeOfferIDs != null) && (details.TradeOfferID != 0) && !acceptedTradeOfferIDs.Contains(details.TradeOfferID)))).Select(details => details.Confirmation)) { + foreach (MobileAuthenticator.Confirmation confirmation in results.Where(details => (details != null) && ((acceptedType != details.Type) || ((details.TradeOfferID != 0) && !acceptedTradeOfferIDs.Contains(details.TradeOfferID)))).Select(details => details.Confirmation)) { confirmations.Remove(confirmation); } @@ -88,12 +89,15 @@ namespace ArchiSteamFarm { return false; } - if ((acceptedTradeOfferIDs == null) || (acceptedTradeOfferIDs.Count == 0) || results.Where(result => (result != null) && (result.TradeOfferID != 0) && confirmations.Contains(result.Confirmation)).Select(result => result.TradeOfferID).Where(acceptedTradeOfferIDs.Remove).Any(tradeOfferID => acceptedTradeOfferIDs.Count == 0)) { + // Check if those are all that we were expected to confirm + HashSet handledTradeOfferIDs = results.Where(details => (details != null) && (details.TradeOfferID != 0)).Select(result => result.TradeOfferID).ToHashSet(); + + if (handledTradeOfferIDs.SetEquals(acceptedTradeOfferIDs)) { return true; } } - return true; + return !waitIfNeeded; } internal async Task AcceptDigitalGiftCards() { @@ -294,7 +298,7 @@ namespace ArchiSteamFarm { (bool success, HashSet mobileTradeOfferIDs) = await Bot.ArchiWebHandler.SendTradeOffer(targetSteamID, inventory, token: Bot.BotConfig.SteamTradeToken).ConfigureAwait(false); if ((mobileTradeOfferIDs != null) && (mobileTradeOfferIDs.Count > 0) && Bot.HasMobileAuthenticator) { - if (!await AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, targetSteamID, mobileTradeOfferIDs, true).ConfigureAwait(false)) { + if (!await AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false)) { return (false, Strings.BotLootingFailed); } } diff --git a/ArchiSteamFarm/Json/Steam.cs b/ArchiSteamFarm/Json/Steam.cs index d2a5faf91..844cccf2a 100644 --- a/ArchiSteamFarm/Json/Steam.cs +++ b/ArchiSteamFarm/Json/Steam.cs @@ -168,7 +168,6 @@ namespace ArchiSteamFarm.Json { [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] internal sealed class ConfirmationDetails : BooleanResponse { internal MobileAuthenticator.Confirmation Confirmation { get; set; } - internal ulong OtherSteamID64 { get; private set; } internal ulong TradeOfferID { get; private set; } internal EType Type { get; private set; } @@ -220,25 +219,6 @@ namespace ArchiSteamFarm.Json { } TradeOfferID = tradeOfferID; - - HtmlNode htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//a/@data-miniprofile"); - if (htmlNode == null) { - ASF.ArchiLogger.LogNullError(nameof(htmlNode)); - return; - } - - string miniProfile = htmlNode.GetAttributeValue("data-miniprofile", null); - if (string.IsNullOrEmpty(miniProfile)) { - ASF.ArchiLogger.LogNullError(nameof(miniProfile)); - return; - } - - if (!uint.TryParse(miniProfile, out uint steamID3) || (steamID3 == 0)) { - ASF.ArchiLogger.LogNullError(nameof(steamID3)); - return; - } - - OtherSteamID64 = new SteamID(steamID3, EUniverse.Public, EAccountType.Individual); } else if (htmlDocument.DocumentNode.SelectSingleNode("//div[@class='mobileconf_listing_prices']") != null) { Type = EType.Market; } else { diff --git a/ArchiSteamFarm/Statistics.cs b/ArchiSteamFarm/Statistics.cs index d88da9d50..ebfa2d263 100644 --- a/ArchiSteamFarm/Statistics.cs +++ b/ArchiSteamFarm/Statistics.cs @@ -468,7 +468,7 @@ namespace ArchiSteamFarm { (bool success, HashSet mobileTradeOfferIDs) = await Bot.ArchiWebHandler.SendTradeOffer(listedUser.SteamID, itemsToGive, itemsToReceive, listedUser.TradeToken, true).ConfigureAwait(false); if ((mobileTradeOfferIDs != null) && (mobileTradeOfferIDs.Count > 0) && Bot.HasMobileAuthenticator) { - if (!await Bot.Actions.AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, listedUser.SteamID, mobileTradeOfferIDs, true).ConfigureAwait(false)) { + if (!await Bot.Actions.AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false)) { Bot.ArchiLogger.LogGenericTrace(Strings.WarningFailed); return false; } diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 21032bf89..750def925 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -403,7 +403,7 @@ namespace ArchiSteamFarm { if (Bot.HasMobileAuthenticator) { HashSet mobileTradeOfferIDs = results.Where(result => (result.TradeResult != null) && (result.TradeResult.Result == ParseTradeResult.EResult.Accepted) && result.RequiresMobileConfirmation).Select(result => result.TradeResult.TradeOfferID).ToHashSet(); if (mobileTradeOfferIDs.Count > 0) { - if (!await Bot.Actions.AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, acceptedTradeOfferIDs: mobileTradeOfferIDs, waitIfNeeded: true).ConfigureAwait(false)) { + if (!await Bot.Actions.AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false)) { return; } }