Skip to content

Commit

Permalink
Add backoff when updating booster info
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Mar 25, 2024
1 parent d4bd335 commit e29981a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions BoosterManager/Boosters/BoosterQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal sealed class BoosterQueue : IDisposable {
internal int BoosterDelay = 0; // Delay, in seconds, added to all booster crafts
private readonly BoosterDatabase? BoosterDatabase;
internal event Action? OnBoosterInfosUpdated;
private float BoosterInfosUpdateBackOffMultiplier = 1.0F;

internal BoosterQueue(Bot bot, BoosterHandler boosterHandler) {
Bot = bot;
Expand Down Expand Up @@ -57,25 +58,31 @@ private async Task Run() {

if (!await UpdateBoosterInfos().ConfigureAwait(false)) {
Bot.ArchiLogger.LogGenericError(Strings.BoosterInfoUpdateFailed);
UpdateTimer(DateTime.Now.AddMinutes(1));
UpdateTimer(DateTime.Now.AddMinutes(Math.Min(15, 1 * BoosterInfosUpdateBackOffMultiplier)));
BoosterInfosUpdateBackOffMultiplier += 0.5F;

return;
}

Booster? booster = GetNextCraftableBooster(BoosterType.Any);
if (booster == null) {
BoosterInfosUpdateBackOffMultiplier = 1.0F;

return;
}

if (DateTime.Now >= booster.GetAvailableAtTime(BoosterDelay)) {
if (booster.Info.Price > GetAvailableGems()) {
BoosterHandler.PerpareStatusReport(String.Format(Strings.NotEnoughGems, String.Format("{0:N0}", GetGemsNeeded(BoosterType.Any, wasCrafted: false) - GetAvailableGems())), suppressDuplicateMessages: true);
OnBoosterInfosUpdated += ForceUpdateBoosterInfos;
UpdateTimer(DateTime.Now.AddMinutes(GetNumBoosters(BoosterType.OneTime) > 0 ? 1 : 15));
UpdateTimer(DateTime.Now.AddMinutes(Math.Min(15, (GetNumBoosters(BoosterType.OneTime) > 0 ? 1 : 15) * BoosterInfosUpdateBackOffMultiplier)));
BoosterInfosUpdateBackOffMultiplier += 0.5F;

return;
}

BoosterInfosUpdateBackOffMultiplier = 1.0F;

if (!await CraftBooster(booster).ConfigureAwait(false)) {
Bot.ArchiLogger.LogGenericError(String.Format(Strings.BoosterCreationFailed, booster.GameID));
VerifyCraftBoosterError(booster);
Expand All @@ -93,6 +100,8 @@ private async Task Run() {
}
}

BoosterInfosUpdateBackOffMultiplier = 1.0F;

DateTime nextBoosterTime = booster.GetAvailableAtTime(BoosterDelay);
if (nextBoosterTime < DateTime.Now.AddSeconds(MinDelayBetweenBoosters)) {
nextBoosterTime = DateTime.Now.AddSeconds(MinDelayBetweenBoosters);
Expand Down

0 comments on commit e29981a

Please sign in to comment.