diff --git a/FreePackages/Commands.cs b/FreePackages/Commands.cs index 3473aa0..2d22507 100644 --- a/FreePackages/Commands.cs +++ b/FreePackages/Commands.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Globalization; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using ArchiSteamFarm.Core; using ArchiSteamFarm.Steam; @@ -127,7 +128,7 @@ internal static class Commands { return responses.Count > 0 ? String.Join(Environment.NewLine, responses) : null; } - private static string? ResponseQueueLicense(Bot bot, EAccess access, string licenses, bool useFilter = false) { + private static string? ResponseQueueLicense(Bot bot, EAccess access, string licenses, bool useFilter = false, [CallerMemberName] string? previousMethodName = null) { if (access < EAccess.Master) { return null; } @@ -178,6 +179,10 @@ internal static class Commands { response.AppendLine(FormatBotResponse(bot, PackageHandler.Handlers[bot.BotName].AddPackage(packageType, gameID, useFilter))); } + if (previousMethodName == nameof(Response)) { + Utilities.InBackground(async() => await PackageHandler.HandleChanges().ConfigureAwait(false)); + } + return response.Length > 0 ? response.ToString() : null; } @@ -196,6 +201,8 @@ internal static class Commands { List responses = new(results.Where(result => !String.IsNullOrEmpty(result))); + Utilities.InBackground(async() => await PackageHandler.HandleChanges().ConfigureAwait(false)); + return responses.Count > 0 ? String.Join(Environment.NewLine, responses) : null; } diff --git a/FreePackages/Handlers/PackageHandler.cs b/FreePackages/Handlers/PackageHandler.cs index 6e73ae2..f3e7043 100644 --- a/FreePackages/Handlers/PackageHandler.cs +++ b/FreePackages/Handlers/PackageHandler.cs @@ -138,10 +138,7 @@ private async static Task IsReady(uint maxWaitTimeSeconds = 120) { } internal async static Task HandleChanges() { - if (!await ProcessChangesSemaphore.WaitAsync(0).ConfigureAwait(false)) { - return; - } - + await ProcessChangesSemaphore.WaitAsync().ConfigureAwait(false); try { await IsReady().ConfigureAwait(false); @@ -485,4 +482,4 @@ internal void AddPackages(HashSet? appIDs, HashSet? packageIDs, bool PackageQueue.AddPackages(packages); } } -} \ No newline at end of file +} diff --git a/FreePackages/Handlers/PackageQueue.cs b/FreePackages/Handlers/PackageQueue.cs index 2f497e7..09f31c6 100644 --- a/FreePackages/Handlers/PackageQueue.cs +++ b/FreePackages/Handlers/PackageQueue.cs @@ -42,18 +42,31 @@ internal void AddPackage(Package package, HashSet? appIDsToRemove = null) } if (package.Type == EPackageType.Sub && appIDsToRemove != null) { - // Remove duplicates. Whenever we're trying to activate and app and also an package for that app, get rid of the app. Because error messages for packages are more descriptive and useful. + // Used to remove duplicates. + // Whenever we're trying to activate an app and also an package for that app, get rid of the app. + // I only really like to do this because the error messages for packages are more descriptive and useful. BotCache.RemoveAppPackages(appIDsToRemove); } + + UpdateTimer(DateTime.Now); } internal void AddPackages(IEnumerable packages) { if (!BotCache.AddPackages(packages)) { return; } + + UpdateTimer(DateTime.Now); } private async Task ProcessQueue() { + DateTime? lastActivation = BotCache.GetLastActivation(); + if (lastActivation != null && lastActivation.Value.AddSeconds(DelayBetweenActivationsSeconds) > DateTime.Now) { + UpdateTimer(lastActivation.Value.AddSeconds(DelayBetweenActivationsSeconds)); + + return; + } + if (!Bot.IsConnectedAndLoggedOn) { UpdateTimer(DateTime.Now.AddMinutes(1)); @@ -67,7 +80,7 @@ private async Task ProcessQueue() { } if (BotCache.NumActivationsPastHour() >= ActivationsPerHour) { - DateTime resumeTime = BotCache.GetLastActivation()!.Value.AddHours(1).AddMinutes(1); + DateTime resumeTime = lastActivation!.Value.AddHours(1).AddMinutes(1); Bot.ArchiLogger.LogGenericInfo(String.Format(Strings.ActivationPaused, String.Format("{0:T}", resumeTime))); UpdateTimer(resumeTime); @@ -285,4 +298,4 @@ internal string GetStatus() { private static int GetMillisecondsFromNow(DateTime then) => Math.Max(0, (int) (then - DateTime.Now).TotalMilliseconds); private void UpdateTimer(DateTime then) => Timer?.Change(GetMillisecondsFromNow(then), Timeout.Infinite); } -} \ No newline at end of file +} diff --git a/FreePackages/IPC/Api/FreePackagesController.cs b/FreePackages/IPC/Api/FreePackagesController.cs index 97be617..562a95f 100644 --- a/FreePackages/IPC/Api/FreePackagesController.cs +++ b/FreePackages/IPC/Api/FreePackagesController.cs @@ -269,7 +269,9 @@ public ActionResult QueueLicenses(string botNames, [FromBody] Q PackageHandler.Handlers[bot.BotName].AddPackages(request.AppIDs, request.PackageIDs, request.UseFilter); } + Utilities.InBackground(async() => await PackageHandler.HandleChanges().ConfigureAwait(false)); + return Ok(new GenericResponse(true)); } } -} \ No newline at end of file +}