Skip to content

Commit

Permalink
Closes #3294
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Sep 27, 2024
1 parent 1fc4ac8 commit 0c21c22
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
11 changes: 3 additions & 8 deletions ArchiSteamFarm/Steam/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3564,14 +3564,7 @@ private async void RedeemGamesInBackground(object? state = null) {
(string? key, string? name) = BotDatabase.GetGameToRedeemInBackground();

if (string.IsNullOrEmpty(key)) {
ArchiLogger.LogNullError(key);

break;
}

if (string.IsNullOrEmpty(name)) {
ArchiLogger.LogNullError(name);

// No more games to redeem left, possible due to e.g. queue purge
break;
}

Expand Down Expand Up @@ -3639,6 +3632,8 @@ private async void RedeemGamesInBackground(object? state = null) {
BotDatabase.RemoveGameToRedeemInBackground(key);

// If user omitted the name or intentionally provided the same name as key, replace it with the Steam result
name ??= key;

if (name.Equals(key, StringComparison.OrdinalIgnoreCase) && (items?.Count > 0)) {
name = string.Join(", ", items.Values);
}
Expand Down
38 changes: 38 additions & 0 deletions ArchiSteamFarm/Steam/Interaction/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public static EAccess GetProxyAccess(Bot bot, EAccess access, ulong steamID = 0)
return ResponseWalletBalance(access);
case "BGR":
return ResponseBackgroundGamesRedeemer(access);
case "BGRCLEAR":
return ResponseBackgroundGamesRedeemerClear(access);
case "EXIT":
return ResponseExit(access);
case "FARM":
Expand Down Expand Up @@ -198,6 +200,8 @@ public static EAccess GetProxyAccess(Bot bot, EAccess access, ulong steamID = 0)
return await ResponseWalletBalance(access, Utilities.GetArgsAsText(args, 1, ","), steamID).ConfigureAwait(false);
case "BGR":
return await ResponseBackgroundGamesRedeemer(access, Utilities.GetArgsAsText(args, 1, ","), steamID).ConfigureAwait(false);
case "BGRCLEAR":
return await ResponseBackgroundGamesRedeemerClear(access, Utilities.GetArgsAsText(args, 1, ","), steamID).ConfigureAwait(false);
case "ENCRYPT" when args.Length > 2:
return ResponseEncrypt(access, args[1], Utilities.GetArgsAsText(message, 2));
case "FARM":
Expand Down Expand Up @@ -1148,6 +1152,40 @@ internal void OnNewLicenseList() {
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

private string? ResponseBackgroundGamesRedeemerClear(EAccess access) {
if (!Enum.IsDefined(access)) {
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
}

if (access < EAccess.Master) {
return null;
}

Bot.BotDatabase.ClearGamesToRedeemInBackground();

return FormatBotResponse(Strings.Done);
}

private static async Task<string?> ResponseBackgroundGamesRedeemerClear(EAccess access, string botNames, ulong steamID = 0) {
if (!Enum.IsDefined(access)) {
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
}

ArgumentException.ThrowIfNullOrEmpty(botNames);

HashSet<Bot>? bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0)) {
return access >= EAccess.Owner ? FormatStaticResponse(Strings.FormatBotNotFound(botNames)) : null;
}

IList<string?> results = await Utilities.InParallel(bots.Select(bot => Task.Run(() => bot.Commands.ResponseBackgroundGamesRedeemerClear(GetProxyAccess(bot, access, steamID))))).ConfigureAwait(false);

List<string> responses = [..results.Where(static result => !string.IsNullOrEmpty(result))!];

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

private static string? ResponseEncrypt(EAccess access, string cryptoMethodText, string stringToEncrypt) {
if (!Enum.IsDefined(access)) {
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
Expand Down
12 changes: 12 additions & 0 deletions ArchiSteamFarm/Steam/Storage/BotDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ internal void AddGamesToRedeemInBackground(IOrderedDictionary games) {
Utilities.InBackground(Save);
}

internal void ClearGamesToRedeemInBackground() {
lock (GamesToRedeemInBackground) {
if (GamesToRedeemInBackground.Count == 0) {
return;
}

GamesToRedeemInBackground.Clear();
}

Utilities.InBackground(Save);
}

internal static async Task<BotDatabase?> CreateOrLoad(string filePath) {
ArgumentException.ThrowIfNullOrEmpty(filePath);

Expand Down

0 comments on commit 0c21c22

Please sign in to comment.