Further misc BGR improvements

This commit is contained in:
Łukasz Domeradzki
2025-11-11 19:50:05 +01:00
parent 970998fb4b
commit 867b7270ec
4 changed files with 22 additions and 26 deletions

View File

@@ -242,18 +242,18 @@ public sealed class BotController : ArchiController {
return BadRequest(new GenericResponse(false, Strings.FormatBotNotFound(botNames)));
}
OrderedDictionary<string, string> 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<string, OrderedDictionary<string, string>> result = new(bots.Count, Bot.BotsComparer);
foreach (Bot bot in bots) {
result[bot.BotName] = validGamesToRedeemInBackground;
result[bot.BotName] = request.GamesToRedeemInBackground;
}
return Ok(new GenericResponse<IReadOnlyDictionary<string, OrderedDictionary<string, string>>>(result));

View File

@@ -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<string, string> GamesToRedeemInBackground { get; private init; } = new(StringComparer.OrdinalIgnoreCase);

View File

@@ -1091,6 +1091,18 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
return true;
}
internal static void FilterGamesToRedeemInBackground(IDictionary<string, string> gamesToRedeemInBackground) {
if ((gamesToRedeemInBackground == null) || (gamesToRedeemInBackground.Count == 0)) {
throw new ArgumentNullException(nameof(gamesToRedeemInBackground));
}
HashSet<string> 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<string, string> 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<string, string> ValidateGamesToRedeemInBackground(IReadOnlyDictionary<string, string> gamesToRedeemInBackground) {
if ((gamesToRedeemInBackground == null) || (gamesToRedeemInBackground.Count == 0)) {
throw new ArgumentNullException(nameof(gamesToRedeemInBackground));
}
OrderedDictionary<string, string> 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;

View File

@@ -191,6 +191,7 @@ public sealed class BotDatabase : GenericDatabase {
[JsonDisallowNull]
[JsonInclude]
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
private OrderedDictionary<string, string> GamesToRedeemInBackground { get; init; } = new(StringComparer.OrdinalIgnoreCase);
private BotDatabase(string filePath) : this() {