Added options '-c' option for '.xpglb' which will show global xp leaderboard only with this server's users

This commit is contained in:
Toastie (DCS Team) 2024-10-08 00:21:43 +13:00
parent c6a4eaf04e
commit 21267db587
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
4 changed files with 53 additions and 24 deletions

View file

@ -88,13 +88,6 @@ public static class DiscordUserExtensions
.Count() .Count()
+ 1; + 1;
public static async Task<IReadOnlyCollection<DiscordUser>> GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page, int perPage)
=> await users.ToLinqToDBTable()
.OrderByDescending(x => x.TotalXp)
.Skip(page * perPage)
.Take(perPage)
.ToArrayAsyncLinqToDB();
public static Task<List<DiscordUser>> GetTopRichest( public static Task<List<DiscordUser>> GetTopRichest(
this DbSet<DiscordUser> users, this DbSet<DiscordUser> users,
ulong botId, ulong botId,

View file

@ -182,22 +182,22 @@ public partial class Xp : EllieModule<XpService>
var (opts, _) = OptionsParser.ParseFrom(new LbOpts(), args); var (opts, _) = OptionsParser.ParseFrom(new LbOpts(), args);
await ctx.Channel.TriggerTypingAsync(); await ctx.Channel.TriggerTypingAsync();
if (opts.Clean)
{
await _tracker.EnsureUsersDownloadedAsync(ctx.Guild);
}
async Task<IReadOnlyCollection<UserXpStats>> GetPageItems(int curPage) async Task<IReadOnlyCollection<UserXpStats>> GetPageItems(int curPage)
{ {
var socketGuild = (SocketGuild)ctx.Guild; var socketGuild = (SocketGuild)ctx.Guild;
if (opts.Clean) if (opts.Clean)
{ {
await ctx.Channel.TriggerTypingAsync(); return await _service.GetGuildUserXps(ctx.Guild.Id,
await _tracker.EnsureUsersDownloadedAsync(ctx.Guild);
return await _service.GetTopUserXps(ctx.Guild.Id,
socketGuild.Users.Select(x => x.Id).ToList(), socketGuild.Users.Select(x => x.Id).ToList(),
curPage); curPage);
} }
return await _service.GetUserXps(ctx.Guild.Id, curPage); return await _service.GetGuildUserXps(ctx.Guild.Id, curPage);
} }
await Response() await Response()
@ -236,14 +236,32 @@ public partial class Xp : EllieModule<XpService>
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task XpGlobalLeaderboard(int page = 1) public async Task XpGlobalLeaderboard(int page = 1, params string[] args)
{ {
if (--page < 0 || page > 99) if (--page < 0 || page > 99)
return; return;
var (opts, _) = OptionsParser.ParseFrom(new LbOpts(), args);
await ctx.Channel.TriggerTypingAsync();
if (opts.Clean)
{
await _tracker.EnsureUsersDownloadedAsync(ctx.Guild);
}
async Task<IReadOnlyCollection<DiscordUser>> GetPageItems(int curPage)
{
if (opts.Clean)
{
return await _service.GetGlobalUserXps(page, ((SocketGuild)ctx.Guild).Users.Select(x => x.Id).ToList());
}
return await _service.GetGlobalUserXps(curPage);
}
await Response() await Response()
.Paginated() .Paginated()
.PageItems(async curPage => await _service.GetUserXps(curPage)) .PageItems(GetPageItems)
.PageSize(9) .PageSize(9)
.Page((users, curPage) => .Page((users, curPage) =>
{ {
@ -281,7 +299,9 @@ public partial class Xp : EllieModule<XpService>
if (role.IsManaged) if (role.IsManaged)
return; return;
var count = await _service.AddXpToUsersAsync(ctx.Guild.Id, amount, role.Members.Select(x => x.Id).ToArray()); var count = await _service.AddXpToUsersAsync(ctx.Guild.Id,
amount,
role.Members.Select(x => x.Id).ToArray());
await Response() await Response()
.Confirm( .Confirm(
strs.xpadd_users(Format.Bold(amount.ToString()), Format.Bold(count.ToString()))) strs.xpadd_users(Format.Bold(amount.ToString()), Format.Bold(count.ToString())))

View file

@ -564,7 +564,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
uow.SaveChanges(); uow.SaveChanges();
} }
public async Task<IReadOnlyCollection<UserXpStats>> GetUserXps(ulong guildId, int page) public async Task<IReadOnlyCollection<UserXpStats>> GetGuildUserXps(ulong guildId, int page)
{ {
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
return await uow return await uow
@ -576,22 +576,38 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
.ToArrayAsyncLinqToDB(); .ToArrayAsyncLinqToDB();
} }
public async Task<IReadOnlyCollection<UserXpStats>> GetTopUserXps(ulong guildId, List<ulong> users, int curPage) public async Task<IReadOnlyCollection<UserXpStats>> GetGuildUserXps(ulong guildId, List<ulong> users, int page)
{ {
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
return await uow.Set<UserXpStats>() return await uow.Set<UserXpStats>()
.Where(x => x.GuildId == guildId && x.UserId.In(users)) .Where(x => x.GuildId == guildId && x.UserId.In(users))
.OrderByDescending(x => x.Xp + x.AwardedXp) .OrderByDescending(x => x.Xp + x.AwardedXp)
.Skip(curPage * 9) .Skip(page * 9)
.Take(9) .Take(9)
.ToArrayAsyncLinqToDB(); .ToArrayAsyncLinqToDB();
} }
public Task<IReadOnlyCollection<DiscordUser>> GetUserXps(int page, int perPage = 9) public async Task<IReadOnlyCollection<DiscordUser>> GetGuildUserXps(int page)
{ {
using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
return uow.Set<DiscordUser>()
.GetUsersXpLeaderboardFor(page, perPage); return await uow.GetTable<DiscordUser>()
.OrderByDescending(x => x.TotalXp)
.Skip(page * 9)
.Take(9)
.ToArrayAsyncLinqToDB();
}
public async Task<IReadOnlyCollection<DiscordUser>> GetGlobalUserXps(int page, List<ulong> users)
{
await using var uow = _db.GetDbContext();
return await uow.GetTable<DiscordUser>()
.Where(x => x.UserId.In(users))
.OrderByDescending(x => x.TotalXp)
.Skip(page * 9)
.Take(9)
.ToArrayAsyncLinqToDB();
} }
public async Task ChangeNotificationType(ulong userId, ulong guildId, XpNotificationLocation type) public async Task ChangeNotificationType(ulong userId, ulong guildId, XpNotificationLocation type)

View file

@ -105,7 +105,7 @@ public sealed class OtherSvc : GrpcOther.GrpcOtherBase, IEService
public override async Task<XpLbReply> GetXpLb(GetLbRequest request, ServerCallContext context) public override async Task<XpLbReply> GetXpLb(GetLbRequest request, ServerCallContext context)
{ {
var users = await _xp.GetUserXps(request.Page, request.PerPage); var users = await _xp.GetGuildUserXps(request.Page, request.PerPage);
var reply = new XpLbReply(); var reply = new XpLbReply();