Skip to content

Commit fd6e2c7

Browse files
committed
Major cleanup & code review of ResponseRedeem()
1 parent 1eed0f7 commit fd6e2c7

File tree

2 files changed

+38
-72
lines changed

2 files changed

+38
-72
lines changed

ArchiSteamFarm/ArchiHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ internal async Task<PurchaseResponseCallback> RedeemKey(string key) {
257257
request.Body.key = key;
258258

259259
Client.Send(request);
260+
260261
try {
261262
return await new AsyncJob<PurchaseResponseCallback>(Client, request.SourceJobID);
262263
} catch (Exception e) {

ArchiSteamFarm/Bot.cs

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -744,52 +744,42 @@ private async Task<string> ResponseRedeem(ulong steamID, string message, bool va
744744
}
745745

746746
StringBuilder response = new StringBuilder();
747-
using (StringReader reader = new StringReader(message)) {
747+
using (StringReader reader = new StringReader(message))
748+
using (IEnumerator<Bot> iterator = Bots.Values.GetEnumerator()) {
748749
string key = reader.ReadLine();
749-
IEnumerator<Bot> iterator = Bots.Values.GetEnumerator();
750750
Bot currentBot = this;
751-
while (key != null) {
752-
if (currentBot == null) {
753-
break;
754-
}
755-
751+
while (!string.IsNullOrEmpty(key) && currentBot != null) {
756752
if (validate && !IsValidCdKey(key)) {
757-
key = reader.ReadLine();
758-
continue;
753+
key = reader.ReadLine(); // Next key
754+
continue; // Without changing the bot
759755
}
760756

761757
ArchiHandler.PurchaseResponseCallback result = await currentBot.ArchiHandler.RedeemKey(key).ConfigureAwait(false);
762758
if (result == null) {
763759
break;
764760
}
765761

766-
var purchaseResult = result.PurchaseResult;
767-
var items = result.Items;
762+
switch (result.PurchaseResult) {
763+
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey:
764+
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey:
765+
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK:
766+
response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + " | Items: " + string.Join("", result.Items));
768767

769-
switch (purchaseResult) {
768+
key = reader.ReadLine(); // Next key
769+
break; // Next bot (if needed)
770770
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.AlreadyOwned:
771771
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.BaseGameRequired:
772772
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OnCooldown:
773773
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.RegionLocked:
774-
response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + purchaseResult + " | Items: " + string.Join("", items));
775-
if (BotConfig.DistributeKeys) {
776-
do {
777-
if (iterator.MoveNext()) {
778-
currentBot = iterator.Current;
779-
} else {
780-
currentBot = null;
781-
}
782-
} while (currentBot == this);
783-
784-
if (!BotConfig.ForwardKeysToOtherBots) {
785-
key = reader.ReadLine();
786-
}
787-
break;
788-
}
774+
response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + result.PurchaseResult + " | Items: " + string.Join("", result.Items));
789775

790776
if (!BotConfig.ForwardKeysToOtherBots) {
791-
key = reader.ReadLine();
792-
break;
777+
key = reader.ReadLine(); // Next key
778+
break; // Next bot (if needed)
779+
}
780+
781+
if (BotConfig.DistributeKeys) {
782+
break; // Next bot, without changing key
793783
}
794784

795785
bool alreadyHandled = false;
@@ -803,58 +793,33 @@ private async Task<string> ResponseRedeem(ulong steamID, string message, bool va
803793
}
804794

805795
ArchiHandler.PurchaseResponseCallback otherResult = await bot.ArchiHandler.RedeemKey(key).ConfigureAwait(false);
806-
807796
if (otherResult == null) {
808-
break; // We're done with this key
797+
break;
809798
}
810799

811-
var otherPurchaseResult = otherResult.PurchaseResult;
812-
var otherItems = otherResult.Items;
813-
814-
switch (otherPurchaseResult) {
815-
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK:
816-
alreadyHandled = true; // We're done with this key
817-
response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherPurchaseResult + " | Items: " + string.Join("", otherItems));
818-
break;
800+
switch (otherResult.PurchaseResult) {
819801
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey:
820802
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey:
821-
alreadyHandled = true; // This key doesn't work, don't try to redeem it anymore
822-
response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherPurchaseResult + " | Items: " + string.Join("", otherItems));
823-
break;
824-
default:
825-
response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherPurchaseResult + " | Items: " + string.Join("", otherItems));
803+
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK:
804+
alreadyHandled = true; // This key is already handled, as we either redeemed it or we're sure it's dupe/invalid
826805
break;
827806
}
807+
808+
response.Append(Environment.NewLine + "<" + bot.BotName + "> Key: " + key + " | Status: " + otherResult.PurchaseResult + " | Items: " + string.Join("", otherResult.Items));
828809
}
829-
key = reader.ReadLine();
830-
break;
831-
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK:
832-
response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + purchaseResult + " | Items: " + string.Join("", items));
833-
if (BotConfig.DistributeKeys) {
834-
do {
835-
if (iterator.MoveNext()) {
836-
currentBot = iterator.Current;
837-
} else {
838-
currentBot = null;
839-
}
840-
} while (currentBot == this);
841-
}
842-
key = reader.ReadLine();
843-
break;
844-
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.DuplicatedKey:
845-
case ArchiHandler.PurchaseResponseCallback.EPurchaseResult.InvalidKey:
846-
response.Append(Environment.NewLine + "<" + currentBot.BotName + "> Key: " + key + " | Status: " + purchaseResult + " | Items: " + string.Join("", items));
847-
if (BotConfig.DistributeKeys && !BotConfig.ForwardKeysToOtherBots) {
848-
do {
849-
if (iterator.MoveNext()) {
850-
currentBot = iterator.Current;
851-
} else {
852-
currentBot = null;
853-
}
854-
} while (currentBot == this);
810+
811+
key = reader.ReadLine(); // Next key
812+
break; // Next bot (if needed)
813+
}
814+
815+
if (BotConfig.DistributeKeys) {
816+
do {
817+
if (iterator.MoveNext()) {
818+
currentBot = iterator.Current;
819+
} else {
820+
currentBot = null;
855821
}
856-
key = reader.ReadLine();
857-
break;
822+
} while (currentBot == this);
858823
}
859824
}
860825
}

0 commit comments

Comments
 (0)