From ab129f27c346fdee26c20da8b9020fc194afb5c7 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 1 Oct 2016 00:56:29 +0200 Subject: [PATCH] Code review --- ArchiSteamFarm/Bot.cs | 211 +++++++++++++------------- ArchiSteamFarm/CardsFarmer.cs | 2 +- ArchiSteamFarm/MobileAuthenticator.cs | 100 ++++++------ 3 files changed, 160 insertions(+), 153 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index d2a519511..66dee46f6 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -297,40 +297,46 @@ namespace ArchiSteamFarm { return false; } - HashSet confirmations = await BotDatabase.MobileAuthenticator.GetConfirmations().ConfigureAwait(false); - if ((confirmations == null) || (confirmations.Count == 0)) { - return true; - } + while (true) { + HashSet confirmations = await BotDatabase.MobileAuthenticator.GetConfirmations().ConfigureAwait(false); + if ((confirmations == null) || (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 (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; + } + + Steam.ConfirmationDetails[] detailsResults = await Task.WhenAll(confirmations.Select(BotDatabase.MobileAuthenticator.GetConfirmationDetails)).ConfigureAwait(false); + + HashSet ignoredConfirmations = new HashSet(detailsResults.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; + } } - - if ((acceptedSteamID == 0) && ((acceptedTradeIDs == null) || (acceptedTradeIDs.Count == 0))) { - return await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false); - } - - Steam.ConfirmationDetails[] detailsResults = await Task.WhenAll(confirmations.Select(BotDatabase.MobileAuthenticator.GetConfirmationDetails)).ConfigureAwait(false); - - HashSet ignoredConfirmations = new HashSet(detailsResults.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) { - return await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false); - } - - confirmations.ExceptWith(ignoredConfirmations); - if (confirmations.Count == 0) { - return true; - } - - return await BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false); } internal async Task RefreshSession() { @@ -636,7 +642,7 @@ namespace ArchiSteamFarm { private async Task InitializeFamilySharing() { HashSet steamIDs = await ArchiWebHandler.GetFamilySharingSteamIDs().ConfigureAwait(false); - if (steamIDs == null || steamIDs.Count == 0) { + if ((steamIDs == null) || (steamIDs.Count == 0)) { return; } @@ -1105,92 +1111,93 @@ namespace ArchiSteamFarm { message = message.Replace(",", Environment.NewLine); StringBuilder response = new StringBuilder(); - using (StringReader reader = new StringReader(message)) - using (IEnumerator iterator = Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value).GetEnumerator()) { - string key = reader.ReadLine(); - Bot currentBot = this; - while (!string.IsNullOrEmpty(key) && (currentBot != null)) { - if (validate && !IsValidCdKey(key)) { - key = reader.ReadLine(); // Next key - continue; // Keep current bot - } + using (StringReader reader = new StringReader(message)) { + using (IEnumerator iterator = Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value).GetEnumerator()) { + string key = reader.ReadLine(); + Bot currentBot = this; + while (!string.IsNullOrEmpty(key) && (currentBot != null)) { + if (validate && !IsValidCdKey(key)) { + key = reader.ReadLine(); // Next key + continue; // Keep current bot + } - if (!currentBot.IsConnectedAndLoggedOn) { - currentBot = null; // Either bot will be changed, or loop aborted - } else { - ArchiHandler.PurchaseResponseCallback result = await currentBot.ArchiHandler.RedeemKey(key).ConfigureAwait(false); - if (result == null) { - response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: Timeout!"); + if (!currentBot.IsConnectedAndLoggedOn) { currentBot = null; // Either bot will be changed, or loop aborted } else { - switch (result.PurchaseResult) { - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK: - response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + " | Items: " + string.Join("", result.Items)); + ArchiHandler.PurchaseResponseCallback result = await currentBot.ArchiHandler.RedeemKey(key).ConfigureAwait(false); + if (result == null) { + response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: Timeout!"); + currentBot = null; // Either bot will be changed, or loop aborted + } else { + switch (result.PurchaseResult) { + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey: + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey: + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK: + response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + " | Items: " + string.Join("", result.Items)); - key = reader.ReadLine(); // Next key + key = reader.ReadLine(); // Next key - if (result.PurchaseResult == ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK) { - break; // Next bot (if needed) - } + if (result.PurchaseResult == ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK) { + break; // Next bot (if needed) + } - continue; // Keep current bot - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.AlreadyOwned: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.BaseGameRequired: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OnCooldown: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.RegionLocked: - response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + " | Items: " + string.Join("", result.Items)); + continue; // Keep current bot + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.AlreadyOwned: + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.BaseGameRequired: + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OnCooldown: + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.RegionLocked: + response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + " | Items: " + string.Join("", result.Items)); + + if (skipForwarding || !BotConfig.ForwardKeysToOtherBots) { + key = reader.ReadLine(); // Next key + break; // Next bot (if needed) + } + + if (BotConfig.DistributeKeys) { + break; // Next bot, without changing key + } + + bool alreadyHandled = false; + foreach (Bot bot in Bots.Where(bot => (bot.Value != this) && bot.Value.IsConnectedAndLoggedOn && ((result.Items.Count == 0) || result.Items.Keys.Any(packageID => !bot.Value.OwnedPackageIDs.Contains(packageID)))).OrderBy(bot => bot.Key).Select(bot => bot.Value)) { + ArchiHandler.PurchaseResponseCallback otherResult = await bot.ArchiHandler.RedeemKey(key).ConfigureAwait(false); + if (otherResult == null) { + response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: Timeout!"); + continue; + } + + switch (otherResult.PurchaseResult) { + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey: + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey: + case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK: + alreadyHandled = true; // This key is already handled, as we either redeemed it or we're sure it's dupe/invalid + break; + } + + response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherResult.PurchaseResult + " | Items: " + string.Join("", otherResult.Items)); + + if (alreadyHandled) { + break; + } + + foreach (KeyValuePair item in otherResult.Items.Where(item => !result.Items.ContainsKey(item.Key))) { + result.Items[item.Key] = item.Value; + } + } - if (skipForwarding || !BotConfig.ForwardKeysToOtherBots) { key = reader.ReadLine(); // Next key break; // Next bot (if needed) - } - - if (BotConfig.DistributeKeys) { - break; // Next bot, without changing key - } - - bool alreadyHandled = false; - foreach (Bot bot in Bots.Where(bot => (bot.Value != this) && bot.Value.IsConnectedAndLoggedOn && ((result.Items.Count == 0) || result.Items.Keys.Any(packageID => !bot.Value.OwnedPackageIDs.Contains(packageID)))).OrderBy(bot => bot.Key).Select(bot => bot.Value)) { - ArchiHandler.PurchaseResponseCallback otherResult = await bot.ArchiHandler.RedeemKey(key).ConfigureAwait(false); - if (otherResult == null) { - response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: Timeout!"); - continue; - } - - switch (otherResult.PurchaseResult) { - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK: - alreadyHandled = true; // This key is already handled, as we either redeemed it or we're sure it's dupe/invalid - break; - } - - response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherResult.PurchaseResult + " | Items: " + string.Join("", otherResult.Items)); - - if (alreadyHandled) { - break; - } - - foreach (KeyValuePair item in otherResult.Items.Where(item => !result.Items.ContainsKey(item.Key))) { - result.Items[item.Key] = item.Value; - } - } - - key = reader.ReadLine(); // Next key - break; // Next bot (if needed) + } } } - } - if (skipForwarding || !BotConfig.DistributeKeys) { - continue; - } + if (skipForwarding || !BotConfig.DistributeKeys) { + continue; + } - do { - currentBot = iterator.MoveNext() ? iterator.Current : null; - } while ((currentBot == this) || ((currentBot != null) && !currentBot.IsConnectedAndLoggedOn)); + do { + currentBot = iterator.MoveNext() ? iterator.Current : null; + } while ((currentBot == this) || ((currentBot != null) && !currentBot.IsConnectedAndLoggedOn)); + } } } diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index e43ade21a..46e563287 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -70,7 +70,7 @@ namespace ArchiSteamFarm { } Game game = obj as Game; - return game != null && Equals(game); + return (game != null) && Equals(game); } public override int GetHashCode() => (int) AppID; diff --git a/ArchiSteamFarm/MobileAuthenticator.cs b/ArchiSteamFarm/MobileAuthenticator.cs index 9a74c4363..1663938ca 100644 --- a/ArchiSteamFarm/MobileAuthenticator.cs +++ b/ArchiSteamFarm/MobileAuthenticator.cs @@ -213,62 +213,62 @@ namespace ArchiSteamFarm { return null; } + HtmlDocument htmlDocument = await Bot.ArchiWebHandler.GetConfirmations(DeviceID, confirmationHash, time).ConfigureAwait(false); + + HtmlNodeCollection confirmationNodes = htmlDocument?.DocumentNode.SelectNodes("//div[@class='mobileconf_list_entry']"); + if (confirmationNodes == null) { + return null; + } + HashSet result = new HashSet(); - while (true) { - HtmlDocument htmlDocument = await Bot.ArchiWebHandler.GetConfirmations(DeviceID, confirmationHash, time).ConfigureAwait(false); - - HtmlNodeCollection confirmationNodes = htmlDocument?.DocumentNode.SelectNodes("//div[@class='mobileconf_list_entry']"); - if (confirmationNodes == null) { - return result; + foreach (HtmlNode confirmationNode in confirmationNodes) { + string idString = confirmationNode.GetAttributeValue("data-confid", null); + if (string.IsNullOrEmpty(idString)) { + Logging.LogNullError(nameof(idString), Bot.BotName); + return null; } - foreach (HtmlNode confirmationNode in confirmationNodes) { - string idString = confirmationNode.GetAttributeValue("data-confid", null); - if (string.IsNullOrEmpty(idString)) { - Logging.LogNullError(nameof(idString), Bot.BotName); - return null; - } - - uint id; - if (!uint.TryParse(idString, out id) || (id == 0)) { - Logging.LogNullError(nameof(id), Bot.BotName); - return null; - } - - string keyString = confirmationNode.GetAttributeValue("data-key", null); - if (string.IsNullOrEmpty(keyString)) { - Logging.LogNullError(nameof(keyString), Bot.BotName); - return null; - } - - ulong key; - if (!ulong.TryParse(keyString, out key) || (key == 0)) { - Logging.LogNullError(nameof(key), Bot.BotName); - return null; - } - - HtmlNode descriptionNode = confirmationNode.SelectSingleNode(".//div[@class='mobileconf_list_entry_description']/div"); - if (descriptionNode == null) { - Logging.LogNullError(nameof(descriptionNode), Bot.BotName); - return null; - } - - Steam.ConfirmationDetails.EType type; - - string description = descriptionNode.InnerText; - if (description.Equals("Sell - Market Listing")) { - type = Steam.ConfirmationDetails.EType.Market; - } else if (description.StartsWith("Trade with ", StringComparison.Ordinal)) { - type = Steam.ConfirmationDetails.EType.Trade; - } else { - Logging.LogGenericWarning("Received unknown confirmation type, please report this: " + description, Bot.BotName); - type = Steam.ConfirmationDetails.EType.Other; - } - - result.Add(new Confirmation(id, key, type)); + uint id; + if (!uint.TryParse(idString, out id) || (id == 0)) { + Logging.LogNullError(nameof(id), Bot.BotName); + return null; } + + string keyString = confirmationNode.GetAttributeValue("data-key", null); + if (string.IsNullOrEmpty(keyString)) { + Logging.LogNullError(nameof(keyString), Bot.BotName); + return null; + } + + ulong key; + if (!ulong.TryParse(keyString, out key) || (key == 0)) { + Logging.LogNullError(nameof(key), Bot.BotName); + return null; + } + + HtmlNode descriptionNode = confirmationNode.SelectSingleNode(".//div[@class='mobileconf_list_entry_description']/div"); + if (descriptionNode == null) { + Logging.LogNullError(nameof(descriptionNode), Bot.BotName); + return null; + } + + Steam.ConfirmationDetails.EType type; + + string description = descriptionNode.InnerText; + if (description.Equals("Sell - Market Listing")) { + type = Steam.ConfirmationDetails.EType.Market; + } else if (description.StartsWith("Trade with ", StringComparison.Ordinal)) { + type = Steam.ConfirmationDetails.EType.Trade; + } else { + Logging.LogGenericWarning("Received unknown confirmation type, please report this: " + description, Bot.BotName); + type = Steam.ConfirmationDetails.EType.Other; + } + + result.Add(new Confirmation(id, key, type)); } + + return result; } private async Task GetSteamTime() {