2024-10-15 19:31:50 +01:00
|
|
|
import { ImageResponse } from "@vercel/og";
|
|
|
|
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
|
|
|
|
import React from "react";
|
2024-10-15 19:43:23 +01:00
|
|
|
import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils";
|
2024-10-16 02:39:42 +01:00
|
|
|
import { getDifficultyFromScoreSaberDifficulty } from "@ssr/common/utils/scoresaber-utils";
|
2024-10-16 03:03:15 +01:00
|
|
|
import { StarIcon } from "../../components/star-icon";
|
|
|
|
import { GlobeIcon } from "../../components/globe-icon";
|
|
|
|
import NodeCache from "node-cache";
|
|
|
|
import ScoreSaberLeaderboardToken from "@ssr/common/types/token/scoresaber/score-saber-leaderboard-token";
|
2024-10-17 15:30:14 +01:00
|
|
|
import ScoreSaberPlayer, { getScoreSaberPlayerFromToken } from "@ssr/common/player/impl/scoresaber-player";
|
2024-10-16 07:47:52 +01:00
|
|
|
import { Jimp } from "jimp";
|
2024-10-16 07:31:52 +01:00
|
|
|
import { extractColors } from "extract-colors";
|
2024-10-17 07:12:03 +01:00
|
|
|
import { Config } from "@ssr/common/config";
|
2024-10-16 02:31:10 +01:00
|
|
|
|
2024-10-16 06:16:55 +01:00
|
|
|
const cache = new NodeCache({ stdTTL: 60 * 60, checkperiod: 120 });
|
|
|
|
const imageOptions = { width: 1200, height: 630 };
|
2024-10-15 19:31:50 +01:00
|
|
|
|
|
|
|
export class ImageService {
|
2024-10-16 07:31:52 +01:00
|
|
|
/**
|
|
|
|
* Gets the average color of an image
|
|
|
|
*
|
|
|
|
* @param src the image url
|
|
|
|
* @returns the average color
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
public static async getAverageImageColor(src: string): Promise<{ color: string } | undefined> {
|
|
|
|
src = decodeURIComponent(src);
|
|
|
|
|
|
|
|
return await this.fetchWithCache<{ color: string }>(`average_color-${src}`, async () => {
|
|
|
|
try {
|
2024-10-16 07:47:52 +01:00
|
|
|
const image = await Jimp.read(src); // Load image using Jimp
|
|
|
|
const { width, height, data } = image.bitmap; // Access image dimensions and pixel data
|
2024-10-16 07:31:52 +01:00
|
|
|
|
2024-10-16 07:47:52 +01:00
|
|
|
// Convert the Buffer data to Uint8ClampedArray
|
|
|
|
const uint8ClampedArray = new Uint8ClampedArray(data);
|
2024-10-16 07:31:52 +01:00
|
|
|
|
2024-10-16 07:47:52 +01:00
|
|
|
// Extract the colors using extract-colors
|
|
|
|
const colors = await extractColors({ data: uint8ClampedArray, width, height });
|
2024-10-16 07:31:52 +01:00
|
|
|
|
2024-10-16 07:47:52 +01:00
|
|
|
// Return the most dominant color, or fallback if none found
|
|
|
|
if (colors && colors.length > 0) {
|
|
|
|
return { color: colors[2].hex }; // Returning the third most dominant color
|
|
|
|
}
|
2024-10-16 07:31:52 +01:00
|
|
|
|
|
|
|
return {
|
2024-10-16 07:47:52 +01:00
|
|
|
color: "#fff", // Fallback color in case no colors are found
|
2024-10-16 07:31:52 +01:00
|
|
|
};
|
|
|
|
} catch (error) {
|
2024-10-16 07:47:52 +01:00
|
|
|
console.error("Error fetching image or extracting colors:", error);
|
2024-10-16 07:31:52 +01:00
|
|
|
return {
|
2024-10-16 07:47:52 +01:00
|
|
|
color: "#fff", // Fallback color in case of an error
|
2024-10-16 07:31:52 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-16 06:16:55 +01:00
|
|
|
/**
|
|
|
|
* Fetches data with caching.
|
|
|
|
*
|
|
|
|
* @param cacheKey The key used for caching.
|
|
|
|
* @param fetchFn The function to fetch data if it's not in cache.
|
|
|
|
*/
|
|
|
|
private static async fetchWithCache<T>(
|
|
|
|
cacheKey: string,
|
|
|
|
fetchFn: () => Promise<T | undefined>
|
|
|
|
): Promise<T | undefined> {
|
|
|
|
if (cache.has(cacheKey)) {
|
|
|
|
return cache.get<T>(cacheKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await fetchFn();
|
|
|
|
if (data) {
|
|
|
|
cache.set(cacheKey, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-10-16 03:03:15 +01:00
|
|
|
/**
|
|
|
|
* The base of the OpenGraph image
|
|
|
|
*
|
|
|
|
* @param children the content of the image
|
|
|
|
*/
|
|
|
|
public static BaseImage({ children }: { children: React.ReactNode }) {
|
|
|
|
return (
|
|
|
|
<div
|
2024-10-16 06:16:55 +01:00
|
|
|
tw="w-full h-full flex flex-col text-white text-3xl p-3 justify-center items-center relative"
|
2024-10-16 03:03:15 +01:00
|
|
|
style={{
|
|
|
|
backgroundColor: "#0a0a0a",
|
|
|
|
background: "radial-gradient(ellipse 60% 60% at 50% -20%, rgba(120,119,198,0.15), rgba(255,255,255,0))",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-16 06:16:55 +01:00
|
|
|
/**
|
|
|
|
* Renders the change for a stat.
|
|
|
|
*
|
|
|
|
* @param change the amount of change
|
|
|
|
* @param format the function to format the value
|
|
|
|
*/
|
|
|
|
private static renderDailyChange(change: number, format: (value: number) => string = formatNumberWithCommas) {
|
|
|
|
if (change === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<p tw={`text-[23px] pl-1 m-0 ${change > 0 ? "text-green-400" : "text-red-400"}`}>
|
|
|
|
{change > 0 ? "+" : ""}
|
|
|
|
{format(change)}
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-15 19:31:50 +01:00
|
|
|
/**
|
|
|
|
* Generates the OpenGraph image for the player
|
|
|
|
*
|
|
|
|
* @param id the player's id
|
|
|
|
*/
|
2024-10-15 19:43:23 +01:00
|
|
|
public static async generatePlayerImage(id: string) {
|
2024-10-16 06:16:55 +01:00
|
|
|
const player = await this.fetchWithCache<ScoreSaberPlayer>(`player-${id}`, async () => {
|
|
|
|
const token = await scoresaberService.lookupPlayer(id);
|
|
|
|
return token ? await getScoreSaberPlayerFromToken(token, Config.apiUrl) : undefined;
|
|
|
|
});
|
|
|
|
if (!player) {
|
2024-10-15 19:31:50 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
2024-10-16 03:03:15 +01:00
|
|
|
|
2024-10-16 06:16:55 +01:00
|
|
|
const { statisticChange } = player;
|
|
|
|
const { daily } = statisticChange ?? {};
|
|
|
|
const rankChange = daily?.countryRank ?? 0;
|
|
|
|
const countryRankChange = daily?.rank ?? 0;
|
|
|
|
const ppChange = daily?.pp ?? 0;
|
|
|
|
|
2024-10-16 03:03:15 +01:00
|
|
|
return new ImageResponse(
|
2024-10-15 19:31:50 +01:00
|
|
|
(
|
2024-10-16 03:03:15 +01:00
|
|
|
<ImageService.BaseImage>
|
2024-10-16 06:16:55 +01:00
|
|
|
{/* Player Avatar */}
|
|
|
|
<img src={player.avatar} width={256} height={256} alt="Player's Avatar" tw="rounded-full mb-3" />
|
|
|
|
|
|
|
|
{/* Player Stats */}
|
2024-10-15 19:31:50 +01:00
|
|
|
<div tw="flex flex-col pl-3 items-center">
|
2024-10-16 06:16:55 +01:00
|
|
|
{/* Player Name */}
|
2024-10-15 19:31:50 +01:00
|
|
|
<p tw="font-bold text-6xl m-0">{player.name}</p>
|
2024-10-16 06:16:55 +01:00
|
|
|
|
|
|
|
{/* Player PP */}
|
|
|
|
<div tw="flex justify-center items-center text-[33px]">
|
|
|
|
<p tw="text-[#606fff] m-0">{formatPp(player.pp)}pp</p>
|
|
|
|
{this.renderDailyChange(ppChange)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Player Stats */}
|
2024-10-15 19:31:50 +01:00
|
|
|
<div tw="flex">
|
2024-10-16 06:16:55 +01:00
|
|
|
{/* Player Rank */}
|
2024-10-15 19:31:50 +01:00
|
|
|
<div tw="flex px-2 justify-center items-center">
|
2024-10-16 03:03:15 +01:00
|
|
|
<GlobeIcon />
|
2024-10-15 19:31:50 +01:00
|
|
|
<p tw="m-0">#{formatNumberWithCommas(player.rank)}</p>
|
2024-10-16 06:16:55 +01:00
|
|
|
{this.renderDailyChange(rankChange)}
|
2024-10-15 19:31:50 +01:00
|
|
|
</div>
|
2024-10-16 06:16:55 +01:00
|
|
|
|
|
|
|
{/* Player Country Rank */}
|
|
|
|
<div tw="flex px-2 justify-center items-center">
|
2024-10-15 19:31:50 +01:00
|
|
|
<img
|
|
|
|
src={`https://ssr.fascinated.cc/assets/flags/${player.country.toLowerCase()}.png`}
|
|
|
|
height={20}
|
|
|
|
alt="Player's Country"
|
|
|
|
/>
|
|
|
|
<p tw="pl-1 m-0">#{formatNumberWithCommas(player.countryRank)}</p>
|
2024-10-16 06:16:55 +01:00
|
|
|
{this.renderDailyChange(countryRankChange)}
|
2024-10-15 19:31:50 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-10-16 06:16:55 +01:00
|
|
|
|
|
|
|
{/* Joined Date */}
|
|
|
|
<p tw="m-0 text-gray-400 mt-2">
|
|
|
|
Joined ScoreSaber in{" "}
|
|
|
|
{player.joinedDate.toLocaleString("en-US", {
|
|
|
|
timeZone: "Europe/London",
|
|
|
|
month: "long",
|
|
|
|
year: "numeric",
|
|
|
|
})}
|
|
|
|
</p>
|
2024-10-15 19:31:50 +01:00
|
|
|
</div>
|
2024-10-16 03:03:15 +01:00
|
|
|
</ImageService.BaseImage>
|
2024-10-15 19:31:50 +01:00
|
|
|
),
|
2024-10-16 06:16:55 +01:00
|
|
|
imageOptions
|
2024-10-15 19:31:50 +01:00
|
|
|
);
|
|
|
|
}
|
2024-10-16 02:27:59 +01:00
|
|
|
|
|
|
|
/**
|
2024-10-16 06:16:55 +01:00
|
|
|
* Generates the OpenGraph image for the leaderboard
|
2024-10-16 02:27:59 +01:00
|
|
|
*
|
2024-10-16 06:16:55 +01:00
|
|
|
* @param id the leaderboard's id
|
2024-10-16 02:27:59 +01:00
|
|
|
*/
|
|
|
|
public static async generateLeaderboardImage(id: string) {
|
2024-10-16 06:16:55 +01:00
|
|
|
const leaderboard = await this.fetchWithCache<ScoreSaberLeaderboardToken>(`leaderboard-${id}`, () =>
|
|
|
|
scoresaberService.lookupLeaderboard(id)
|
|
|
|
);
|
|
|
|
if (!leaderboard) {
|
2024-10-16 02:27:59 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ranked = leaderboard.stars > 0;
|
2024-10-16 06:16:55 +01:00
|
|
|
|
2024-10-16 03:03:15 +01:00
|
|
|
return new ImageResponse(
|
2024-10-16 02:27:59 +01:00
|
|
|
(
|
2024-10-16 03:03:15 +01:00
|
|
|
<ImageService.BaseImage>
|
2024-10-16 06:16:55 +01:00
|
|
|
{/* Leaderboard Cover Image */}
|
|
|
|
<img src={leaderboard.coverImage} width={256} height={256} alt="Leaderboard Cover" tw="rounded-full mb-3" />
|
|
|
|
|
|
|
|
{/* Leaderboard Name */}
|
2024-10-16 02:27:59 +01:00
|
|
|
<p tw="font-bold text-6xl m-0">
|
|
|
|
{leaderboard.songName} {leaderboard.songSubName}
|
|
|
|
</p>
|
2024-10-16 06:16:55 +01:00
|
|
|
|
2024-10-16 02:27:59 +01:00
|
|
|
<div tw="flex justify-center items-center text-center">
|
2024-10-16 06:16:55 +01:00
|
|
|
{/* Leaderboard Stars */}
|
2024-10-16 02:27:59 +01:00
|
|
|
{ranked && (
|
|
|
|
<div tw="flex justify-center items-center text-4xl">
|
|
|
|
<p tw="font-bold m-0">{leaderboard.stars}</p>
|
2024-10-16 03:03:15 +01:00
|
|
|
<StarIcon />
|
2024-10-16 02:27:59 +01:00
|
|
|
</div>
|
|
|
|
)}
|
2024-10-16 06:16:55 +01:00
|
|
|
|
|
|
|
{/* Leaderboard Difficulty */}
|
2024-10-16 02:27:59 +01:00
|
|
|
<p tw={"font-bold m-0 text-4xl" + (ranked ? " pl-3" : "")}>
|
|
|
|
{getDifficultyFromScoreSaberDifficulty(leaderboard.difficulty.difficulty)}
|
|
|
|
</p>
|
|
|
|
</div>
|
2024-10-16 06:16:55 +01:00
|
|
|
|
|
|
|
{/* Leaderboard Author */}
|
|
|
|
<p tw="font-bold text-2xl text-gray-400 m-0 mt-2">Mapped by {leaderboard.levelAuthorName}</p>
|
2024-10-16 03:03:15 +01:00
|
|
|
</ImageService.BaseImage>
|
2024-10-16 02:27:59 +01:00
|
|
|
),
|
2024-10-16 06:16:55 +01:00
|
|
|
imageOptions
|
2024-10-16 02:27:59 +01:00
|
|
|
);
|
|
|
|
}
|
2024-10-15 19:31:50 +01:00
|
|
|
}
|