diff --git a/src/EllieBot/Db/Extensions/UserXpExtensions.cs b/src/EllieBot/Db/Extensions/UserXpExtensions.cs index ab3e8bb..97e3e0a 100644 --- a/src/EllieBot/Db/Extensions/UserXpExtensions.cs +++ b/src/EllieBot/Db/Extensions/UserXpExtensions.cs @@ -26,17 +26,6 @@ public static class UserXpExtensions return usr; } - public static async Task> GetUsersFor( - this DbSet xps, - ulong guildId, - int page) - => await xps.ToLinqToDBTable() - .Where(x => x.GuildId == guildId) - .OrderByDescending(x => x.Xp + x.AwardedXp) - .Skip(page * 9) - .Take(9) - .ToArrayAsyncLinqToDB(); - public static async Task> GetTopUserXps(this DbSet xps, ulong guildId, int count) => await xps.ToLinqToDBTable() .Where(x => x.GuildId == guildId) diff --git a/src/EllieBot/Modules/Xp/Xp.cs b/src/EllieBot/Modules/Xp/Xp.cs index e961d87..2ef0505 100644 --- a/src/EllieBot/Modules/Xp/Xp.cs +++ b/src/EllieBot/Modules/Xp/Xp.cs @@ -183,27 +183,26 @@ public partial class Xp : EllieModule await ctx.Channel.TriggerTypingAsync(); - var socketGuild = (SocketGuild)ctx.Guild; - var allCleanUsers = new List(); - if (opts.Clean) + + async Task> GetPageItems(int curPage) { - await ctx.Channel.TriggerTypingAsync(); - await _tracker.EnsureUsersDownloadedAsync(ctx.Guild); + var socketGuild = (SocketGuild)ctx.Guild; + if (opts.Clean) + { + await ctx.Channel.TriggerTypingAsync(); + await _tracker.EnsureUsersDownloadedAsync(ctx.Guild); - allCleanUsers = (await _service.GetTopUserXps(ctx.Guild.Id, 1000)) - .Where(user => socketGuild.GetUser(user.UserId) is not null) - .ToList(); + return await _service.GetTopUserXps(ctx.Guild.Id, + socketGuild.Users.Select(x => x.Id).ToList(), + curPage); + } + + return await _service.GetUserXps(ctx.Guild.Id, curPage); } - var res = opts.Clean - ? Response() + await Response() .Paginated() - .Items(allCleanUsers) - : Response() - .Paginated() - .PageItems((curPage) => _service.GetUserXps(ctx.Guild.Id, curPage)); - - await res + .PageItems(GetPageItems) .PageSize(9) .CurrentPage(page) .Page((users, curPage) => diff --git a/src/EllieBot/Modules/Xp/XpService.cs b/src/EllieBot/Modules/Xp/XpService.cs index b4cc0b5..37bb31f 100644 --- a/src/EllieBot/Modules/Xp/XpService.cs +++ b/src/EllieBot/Modules/Xp/XpService.cs @@ -12,6 +12,7 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using System.Threading.Channels; using LinqToDB.EntityFrameworkCore; +using LinqToDB.Tools; using EllieBot.Modules.Patronage; using Color = SixLabors.ImageSharp.Color; using Exception = System.Exception; @@ -191,9 +192,9 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand var items = await ctx.Set() .Where(x => group.Contains(x.UserId)) .UpdateWithOutputAsync(old => new() - { - TotalXp = old.TotalXp + group.Key - }, + { + TotalXp = old.TotalXp + group.Key + }, (_, n) => n); await ctx.Set() @@ -216,9 +217,9 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand .Where(x => x.GuildId == guildId) .Where(x => group.Contains(x.UserId)) .UpdateWithOutputAsync(old => new() - { - Xp = old.Xp + group.Key - }, + { + Xp = old.Xp + group.Key + }, (_, n) => n); gxps.AddRange(items); @@ -230,14 +231,14 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand .Set() .ToLinqToDBTable() .InsertOrUpdateAsync(() => new UserXpStats() - { - UserId = userId, - GuildId = guildId, - Xp = group.Key, - DateAdded = DateTime.UtcNow, - AwardedXp = 0, - NotifyOnLevelUp = XpNotificationLocation.None - }, + { + UserId = userId, + GuildId = guildId, + Xp = group.Key, + DateAdded = DateTime.UtcNow, + AwardedXp = 0, + NotifyOnLevelUp = XpNotificationLocation.None + }, _ => new() { }, @@ -566,13 +567,24 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand public async Task> GetUserXps(ulong guildId, int page) { await using var uow = _db.GetDbContext(); - return await uow.Set().GetUsersFor(guildId, page); + return await uow + .UserXpStats + .Where(x => x.GuildId == guildId) + .OrderByDescending(x => x.Xp + x.AwardedXp) + .Skip(page * 9) + .Take(9) + .ToArrayAsyncLinqToDB(); } - public async Task> GetTopUserXps(ulong guildId, int count) + public async Task> GetTopUserXps(ulong guildId, List users, int curPage) { await using var uow = _db.GetDbContext(); - return await uow.Set().GetTopUserXps(guildId, count); + return await uow.Set() + .Where(x => x.GuildId == guildId && x.UserId.In(users)) + .OrderByDescending(x => x.Xp + x.AwardedXp) + .Skip(curPage * 9) + .Take(9) + .ToArrayAsyncLinqToDB(); } public Task> GetUserXps(int page, int perPage = 9) @@ -1011,12 +1023,12 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand img.Mutate(x => { x.DrawText(new RichTextOptions(usernameFont) - { - HorizontalAlignment = HorizontalAlignment.Left, - VerticalAlignment = VerticalAlignment.Center, - FallbackFontFamilies = _fonts.FallBackFonts, - Origin = new(template.User.Name.Pos.X, template.User.Name.Pos.Y + 8) - }, + { + HorizontalAlignment = HorizontalAlignment.Left, + VerticalAlignment = VerticalAlignment.Center, + FallbackFontFamilies = _fonts.FallBackFonts, + Origin = new(template.User.Name.Pos.X, template.User.Name.Pos.Y + 8) + }, "@" + username, Brushes.Solid(template.User.Name.Color), outlinePen); @@ -1032,12 +1044,12 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand var clubFont = _fonts.NotoSans.CreateFont(template.Club.Name.FontSize, FontStyle.Regular); img.Mutate(x => x.DrawText(new RichTextOptions(clubFont) - { - HorizontalAlignment = HorizontalAlignment.Right, - VerticalAlignment = VerticalAlignment.Top, - FallbackFontFamilies = _fonts.FallBackFonts, - Origin = new(template.Club.Name.Pos.X + 50, template.Club.Name.Pos.Y - 8) - }, + { + HorizontalAlignment = HorizontalAlignment.Right, + VerticalAlignment = VerticalAlignment.Top, + FallbackFontFamilies = _fonts.FallBackFonts, + Origin = new(template.Club.Name.Pos.X + 50, template.Club.Name.Pos.Y - 8) + }, clubName, Brushes.Solid(template.Club.Name.Color), outlinePen)); @@ -1249,9 +1261,9 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (template.Club.Icon.Show) await DrawClubImage(img, stats); -// #if GLOBAL_ELLIE + // #if GLOBAL_ELLIE await DrawFrame(img, stats.User.UserId); -// #endif + // #endif var outputSize = template.OutputSize; if (outputSize.X != img.Width || outputSize.Y != img.Height) @@ -1309,7 +1321,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (frame is not null) img.Mutate(x => x.DrawImage(frame, new Point(0, 0), new GraphicsOptions())); } -// #endif + // #endif private void DrawXpBar(float percent, XpBar info, Image img) { diff --git a/src/EllieBot/data/marmalades/marmalade.yml b/src/EllieBot/data/marmalades/marmalade.yml index 5a807b1..fe3258f 100644 --- a/src/EllieBot/data/marmalades/marmalade.yml +++ b/src/EllieBot/data/marmalades/marmalade.yml @@ -1,5 +1,4 @@ # DO NOT CHANGE version: 1 # List of marmalades automatically loaded at startup -loaded: - - ngrpc +loaded: []