From 9f660431c20743a972f07640f1ef4bedf3b80a21 Mon Sep 17 00:00:00 2001 From: Toastie <toastie@toastiet0ast.com> Date: Sat, 8 Feb 2025 16:35:53 +1300 Subject: [PATCH] unused field --- src/EllieBot/Modules/Xp/XpService.cs | 107 +++++++++++++-------------- 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/src/EllieBot/Modules/Xp/XpService.cs b/src/EllieBot/Modules/Xp/XpService.cs index 17072ad..cc5cf84 100644 --- a/src/EllieBot/Modules/Xp/XpService.cs +++ b/src/EllieBot/Modules/Xp/XpService.cs @@ -39,7 +39,6 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand private readonly ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> _excludedChannels = new(); private readonly ConcurrentHashSet<ulong> _excludedServers; - private XpTemplate _template = new(); private readonly DiscordSocketClient _client; private readonly IPatronageService _ps; @@ -48,6 +47,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand private readonly INotifySubscriber _notifySub; private readonly IMemoryCache _memCache; private readonly ShardData _shardData; + private readonly XpTemplateService _templateService; private readonly QueueRunner _levelUpQueue = new QueueRunner(0, 100); @@ -65,7 +65,8 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand IPatronageService ps, INotifySubscriber notifySub, IMemoryCache memCache, - ShardData shardData) + ShardData shardData, + XpTemplateService templateService) { _db = db; _images = images; @@ -78,6 +79,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand _notifySub = notifySub; _memCache = memCache; _shardData = shardData; + _templateService = templateService; _excludedServers = []; _excludedChannels = new(); _client = client; @@ -755,11 +757,10 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } - private int _lastKnownTemplateHashCode; - public Task<(Stream Image, IImageFormat Format)> GenerateXpImageAsync(FullUserStats stats) => Task.Run(async () => { + var template = _templateService.GetTemplate(); var bgBytes = await GetXpBackgroundAsync(stats.User.UserId); if (bgBytes is null) @@ -782,9 +783,9 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand using (var tempDraw = Image.Load<Rgba32>(avatarData)) { tempDraw.Mutate(x => x - .Resize(_template.User.Icon.Size.X, _template.User.Icon.Size.Y) - .ApplyRoundedCorners(Math.Max(_template.User.Icon.Size.X, - _template.User.Icon.Size.Y) + .Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y) + .ApplyRoundedCorners(Math.Max(template.User.Icon.Size.X, + template.User.Icon.Size.Y) / 2.0f)); await using (var stream = await tempDraw.ToStreamAsync()) { @@ -801,16 +802,16 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand using var img = Image.Load<Rgba32>(bgBytes); - if (_template.User.Name.Show) + if (template.User.Name.Show) { - var fontSize = (int)(_template.User.Name.FontSize * 0.9); + var fontSize = (int)(template.User.Name.FontSize * 0.9); var username = stats.User.ToString(); var usernameFont = _fonts.NotoSans.CreateFont(fontSize, FontStyle.Bold); var size = TextMeasurer.MeasureSize($"@{username}", new(usernameFont)); var scale = 400f / size.Width; if (scale < 1) - usernameFont = _fonts.NotoSans.CreateFont(_template.User.Name.FontSize * scale, FontStyle.Bold); + usernameFont = _fonts.NotoSans.CreateFont(template.User.Name.FontSize * scale, FontStyle.Bold); img.Mutate(x => { @@ -819,30 +820,30 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, FallbackFontFamilies = _fonts.FallBackFonts, - Origin = new(_template.User.Name.Pos.X, _template.User.Name.Pos.Y + 8) + Origin = new(template.User.Name.Pos.X, template.User.Name.Pos.Y + 8) }, "@" + username, - Brushes.Solid(_template.User.Name.Color), + Brushes.Solid(template.User.Name.Color), outlinePen); //club name - if (_template.Club.Name.Show) + if (template.Club.Name.Show) { var clubName = stats.User.Club?.ToString() ?? "-"; - var clubFont = _fonts.NotoSans.CreateFont(_template.Club.Name.FontSize, FontStyle.Regular); + var clubFont = _fonts.NotoSans.CreateFont(template.Club.Name.FontSize, FontStyle.Regular); 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) + Origin = new(template.Club.Name.Pos.X + 50, template.Club.Name.Pos.Y - 8) }, clubName, - Brushes.Solid(_template.Club.Name.Color), + Brushes.Solid(template.Club.Name.Color), outlinePen); } @@ -863,13 +864,13 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } - if (_template.User.GlobalLevel.Show) + if (template.User.GlobalLevel.Show) { // up to 83 width var globalLevelFont = GetTruncatedFont( _fonts.NotoSans, - _template.User.GlobalLevel.FontSize, + template.User.GlobalLevel.FontSize, FontStyle.Bold, stats.Global.Level.ToString(), 75); @@ -877,15 +878,15 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand x.DrawText(stats.Global.Level.ToString(), globalLevelFont, - _template.User.GlobalLevel.Color, - new(_template.User.GlobalLevel.Pos.X, _template.User.GlobalLevel.Pos.Y)); //level + template.User.GlobalLevel.Color, + new(template.User.GlobalLevel.Pos.X, template.User.GlobalLevel.Pos.Y)); //level } - if (_template.User.GuildLevel.Show) + if (template.User.GuildLevel.Show) { var guildLevelFont = GetTruncatedFont( _fonts.NotoSans, - _template.User.GuildLevel.FontSize, + template.User.GuildLevel.FontSize, FontStyle.Bold, stats.Guild.Level.ToString(), 75); @@ -893,8 +894,8 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand x.DrawText(stats.Guild.Level.ToString(), guildLevelFont, - _template.User.GuildLevel.Color, - new(_template.User.GuildLevel.Pos.X, _template.User.GuildLevel.Pos.Y)); + template.User.GuildLevel.Color, + new(template.User.GuildLevel.Pos.X, template.User.GuildLevel.Pos.Y)); } @@ -902,53 +903,53 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand var guild = stats.Guild; //xp bar - if (_template.User.Xp.Bar.Show) + if (template.User.Xp.Bar.Show) { var xpPercent = global.LevelXp / (float)global.RequiredXp; - DrawXpBar(xpPercent, _template.User.Xp.Bar.Global, img); + DrawXpBar(xpPercent, template.User.Xp.Bar.Global, img); xpPercent = guild.LevelXp / (float)guild.RequiredXp; - DrawXpBar(xpPercent, _template.User.Xp.Bar.Guild, img); + DrawXpBar(xpPercent, template.User.Xp.Bar.Guild, img); } - if (_template.User.Xp.Global.Show) + if (template.User.Xp.Global.Show) { x.DrawText( - new RichTextOptions(_fonts.NotoSans.CreateFont(_template.User.Xp.Global.FontSize, + new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize, FontStyle.Bold)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, - Origin = new(_template.User.Xp.Global.Pos.X, _template.User.Xp.Global.Pos.Y), + Origin = new(template.User.Xp.Global.Pos.X, template.User.Xp.Global.Pos.Y), }, $"{global.LevelXp}/{global.RequiredXp}", - Brushes.Solid(_template.User.Xp.Global.Color), + Brushes.Solid(template.User.Xp.Global.Color), outlinePen); } - if (_template.User.Xp.Guild.Show) + if (template.User.Xp.Guild.Show) { x.DrawText( - new RichTextOptions(_fonts.NotoSans.CreateFont(_template.User.Xp.Guild.FontSize, + new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize, FontStyle.Bold)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, - Origin = new(_template.User.Xp.Guild.Pos.X, _template.User.Xp.Guild.Pos.Y) + Origin = new(template.User.Xp.Guild.Pos.X, template.User.Xp.Guild.Pos.Y) }, $"{guild.LevelXp}/{guild.RequiredXp}", - Brushes.Solid(_template.User.Xp.Guild.Color), + Brushes.Solid(template.User.Xp.Guild.Color), outlinePen); } var rankPen = new SolidPen(Color.White, 1); //ranking - if (_template.User.GlobalRank.Show) + if (template.User.GlobalRank.Show) { var globalRankStr = stats.GlobalRanking.ToString(); var globalRankFont = GetTruncatedFont( _fonts.UniSans, - _template.User.GlobalRank.FontSize, + template.User.GlobalRank.FontSize, FontStyle.Bold, globalRankStr, 68); @@ -956,21 +957,21 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand x.DrawText( new RichTextOptions(globalRankFont) { - Origin = new(_template.User.GlobalRank.Pos.X, _template.User.GlobalRank.Pos.Y) + Origin = new(template.User.GlobalRank.Pos.X, template.User.GlobalRank.Pos.Y) }, globalRankStr, - Brushes.Solid(_template.User.GlobalRank.Color), + Brushes.Solid(template.User.GlobalRank.Color), rankPen ); } - if (_template.User.GuildRank.Show) + if (template.User.GuildRank.Show) { var guildRankStr = stats.GuildRanking.ToString(); var guildRankFont = GetTruncatedFont( _fonts.UniSans, - _template.User.GuildRank.FontSize, + template.User.GuildRank.FontSize, FontStyle.Bold, guildRankStr, 43); @@ -978,25 +979,25 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand x.DrawText( new RichTextOptions(guildRankFont) { - Origin = new(_template.User.GuildRank.Pos.X, _template.User.GuildRank.Pos.Y) + Origin = new(template.User.GuildRank.Pos.X, template.User.GuildRank.Pos.Y) }, guildRankStr, - Brushes.Solid(_template.User.GuildRank.Color), + Brushes.Solid(template.User.GuildRank.Color), rankPen ); } - if (_template.User.Icon.Show) + if (template.User.Icon.Show) { try { using var toDraw = Image.Load(avatarImageData); - if (toDraw.Size != new Size(_template.User.Icon.Size.X, _template.User.Icon.Size.Y)) + if (toDraw.Size != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y)) toDraw.Mutate(x - => x.Resize(_template.User.Icon.Size.X, _template.User.Icon.Size.Y)); + => x.Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y)); x.DrawImage(toDraw, - new Point(_template.User.Icon.Pos.X, _template.User.Icon.Pos.Y), + new Point(template.User.Icon.Pos.X, template.User.Icon.Pos.Y), 1); } catch (Exception ex) @@ -1008,14 +1009,14 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } //club image - if (_template.Club.Icon.Show) + if (template.Club.Icon.Show) await DrawClubImage(img, stats); await DrawFrame(img, stats.User.UserId); - var outputSize = _template.OutputSize; + var outputSize = template.OutputSize; if (outputSize.X != img.Width || outputSize.Y != img.Height) - img.Mutate(x => x.Resize(_template.OutputSize.X, _template.OutputSize.Y)); + img.Mutate(x => x.Resize(template.OutputSize.X, template.OutputSize.Y)); var imageFormat = img.Metadata.DecodedImageFormat; var output = ((Stream)await img.ToStreamAsync(imageFormat), imageFormat); @@ -1382,7 +1383,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } } -public sealed class XpTemplateService : IEService, IReadyExecutor +public sealed class XpTemplateService : INService, IReadyExecutor { private const string XP_TEMPLATE_PATH = "./data/xp_template.json"; @@ -1438,8 +1439,4 @@ public sealed class XpTemplateService : IEService, IReadyExecutor public XpTemplate GetTemplate() => _template; -} - -public sealed class ReadyXpTempalte(XpTemplate template) -{ } \ No newline at end of file