PluralKit/PluralKit.Bot/Commands/SystemFront.cs

132 lines
5.2 KiB
C#
Raw Normal View History

2020-04-24 21:50:34 +00:00
using System;
2020-04-24 19:50:28 +00:00
using System.Linq;
2021-01-31 15:02:34 +00:00
using System.Text;
2020-02-01 12:03:02 +00:00
using System.Threading.Tasks;
using NodaTime;
using PluralKit.Core;
2020-02-01 12:03:02 +00:00
namespace PluralKit.Bot
2020-02-01 12:03:02 +00:00
{
public class SystemFront
{
2020-08-29 11:46:27 +00:00
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly EmbedService _embeds;
2020-02-01 12:03:02 +00:00
2020-08-29 11:46:27 +00:00
public SystemFront(EmbedService embeds, IDatabase db, ModelRepository repo)
2020-02-01 12:03:02 +00:00
{
_embeds = embeds;
2020-08-29 11:46:27 +00:00
_db = db;
_repo = repo;
2020-02-01 12:03:02 +00:00
}
struct FrontHistoryEntry
{
2020-08-29 11:46:27 +00:00
public readonly Instant? LastTime;
public readonly PKSwitch ThisSwitch;
2020-02-01 12:03:02 +00:00
public FrontHistoryEntry(Instant? lastTime, PKSwitch thisSwitch)
{
LastTime = lastTime;
ThisSwitch = thisSwitch;
}
}
public async Task SystemFronter(Context ctx, PKSystem system)
{
if (system == null) throw Errors.NoSystemError;
ctx.CheckSystemPrivacy(system, system.FrontPrivacy);
2020-08-29 11:46:27 +00:00
await using var conn = await _db.Obtain();
2020-02-01 12:03:02 +00:00
2020-08-29 11:46:27 +00:00
var sw = await _repo.GetLatestSwitch(conn, system.Id);
2020-02-01 12:03:02 +00:00
if (sw == null) throw Errors.NoRegisteredSwitches;
await ctx.Reply(embed: await _embeds.CreateFronterEmbed(sw, system.Zone, ctx.LookupContextFor(system)));
2020-02-01 12:03:02 +00:00
}
public async Task SystemFrontHistory(Context ctx, PKSystem system)
{
if (system == null) throw Errors.NoSystemError;
ctx.CheckSystemPrivacy(system, system.FrontHistoryPrivacy);
2020-08-29 11:46:27 +00:00
// Gotta be careful here: if we dispose of the connection while the IAE is alive, boom
await using var conn = await _db.Obtain();
2020-02-01 12:03:02 +00:00
2020-08-29 11:46:27 +00:00
var totalSwitches = await _repo.GetSwitchCount(conn, system.Id);
if (totalSwitches == 0) throw Errors.NoRegisteredSwitches;
var sws = _repo.GetSwitches(conn, system.Id)
.Scan(new FrontHistoryEntry(null, null),
(lastEntry, newSwitch) => new FrontHistoryEntry(lastEntry.ThisSwitch?.Timestamp, newSwitch));
2020-02-01 12:03:02 +00:00
var embedTitle = system.Name != null ? $"Front history of {system.Name} (`{system.Hid}`)" : $"Front history of `{system.Hid}`";
await ctx.Paginate(
sws,
totalSwitches,
10,
embedTitle,
2021-03-28 17:22:31 +00:00
system.Color,
2020-02-01 12:03:02 +00:00
async (builder, switches) =>
{
2021-01-31 15:02:34 +00:00
var sb = new StringBuilder();
2020-02-01 12:03:02 +00:00
foreach (var entry in switches)
{
var lastSw = entry.LastTime;
var sw = entry.ThisSwitch;
2020-08-29 11:46:27 +00:00
2020-02-01 12:03:02 +00:00
// Fetch member list and format
2020-08-29 11:46:27 +00:00
await using var conn = await _db.Obtain();
var members = await _db.Execute(c => _repo.GetSwitchMembers(c, sw.Id)).ToListAsync();
var membersStr = members.Any() ? string.Join(", ", members.Select(m => m.NameFor(ctx))) : "no fronter";
2020-02-01 12:03:02 +00:00
var switchSince = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp;
// If this isn't the latest switch, we also show duration
string stringToAdd;
if (lastSw != null)
{
// Calculate the time between the last switch (that we iterated - ie. the next one on the timeline) and the current one
var switchDuration = lastSw.Value - sw.Timestamp;
stringToAdd =
$"**{membersStr}** ({sw.Timestamp.FormatZoned(system.Zone)}, {switchSince.FormatDuration()} ago, for {switchDuration.FormatDuration()})\n";
2020-02-01 12:03:02 +00:00
}
else
{
stringToAdd =
$"**{membersStr}** ({sw.Timestamp.FormatZoned(system.Zone)}, {switchSince.FormatDuration()} ago)\n";
2020-02-01 12:03:02 +00:00
}
2021-01-31 15:02:34 +00:00
if (sb.Length + stringToAdd.Length >= 1024)
2020-04-24 19:50:28 +00:00
break;
2021-01-31 15:02:34 +00:00
sb.Append(stringToAdd);
2020-02-01 12:03:02 +00:00
}
2021-01-31 15:02:34 +00:00
builder.Description(sb.ToString());
2020-02-01 12:03:02 +00:00
}
);
}
public async Task SystemFrontPercent(Context ctx, PKSystem system)
{
if (system == null) throw Errors.NoSystemError;
ctx.CheckSystemPrivacy(system, system.FrontHistoryPrivacy);
string durationStr = ctx.RemainderOrNull() ?? "30d";
var now = SystemClock.Instance.GetCurrentInstant();
var rangeStart = DateUtils.ParseDateTime(durationStr, true, system.Zone);
2020-02-01 12:03:02 +00:00
if (rangeStart == null) throw Errors.InvalidDateTime(durationStr);
if (rangeStart.Value.ToInstant() > now) throw Errors.FrontPercentTimeInFuture;
2020-08-29 11:46:27 +00:00
2021-03-28 10:15:21 +00:00
var frontpercent = await _db.Execute(c => _repo.GetFrontBreakdown(c, system.Id, rangeStart.Value.ToInstant(), now));
2021-03-28 10:22:19 +00:00
await ctx.Reply(embed: await _embeds.CreateFrontPercentEmbed(frontpercent, system.Zone, ctx.LookupContextFor(system)));
2020-02-01 12:03:02 +00:00
}
}
}