From 217f5ed1f69927cec26888efda268eef67e20622 Mon Sep 17 00:00:00 2001 From: maxisoft Date: Sat, 11 May 2024 11:26:44 +0200 Subject: [PATCH 1/2] Fix KeyNotFoundException for scheduled free game collection (#70) This commit resolves the `KeyNotFoundException` encountered during the scheduled free game collection process. The issue was caused by an attempt to access a non-existent key in the bot dictionary, as reported in issue #70. Changes include: - Added a check in `ASFFreeGamesPlugin.cs` to ensure there are viable bots before proceeding with the free game collection command. - Modified `FreeGamesCommand.cs` to handle bot collections more robustly, trimming bot names to prevent whitespace issues and using `GetValueOrDefault` to avoid exceptions when a bot name is not found. - Updated the `HandleInternalCollectCommand` method to return a more informative response, indicating the number of collected games and the bots involved in the operation. These updates aim to enhance the stability and reliability of the free game collection feature in ASF. --- ASFFreeGames/ASFFreeGamesPlugin.cs | 6 ++++++ ASFFreeGames/Commands/FreeGamesCommand.cs | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/ASFFreeGames/ASFFreeGamesPlugin.cs b/ASFFreeGames/ASFFreeGamesPlugin.cs index cd96e28..d23f445 100644 --- a/ASFFreeGames/ASFFreeGamesPlugin.cs +++ b/ASFFreeGames/ASFFreeGamesPlugin.cs @@ -127,6 +127,12 @@ public async void CollectGamesOnClock(object? source) { Array.Sort(reorderedBots, comparison); } + if (reorderedBots.Length == 0) { + ArchiLogger.LogGenericDebug("no viable bot found for freegame scheduled operation"); + + return; + } + if (!cts.IsCancellationRequested) { string cmd = $"FREEGAMES {FreeGamesCommand.CollectInternalCommandString} " + string.Join(' ', reorderedBots.Select(static bot => bot.BotName)); await OnBotCommand(null!, EAccess.None, cmd, cmd.Split()).ConfigureAwait(false); diff --git a/ASFFreeGames/Commands/FreeGamesCommand.cs b/ASFFreeGames/Commands/FreeGamesCommand.cs index 6d6b5f1..113f8ad 100644 --- a/ASFFreeGames/Commands/FreeGamesCommand.cs +++ b/ASFFreeGames/Commands/FreeGamesCommand.cs @@ -122,7 +122,7 @@ internal sealed class FreeGamesCommand : IBotCommand, IDisposable { } private async Task HandleCollectCommand(Bot? bot) { - int collected = await CollectGames(bot is not null ? new[] { bot } : Context.Bots, ECollectGameRequestSource.RequestedByUser, Context.CancellationToken).ConfigureAwait(false); + int collected = await CollectGames(bot is not null ? [bot] : Context.Bots.ToArray(), ECollectGameRequestSource.RequestedByUser, Context.CancellationToken).ConfigureAwait(false); return FormatBotResponse(bot, $"Collected a total of {collected} free game(s)"); } @@ -134,10 +134,21 @@ internal sealed class FreeGamesCommand : IBotCommand, IDisposable { } private async ValueTask HandleInternalCollectCommand(Bot? bot, string[] args, CancellationToken cancellationToken) { - Dictionary botMap = Context.Bots.ToDictionary(static b => b.BotName, static b => b, StringComparer.InvariantCultureIgnoreCase); - int collected = await CollectGames(args.Skip(2).Select(botName => botMap[botName]), ECollectGameRequestSource.Scheduled, cancellationToken).ConfigureAwait(false); + Dictionary botMap = Context.Bots.ToDictionary(static b => b.BotName.Trim(), static b => b, StringComparer.InvariantCultureIgnoreCase); - return FormatBotResponse(bot, $"Collected a total of {collected} free game(s)"); + Bot[] bots = args.Skip(2).Select(botName => botMap.GetValueOrDefault(botName.Trim())).Where(static b => b is not null).ToArray()!; + + if (bots.Length == 0) { + if (bot is null) { + return null; + } + + bots = [bot]; + } + + int collected = await CollectGames(bots, ECollectGameRequestSource.Scheduled, cancellationToken).ConfigureAwait(false); + + return FormatBotResponse(bot, $"Collected a total of {collected} free game(s)" + (bots.Length > 1 ? $" on {bots.Length} bots" : $" on {bots.FirstOrDefault()?.BotName}")); } private async Task SaveOptions(CancellationToken cancellationToken) { From 18172cb80d46334e64f9e35f91dd543cc285929f Mon Sep 17 00:00:00 2001 From: maxisoft Date: Sat, 11 May 2024 11:32:06 +0200 Subject: [PATCH 2/2] Bump to 1.5.2 --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 6bf6713..b6fa018 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -3,7 +3,7 @@ ASFFreeGames - 1.5.1.0 + 1.5.2.0 net8.0