Updated image library

This commit is contained in:
Toastie 2024-09-04 22:02:50 +12:00
parent 89ab9a2ceb
commit 742d98a4c1
No known key found for this signature in database
9 changed files with 54 additions and 43 deletions
src/EllieBot/Modules/Gambling/PlantPick

View file

@ -111,13 +111,17 @@ public class PlantPickService : IEService, IExecNoCommand
{
var curImg = await _images.GetCurrencyImageAsync();
if (curImg is null)
return (new MemoryStream(), null);
if (string.IsNullOrWhiteSpace(pass))
{
// determine the extension
using var load = _ = Image.Load(curImg, out var format);
using var load = Image.Load(curImg);
var format = load.Metadata.DecodedImageFormat;
// return the image
return (curImg.ToStream(), format.FileExtensions.FirstOrDefault() ?? "png");
return (curImg.ToStream(), format?.FileExtensions.FirstOrDefault() ?? "png");
}
// get the image stream and extension
@ -134,16 +138,17 @@ public class PlantPickService : IEService, IExecNoCommand
{
// draw lower, it looks better
pass = pass.TrimTo(10, true).ToLowerInvariant();
using var img = Image.Load<Rgba32>(curImg, out var format);
using var img = Image.Load<Rgba32>(curImg);
// choose font size based on the image height, so that it's visible
var font = _fonts.NotoSans.CreateFont(img.Height / 12.0f, FontStyle.Bold);
img.Mutate(x =>
{
// measure the size of the text to be drawing
var size = TextMeasurer.Measure(pass, new TextOptions(font)
{
Origin = new PointF(0, 0)
});
var size = TextMeasurer.MeasureSize(pass,
new TextOptions(font)
{
Origin = new PointF(0, 0)
});
// fill the background with black, add 5 pixels on each side to make it look better
x.FillPolygon(Color.ParseHex("00000080"),
@ -156,6 +161,7 @@ public class PlantPickService : IEService, IExecNoCommand
x.DrawText(pass, font, Color.White, new(0, 0));
});
// return image as a stream for easy sending
var format = img.Metadata.DecodedImageFormat;
return (img.ToStream(format), format.FileExtensions.FirstOrDefault() ?? "png");
}
@ -256,7 +262,8 @@ public class PlantPickService : IEService, IExecNoCommand
pass = pass?.Trim().TrimTo(10, true).ToUpperInvariant();
// gets all plants in this channel with the same password
var entries = uow.Set<PlantedCurrency>().AsQueryable()
var entries = uow.Set<PlantedCurrency>()
.AsQueryable()
.Where(x => x.ChannelId == ch.Id && pass == x.Password)
.ToList();
// sum how much currency that is, and get all of the message ids (so that i can delete them)
@ -368,15 +375,16 @@ public class PlantPickService : IEService, IExecNoCommand
string pass)
{
await using var uow = _db.GetDbContext();
uow.Set<PlantedCurrency>().Add(new()
{
Amount = amount,
GuildId = gid,
ChannelId = cid,
Password = pass,
UserId = uid,
MessageId = mid
});
uow.Set<PlantedCurrency>()
.Add(new()
{
Amount = amount,
GuildId = gid,
ChannelId = cid,
Password = pass,
UserId = uid,
MessageId = mid
});
await uow.SaveChangesAsync();
}
}