Skip to content

Commit

Permalink
Fix saving multiple times on filter match, move filter matches outsid…
Browse files Browse the repository at this point in the history
…e of the loop.

Send a notification per filter.
Only send notifications if webhooks are enabled.
  • Loading branch information
Koi-3088 committed Apr 24, 2023
1 parent 9bc5b82 commit f8e996a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions RaidCrawler.Core/Discord/NotificationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static HttpClient Client

private static string[]? DiscordWebhooks;

public static async Task SendNotifications(IWebhookConfig c, ITeraRaid? encounter, Raid raid, IEnumerable<RaidFilter> 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;
Expand All @@ -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);
Expand Down Expand Up @@ -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<RaidFilter> 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
Expand Down Expand Up @@ -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, },
},
Expand Down
30 changes: 16 additions & 14 deletions RaidCrawler.WinForms/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,43 +449,46 @@ 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<RaidFilter>();
foreach (var filter in RaidFilters)
{
if (filter is null)
continue;

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}]");
Expand Down Expand Up @@ -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<RaidFilter> { filter };

int i = -1;
if (InvokeRequired)
Expand All @@ -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.");
}
Expand Down

0 comments on commit f8e996a

Please sign in to comment.