diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 1f30a44fd..1b11c5c91 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -45,6 +45,7 @@ namespace ArchiSteamFarm { internal static readonly Dictionary Bots = new Dictionary(); private static readonly uint LoginID = MsgClientLogon.ObfuscationMask; // This must be the same for all ASF bots and all ASF processes + private static readonly SemaphoreSlim GiftsSemaphore = new SemaphoreSlim(1); private static readonly SemaphoreSlim LoginSemaphore = new SemaphoreSlim(1); internal readonly string BotName; @@ -84,7 +85,7 @@ namespace ArchiSteamFarm { initialized = true; } catch (Exception e) { Logging.LogGenericException(e); - await Utilities.SleepAsync(1000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); } } @@ -113,10 +114,18 @@ namespace ArchiSteamFarm { return false; } + private static async Task LimitGiftsRequestsAsync() { + await GiftsSemaphore.WaitAsync().ConfigureAwait(false); + Task.Run(async () => { + await Task.Delay(Program.GlobalConfig.GiftsLimiterDelay * 1000).ConfigureAwait(false); + GiftsSemaphore.Release(); + }).Forget(); + } + private static async Task LimitLoginRequestsAsync() { await LoginSemaphore.WaitAsync().ConfigureAwait(false); Task.Run(async () => { - await Utilities.SleepAsync(Program.GlobalConfig.LoginLimiterDelay * 1000).ConfigureAwait(false); + await Task.Delay(Program.GlobalConfig.LoginLimiterDelay * 1000).ConfigureAwait(false); LoginSemaphore.Release(); }).Forget(); } @@ -812,7 +821,7 @@ namespace ArchiSteamFarm { // Schedule the task after some time so user can receive response Task.Run(async () => { - await Utilities.SleepAsync(1000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); Program.Exit(); }).Forget(); @@ -1014,7 +1023,7 @@ namespace ArchiSteamFarm { // Schedule the task after some time so user can receive response Task.Run(async () => { - await Utilities.SleepAsync(1000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); Program.Restart(); }).Forget(); @@ -1532,7 +1541,7 @@ namespace ArchiSteamFarm { Logging.LogGenericInfo("Removed expired login key", BotName); } else { // If we didn't use login key, InvalidPassword usually means we got captcha or other network-based throttling Logging.LogGenericInfo("Will retry after 25 minutes...", BotName); - await Utilities.SleepAsync(25 * 60 * 1000).ConfigureAwait(false); // Captcha disappears after around 20 minutes, so we make it 25 + await Task.Delay(25 * 60 * 1000).ConfigureAwait(false); // Captcha disappears after around 20 minutes, so we make it 25 } } @@ -1577,7 +1586,10 @@ namespace ArchiSteamFarm { bool acceptedSomething = false; foreach (ulong gid in callback.GuestPasses.Select(guestPass => guestPass["gid"].AsUnsignedLong()).Where(gid => (gid != 0) && !HandledGifts.Contains(gid))) { HandledGifts.Add(gid); + Logging.LogGenericInfo("Accepting gift: " + gid + "...", BotName); + await LimitGiftsRequestsAsync().ConfigureAwait(false); + if (await ArchiWebHandler.AcceptGift(gid).ConfigureAwait(false)) { acceptedSomething = true; Logging.LogGenericInfo("Success!", BotName); @@ -1796,7 +1808,7 @@ namespace ArchiSteamFarm { Trading.CheckTrades().Forget(); - await Utilities.SleepAsync(1000).ConfigureAwait(false); // Wait a second for eventual PlayingSessionStateCallback + await Task.Delay(1000).ConfigureAwait(false); // Wait a second for eventual PlayingSessionStateCallback CardsFarmer.StartFarming().Forget(); break; case EResult.NoConnection: diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index 9a1faa78c..069911313 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -180,7 +180,7 @@ namespace ArchiSteamFarm { Logging.LogGenericInfo("Waiting for reaction...", Bot.BotName); for (byte i = 0; (i < 5) && NowFarming; i++) { - await Utilities.SleepAsync(1000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); } if (NowFarming) { diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index d03a37831..9d9790f2e 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -86,6 +86,9 @@ namespace ArchiSteamFarm { [JsonProperty(Required = Required.DisallowNull)] internal byte InventoryLimiterDelay { get; private set; } = 3; + [JsonProperty(Required = Required.DisallowNull)] + internal byte GiftsLimiterDelay { get; private set; } = 1; + [JsonProperty(Required = Required.DisallowNull)] internal bool ForceHttp { get; private set; } = false; diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 6ed4a33c9..d6d56800f 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -91,7 +91,7 @@ namespace ArchiSteamFarm { // We booted successfully so we can now remove old exe file if (File.Exists(oldExeFile)) { // It's entirely possible that old process is still running, allow at least a second before trying to remove the file - await Utilities.SleepAsync(1000).ConfigureAwait(false); + await Task.Delay(1000).ConfigureAwait(false); try { File.Delete(oldExeFile); @@ -172,7 +172,7 @@ namespace ArchiSteamFarm { if (!updateOverride && !GlobalConfig.AutoUpdates) { Logging.LogGenericInfo("New version is available!"); Logging.LogGenericInfo("Consider updating yourself!"); - await Utilities.SleepAsync(5000).ConfigureAwait(false); + await Task.Delay(5000).ConfigureAwait(false); return; } @@ -248,11 +248,11 @@ namespace ArchiSteamFarm { if (GlobalConfig.AutoRestart) { Logging.LogGenericInfo("Restarting..."); - await Utilities.SleepAsync(5000).ConfigureAwait(false); + await Task.Delay(5000).ConfigureAwait(false); Restart(); } else { Logging.LogGenericInfo("Exiting..."); - await Utilities.SleepAsync(5000).ConfigureAwait(false); + await Task.Delay(5000).ConfigureAwait(false); Exit(); } } diff --git a/ArchiSteamFarm/Utilities.cs b/ArchiSteamFarm/Utilities.cs index 026b06c2a..e55c272d0 100644 --- a/ArchiSteamFarm/Utilities.cs +++ b/ArchiSteamFarm/Utilities.cs @@ -53,14 +53,5 @@ namespace ArchiSteamFarm { } internal static uint GetUnixTime() => (uint) DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; - - internal static Task SleepAsync(int miliseconds) { - if (miliseconds >= 0) { - return Task.Delay(miliseconds); - } - - Logging.LogNullError(nameof(miliseconds)); - return Task.FromResult(true); - } } } diff --git a/ArchiSteamFarm/config/ASF.json b/ArchiSteamFarm/config/ASF.json index 5a567016e..9ca70f6b6 100644 --- a/ArchiSteamFarm/config/ASF.json +++ b/ArchiSteamFarm/config/ASF.json @@ -11,6 +11,7 @@ "FarmingDelay": 15, "LoginLimiterDelay": 10, "InventoryLimiterDelay": 3, + "GiftsLimiterDelay": 1, "ForceHttp": false, "HttpTimeout": 60, "WCFHostname": "localhost", diff --git a/ConfigGenerator/GlobalConfig.cs b/ConfigGenerator/GlobalConfig.cs index 8509946af..3796dd0c4 100644 --- a/ConfigGenerator/GlobalConfig.cs +++ b/ConfigGenerator/GlobalConfig.cs @@ -86,6 +86,9 @@ namespace ConfigGenerator { [JsonProperty(Required = Required.DisallowNull)] public byte InventoryLimiterDelay { get; set; } = 3; + [JsonProperty(Required = Required.DisallowNull)] + public byte GiftsLimiterDelay { get; set; } = 1; + [JsonProperty(Required = Required.DisallowNull)] public bool ForceHttp { get; set; } = false;