From fd6e2c72d78e86a0c872cde271ec7359b87fe203 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 2 Apr 2016 19:23:09 +0200 Subject: [PATCH] Major cleanup & code review of ResponseRedeem() --- ArchiSteamFarm/ArchiHandler.cs | 1 + ArchiSteamFarm/Bot.cs | 109 +++++++++++---------------------- 2 files changed, 38 insertions(+), 72 deletions(-) diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index 97c8d50f0..10d8cd918 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -257,6 +257,7 @@ namespace ArchiSteamFarm { request.Body.key = key; Client.Send(request); + try { return await new AsyncJob(Client, request.SourceJobID); } catch (Exception e) { diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 6aa1f5881..2bf386aa7 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -744,18 +744,14 @@ namespace ArchiSteamFarm { } StringBuilder response = new StringBuilder(); - using (StringReader reader = new StringReader(message)) { + using (StringReader reader = new StringReader(message)) + using (IEnumerator iterator = Bots.Values.GetEnumerator()) { string key = reader.ReadLine(); - IEnumerator iterator = Bots.Values.GetEnumerator(); Bot currentBot = this; - while (key != null) { - if (currentBot == null) { - break; - } - + while (!string.IsNullOrEmpty(key) && currentBot != null) { if (validate && !IsValidCdKey(key)) { - key = reader.ReadLine(); - continue; + key = reader.ReadLine(); // Next key + continue; // Without changing the bot } ArchiHandler.PurchaseResponseCallback result = await currentBot.ArchiHandler.RedeemKey(key).ConfigureAwait(false); @@ -763,33 +759,27 @@ namespace ArchiSteamFarm { break; } - var purchaseResult = result.PurchaseResult; - var items = result.Items; + 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)); - switch (purchaseResult) { + key = reader.ReadLine(); // Next key + break; // Next bot (if needed) 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: " + purchaseResult + " | Items: " + string.Join("", items)); - if (BotConfig.DistributeKeys) { - do { - if (iterator.MoveNext()) { - currentBot = iterator.Current; - } else { - currentBot = null; - } - } while (currentBot == this); - - if (!BotConfig.ForwardKeysToOtherBots) { - key = reader.ReadLine(); - } - break; - } + response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + " | Items: " + string.Join("", result.Items)); if (!BotConfig.ForwardKeysToOtherBots) { - key = reader.ReadLine(); - break; + key = reader.ReadLine(); // Next key + break; // Next bot (if needed) + } + + if (BotConfig.DistributeKeys) { + break; // Next bot, without changing key } bool alreadyHandled = false; @@ -803,58 +793,33 @@ namespace ArchiSteamFarm { } ArchiHandler.PurchaseResponseCallback otherResult = await bot.ArchiHandler.RedeemKey(key).ConfigureAwait(false); - if (otherResult == null) { - break; // We're done with this key + break; } - var otherPurchaseResult = otherResult.PurchaseResult; - var otherItems = otherResult.Items; - - switch (otherPurchaseResult) { - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK: - alreadyHandled = true; // We're done with this key - response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherPurchaseResult + " | Items: " + string.Join("", otherItems)); - break; + switch (otherResult.PurchaseResult) { case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey: case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey: - alreadyHandled = true; // This key doesn't work, don't try to redeem it anymore - response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherPurchaseResult + " | Items: " + string.Join("", otherItems)); - break; - default: - response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherPurchaseResult + " | Items: " + string.Join("", otherItems)); + 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)); } - key = reader.ReadLine(); - break; - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK: - response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + purchaseResult + " | Items: " + string.Join("", items)); - if (BotConfig.DistributeKeys) { - do { - if (iterator.MoveNext()) { - currentBot = iterator.Current; - } else { - currentBot = null; - } - } while (currentBot == this); + + key = reader.ReadLine(); // Next key + break; // Next bot (if needed) + } + + if (BotConfig.DistributeKeys) { + do { + if (iterator.MoveNext()) { + currentBot = iterator.Current; + } else { + currentBot = null; } - key = reader.ReadLine(); - break; - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey: - case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey: - response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + purchaseResult + " | Items: " + string.Join("", items)); - if (BotConfig.DistributeKeys && !BotConfig.ForwardKeysToOtherBots) { - do { - if (iterator.MoveNext()) { - currentBot = iterator.Current; - } else { - currentBot = null; - } - } while (currentBot == this); - } - key = reader.ReadLine(); - break; + } while (currentBot == this); } } }