unused field

This commit is contained in:
Toastie 2025-02-08 16:35:53 +13:00
parent 06970eb9d3
commit 9f660431c2
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7

View file

@ -39,7 +39,6 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
private readonly ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> _excludedChannels = new(); private readonly ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> _excludedChannels = new();
private readonly ConcurrentHashSet<ulong> _excludedServers; private readonly ConcurrentHashSet<ulong> _excludedServers;
private XpTemplate _template = new();
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
private readonly IPatronageService _ps; private readonly IPatronageService _ps;
@ -48,6 +47,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
private readonly INotifySubscriber _notifySub; private readonly INotifySubscriber _notifySub;
private readonly IMemoryCache _memCache; private readonly IMemoryCache _memCache;
private readonly ShardData _shardData; private readonly ShardData _shardData;
private readonly XpTemplateService _templateService;
private readonly QueueRunner _levelUpQueue = new QueueRunner(0, 100); private readonly QueueRunner _levelUpQueue = new QueueRunner(0, 100);
@ -65,7 +65,8 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
IPatronageService ps, IPatronageService ps,
INotifySubscriber notifySub, INotifySubscriber notifySub,
IMemoryCache memCache, IMemoryCache memCache,
ShardData shardData) ShardData shardData,
XpTemplateService templateService)
{ {
_db = db; _db = db;
_images = images; _images = images;
@ -78,6 +79,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
_notifySub = notifySub; _notifySub = notifySub;
_memCache = memCache; _memCache = memCache;
_shardData = shardData; _shardData = shardData;
_templateService = templateService;
_excludedServers = []; _excludedServers = [];
_excludedChannels = new(); _excludedChannels = new();
_client = client; _client = client;
@ -755,11 +757,10 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
} }
private int _lastKnownTemplateHashCode;
public Task<(Stream Image, IImageFormat Format)> GenerateXpImageAsync(FullUserStats stats) public Task<(Stream Image, IImageFormat Format)> GenerateXpImageAsync(FullUserStats stats)
=> Task.Run(async () => => Task.Run(async () =>
{ {
var template = _templateService.GetTemplate();
var bgBytes = await GetXpBackgroundAsync(stats.User.UserId); var bgBytes = await GetXpBackgroundAsync(stats.User.UserId);
if (bgBytes is null) if (bgBytes is null)
@ -782,9 +783,9 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
using (var tempDraw = Image.Load<Rgba32>(avatarData)) using (var tempDraw = Image.Load<Rgba32>(avatarData))
{ {
tempDraw.Mutate(x => x tempDraw.Mutate(x => x
.Resize(_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, .ApplyRoundedCorners(Math.Max(template.User.Icon.Size.X,
_template.User.Icon.Size.Y) template.User.Icon.Size.Y)
/ 2.0f)); / 2.0f));
await using (var stream = await tempDraw.ToStreamAsync()) await using (var stream = await tempDraw.ToStreamAsync())
{ {
@ -801,16 +802,16 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
using var img = Image.Load<Rgba32>(bgBytes); 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 username = stats.User.ToString();
var usernameFont = _fonts.NotoSans.CreateFont(fontSize, FontStyle.Bold); var usernameFont = _fonts.NotoSans.CreateFont(fontSize, FontStyle.Bold);
var size = TextMeasurer.MeasureSize($"@{username}", new(usernameFont)); var size = TextMeasurer.MeasureSize($"@{username}", new(usernameFont));
var scale = 400f / size.Width; var scale = 400f / size.Width;
if (scale < 1) 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 => img.Mutate(x =>
{ {
@ -819,30 +820,30 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
HorizontalAlignment = HorizontalAlignment.Left, HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center, VerticalAlignment = VerticalAlignment.Center,
FallbackFontFamilies = _fonts.FallBackFonts, 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, "@" + username,
Brushes.Solid(_template.User.Name.Color), Brushes.Solid(template.User.Name.Color),
outlinePen); outlinePen);
//club name //club name
if (_template.Club.Name.Show) if (template.Club.Name.Show)
{ {
var clubName = stats.User.Club?.ToString() ?? "-"; 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) x.DrawText(new RichTextOptions(clubFont)
{ {
HorizontalAlignment = HorizontalAlignment.Right, HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top, VerticalAlignment = VerticalAlignment.Top,
FallbackFontFamilies = _fonts.FallBackFonts, 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, clubName,
Brushes.Solid(_template.Club.Name.Color), Brushes.Solid(template.Club.Name.Color),
outlinePen); 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 // up to 83 width
var globalLevelFont = GetTruncatedFont( var globalLevelFont = GetTruncatedFont(
_fonts.NotoSans, _fonts.NotoSans,
_template.User.GlobalLevel.FontSize, template.User.GlobalLevel.FontSize,
FontStyle.Bold, FontStyle.Bold,
stats.Global.Level.ToString(), stats.Global.Level.ToString(),
75); 75);
@ -877,15 +878,15 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
x.DrawText(stats.Global.Level.ToString(), x.DrawText(stats.Global.Level.ToString(),
globalLevelFont, globalLevelFont,
_template.User.GlobalLevel.Color, template.User.GlobalLevel.Color,
new(_template.User.GlobalLevel.Pos.X, _template.User.GlobalLevel.Pos.Y)); //level 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( var guildLevelFont = GetTruncatedFont(
_fonts.NotoSans, _fonts.NotoSans,
_template.User.GuildLevel.FontSize, template.User.GuildLevel.FontSize,
FontStyle.Bold, FontStyle.Bold,
stats.Guild.Level.ToString(), stats.Guild.Level.ToString(),
75); 75);
@ -893,8 +894,8 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
x.DrawText(stats.Guild.Level.ToString(), x.DrawText(stats.Guild.Level.ToString(),
guildLevelFont, guildLevelFont,
_template.User.GuildLevel.Color, template.User.GuildLevel.Color,
new(_template.User.GuildLevel.Pos.X, _template.User.GuildLevel.Pos.Y)); 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; var guild = stats.Guild;
//xp bar //xp bar
if (_template.User.Xp.Bar.Show) if (template.User.Xp.Bar.Show)
{ {
var xpPercent = global.LevelXp / (float)global.RequiredXp; 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; 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( x.DrawText(
new RichTextOptions(_fonts.NotoSans.CreateFont(_template.User.Xp.Global.FontSize, new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize,
FontStyle.Bold)) FontStyle.Bold))
{ {
HorizontalAlignment = HorizontalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.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}", $"{global.LevelXp}/{global.RequiredXp}",
Brushes.Solid(_template.User.Xp.Global.Color), Brushes.Solid(template.User.Xp.Global.Color),
outlinePen); outlinePen);
} }
if (_template.User.Xp.Guild.Show) if (template.User.Xp.Guild.Show)
{ {
x.DrawText( x.DrawText(
new RichTextOptions(_fonts.NotoSans.CreateFont(_template.User.Xp.Guild.FontSize, new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize,
FontStyle.Bold)) FontStyle.Bold))
{ {
HorizontalAlignment = HorizontalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.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}", $"{guild.LevelXp}/{guild.RequiredXp}",
Brushes.Solid(_template.User.Xp.Guild.Color), Brushes.Solid(template.User.Xp.Guild.Color),
outlinePen); outlinePen);
} }
var rankPen = new SolidPen(Color.White, 1); var rankPen = new SolidPen(Color.White, 1);
//ranking //ranking
if (_template.User.GlobalRank.Show) if (template.User.GlobalRank.Show)
{ {
var globalRankStr = stats.GlobalRanking.ToString(); var globalRankStr = stats.GlobalRanking.ToString();
var globalRankFont = GetTruncatedFont( var globalRankFont = GetTruncatedFont(
_fonts.UniSans, _fonts.UniSans,
_template.User.GlobalRank.FontSize, template.User.GlobalRank.FontSize,
FontStyle.Bold, FontStyle.Bold,
globalRankStr, globalRankStr,
68); 68);
@ -956,21 +957,21 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
x.DrawText( x.DrawText(
new RichTextOptions(globalRankFont) 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, globalRankStr,
Brushes.Solid(_template.User.GlobalRank.Color), Brushes.Solid(template.User.GlobalRank.Color),
rankPen rankPen
); );
} }
if (_template.User.GuildRank.Show) if (template.User.GuildRank.Show)
{ {
var guildRankStr = stats.GuildRanking.ToString(); var guildRankStr = stats.GuildRanking.ToString();
var guildRankFont = GetTruncatedFont( var guildRankFont = GetTruncatedFont(
_fonts.UniSans, _fonts.UniSans,
_template.User.GuildRank.FontSize, template.User.GuildRank.FontSize,
FontStyle.Bold, FontStyle.Bold,
guildRankStr, guildRankStr,
43); 43);
@ -978,25 +979,25 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
x.DrawText( x.DrawText(
new RichTextOptions(guildRankFont) 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, guildRankStr,
Brushes.Solid(_template.User.GuildRank.Color), Brushes.Solid(template.User.GuildRank.Color),
rankPen rankPen
); );
} }
if (_template.User.Icon.Show) if (template.User.Icon.Show)
{ {
try try
{ {
using var toDraw = Image.Load(avatarImageData); 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 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, 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); 1);
} }
catch (Exception ex) catch (Exception ex)
@ -1008,14 +1009,14 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
} }
//club image //club image
if (_template.Club.Icon.Show) if (template.Club.Icon.Show)
await DrawClubImage(img, stats); await DrawClubImage(img, stats);
await DrawFrame(img, stats.User.UserId); await DrawFrame(img, stats.User.UserId);
var outputSize = _template.OutputSize; var outputSize = template.OutputSize;
if (outputSize.X != img.Width || outputSize.Y != img.Height) 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 imageFormat = img.Metadata.DecodedImageFormat;
var output = ((Stream)await img.ToStreamAsync(imageFormat), imageFormat); 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"; private const string XP_TEMPLATE_PATH = "./data/xp_template.json";
@ -1438,8 +1439,4 @@ public sealed class XpTemplateService : IEService, IReadyExecutor
public XpTemplate GetTemplate() public XpTemplate GetTemplate()
=> _template; => _template;
}
public sealed class ReadyXpTempalte(XpTemplate template)
{
} }