From b6ba24d17100707cf827846d11ccec47a8a5ca7f Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 15 Jul 2019 21:51:41 +0200 Subject: [PATCH] Show front percent for switches with no fronter Closes #113. --- PluralKit.Bot/Services/EmbedService.cs | 12 +++++++++--- PluralKit.Core/Stores.cs | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 7c134580..26492596 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -141,7 +141,7 @@ namespace PluralKit.Bot { .Build(); } - public async Task CreateFrontPercentEmbed(IDictionary frontpercent, ZonedDateTime startingFrom) + public async Task CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, ZonedDateTime startingFrom) { var totalDuration = SystemClock.Instance.GetCurrentInstant() - startingFrom.ToInstant(); @@ -150,12 +150,18 @@ namespace PluralKit.Bot { .WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(startingFrom)} ({Formats.DurationFormat.Format(totalDuration)} ago)"); var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others" + + // We convert to a list of pairs so we can add the no-fronter value + // Dictionary doesn't allow for null keys so we instead have a pair with a null key ;) + var pairs = frontpercent.MemberSwitchDurations.ToList(); + if (frontpercent.NoFronterDuration != Duration.Zero) + pairs.Add(new KeyValuePair(null, frontpercent.NoFronterDuration)); - var membersOrdered = frontpercent.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList(); + var membersOrdered = pairs.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList(); foreach (var pair in membersOrdered) { var frac = pair.Value / totalDuration; - eb.AddField(pair.Key.Name, $"{frac*100:F0}% ({Formats.DurationFormat.Format(pair.Value)})"); + eb.AddField(pair.Key?.Name ?? "*(no fronter)*", $"{frac*100:F0}% ({Formats.DurationFormat.Format(pair.Value)})"); } if (membersOrdered.Count > maxEntriesToDisplay) diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index 38c40ec4..1192f489 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -314,11 +314,19 @@ namespace PluralKit { return outList; } - public async Task> GetPerMemberSwitchDuration(PKSystem system, Instant periodStart, + public struct PerMemberSwitchDuration + { + public Dictionary MemberSwitchDurations; + public Duration NoFronterDuration; + } + + public async Task GetPerMemberSwitchDuration(PKSystem system, Instant periodStart, Instant periodEnd) { var dict = new Dictionary(); + var noFronterDuration = Duration.Zero; + // Sum up all switch durations for each member // switches with multiple members will result in the duration to add up to more than the actual period range foreach (var sw in await GetTruncatedSwitchList(system, periodStart, periodEnd)) @@ -328,9 +336,15 @@ namespace PluralKit { if (!dict.ContainsKey(member)) dict.Add(member, sw.TimespanWithinRange); else dict[member] += sw.TimespanWithinRange; } + + if (sw.Members.Count == 0) noFronterDuration += sw.TimespanWithinRange; } - return dict; + return new PerMemberSwitchDuration + { + MemberSwitchDurations = dict, + NoFronterDuration = noFronterDuration + }; } } } \ No newline at end of file