Fix front percent returning negative percentages and ranges

This commit is contained in:
Ske 2019-07-17 13:37:43 +02:00
parent 548fe09fd4
commit f53344cd89
4 changed files with 12 additions and 11 deletions

View File

@ -214,15 +214,15 @@ namespace PluralKit.Bot.Commands
{
var system = ContextEntity ?? Context.SenderSystem;
if (system == null) throw Errors.NoSystemError;
var now = SystemClock.Instance.GetCurrentInstant();
var duration = PluralKit.Utils.ParsePeriod(durationStr);
if (duration == null) throw Errors.InvalidDateTime(durationStr);
var rangeStart = PluralKit.Utils.ParseDateTime(durationStr);
if (rangeStart == null) throw Errors.InvalidDateTime(durationStr);
if (rangeStart.Value.ToInstant() > now) throw Errors.FrontPercentTimeInFuture;
var rangeEnd = SystemClock.Instance.GetCurrentInstant();
var rangeStart = rangeEnd - duration.Value;
var frontpercent = await Switches.GetPerMemberSwitchDuration(system, rangeEnd - duration.Value, rangeEnd);
await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontPercentEmbed(frontpercent, rangeStart.InZone(system.Zone)));
var frontpercent = await Switches.GetPerMemberSwitchDuration(system, rangeStart.Value.ToInstant(), now);
await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontPercentEmbed(frontpercent, system.Zone));
}
[Command("timezone")]

View File

@ -70,5 +70,6 @@ namespace PluralKit.Bot {
public static PKError MessageNotFound(ulong id) => new PKError($"Message with ID '{id}' not found. Are you sure it's a message proxied by PluralKit?");
public static PKError DurationParseError(string durationStr) => new PKError($"Could not parse '{durationStr.Sanitize()}' as a valid duration. Try a format such as `30d`, `1d3h` or `20m30s`.");
public static PKError FrontPercentTimeInFuture => new PKError("Cannot get the front percent between now and a time in the future.");
}
}

View File

@ -146,12 +146,12 @@ namespace PluralKit.Bot {
.Build();
}
public async Task<Embed> CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, ZonedDateTime startingFrom)
public async Task<Embed> CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, DateTimeZone tz)
{
var actualPeriod = frontpercent.RangeEnd - frontpercent.RangeStart;
var eb = new EmbedBuilder()
.WithColor(Color.Blue)
.WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(startingFrom)} ({Formats.DurationFormat.Format(actualPeriod)} ago)");
.WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(frontpercent.RangeStart.InZone(tz))} ({Formats.DurationFormat.Format(actualPeriod)} ago)");
var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others"

View File

@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions;
@ -325,7 +326,6 @@ namespace PluralKit {
var switchStartClamped = switchInRange.Timestamp;
if (switchStartClamped < periodStart) switchStartClamped = periodStart;
var span = endTime - switchStartClamped;
outList.Add(new SwitchListEntry
{
Members = (await GetSwitchMemberIds(switchInRange)).Select(id => memberObjects[id]).ToList(),
@ -373,7 +373,7 @@ namespace PluralKit {
if (sw.Members.Count == 0) noFronterDuration += span;
if (sw.TimespanStart < actualStart) actualStart = sw.TimespanStart;
if (sw.TimespanEnd < actualStart) actualStart = sw.TimespanEnd;
if (sw.TimespanEnd > actualEnd) actualEnd = sw.TimespanEnd;
}
return new PerMemberSwitchDuration