From f8e996aac4b134e6eb6231d539c345748fead490 Mon Sep 17 00:00:00 2001 From: Koi-3088 Date: Sun, 23 Apr 2023 23:38:30 -0500 Subject: [PATCH] Fix saving multiple times on filter match, move filter matches outside of the loop. Send a notification per filter. Only send notifications if webhooks are enabled. --- .../Discord/NotificationHandler.cs | 8 ++--- RaidCrawler.WinForms/MainWindow.cs | 30 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/RaidCrawler.Core/Discord/NotificationHandler.cs b/RaidCrawler.Core/Discord/NotificationHandler.cs index f0f875e..fc56951 100644 --- a/RaidCrawler.Core/Discord/NotificationHandler.cs +++ b/RaidCrawler.Core/Discord/NotificationHandler.cs @@ -21,7 +21,7 @@ public static HttpClient Client private static string[]? DiscordWebhooks; - public static async Task SendNotifications(IWebhookConfig c, ITeraRaid? encounter, Raid raid, IEnumerable filters, string time, IReadOnlyList<(int, int, int)> RewardsList, string hexColor, string spriteName, CancellationToken token) + public static async Task SendNotification(IWebhookConfig c, ITeraRaid? encounter, Raid raid, RaidFilter filter, string time, IReadOnlyList<(int, int, int)> RewardsList, string hexColor, string spriteName, CancellationToken token) { if (encounter is null) return; @@ -30,7 +30,7 @@ public static async Task SendNotifications(IWebhookConfig c, ITeraRaid? encounte if (DiscordWebhooks is null) return; - var webhook = GenerateWebhook(c, encounter, raid, filters, time, RewardsList, hexColor, spriteName); + var webhook = GenerateWebhook(c, encounter, raid, filter, time, RewardsList, hexColor, spriteName); var content = new StringContent(JsonSerializer.Serialize(webhook), System.Text.Encoding.UTF8, "application/json"); foreach (var url in DiscordWebhooks) await Client.PostAsync(url.Trim(), content, token).ConfigureAwait(false); @@ -58,7 +58,7 @@ public static async Task SendScreenshot(IWebhookConfig c, ISwitchConnectionAsync await Client.PostAsync(url.Trim(), content, token).ConfigureAwait(false); } - public static object GenerateWebhook(IWebhookConfig c, ITeraRaid encounter, Raid raid, IEnumerable filters, string time, IReadOnlyList<(int, int, int)> RewardsList, string hexColor, string spriteName) + public static object GenerateWebhook(IWebhookConfig c, ITeraRaid encounter, Raid raid, RaidFilter filter, string time, IReadOnlyList<(int, int, int)> RewardsList, string hexColor, string spriteName) { var param = encounter.GetParam(); var blank = new PK9 @@ -116,7 +116,7 @@ public static object GenerateWebhook(IWebhookConfig c, ITeraRaid encounter, Raid new { name = "Location󠀠󠀠󠀠", value = area, inline = true, }, new { name = "Search Time󠀠󠀠󠀠", value = time, inline = true, }, - new { name = "Filter Name" + (filters.Count() > 1 ? "s" : string.Empty), value = string.Join(", ", filters.Select(z => z.Name)), inline = true, }, + new { name = "Filter Name", value = filter.Name, inline = true, }, new { name = rewards != "" ? "Rewards" : "", value = rewards, inline = false, }, }, diff --git a/RaidCrawler.WinForms/MainWindow.cs b/RaidCrawler.WinForms/MainWindow.cs index 41f59be..7b0325a 100644 --- a/RaidCrawler.WinForms/MainWindow.cs +++ b/RaidCrawler.WinForms/MainWindow.cs @@ -449,9 +449,9 @@ private async Task AdvanceDateClick(CancellationToken token) var encounters = RaidContainer.Container.Encounters; var rewards = RaidContainer.Container.Rewards; var boost = Invoke(() => { return RaidBoost.SelectedIndex; }); + var satisfied_filters = new List<(RaidFilter, ITeraRaid, Raid, IReadOnlyList<(int, int, int)>)>(); for (int i = 0; i < raids.Count; i++) { - var satisfied_filters = new List(); foreach (var filter in RaidFilters) { if (filter is null) @@ -459,33 +459,36 @@ private async Task AdvanceDateClick(CancellationToken token) if (filter.FilterSatisfied(encounters[i], raids[i], boost)) { - satisfied_filters.Add(filter); + satisfied_filters.Add((filter, encounters[i], raids[i], rewards[i])); if (InvokeRequired) Invoke(() => { ComboIndex.SelectedIndex = i; }); else ComboIndex.SelectedIndex = i; } } + } - if (satisfied_filters.Count > 0) + if (Config.EnableNotification) + { + foreach (var satisfied in satisfied_filters) { - // Save game on match. - if (Config.SaveOnMatch) - await ConnectionWrapper.SaveGame(Config, token).ConfigureAwait(false); - - var teraType = raids[i].GetTeraType(encounters[i]); + var teraType = satisfied.Item3.GetTeraType(satisfied.Item2); var color = TypeColor.GetTypeSpriteColor((byte)teraType); var hexColor = $"{color.R:X2}{color.G:X2}{color.B:X2}"; var blank = new PK9 { - Species = encounters[i].Species, - Form = encounters[i].Form + Species = satisfied.Item2.Species, + Form = satisfied.Item2.Form }; - var spriteName = GetSpriteNameForUrl(blank, raids[i].CheckIsShiny(encounters[i])); - await NotificationHandler.SendNotifications(Config, encounters[i], raids[i], satisfied_filters, time, rewards[i], hexColor, spriteName, Source.Token).ConfigureAwait(false); + var spriteName = GetSpriteNameForUrl(blank, satisfied.Item3.CheckIsShiny(satisfied.Item2)); + await NotificationHandler.SendNotification(Config, satisfied.Item2, satisfied.Item3, satisfied.Item1, time, satisfied.Item4, hexColor, spriteName, Source.Token).ConfigureAwait(false); } } + // Save game on match. + if (Config.SaveOnMatch && satisfied_filters.Count > 0) + await ConnectionWrapper.SaveGame(Config, token).ConfigureAwait(false); + if (Config.EnableAlertWindow) ShowMessageBox($"{Config.AlertWindowMessage}\n\nTime Spent: {time}", "Result found!"); Invoke(() => Text = $"{formTitle} [Match Found in {time}]"); @@ -1346,7 +1349,6 @@ public void TestWebhook() private async Task TestWebhookAsync(CancellationToken token) { var filter = new RaidFilter { Name = "Test Webhook" }; - var satisfied_filters = new List { filter }; int i = -1; if (InvokeRequired) @@ -1373,7 +1375,7 @@ private async Task TestWebhookAsync(CancellationToken token) blank.SetSuggestedFormArgument(); var spriteName = GetSpriteNameForUrl(blank, raids[i].CheckIsShiny(encounters[i])); - await NotificationHandler.SendNotifications(Config, encounters[i], raids[i], satisfied_filters, time, rewards[i], hexColor, spriteName, token).ConfigureAwait(false); + await NotificationHandler.SendNotification(Config, encounters[i], raids[i], filter, time, rewards[i], hexColor, spriteName, token).ConfigureAwait(false); } else ShowMessageBox("Please connect to your device and ensure a raid has been found."); }