From 95d1fdd32712721ce065780d4be44ea8f6b6db59 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Fri, 26 Sep 2025 00:51:22 -0700 Subject: implement dancearound custom score view --- backend/src/routes/score.ts | 54 ++- .../src/assets/games/dancearound/grade_clear.webp | Bin 0 -> 1162 bytes .../assets/games/dancearound/grade_excellent.webp | Bin 0 -> 1496 bytes .../src/assets/games/dancearound/grade_failed.webp | Bin 0 -> 1096 bytes .../assets/games/dancearound/grade_fullcombo.webp | Bin 0 -> 1364 bytes .../displays/DancearoundScoreDisplay.tsx | 473 +++++++++++++++++++++ .../components/displays/GenericScoreDisplay.tsx | 2 +- frontend/src/pages/Chart.tsx | 16 +- frontend/src/pages/Score.tsx | 12 + 9 files changed, 534 insertions(+), 23 deletions(-) create mode 100644 frontend/src/assets/games/dancearound/grade_clear.webp create mode 100644 frontend/src/assets/games/dancearound/grade_excellent.webp create mode 100644 frontend/src/assets/games/dancearound/grade_failed.webp create mode 100644 frontend/src/assets/games/dancearound/grade_fullcombo.webp create mode 100644 frontend/src/components/displays/DancearoundScoreDisplay.tsx diff --git a/backend/src/routes/score.ts b/backend/src/routes/score.ts index 1f6bdf6..a652b93 100644 --- a/backend/src/routes/score.ts +++ b/backend/src/routes/score.ts @@ -375,13 +375,15 @@ export const handleGetScoresByChartId = async ( ) => { try { const { chartId } = req.params; - const { sortKey, direction, pageNum, pbOnly } = req.query; + const { sortKey, direction, pageNum, pbOnly, game } = req.query; const chartIdString = chartId as string; const pageNumber = parseInt(pageNum as string); const sortKeyString = (sortKey as string) || "timestamp"; const directionString = (direction as string)?.toLowerCase() === "asc" ? "asc" : "desc"; const pbOnlyFlag = pbOnly === "true"; + const gameInternalName = game as string; + if ( directionString && directionString !== "asc" && @@ -400,20 +402,22 @@ export const handleGetScoresByChartId = async ( SELECT DISTINCT ON (s."userId") s.*, u.username FROM "Score" s JOIN "User" u ON s."userId" = u.id - WHERE s."chartId" = $1 + WHERE s."chartId" = $1 AND s."gameInternalName" = $2 ORDER BY s."userId", s."timestamp" ${directionString.toUpperCase()} - OFFSET $2 - LIMIT $3 + OFFSET $3 + LIMIT $4 `, chartIdString, + gameInternalName, (pageNumber - 1) * PAGE_SIZE, PAGE_SIZE, ); } else { // Check if we need grade-based sorting by sampling one score const sampleScore = await prisma.$queryRawUnsafe( - `SELECT s.data->>'${sortKeyString}' as value FROM "Score" s WHERE s."chartId" = $1 AND s.data->>'${sortKeyString}' IS NOT NULL LIMIT 1`, - chartIdString + `SELECT s.data->>'${sortKeyString}' as value FROM "Score" s WHERE s."chartId" = $1 AND s."gameInternalName" = $2 AND s.data->>'${sortKeyString}' IS NOT NULL LIMIT 1`, + chartIdString, + gameInternalName ); const isGradeSort = sampleScore.length > 0 && @@ -426,12 +430,13 @@ export const handleGetScoresByChartId = async ( SELECT DISTINCT ON (s."userId") s.*, u.username FROM "Score" s JOIN "User" u ON s."userId" = u.id - WHERE s."chartId" = $1 + WHERE s."chartId" = $1 AND s."gameInternalName" = $2 ORDER BY s."userId", ${createGradeCaseStatement(sortKeyString, directionString)} - OFFSET $2 - LIMIT $3 + OFFSET $3 + LIMIT $4 `, chartIdString, + gameInternalName, (pageNumber - 1) * PAGE_SIZE, PAGE_SIZE, ); @@ -441,12 +446,13 @@ export const handleGetScoresByChartId = async ( SELECT DISTINCT ON (s."userId") s.*, u.username FROM "Score" s JOIN "User" u ON s."userId" = u.id - WHERE s."chartId" = $1 + WHERE s."chartId" = $1 AND s."gameInternalName" = $2 ORDER BY s."userId", (s.data->>'${sortKeyString}')::numeric ${directionString.toUpperCase()} - OFFSET $2 - LIMIT $3 + OFFSET $3 + LIMIT $4 `, chartIdString, + gameInternalName, (pageNumber - 1) * PAGE_SIZE, PAGE_SIZE, ); @@ -458,15 +464,17 @@ export const handleGetScoresByChartId = async ( ` SELECT COUNT(DISTINCT "userId") as count FROM "Score" - WHERE "chartId" = $1 + WHERE "chartId" = $1 AND "gameInternalName" = $2 `, chartIdString, + gameInternalName, ); totalScores = Number(userCountResult[0]?.count || 0); } else { totalScores = await prisma.score.count({ where: { chartId: chartIdString, + gameInternalName: gameInternalName, }, }); @@ -474,6 +482,7 @@ export const handleGetScoresByChartId = async ( scores = await prisma.score.findMany({ where: { chartId: chartIdString, + gameInternalName: gameInternalName, }, include: { user: { @@ -491,8 +500,9 @@ export const handleGetScoresByChartId = async ( } else { // Check if we need grade-based sorting by sampling one score const sampleScore = await prisma.$queryRawUnsafe( - `SELECT s.data->>'${sortKeyString}' as value FROM "Score" s WHERE s."chartId" = $1 AND s.data->>'${sortKeyString}' IS NOT NULL LIMIT 1`, - chartIdString + `SELECT s.data->>'${sortKeyString}' as value FROM "Score" s WHERE s."chartId" = $1 AND s."gameInternalName" = $2 AND s.data->>'${sortKeyString}' IS NOT NULL LIMIT 1`, + chartIdString, + gameInternalName ); const isGradeSort = sampleScore.length > 0 && @@ -504,12 +514,13 @@ export const handleGetScoresByChartId = async ( ` SELECT s.*, u.username FROM "Score" s JOIN "User" u ON s."userId" = u.id - WHERE s."chartId" = $1 + WHERE s."chartId" = $1 AND s."gameInternalName" = $2 ORDER BY ${createGradeCaseStatement(sortKeyString, directionString)} - OFFSET $2 - LIMIT $3 + OFFSET $3 + LIMIT $4 `, chartIdString, + gameInternalName, (pageNumber - 1) * PAGE_SIZE, PAGE_SIZE, ); @@ -519,12 +530,13 @@ export const handleGetScoresByChartId = async ( ` SELECT s.*, u.username FROM "Score" s JOIN "User" u ON s."userId" = u.id - WHERE s."chartId" = $1 + WHERE s."chartId" = $1 AND s."gameInternalName" = $2 ORDER BY (s.data->>'${sortKeyString}')::numeric ${directionString.toUpperCase()} - OFFSET $2 - LIMIT $3 + OFFSET $3 + LIMIT $4 `, chartIdString, + gameInternalName, (pageNumber - 1) * PAGE_SIZE, PAGE_SIZE, ); diff --git a/frontend/src/assets/games/dancearound/grade_clear.webp b/frontend/src/assets/games/dancearound/grade_clear.webp new file mode 100644 index 0000000..ada2388 Binary files /dev/null and b/frontend/src/assets/games/dancearound/grade_clear.webp differ diff --git a/frontend/src/assets/games/dancearound/grade_excellent.webp b/frontend/src/assets/games/dancearound/grade_excellent.webp new file mode 100644 index 0000000..cae0309 Binary files /dev/null and b/frontend/src/assets/games/dancearound/grade_excellent.webp differ diff --git a/frontend/src/assets/games/dancearound/grade_failed.webp b/frontend/src/assets/games/dancearound/grade_failed.webp new file mode 100644 index 0000000..c10fa89 Binary files /dev/null and b/frontend/src/assets/games/dancearound/grade_failed.webp differ diff --git a/frontend/src/assets/games/dancearound/grade_fullcombo.webp b/frontend/src/assets/games/dancearound/grade_fullcombo.webp new file mode 100644 index 0000000..5822f7a Binary files /dev/null and b/frontend/src/assets/games/dancearound/grade_fullcombo.webp differ diff --git a/frontend/src/components/displays/DancearoundScoreDisplay.tsx b/frontend/src/components/displays/DancearoundScoreDisplay.tsx new file mode 100644 index 0000000..26a54fa --- /dev/null +++ b/frontend/src/components/displays/DancearoundScoreDisplay.tsx @@ -0,0 +1,473 @@ +import React from "react"; +import {Link} from "react-router"; +import { globalSkipKeys } from "../../types/constants"; +import lampExcellent from "../../assets/games/dancearound/grade_excellent.webp"; +import lampFullcombo from "../../assets/games/dancearound/grade_fullcombo.webp"; +import lampClear from "../../assets/games/dancearound/grade_clear.webp"; +import lampFailure from "../../assets/games/dancearound/grade_failed.webp"; +import SHA1 from "crypto-js/sha1"; + +interface Score { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [key: string]: any; + timestamp: string | number; + username?: string; +} + +interface ScoreDisplayProps { + scores: Score[]; + viewMode: "cards" | "table"; + sortField: string; + sortDirection: "asc" | "desc"; + onSort: (field: string) => void; + onDelete?: (scoreId: number) => void; + showUsername?: boolean; + hideTitleArtist?: boolean; +} + +const DancearoundScoreDisplay: React.FC = ({ + scores, + viewMode, + sortField, + sortDirection, + onSort, + onDelete, + showUsername = false, + hideTitleArtist = false, +}) => { + // Key mappings for better display names. Hit or miss + const keyDisplayNames: Record = { + title: "Title", + artist: "Artist", + score: "Score", + difficulty: "Difficulty Level", + lamp: "Rank", + diff_lamp: "Chart Difficulty", + timestamp: "Date", + judgements: "Judgements", + maxCombo: "Max Combo", + perfect: "Perfect", + great: "Great", + good: "Good", + bad: "Bad", + miss: "Miss", + username: "Username", + clear_status: "Status" + }; + + const primaryKeys = ["title", "artist", "song"]; + const mainStatKeys = [ + "score", + "difficulty", + "lamp", + "diff_lamp", + ]; + const expandableKeys = ["judgements", "optional"]; + const localSkipKeys = ["num_players"] + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const formatValue = (value: any, key: string): string => { + if (value === null || value === undefined) return "N/A"; + + // Handle timestamps + if (key === "timestamp" || key === "date") { + const date = new Date(typeof value === "number" ? value : value); + return date.toLocaleDateString(); + } + + if (typeof value === "number") { + if (key === "score" || key === "maxCombo" || key === "combo") { + return value.toLocaleString(); + } + return value.toString(); + } + + return String(value); + }; + + const getDisplayName = (key: string): string => { + return keyDisplayNames[key] || key.charAt(0).toUpperCase() + key.slice(1); + }; + + const renderValue = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + value: any, + key: string, + compact: boolean = false, + ): React.ReactElement => { + if (value === null || value === undefined) + return N/A; + + if (key === "clear_status") { + let lampImg; + if (value === "EXC") { + lampImg = lampExcellent; + } else if (value === "FULL COMBO" ) { + lampImg = lampFullcombo; + } else if (value === "PASSED") { + lampImg = lampClear; + } else if (value === "FAILURE") { + lampImg = lampFailure; + } + return ( +
+ {lampImg ? ( + {`${value} + ) : ( + + {formatValue(value, key)} + + )} +
+ ); + } + + // Handle judgements specially + if (key === "judgements" && typeof value === "object") { + const judgementEntries = Object.entries(value); + + if (compact) { + return ( +
+ {judgementEntries.map(([jKey, jValue]) => ( +
+ + {getDisplayName(jKey)}: + + {formatValue(jValue, jKey)} +
+ ))} +
+ ); + } + return ( +
+ {judgementEntries.map(([jKey, jValue]) => ( + + {getDisplayName(jKey)}:{" "} + {formatValue(jValue, jKey)} + + ))} +
+ ); + } + + if (typeof value === "object" && !Array.isArray(value)) { + return ( +
+ {Object.entries(value).map(([subKey, subValue]) => ( +
+ {getDisplayName(subKey)}: + + {formatValue(subValue, subKey)} + +
+ ))} +
+ ); + } + + + return {formatValue(value, key)}; + }; + + const getScoreEntries = (score: Score) => { + const entries = Object.entries(score).filter( + ([key]) => !globalSkipKeys.includes(key) && !localSkipKeys.includes(key), + ); + + const primary = entries.filter(([key]) => primaryKeys.includes(key)); + const mainStats = entries.filter(([key]) => mainStatKeys.includes(key)); + const expandable = entries.filter(([key]) => expandableKeys.includes(key)); + const others = entries.filter( + ([key]) => + !primaryKeys.includes(key) && + !mainStatKeys.includes(key) && + !expandableKeys.includes(key) && + key !== "timestamp", + ); + + return { + primary, + mainStats, + expandable, + others, + timestamp: score.timestamp, + }; + }; + + const SortIcon = ({ field }: { field: string }) => { + if (sortField !== field) { + return ; + } + return sortDirection === "asc" ? ( + + ) : ( + + ); + }; + + const sortedScores = [...scores].sort((a, b) => { + const aVal = a[sortField]; + const bVal = b[sortField]; + + if (aVal === undefined || aVal === null) return 1; + if (bVal === undefined || bVal === null) return -1; + + let comparison = 0; + + if (typeof aVal === "string" && typeof bVal === "string") { + comparison = aVal.localeCompare(bVal); + } else if (typeof aVal === "number" && typeof bVal === "number") { + comparison = aVal - bVal; + } else if (aVal instanceof Date && bVal instanceof Date) { + comparison = aVal.getTime() - bVal.getTime(); + } else { + comparison = String(aVal).localeCompare(String(bVal)); + } + + return sortDirection === "asc" ? comparison : -comparison; + }); + + // Get all possible keys for table headers + const allKeys = Array.from( + new Set(scores.flatMap((score) => Object.keys(score))), + ).filter((key) => !globalSkipKeys.includes(key) && !localSkipKeys.includes(key)); + + // Prioritize important keys for table display + const tableKeys = [ + ...(hideTitleArtist ? [] : ["title", "song", "artist"]), + ...(showUsername ? ["username"] : []), + "score", + "difficulty", + "lamp", + "diff_lamp", + "clear_status", + "rating", + "percent", + "grade", + "judgements", + "maxCombo", + "combo", + "timestamp", + ].filter((key) => allKeys.includes(key)); + + // Add actions column if delete function is provided + const showActions = onDelete && viewMode === "table"; + + if (scores.length === 0) { + return ( +
+
+ 🎵 +
+

+ No scores found +

+

Import some score data to get started!

+
+ ); + } + + if (viewMode === "cards") { + return ( +
+ {sortedScores.map((score, index) => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { primary, mainStats, expandable, others, timestamp } = + getScoreEntries(score); + const chartIdHash = SHA1(`dancearound${score.title}${score.artist}`).toString(); + return ( +
+ {/* Primary Info */} +
+
+ {!hideTitleArtist && ( + +

+ {score.title || score.song || "Unknown Title"} +

+ {score.artist && ( +

+ {score.artist} +

+ )} + + )} + {showUsername && score.username && ( +

+ by {score.username} +

+ )} +
+
+ + {/* Main Stats */} + {mainStats.length > 0 && ( +
+ {mainStats.slice(0, 5).map(([key, value]) => ( +
+

+ {getDisplayName(key)} +

+

+ {renderValue(value, key)} +

+
+ ))} +
+ )} + + {/* Expandable sections (judgements, optional) */} + {expandable.map(([key, value]) => ( +
+

+ {getDisplayName(key)} +

+ {renderValue(value, key)} +
+ ))} + + {/* Other fields */} + {others.length > 0 && ( +
+
+ {others.map(([key, value]) => ( +
+ + {getDisplayName(key)}: + + + {renderValue(value, key)} + +
+ ))} +
+
+ )} + + {/* Timestamp */} +
+

+ {new Date( + typeof timestamp === "number" ? timestamp : timestamp, + ).toLocaleDateString()}{" "} + •{" "} + {new Date( + typeof timestamp === "number" ? timestamp : timestamp, + ).toLocaleTimeString([], { + hour: "2-digit", + minute: "2-digit", + })} +

+
+
+ ); + })} +
+ ); + } + + // Table + return ( +
+
+ + + + {tableKeys.map((key) => ( + + ))} + {showActions && ( + + )} + + + + {sortedScores.map((score, index) => ( + + {tableKeys.map((key) => ( + + ))} + {showActions && ( + + )} + + ))} + +
+ {key === "judgements" ? ( + {getDisplayName(key)} + ) : ( + + )} + + Actions +
+ {key === "lamp" || key === "diff_lamp" ? ( +
+ + {score[key] || "No Clear"} + +
+ ) : key === "judgements" ? ( +
+ {renderValue(score[key], key, true)} +
+ ) : key === "timestamp" ? ( + + {new Date( + typeof score[key] === "number" + ? score[key] + : score[key], + ).toLocaleDateString()} + + ) : key === "username" ? ( + + {score[key] || "Unknown"} + + ) : ( + + {renderValue(score[key], key)} + + )} +
+ +
+
+
+ ); +}; + +export default DancearoundScoreDisplay; diff --git a/frontend/src/components/displays/GenericScoreDisplay.tsx b/frontend/src/components/displays/GenericScoreDisplay.tsx index 003f022..05d9f1e 100644 --- a/frontend/src/components/displays/GenericScoreDisplay.tsx +++ b/frontend/src/components/displays/GenericScoreDisplay.tsx @@ -285,7 +285,7 @@ const ScoreDisplay: React.FC = ({
{!hideTitleArtist && ( - +

{score.title || score.song || "Unknown Title"}

diff --git a/frontend/src/pages/Chart.tsx b/frontend/src/pages/Chart.tsx index 19f57cc..757cbbb 100644 --- a/frontend/src/pages/Chart.tsx +++ b/frontend/src/pages/Chart.tsx @@ -10,6 +10,7 @@ type SortField = string; type SortDirection = "asc" | "desc"; import { getFilterOptions } from "../types/constants"; +import DancearoundScoreDisplay from "../components/displays/DancearoundScoreDisplay"; const Chart = () => { const { user, isLoading, logout } = useAuth(); @@ -82,6 +83,7 @@ const Chart = () => { url.searchParams.append("sortKey", requestOrder); url.searchParams.append("direction", "asc"); url.searchParams.append("pbOnly", "true"); + url.searchParams.append("game", gameName); const response = await fetch(url.toString(), {credentials: 'include'}); if (!response.ok) throw new Error("Failed to fetch scores"); @@ -97,7 +99,7 @@ const Chart = () => { setLoading(false); } }, - [user, requestOrder, chartIdHash], + [user, requestOrder, chartIdHash, gameName], ); useEffect(() => { @@ -184,6 +186,18 @@ const Chart = () => { hideTitleArtist={true} /> ); + case "dancearound": + return ( + + ); default: return ( { onDelete={handleDeleteScore} /> ); + case "dancearound": + return ( + + ); default: return (