Skip to content

Commit

Permalink
Merge pull request #71 from maxisoft/dev
Browse files Browse the repository at this point in the history
Resolve KeyNotFoundException and Enhance Bot Handling in Free Game Collection
  • Loading branch information
maxisoft authored May 11, 2024
2 parents 0348c31 + 18172cb commit 1e8c529
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions ASFFreeGames/ASFFreeGamesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 15 additions & 4 deletions ASFFreeGames/Commands/FreeGamesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal sealed class FreeGamesCommand : IBotCommand, IDisposable {
}

private async Task<string?> 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)");
}
Expand All @@ -134,10 +134,21 @@ internal sealed class FreeGamesCommand : IBotCommand, IDisposable {
}

private async ValueTask<string?> HandleInternalCollectCommand(Bot? bot, string[] args, CancellationToken cancellationToken) {
Dictionary<string, Bot> 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<string, Bot> 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) {
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<PluginName>ASFFreeGames</PluginName>
<Version>1.5.1.0</Version>
<Version>1.5.2.0</Version>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

Expand Down

0 comments on commit 1e8c529

Please sign in to comment.