import { ImageResponse } from "@vercel/og"; import { scoresaberService } from "@ssr/common/service/impl/scoresaber"; import React from "react"; import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils"; import { getDifficultyFromScoreSaberDifficulty } from "website/src/common/scoresaber-utils"; import NodeCache from "node-cache"; const imageCache = new NodeCache({ stdTTL: 60 * 60, // 1 hour checkperiod: 120, }); export class ImageService { /** * Generates the OpenGraph image for the player * * @param id the player's id */ public static async generatePlayerImage(id: string) { const cacheKey = `player-${id}`; if (imageCache.has(cacheKey)) { return imageCache.get(cacheKey) as ImageResponse; } const player = await scoresaberService.lookupPlayer(id); if (player == undefined) { return undefined; } const imageResponse = new ImageResponse( (
{player.name}
{formatPp(player.pp)}pp
#{formatNumberWithCommas(player.rank)}
#{formatNumberWithCommas(player.countryRank)}
{leaderboard.songName} {leaderboard.songSubName}
{leaderboard.stars}
{getDifficultyFromScoreSaberDifficulty(leaderboard.difficulty.difficulty)}
Mapped by {leaderboard.levelAuthorName}