diff --git a/FreePackages/Data/BotCache.cs b/FreePackages/Data/BotCache.cs index faf9c0a..dffe736 100644 --- a/FreePackages/Data/BotCache.cs +++ b/FreePackages/Data/BotCache.cs @@ -129,6 +129,7 @@ internal bool RemoveAppPackages(HashSet appIDsToRemove) { } internal Package? GetNextPackage() { + // Return the package which should be activated first, prioritizing first packages which have a start and end date ulong now = DateUtils.DateTimeToUnixTime(DateTime.UtcNow); Package? package = Packages.FirstOrDefault(x => x.StartTime != null && now > x.StartTime); if (package != null) { diff --git a/FreePackages/Handlers/PackageHandler.cs b/FreePackages/Handlers/PackageHandler.cs index 421be42..42a84d3 100644 --- a/FreePackages/Handlers/PackageHandler.cs +++ b/FreePackages/Handlers/PackageHandler.cs @@ -405,6 +405,11 @@ internal void HandleLicenseList(SteamApps.LicenseListCallback callback) { HashSet ownedPackageIDs = callback.LicenseList.Select(license => license.PackageID).ToHashSet(); HashSet newOwnedPackageIDs = ownedPackageIDs.Except(BotCache.SeenPackages).ToHashSet(); + if (newOwnedPackageIDs.Count == 0) { + return; + } + + // Cached seen packages need to be initialized if (BotCache.SeenPackages.Count > 0) { BotCache.AddChanges(newOwnedPackageIDs: newOwnedPackageIDs); Utilities.InBackground(async() => await HandleChanges().ConfigureAwait(false)); diff --git a/FreePackages/Handlers/PackageQueue.cs b/FreePackages/Handlers/PackageQueue.cs index 09f31c6..c6b379e 100644 --- a/FreePackages/Handlers/PackageQueue.cs +++ b/FreePackages/Handlers/PackageQueue.cs @@ -47,40 +47,32 @@ internal void AddPackage(Package package, HashSet? appIDsToRemove = null) // 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)); return; } - if (BotCache.Packages.Count == 0) { + Package? package = BotCache.GetNextPackage(); + if (package == null) { + // No packages to activate UpdateTimer(DateTime.Now.AddMinutes(1)); return; } if (BotCache.NumActivationsPastHour() >= ActivationsPerHour) { - DateTime resumeTime = lastActivation!.Value.AddHours(1).AddMinutes(1); + // Rate limit reached + DateTime resumeTime = BotCache.GetLastActivation()!.Value.AddHours(1).AddMinutes(1); Bot.ArchiLogger.LogGenericInfo(String.Format(Strings.ActivationPaused, String.Format("{0:T}", resumeTime))); UpdateTimer(resumeTime); @@ -94,14 +86,6 @@ private async Task ProcessQueue() { return; } - Package? package = BotCache.GetNextPackage(); - if (package == null) { - // There are packages to redeem, but they aren't active yet - UpdateTimer(DateTime.Now.AddMinutes(1)); - - return; - } - EResult result = await ClaimPackage(package).ConfigureAwait(false); if (result == EResult.RateLimitExceeded) { @@ -115,7 +99,7 @@ private async Task ProcessQueue() { } // Note: Not everything counts against the activation limit, ex: All playtests?, Some sub errors (dunno which), Maybe some app errors - // Might be worth revisiting later, but for now I feel comfortable just assuming everything counts + // Might be worth revisiting later, but for now I feel comfortable just assuming everything that doesnt get a rate limit response counts BotCache.AddActivation(DateTime.Now); if (result == EResult.OK || result == EResult.Invalid) {