From 4986bee410a1f0231baa6d15e87116cded927b16 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 1 Oct 2016 21:40:11 +0200 Subject: [PATCH] Code review --- ArchiSteamFarm/ArchiHandler.cs | 27 ++++++++++++++++++++------- ArchiSteamFarm/Bot.cs | 20 ++++++++++++++------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index ae81e0246..d411165ef 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -151,16 +151,29 @@ namespace ArchiSteamFarm { Logging.LogNullError(nameof(ms)); return; } + } - List lineItems = receiptInfo["lineitems"].Children; - Items = new Dictionary(lineItems.Count); + List lineItems = receiptInfo["lineitems"].Children; + if (lineItems.Count == 0) { + return; + } - foreach (KeyValue lineItem in lineItems) { - uint appID = lineItem["PackageID"].AsUnsignedInteger(); - string gameName = lineItem["ItemDescription"].Value; - gameName = WebUtility.HtmlDecode(gameName); // Apparently steam expects client to decode sent HTML - Items[appID] = gameName; + Items = new Dictionary(lineItems.Count); + foreach (KeyValue lineItem in lineItems) { + uint packageID = lineItem["PackageID"].AsUnsignedInteger(); + if (packageID == 0) { + Logging.LogNullError(nameof(packageID)); + return; } + + string gameName = lineItem["ItemDescription"].Value; + if (string.IsNullOrEmpty(gameName)) { + Logging.LogNullError(nameof(gameName)); + return; + } + + gameName = WebUtility.HtmlDecode(gameName); // Apparently steam expects client to decode sent HTML + Items[packageID] = WebUtility.HtmlDecode(gameName); } } } diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 4a5897062..3520da93e 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -28,6 +28,7 @@ using SteamKit2.Internal; using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Security.Cryptography; @@ -1099,6 +1100,7 @@ namespace ArchiSteamFarm { return "https://github.com/" + SharedInfo.GithubRepo + "/wiki/Commands"; } + [SuppressMessage("ReSharper", "FunctionComplexityOverflow")] private async Task ResponseRedeem(ulong steamID, string message, bool validate, bool skipForwarding) { if ((steamID == 0) || string.IsNullOrEmpty(message)) { Logging.LogNullError(nameof(steamID) + " || " + nameof(message), BotName); @@ -1134,7 +1136,7 @@ namespace ArchiSteamFarm { 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)); + response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + ((result.Items != null) && (result.Items.Count > 0) ? " | Items: " + string.Join("", result.Items) : "")); key = reader.ReadLine(); // Next key @@ -1147,7 +1149,7 @@ namespace ArchiSteamFarm { 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)); + response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + ((result.Items != null) && (result.Items.Count > 0) ? " | Items: " + string.Join("", result.Items) : "")); if (skipForwarding || !BotConfig.ForwardKeysToOtherBots) { key = reader.ReadLine(); // Next key @@ -1158,8 +1160,10 @@ namespace ArchiSteamFarm { break; // Next bot, without changing key } + Dictionary items = result.Items ?? new Dictionary(); + 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)) { + foreach (Bot bot in Bots.Where(bot => (bot.Value != this) && bot.Value.IsConnectedAndLoggedOn && ((items.Count == 0) || 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!"); @@ -1174,14 +1178,18 @@ namespace ArchiSteamFarm { break; } - response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherResult.PurchaseResult + " | Items: " + string.Join("", otherResult.Items)); + response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherResult.PurchaseResult + ((otherResult.Items != null) && (otherResult.Items.Count > 0) ? " | 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 (otherResult.Items == null) { + continue; + } + + foreach (KeyValuePair item in otherResult.Items.Where(item => !items.ContainsKey(item.Key))) { + items[item.Key] = item.Value; } }