Skip to content

Commit 2bd39da

Browse files
committed
Fix duplicate activation attempts
1 parent 5a4fac8 commit 2bd39da

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

FreePackages/Data/BotCache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ internal bool RemoveAppPackages(HashSet<uint> appIDsToRemove) {
129129
}
130130

131131
internal Package? GetNextPackage() {
132+
// Return the package which should be activated first, prioritizing first packages which have a start and end date
132133
ulong now = DateUtils.DateTimeToUnixTime(DateTime.UtcNow);
133134
Package? package = Packages.FirstOrDefault(x => x.StartTime != null && now > x.StartTime);
134135
if (package != null) {

FreePackages/Handlers/PackageHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ internal void HandleLicenseList(SteamApps.LicenseListCallback callback) {
405405
HashSet<uint> ownedPackageIDs = callback.LicenseList.Select(license => license.PackageID).ToHashSet();
406406
HashSet<uint> newOwnedPackageIDs = ownedPackageIDs.Except(BotCache.SeenPackages).ToHashSet();
407407

408+
if (newOwnedPackageIDs.Count == 0) {
409+
return;
410+
}
411+
412+
// Cached seen packages need to be initialized
408413
if (BotCache.SeenPackages.Count > 0) {
409414
BotCache.AddChanges(newOwnedPackageIDs: newOwnedPackageIDs);
410415
Utilities.InBackground(async() => await HandleChanges().ConfigureAwait(false));

FreePackages/Handlers/PackageQueue.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,32 @@ internal void AddPackage(Package package, HashSet<uint>? appIDsToRemove = null)
4747
// I only really like to do this because the error messages for packages are more descriptive and useful.
4848
BotCache.RemoveAppPackages(appIDsToRemove);
4949
}
50-
51-
UpdateTimer(DateTime.Now);
5250
}
5351

5452
internal void AddPackages(IEnumerable<Package> packages) {
5553
if (!BotCache.AddPackages(packages)) {
5654
return;
5755
}
58-
59-
UpdateTimer(DateTime.Now);
6056
}
6157

6258
private async Task ProcessQueue() {
63-
DateTime? lastActivation = BotCache.GetLastActivation();
64-
if (lastActivation != null && lastActivation.Value.AddSeconds(DelayBetweenActivationsSeconds) > DateTime.Now) {
65-
UpdateTimer(lastActivation.Value.AddSeconds(DelayBetweenActivationsSeconds));
66-
67-
return;
68-
}
69-
7059
if (!Bot.IsConnectedAndLoggedOn) {
7160
UpdateTimer(DateTime.Now.AddMinutes(1));
7261

7362
return;
7463
}
7564

76-
if (BotCache.Packages.Count == 0) {
65+
Package? package = BotCache.GetNextPackage();
66+
if (package == null) {
67+
// No packages to activate
7768
UpdateTimer(DateTime.Now.AddMinutes(1));
7869

7970
return;
8071
}
8172

8273
if (BotCache.NumActivationsPastHour() >= ActivationsPerHour) {
83-
DateTime resumeTime = lastActivation!.Value.AddHours(1).AddMinutes(1);
74+
// Rate limit reached
75+
DateTime resumeTime = BotCache.GetLastActivation()!.Value.AddHours(1).AddMinutes(1);
8476
Bot.ArchiLogger.LogGenericInfo(String.Format(Strings.ActivationPaused, String.Format("{0:T}", resumeTime)));
8577
UpdateTimer(resumeTime);
8678

@@ -94,14 +86,6 @@ private async Task ProcessQueue() {
9486
return;
9587
}
9688

97-
Package? package = BotCache.GetNextPackage();
98-
if (package == null) {
99-
// There are packages to redeem, but they aren't active yet
100-
UpdateTimer(DateTime.Now.AddMinutes(1));
101-
102-
return;
103-
}
104-
10589
EResult result = await ClaimPackage(package).ConfigureAwait(false);
10690

10791
if (result == EResult.RateLimitExceeded) {
@@ -115,7 +99,7 @@ private async Task ProcessQueue() {
11599
}
116100

117101
// Note: Not everything counts against the activation limit, ex: All playtests?, Some sub errors (dunno which), Maybe some app errors
118-
// Might be worth revisiting later, but for now I feel comfortable just assuming everything counts
102+
// Might be worth revisiting later, but for now I feel comfortable just assuming everything that doesnt get a rate limit response counts
119103
BotCache.AddActivation(DateTime.Now);
120104

121105
if (result == EResult.OK || result == EResult.Invalid) {

0 commit comments

Comments
 (0)