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 NodeCache from "node-cache"; import { getDifficultyFromScoreSaberDifficulty } from "@ssr/common/utils/scoresaber-utils"; 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's Avatar

{player.name}

{formatPp(player.pp)}pp

#{formatNumberWithCommas(player.rank)}

{/* eslint-disable-next-line @next/next/no-img-element */} Player's Country

#{formatNumberWithCommas(player.countryRank)}

), { width: 1200, height: 630, emoji: "twemoji", } ); imageCache.set(cacheKey, imageResponse); return imageResponse; } /** * Generates the OpenGraph image for the player * * @param id the player's id */ public static async generateLeaderboardImage(id: string) { const cacheKey = `leaderboard-${id}`; if (imageCache.has(cacheKey)) { return imageCache.get(cacheKey) as ImageResponse; } const leaderboard = await scoresaberService.lookupLeaderboard(id); if (leaderboard == undefined) { return undefined; } const ranked = leaderboard.stars > 0; const imageResponse = new ImageResponse( (
Player's Avatar

{leaderboard.songName} {leaderboard.songSubName}

{ranked && (

{leaderboard.stars}

)}

{getDifficultyFromScoreSaberDifficulty(leaderboard.difficulty.difficulty)}

Mapped by {leaderboard.levelAuthorName}

), { width: 1200, height: 630, emoji: "twemoji", } ); imageCache.set(cacheKey, imageResponse); return imageResponse; } }