From 21267db5879c97baf73fa042630aabcf3c1bdbd9 Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 8 Oct 2024 00:21:43 +1300 Subject: [PATCH] Added options '-c' option for '.xpglb' which will show global xp leaderboard only with this server's users --- .../Db/Extensions/DiscordUserExtensions.cs | 7 ---- src/EllieBot/Modules/Xp/Xp.cs | 38 ++++++++++++++----- src/EllieBot/Modules/Xp/XpService.cs | 30 +++++++++++---- src/EllieBot/Services/GrpcApi/OtherSvc.cs | 2 +- 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/EllieBot/Db/Extensions/DiscordUserExtensions.cs b/src/EllieBot/Db/Extensions/DiscordUserExtensions.cs index bdf3c05..255a8b0 100644 --- a/src/EllieBot/Db/Extensions/DiscordUserExtensions.cs +++ b/src/EllieBot/Db/Extensions/DiscordUserExtensions.cs @@ -88,13 +88,6 @@ public static class DiscordUserExtensions .Count() + 1; - public static async Task> GetUsersXpLeaderboardFor(this DbSet users, int page, int perPage) - => await users.ToLinqToDBTable() - .OrderByDescending(x => x.TotalXp) - .Skip(page * perPage) - .Take(perPage) - .ToArrayAsyncLinqToDB(); - public static Task> GetTopRichest( this DbSet users, ulong botId, diff --git a/src/EllieBot/Modules/Xp/Xp.cs b/src/EllieBot/Modules/Xp/Xp.cs index 2ef0505..f4ef40e 100644 --- a/src/EllieBot/Modules/Xp/Xp.cs +++ b/src/EllieBot/Modules/Xp/Xp.cs @@ -182,22 +182,22 @@ public partial class Xp : EllieModule var (opts, _) = OptionsParser.ParseFrom(new LbOpts(), args); await ctx.Channel.TriggerTypingAsync(); + if (opts.Clean) + { + await _tracker.EnsureUsersDownloadedAsync(ctx.Guild); + } - async Task> GetPageItems(int curPage) { var socketGuild = (SocketGuild)ctx.Guild; if (opts.Clean) { - await ctx.Channel.TriggerTypingAsync(); - await _tracker.EnsureUsersDownloadedAsync(ctx.Guild); - - return await _service.GetTopUserXps(ctx.Guild.Id, + return await _service.GetGuildUserXps(ctx.Guild.Id, socketGuild.Users.Select(x => x.Id).ToList(), curPage); } - return await _service.GetUserXps(ctx.Guild.Id, curPage); + return await _service.GetGuildUserXps(ctx.Guild.Id, curPage); } await Response() @@ -236,14 +236,32 @@ public partial class Xp : EllieModule [Cmd] [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) return; + var (opts, _) = OptionsParser.ParseFrom(new LbOpts(), args); + + await ctx.Channel.TriggerTypingAsync(); + if (opts.Clean) + { + await _tracker.EnsureUsersDownloadedAsync(ctx.Guild); + } + + async Task> 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() .Paginated() - .PageItems(async curPage => await _service.GetUserXps(curPage)) + .PageItems(GetPageItems) .PageSize(9) .Page((users, curPage) => { @@ -281,7 +299,9 @@ public partial class Xp : EllieModule if (role.IsManaged) 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() .Confirm( strs.xpadd_users(Format.Bold(amount.ToString()), Format.Bold(count.ToString()))) diff --git a/src/EllieBot/Modules/Xp/XpService.cs b/src/EllieBot/Modules/Xp/XpService.cs index 37bb31f..4143fab 100644 --- a/src/EllieBot/Modules/Xp/XpService.cs +++ b/src/EllieBot/Modules/Xp/XpService.cs @@ -564,7 +564,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand uow.SaveChanges(); } - public async Task> GetUserXps(ulong guildId, int page) + public async Task> GetGuildUserXps(ulong guildId, int page) { await using var uow = _db.GetDbContext(); return await uow @@ -576,22 +576,38 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand .ToArrayAsyncLinqToDB(); } - public async Task> GetTopUserXps(ulong guildId, List users, int curPage) + public async Task> GetGuildUserXps(ulong guildId, List users, int page) { await using var uow = _db.GetDbContext(); return await uow.Set() .Where(x => x.GuildId == guildId && x.UserId.In(users)) .OrderByDescending(x => x.Xp + x.AwardedXp) - .Skip(curPage * 9) + .Skip(page * 9) .Take(9) .ToArrayAsyncLinqToDB(); } - public Task> GetUserXps(int page, int perPage = 9) + public async Task> GetGuildUserXps(int page) { - using var uow = _db.GetDbContext(); - return uow.Set() - .GetUsersXpLeaderboardFor(page, perPage); + await using var uow = _db.GetDbContext(); + + return await uow.GetTable() + .OrderByDescending(x => x.TotalXp) + .Skip(page * 9) + .Take(9) + .ToArrayAsyncLinqToDB(); + } + + public async Task> GetGlobalUserXps(int page, List users) + { + await using var uow = _db.GetDbContext(); + + return await uow.GetTable() + .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) diff --git a/src/EllieBot/Services/GrpcApi/OtherSvc.cs b/src/EllieBot/Services/GrpcApi/OtherSvc.cs index 12143ca..a2b8737 100644 --- a/src/EllieBot/Services/GrpcApi/OtherSvc.cs +++ b/src/EllieBot/Services/GrpcApi/OtherSvc.cs @@ -105,7 +105,7 @@ public sealed class OtherSvc : GrpcOther.GrpcOtherBase, IEService public override async Task 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();