From 00263a03b7c81a76383f222abfc6598cac5f4d6b Mon Sep 17 00:00:00 2001 From: JustArchi Date: Thu, 29 Jun 2017 15:22:27 +0200 Subject: [PATCH] Closes #583 --- ArchiSteamFarm/Bot.cs | 35 +++++++++++++++++++++++++---------- ArchiSteamFarm/Trading.cs | 7 +++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index d2714ef0b..2435240ad 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -80,6 +80,7 @@ namespace ArchiSteamFarm { private readonly ConcurrentHashSet HandledGifts = new ConcurrentHashSet(); private readonly Timer HeartBeatTimer; private readonly SemaphoreSlim InitializationSemaphore = new SemaphoreSlim(1); + private readonly SemaphoreSlim LootingSemaphore = new SemaphoreSlim(1); private readonly ConcurrentHashSet OwnedPackageIDs = new ConcurrentHashSet(); private readonly Statistics Statistics; @@ -2498,21 +2499,35 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotLootingYourself); } - HashSet inventory = await ArchiWebHandler.GetMySteamInventory(true, BotConfig.LootableTypes).ConfigureAwait(false); - if ((inventory == null) || (inventory.Count == 0)) { - return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); - } - - if (!await ArchiWebHandler.MarkSentTrades().ConfigureAwait(false)) { + if (!LootingSemaphore.Wait(0)) { return FormatBotResponse(Strings.BotLootingFailed); } - if (!await ArchiWebHandler.SendTradeOffer(inventory, targetSteamMasterID, BotConfig.SteamTradeToken).ConfigureAwait(false)) { - return FormatBotResponse(Strings.BotLootingFailed); + try { + HashSet inventory = await ArchiWebHandler.GetMySteamInventory(true, BotConfig.LootableTypes).ConfigureAwait(false); + if ((inventory == null) || (inventory.Count == 0)) { + return FormatBotResponse(string.Format(Strings.ErrorIsEmpty, nameof(inventory))); + } + + if (!await ArchiWebHandler.MarkSentTrades().ConfigureAwait(false)) { + return FormatBotResponse(Strings.BotLootingFailed); + } + + if (!await ArchiWebHandler.SendTradeOffer(inventory, targetSteamMasterID, BotConfig.SteamTradeToken).ConfigureAwait(false)) { + return FormatBotResponse(Strings.BotLootingFailed); + } + + if (HasMobileAuthenticator) { + // Give Steam network some time to generate confirmations + await Task.Delay(3000).ConfigureAwait(false); + if (!await AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, targetSteamMasterID).ConfigureAwait(false)) { + return FormatBotResponse(Strings.BotLootingFailed); + } + } + } finally { + LootingSemaphore.Release(); } - await Task.Delay(3000).ConfigureAwait(false); // Sometimes we can be too fast for Steam servers to generate confirmations, wait a short moment - await AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, targetSteamMasterID).ConfigureAwait(false); return FormatBotResponse(Strings.BotLootingSuccess); } diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 88d98c3e8..01e59699e 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -104,8 +104,11 @@ namespace ArchiSteamFarm { if (Bot.HasMobileAuthenticator) { HashSet acceptedWithItemLoseTradeIDs = new HashSet(results.Where(result => (result != null) && (result.Result == ParseTradeResult.EResult.AcceptedWithItemLose)).Select(result => result.TradeID)); if (acceptedWithItemLoseTradeIDs.Count > 0) { - await Task.Delay(3000).ConfigureAwait(false); // Sometimes we can be too fast for Steam servers to generate confirmations, wait a short moment - await Bot.AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, 0, acceptedWithItemLoseTradeIDs).ConfigureAwait(false); + if (Bot.HasMobileAuthenticator) { + // Give Steam network some time to generate confirmations + await Task.Delay(3000).ConfigureAwait(false); + await Bot.AcceptConfirmations(true, Steam.ConfirmationDetails.EType.Trade, 0, acceptedWithItemLoseTradeIDs).ConfigureAwait(false); + } } }