From d56a85c3423598f57be6a2519c9d3e41d5f0fe9d Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 15 Oct 2024 19:31:50 +0100 Subject: [PATCH] cleanup --- .../src/controller/image.controller.ts | 19 ++++++ projects/backend/src/index.ts | 3 +- .../backend/src/service/image.service.tsx | 64 +++++++++++++++++++ .../{player.service.tsx => player.service.ts} | 58 ----------------- .../src/app/(pages)/player/[...slug]/page.tsx | 2 +- 5 files changed, 86 insertions(+), 60 deletions(-) create mode 100644 projects/backend/src/controller/image.controller.ts create mode 100644 projects/backend/src/service/image.service.tsx rename projects/backend/src/service/{player.service.tsx => player.service.ts} (68%) diff --git a/projects/backend/src/controller/image.controller.ts b/projects/backend/src/controller/image.controller.ts new file mode 100644 index 0000000..79b0c30 --- /dev/null +++ b/projects/backend/src/controller/image.controller.ts @@ -0,0 +1,19 @@ +import { Controller, Get } from "elysia-decorators"; +import { PlayerService } from "../service/player.service"; +import { t } from "elysia"; +import { PlayerHistory } from "@ssr/common/types/player/player-history"; +import { PlayerTrackedSince } from "@ssr/common/types/player/player-tracked-since"; +import { ImageService } from "../service/image.service"; + +@Controller("/image") +export default class ImageController { + @Get("/player/:id", { + config: {}, + params: t.Object({ + id: t.String({ required: true }), + }), + }) + public async getOpenGraphImage({ params: { id } }: { params: { id: string } }) { + return await ImageService.generateOpenGraphImage(id); + } +} diff --git a/projects/backend/src/index.ts b/projects/backend/src/index.ts index c02cc09..503199d 100644 --- a/projects/backend/src/index.ts +++ b/projects/backend/src/index.ts @@ -19,6 +19,7 @@ import { PlayerDocument, PlayerModel } from "./model/player"; import { scoresaberService } from "@ssr/common/service/impl/scoresaber"; import { delay } from "@ssr/common/utils/utils"; import { connectScoreSaberWebSocket } from "@ssr/common/websocket/scoresaber-websocket"; +import ImageController from "./controller/image.controller"; // Load .env file dotenv.config({ @@ -151,7 +152,7 @@ app.use( */ app.use( decorators({ - controllers: [AppController, PlayerController], + controllers: [AppController, PlayerController, ImageController], }) ); diff --git a/projects/backend/src/service/image.service.tsx b/projects/backend/src/service/image.service.tsx new file mode 100644 index 0000000..7d9daa3 --- /dev/null +++ b/projects/backend/src/service/image.service.tsx @@ -0,0 +1,64 @@ +import { ImageResponse } from "@vercel/og"; +import { scoresaberService } from "@ssr/common/service/impl/scoresaber"; +import { formatNumberWithCommas, formatPp } from "website/src/common/number-utils"; +import React from "react"; + +export class ImageService { + /** + * Generates the OpenGraph image for the player + * + * @param id the player's id + */ + public static async generateOpenGraphImage(id: string) { + const player = await scoresaberService.lookupPlayer(id); + if (player == undefined) { + return undefined; + } + return 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", + } + ); + } +} diff --git a/projects/backend/src/service/player.service.tsx b/projects/backend/src/service/player.service.ts similarity index 68% rename from projects/backend/src/service/player.service.tsx rename to projects/backend/src/service/player.service.ts index 244b86d..ccd39af 100644 --- a/projects/backend/src/service/player.service.tsx +++ b/projects/backend/src/service/player.service.ts @@ -159,62 +159,4 @@ export class PlayerService { `Updated scores set statistic for "${playerName}"(${playerId}), scores today: ${scores.rankedScores} ranked, ${scores.unrankedScores} unranked` ); } - - /** - * Generates the OpenGraph image for the player - * - * @param id the player's id - */ - public static async generateOpenGraphImage(id: string) { - const player = await scoresaberService.lookupPlayer(id); - if (player == undefined) { - return undefined; - } - return 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", - } - ); - } } diff --git a/projects/website/src/app/(pages)/player/[...slug]/page.tsx b/projects/website/src/app/(pages)/player/[...slug]/page.tsx index 2d5c015..10de6af 100644 --- a/projects/website/src/app/(pages)/player/[...slug]/page.tsx +++ b/projects/website/src/app/(pages)/player/[...slug]/page.tsx @@ -97,7 +97,7 @@ export async function generateMetadata(props: Props): Promise { title: `ScoreSaber Reloaded - ${player.name}`, images: [ { - url: `${config.siteApi}/player/og/${player.id}`, + url: `${config.siteApi}/image/player/${player.id}`, }, ], },