Fix front percent returning negative percentages and ranges
This commit is contained in:
parent
548fe09fd4
commit
f53344cd89
@ -215,14 +215,14 @@ namespace PluralKit.Bot.Commands
|
|||||||
var system = ContextEntity ?? Context.SenderSystem;
|
var system = ContextEntity ?? Context.SenderSystem;
|
||||||
if (system == null) throw Errors.NoSystemError;
|
if (system == null) throw Errors.NoSystemError;
|
||||||
|
|
||||||
var duration = PluralKit.Utils.ParsePeriod(durationStr);
|
var now = SystemClock.Instance.GetCurrentInstant();
|
||||||
if (duration == null) throw Errors.InvalidDateTime(durationStr);
|
|
||||||
|
|
||||||
var rangeEnd = SystemClock.Instance.GetCurrentInstant();
|
var rangeStart = PluralKit.Utils.ParseDateTime(durationStr);
|
||||||
var rangeStart = rangeEnd - duration.Value;
|
if (rangeStart == null) throw Errors.InvalidDateTime(durationStr);
|
||||||
|
if (rangeStart.Value.ToInstant() > now) throw Errors.FrontPercentTimeInFuture;
|
||||||
|
|
||||||
var frontpercent = await Switches.GetPerMemberSwitchDuration(system, rangeEnd - duration.Value, rangeEnd);
|
var frontpercent = await Switches.GetPerMemberSwitchDuration(system, rangeStart.Value.ToInstant(), now);
|
||||||
await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontPercentEmbed(frontpercent, rangeStart.InZone(system.Zone)));
|
await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontPercentEmbed(frontpercent, system.Zone));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("timezone")]
|
[Command("timezone")]
|
||||||
|
@ -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 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 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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -146,12 +146,12 @@ namespace PluralKit.Bot {
|
|||||||
.Build();
|
.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 actualPeriod = frontpercent.RangeEnd - frontpercent.RangeStart;
|
||||||
var eb = new EmbedBuilder()
|
var eb = new EmbedBuilder()
|
||||||
.WithColor(Color.Blue)
|
.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"
|
var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others"
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ using System.Collections;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Dapper;
|
using Dapper;
|
||||||
using Dapper.Contrib.Extensions;
|
using Dapper.Contrib.Extensions;
|
||||||
@ -325,7 +326,6 @@ namespace PluralKit {
|
|||||||
var switchStartClamped = switchInRange.Timestamp;
|
var switchStartClamped = switchInRange.Timestamp;
|
||||||
if (switchStartClamped < periodStart) switchStartClamped = periodStart;
|
if (switchStartClamped < periodStart) switchStartClamped = periodStart;
|
||||||
|
|
||||||
var span = endTime - switchStartClamped;
|
|
||||||
outList.Add(new SwitchListEntry
|
outList.Add(new SwitchListEntry
|
||||||
{
|
{
|
||||||
Members = (await GetSwitchMemberIds(switchInRange)).Select(id => memberObjects[id]).ToList(),
|
Members = (await GetSwitchMemberIds(switchInRange)).Select(id => memberObjects[id]).ToList(),
|
||||||
@ -373,7 +373,7 @@ namespace PluralKit {
|
|||||||
if (sw.Members.Count == 0) noFronterDuration += span;
|
if (sw.Members.Count == 0) noFronterDuration += span;
|
||||||
|
|
||||||
if (sw.TimespanStart < actualStart) actualStart = sw.TimespanStart;
|
if (sw.TimespanStart < actualStart) actualStart = sw.TimespanStart;
|
||||||
if (sw.TimespanEnd < actualStart) actualStart = sw.TimespanEnd;
|
if (sw.TimespanEnd > actualEnd) actualEnd = sw.TimespanEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PerMemberSwitchDuration
|
return new PerMemberSwitchDuration
|
||||||
|
Loading…
Reference in New Issue
Block a user