From bb5506148cf0294512704095f117d2d3b42c2f28 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 30 Sep 2024 12:33:26 +0100 Subject: [PATCH] fix acc colors >= 95 not working --- src/common/song-utils.ts | 52 +++++++++++++++--------- src/components/navbar/profile-button.tsx | 2 +- src/components/score/score-info.tsx | 2 +- src/components/score/score-stats.tsx | 4 +- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/common/song-utils.ts b/src/common/song-utils.ts index 3003d41..8d98922 100644 --- a/src/common/song-utils.ts +++ b/src/common/song-utils.ts @@ -6,13 +6,15 @@ const diffColors: Record = { expertplus: "#8f48db", }; -const badgesDef: { +export type ScoreBadge = { name: string; min: number | null; max: number | null; color: string; -}[] = [ - { name: "SS+", min: 95, max: null, color: diffColors.expertPlus }, +}; + +const scoreBadges: ScoreBadge[] = [ + { name: "SS+", min: 95, max: null, color: diffColors.expertplus }, { name: "SS", min: 90, max: 95, color: diffColors.expert }, { name: "S+", min: 85, max: 90, color: diffColors.hard }, { name: "S", min: 80, max: 85, color: diffColors.normal }, @@ -20,6 +22,33 @@ const badgesDef: { { name: "-", min: null, max: 70, color: "hsl(var(--accent))" }, ]; +/** + * Returns the color based on the accuracy provided. + * + * @param acc - The accuracy for the score + * @returns The corresponding color for the accuracy. + */ +export function getScoreColorFromAccuracy(acc: number): ScoreBadge { + // Check for SS+ first since it has no upper limit + if (acc >= 95) { + return scoreBadges[0]; // SS+ color + } + + // Iterate through the rest of the badges + for (const badge of scoreBadges) { + const min = badge.min ?? -Infinity; // Treat null `min` as -Infinity + const max = badge.max ?? Infinity; // Treat null `max` as Infinity + + // Check if the accuracy falls within the badge's range + if (acc >= min && acc < (max === null ? Infinity : max)) { + return badge; // Return the color of the matching badge + } + } + + // Fallback color if no badge matches (should not happen) + return scoreBadges[scoreBadges.length - 1]; +} + /** * Turns the difficulty of a song into a color * @@ -30,20 +59,3 @@ export function songDifficultyToColor(diff: string) { diff = diff.replace("+", "Plus"); return diffColors[diff.toLowerCase() as keyof typeof diffColors]; } - -/** - * Formats the accuracy into a color - * - * @param acc the accuracy to get the color for - */ -export function accuracyToColor(acc: number): string { - for (const badge of badgesDef) { - if ( - (badge.min === null || acc >= badge.min) && - (badge.max === null || acc < badge.max) - ) { - return badge.color; - } - } - return "#000000"; // fallback color -} diff --git a/src/components/navbar/profile-button.tsx b/src/components/navbar/profile-button.tsx index f692aae..547423c 100644 --- a/src/components/navbar/profile-button.tsx +++ b/src/components/navbar/profile-button.tsx @@ -27,7 +27,7 @@ export default function ProfileButton() {

You

diff --git a/src/components/score/score-info.tsx b/src/components/score/score-info.tsx index 0668df2..9fafc2a 100644 --- a/src/components/score/score-info.tsx +++ b/src/components/score/score-info.tsx @@ -1,12 +1,12 @@ import BeatSaverMap from "@/common/database/types/beatsaver-map"; import ScoreSaberLeaderboardToken from "@/common/model/token/scoresaber/score-saber-leaderboard-token"; import { getDifficultyFromScoreSaberDifficulty } from "@/common/scoresaber-utils"; -import { songDifficultyToColor } from "@/common/song-utils"; import FallbackLink from "@/components/fallback-link"; import Tooltip from "@/components/tooltip"; import { StarIcon } from "@heroicons/react/24/solid"; import clsx from "clsx"; import Image from "next/image"; +import { songDifficultyToColor } from "@/common/song-utils"; type Props = { leaderboard: ScoreSaberLeaderboardToken; diff --git a/src/components/score/score-stats.tsx b/src/components/score/score-stats.tsx index 8b005de..570f7a7 100644 --- a/src/components/score/score-stats.tsx +++ b/src/components/score/score-stats.tsx @@ -1,7 +1,7 @@ import ScoreSaberLeaderboardToken from "@/common/model/token/scoresaber/score-saber-leaderboard-token"; import ScoreSaberScoreToken from "@/common/model/token/scoresaber/score-saber-score-token"; import { formatNumberWithCommas, formatPp } from "@/common/number-utils"; -import { accuracyToColor } from "@/common/song-utils"; +import { getScoreColorFromAccuracy } from "@/common/song-utils"; import StatValue from "@/components/stat-value"; import { XMarkIcon } from "@heroicons/react/24/solid"; import clsx from "clsx"; @@ -39,7 +39,7 @@ const badges: Badge[] = [ leaderboard: ScoreSaberLeaderboardToken, ) => { const acc = (score.baseScore / leaderboard.maxScore) * 100; - return accuracyToColor(acc); + return getScoreColorFromAccuracy(acc).color; }, create: ( score: ScoreSaberScoreToken,