From 867b7270ecd0316742337de2757709490850918c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Tue, 11 Nov 2025 19:50:05 +0100 Subject: [PATCH] Further misc BGR improvements --- .../IPC/Controllers/Api/BotController.cs | 10 +++--- .../BotGamesToRedeemInBackgroundRequest.cs | 1 + ArchiSteamFarm/Steam/Bot.cs | 36 ++++++++----------- ArchiSteamFarm/Steam/Storage/BotDatabase.cs | 1 + 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs index d6090a904..132a0c516 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs @@ -242,18 +242,18 @@ public sealed class BotController : ArchiController { return BadRequest(new GenericResponse(false, Strings.FormatBotNotFound(botNames))); } - OrderedDictionary validGamesToRedeemInBackground = Bot.ValidateGamesToRedeemInBackground(request.GamesToRedeemInBackground); + Bot.FilterGamesToRedeemInBackground(request.GamesToRedeemInBackground); - if (validGamesToRedeemInBackground.Count == 0) { - return BadRequest(new GenericResponse(false, Strings.FormatErrorIsEmpty(nameof(validGamesToRedeemInBackground)))); + if (request.GamesToRedeemInBackground.Count == 0) { + return BadRequest(new GenericResponse(false, Strings.FormatErrorIsEmpty(nameof(request.GamesToRedeemInBackground)))); } - await Utilities.InParallel(bots.Select(bot => Task.Run(() => bot.AddGamesToRedeemInBackground(validGamesToRedeemInBackground)))).ConfigureAwait(false); + await Utilities.InParallel(bots.Select(bot => Task.Run(() => bot.AddGamesToRedeemInBackground(request.GamesToRedeemInBackground)))).ConfigureAwait(false); Dictionary> result = new(bots.Count, Bot.BotsComparer); foreach (Bot bot in bots) { - result[bot.BotName] = validGamesToRedeemInBackground; + result[bot.BotName] = request.GamesToRedeemInBackground; } return Ok(new GenericResponse>>(result)); diff --git a/ArchiSteamFarm/IPC/Requests/BotGamesToRedeemInBackgroundRequest.cs b/ArchiSteamFarm/IPC/Requests/BotGamesToRedeemInBackgroundRequest.cs index da5fc46ec..0ea5f9190 100644 --- a/ArchiSteamFarm/IPC/Requests/BotGamesToRedeemInBackgroundRequest.cs +++ b/ArchiSteamFarm/IPC/Requests/BotGamesToRedeemInBackgroundRequest.cs @@ -34,6 +34,7 @@ namespace ArchiSteamFarm.IPC.Requests; public sealed class BotGamesToRedeemInBackgroundRequest { [Description("A string-string map that maps cd-key to redeem (key) to its name (value). Key in the map must be a valid and unique Steam cd-key. Value in the map must be a non-null and non-empty name of the key (e.g. game's name, but can be anything)")] [JsonInclude] + [JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] [JsonRequired] [Required] public OrderedDictionary GamesToRedeemInBackground { get; private init; } = new(StringComparer.OrdinalIgnoreCase); diff --git a/ArchiSteamFarm/Steam/Bot.cs b/ArchiSteamFarm/Steam/Bot.cs index 640043c8e..ca0f2b8b3 100644 --- a/ArchiSteamFarm/Steam/Bot.cs +++ b/ArchiSteamFarm/Steam/Bot.cs @@ -1091,6 +1091,18 @@ public sealed class Bot : IAsyncDisposable, IDisposable { return true; } + internal static void FilterGamesToRedeemInBackground(IDictionary gamesToRedeemInBackground) { + if ((gamesToRedeemInBackground == null) || (gamesToRedeemInBackground.Count == 0)) { + throw new ArgumentNullException(nameof(gamesToRedeemInBackground)); + } + + HashSet invalidKeys = gamesToRedeemInBackground.Where(static entry => !BotDatabase.IsValidGameToRedeemInBackground(entry.Key, entry.Value)).Select(static game => game.Key).ToHashSet(StringComparer.OrdinalIgnoreCase); + + foreach (string invalidKey in invalidKeys) { + gamesToRedeemInBackground.Remove(invalidKey); + } + } + internal static string FormatBotResponse(string response, string botName) { ArgumentException.ThrowIfNullOrEmpty(response); ArgumentException.ThrowIfNullOrEmpty(botName); @@ -1487,10 +1499,10 @@ public sealed class Bot : IAsyncDisposable, IDisposable { } if (gamesToRedeemInBackground.Count > 0) { - OrderedDictionary validGamesToRedeemInBackground = ValidateGamesToRedeemInBackground(gamesToRedeemInBackground); + FilterGamesToRedeemInBackground(gamesToRedeemInBackground); - if (validGamesToRedeemInBackground.Count > 0) { - AddGamesToRedeemInBackground(validGamesToRedeemInBackground); + if (gamesToRedeemInBackground.Count > 0) { + AddGamesToRedeemInBackground(gamesToRedeemInBackground); } } @@ -2007,24 +2019,6 @@ public sealed class Bot : IAsyncDisposable, IDisposable { return true; } - internal static OrderedDictionary ValidateGamesToRedeemInBackground(IReadOnlyDictionary gamesToRedeemInBackground) { - if ((gamesToRedeemInBackground == null) || (gamesToRedeemInBackground.Count == 0)) { - throw new ArgumentNullException(nameof(gamesToRedeemInBackground)); - } - - OrderedDictionary result = new(StringComparer.OrdinalIgnoreCase); - - foreach ((string key, string name) in gamesToRedeemInBackground) { - if (!BotDatabase.IsValidGameToRedeemInBackground(key, name)) { - continue; - } - - result[key] = name; - } - - return result; - } - private async Task Connect() { if (!KeepRunning || SteamClient.IsConnected) { return; diff --git a/ArchiSteamFarm/Steam/Storage/BotDatabase.cs b/ArchiSteamFarm/Steam/Storage/BotDatabase.cs index a35c03aae..0aa0da6fb 100644 --- a/ArchiSteamFarm/Steam/Storage/BotDatabase.cs +++ b/ArchiSteamFarm/Steam/Storage/BotDatabase.cs @@ -191,6 +191,7 @@ public sealed class BotDatabase : GenericDatabase { [JsonDisallowNull] [JsonInclude] + [JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] private OrderedDictionary GamesToRedeemInBackground { get; init; } = new(StringComparer.OrdinalIgnoreCase); private BotDatabase(string filePath) : this() {