From d428eddec6fc7a2a39abfcba8d9215939642479c Mon Sep 17 00:00:00 2001 From: Pfych Date: Sat, 25 Sep 2021 15:20:51 +1000 Subject: [PATCH] Added top score details --- bot/src/chartSearch/buildChartEmbed.ts | 74 +++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/bot/src/chartSearch/buildChartEmbed.ts b/bot/src/chartSearch/buildChartEmbed.ts index 376018820..31461b4a4 100644 --- a/bot/src/chartSearch/buildChartEmbed.ts +++ b/bot/src/chartSearch/buildChartEmbed.ts @@ -1,14 +1,20 @@ import { InteractionReplyOptions, MessageActionRow, MessageEmbed, MessagePayload, MessageSelectMenu } from "discord.js"; -import { Game, Playtypes } from "tachi-common"; +import { Game, IDStrings, Playtypes, PublicUserDocument, UGSRatingsLookup } from "tachi-common"; +import { PBScoreDocument } from "tachi-common/js/types"; import { LoggerLayers } from "../config"; import { validSelectCustomIdPrefaces } from "../interactionHandlers/selectMenu/handleIsSelectMenu"; import { TachiServerV1Get } from "../utils/fetch-tachi"; import { createLayeredLogger } from "../utils/logger"; -import { getGameImage } from "../utils/utils"; +import { formatGameScoreRating, getGameImage } from "../utils/utils"; import { getDetailedSongData, SongSearchResult } from "./chartSearch"; const logger = createLayeredLogger(LoggerLayers.buildChartEmbed); +export interface PBResponse { + users: PublicUserDocument[]; + pbs: PBScoreDocument[]; +} + export const buildSongSelect = (songs: SongSearchResult[], playtype: Playtypes[T], game: Game) => { return new MessageActionRow().addComponents( new MessageSelectMenu() @@ -25,7 +31,42 @@ export const buildSongSelect = (songs: SongSearchResult[], playt ); }; -export const buildChartEmbed = async (args: { +interface SimplePBDocument + extends Pick, + Pick, + Partial {} + +export const getPBForChart = async ( + chartId: string, + playtype: Playtypes[T], + game: Game +): Promise => { + try { + const data = await TachiServerV1Get(`/games/${game}/${playtype}/charts/${chartId}/pbs`); + if (data.success) { + const pbs = data.body.pbs; + const users = data.body.users; + + const topPb = pbs[0]; + const topUser = users.find((user) => user.id === topPb.userID); + + return { + ...topUser, + scoreData: topPb.scoreData, + calculatedData: topPb.calculatedData + }; + } else { + logger.error(data.description); + throw new Error(data.description); + } + } catch (e) { + logger.info(e); + + throw new Error("Unable to fetch PBs for chart"); + } +}; + +export const buildChartEmbed = async (args: { searchResults?: SongSearchResult[]; songId?: string; playtype: Playtypes[T]; @@ -46,9 +87,30 @@ export const buildChartEmbed = async (args: { const sortedCharts = details.charts.sort((a, b) => a.levelNum - b.levelNum); - sortedCharts.forEach((chart) => { - embed.addField(`${chart.difficulty} (${chart.level})`, "Parse relevant data here", true); - }); + for (const chart of sortedCharts) { + try { + const PB = await getPBForChart(chart.chartID, chart.playtype, game); + embed.addField( + `${chart.difficulty} (${chart.level})`, + `Server Top: **[${PB.username}](https://kamaitachi.xyz/dashboard/profiles/${ + PB.id + }/games/${game}?playtype=${playtype})**\n${PB.scoreData.percent.toFixed(2)}% [${ + PB.scoreData.score + }]\n${PB.scoreData.grade} [${PB.scoreData.lamp}]\n${Object.keys(PB.calculatedData) + .map((item) => { + return `${item}: ${formatGameScoreRating( + { game, playtype }, + item, + PB.calculatedData[item as never] || 0 + )}`; + }) + .join("\n")}`, + false + ); + } catch { + embed.addField(`${chart.difficulty} (${chart.level})`, "No Scores Available!"); + } + } return { embeds: [embed] }; } else {