Skip to content

Instantly share code, notes, and snippets.

@Hona
Created June 14, 2024 06:01
Show Gist options
  • Select an option

  • Save Hona/607688b6339423109b916269cb2f4ec9 to your computer and use it in GitHub Desktop.

Select an option

Save Hona/607688b6339423109b916269cb2f4ec9 to your computer and use it in GitHub Desktop.
Tempus Author Tier List Generator
using System.Diagnostics;
using Microsoft.Win32;
using SixLabors.Fonts;
using TempusApi;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
var tempus = new TempusClient(new HttpClient());
var mapList = await tempus.GetDetailedMapListAsync();
var uniqueAuthors = mapList
.Where(x => x.Authors.Count is 1)
.SelectMany(map => map.Authors)
.GroupBy(x => x.Id)
.OrderByDescending(x => x.Count())
.Select(x => (Author: x.First(), Total: x.Count()))
.ToList();
Console.WriteLine($"Unique authors: {uniqueAuthors.Count}");
Console.WriteLine($"Total maps: {mapList.Count}");
Console.WriteLine($"Total non-collab maps: {mapList.Count(x => x.Authors.Count is 1)}");
foreach (var (author, total) in uniqueAuthors)
{
Console.WriteLine($"{author.Name} ({author.Id}): {total}");
}
// Generate the mapper name as an image for Tierlistmaker
var width = 1600; // or any width with a 16:9 ratio
var height = 900; // or any height with a 16:9 ratio
var backgroundColor = Color.ParseHex("#192E39");
var textColor = Color.ParseHex("#06D6A0");
// Load the font
var jetBrainsMonoPath = GetFontFilePath("JetBrains Mono");
var fontCollection = new FontCollection();
var fontFamily = fontCollection.Add(jetBrainsMonoPath);
foreach (var (author, total) in uniqueAuthors)
{
// Create and configure the image
using (var image = new Image<Rgba32>(width, height))
{
image.Mutate(ctx =>
{
// Set the background color
ctx.Fill(backgroundColor);
// Start with a large font size and decrease until the text fits
int fontSize = 1000; // Start with a large font size, e.g., 240
Font font;
FontRectangle size;
do
{
font = fontFamily.CreateFont(fontSize);
// Measure how much space the text would occupy
size = TextMeasurer.MeasureSize(author.Name, new TextOptions(font));
fontSize -= 5; // Decrease font size
}
while (size.Width > width * 0.8f || size.Height > height); // Adjust width factor as needed
// Define the text options
var textOptions = new RichTextOptions(font)
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
WrappingLength = width * 0.8f,
KerningMode = KerningMode.Standard,
Origin = new PointF(width / 2, height / 2) // Center the text
};
// Draw the text
ctx.DrawText(textOptions, author.Name, textColor);
});
// Save the image to a file
var outputPath = $"images/{author.Id}.png";
image.Save(outputPath);
}
}
static string GetFontFilePath(string fontName)
{
const string fontsRegistryPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts";
using (var fontsKey = Registry.LocalMachine.OpenSubKey(fontsRegistryPath))
{
if (fontsKey != null)
{
foreach (var valueName in fontsKey.GetValueNames())
{
if (valueName != null && valueName.ToLowerInvariant().Contains(fontName.ToLowerInvariant()))
{
var fontPath = fontsKey.GetValue(valueName) as string;
if (fontPath != null)
{
if (!Path.IsPathRooted(fontPath))
{
// If the path is not absolute, it's relative to the Windows\Fonts folder
var windowsFontsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts");
fontPath = Path.Combine(windowsFontsPath, fontPath);
}
return fontPath;
}
}
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment