Skip to content

Commit

Permalink
Fix duplicate activation attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed May 11, 2024
1 parent 5a4fac8 commit 2bd39da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
1 change: 1 addition & 0 deletions FreePackages/Data/BotCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ internal bool RemoveAppPackages(HashSet<uint> 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) {
Expand Down
5 changes: 5 additions & 0 deletions FreePackages/Handlers/PackageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ internal void HandleLicenseList(SteamApps.LicenseListCallback callback) {
HashSet<uint> ownedPackageIDs = callback.LicenseList.Select(license => license.PackageID).ToHashSet();
HashSet<uint> 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));
Expand Down
28 changes: 6 additions & 22 deletions FreePackages/Handlers/PackageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,32 @@ internal void AddPackage(Package package, HashSet<uint>? 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<Package> 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);

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 2bd39da

Please sign in to comment.