From 39891b835d4e355c52295839d8f1b6a4c092ea6d Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 23 Jul 2017 04:12:00 +0200 Subject: [PATCH] Closes #509 --- ArchiSteamFarm/Bot.cs | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 85dde7cb6..cfdc4ca12 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -107,6 +107,7 @@ namespace ArchiSteamFarm { private EAccountFlags AccountFlags; private string AuthCode; + private Timer CardsFarmerResumeTimer; private Timer ConnectionFailureTimer; private string DeviceID; private Timer FamilySharingInactivityTimer; @@ -237,6 +238,7 @@ namespace ArchiSteamFarm { Trading.Dispose(); // Those are objects that might be null and the check should be in-place + CardsFarmerResumeTimer?.Dispose(); ConnectionFailureTimer?.Dispose(); FamilySharingInactivityTimer?.Dispose(); SendItemsTimer?.Dispose(); @@ -750,6 +752,12 @@ namespace ArchiSteamFarm { return await ResponsePause(steamID, args[1], true).ConfigureAwait(false); case "!PAUSE~": return await ResponsePause(steamID, args[1], false).ConfigureAwait(false); + case "!PAUSE&": + if (args.Length > 2) { + return await ResponsePause(steamID, args[1], true, args[2]).ConfigureAwait(false); + } + + return await ResponsePause(steamID, true, args[1]).ConfigureAwait(false); case "!PLAY": if (args.Length > 2) { return await ResponsePlay(steamID, args[1], args[2]).ConfigureAwait(false); @@ -3058,7 +3066,7 @@ namespace ArchiSteamFarm { return responses.Count > 0 ? string.Join("", responses) : null; } - private async Task ResponsePause(ulong steamID, bool sticky) { + private async Task ResponsePause(ulong steamID, bool sticky, string timeout = null) { if (steamID == 0) { ArchiLogger.LogNullError(nameof(steamID)); return null; @@ -3080,6 +3088,14 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotAutomaticIdlingPausedAlready); } + ushort resumeInSeconds = 0; + + if (sticky && !string.IsNullOrEmpty(timeout)) { + if (!ushort.TryParse(timeout, out resumeInSeconds) || (resumeInSeconds == 0)) { + return FormatBotResponse(string.Format(Strings.ErrorIsInvalid, nameof(timeout))); + } + } + await CardsFarmer.Pause(sticky).ConfigureAwait(false); if (BotConfig.GamesPlayedWhileIdle.Count > 0) { @@ -3090,6 +3106,20 @@ namespace ArchiSteamFarm { ArchiHandler.PlayGames(Enumerable.Empty(), BotConfig.CustomGamePlayedWhileIdle); } + if (resumeInSeconds > 0) { + if (CardsFarmerResumeTimer != null) { + CardsFarmerResumeTimer.Dispose(); + CardsFarmerResumeTimer = null; + } + + CardsFarmerResumeTimer = new Timer( + e => ResponseResume(steamID), + null, + TimeSpan.FromSeconds(resumeInSeconds), // Delay + Timeout.InfiniteTimeSpan // Period + ); + } + if (IsOperator(steamID)) { return FormatBotResponse(Strings.BotAutomaticIdlingNowPaused); } @@ -3098,7 +3128,7 @@ namespace ArchiSteamFarm { return FormatBotResponse(string.Format(Strings.BotAutomaticIdlingPausedWithCountdown, TimeSpan.FromMinutes(FamilySharingInactivityMinutes).ToHumanReadable())); } - private static async Task ResponsePause(ulong steamID, string botNames, bool sticky) { + private static async Task ResponsePause(ulong steamID, string botNames, bool sticky, string timeout = null) { if ((steamID == 0) || string.IsNullOrEmpty(botNames)) { ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(botNames)); return null; @@ -3109,7 +3139,7 @@ namespace ArchiSteamFarm { return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null; } - IEnumerable> tasks = bots.Select(bot => bot.ResponsePause(steamID, sticky)); + IEnumerable> tasks = bots.Select(bot => bot.ResponsePause(steamID, sticky, timeout)); ICollection results; switch (Program.GlobalConfig.OptimizationMode) { @@ -3513,6 +3543,11 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotAutomaticIdlingResumedAlready); } + if (CardsFarmerResumeTimer != null) { + CardsFarmerResumeTimer.Dispose(); + CardsFarmerResumeTimer = null; + } + StopFamilySharingInactivityTimer(); CardsFarmer.Resume(true).Forget(); return FormatBotResponse(Strings.BotAutomaticIdlingNowResumed);