From 6c7bf4399eb35da3d473eff9abc6ea05b6252eea Mon Sep 17 00:00:00 2001 From: grimsi <9295182+grimsi@users.noreply.github.com> Date: Thu, 4 Sep 2025 18:58:07 +0200 Subject: [PATCH] Fix rating calculation --- app/src/main/frontend/util/utils.ts | 21 ++++++++++++--------- app/src/main/frontend/views/GameView.tsx | 4 ++-- app/src/main/frontend/views/SearchView.tsx | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/src/main/frontend/util/utils.ts b/app/src/main/frontend/util/utils.ts index 8c4cffb..23743fe 100644 --- a/app/src/main/frontend/util/utils.ts +++ b/app/src/main/frontend/util/utils.ts @@ -238,6 +238,7 @@ export function metadataCompleteness(game: GameDto) { * @returns The scaled number */ function convertRange(value: number, originalRange: number[], targetRange: number[]): number { + if (originalRange[0] === targetRange[0] && originalRange[1] === targetRange[1]) return value; return (value - originalRange[0]) * (targetRange[1] - targetRange[0]) / (originalRange[1] - originalRange[0]) + targetRange[0]; } @@ -247,22 +248,25 @@ function convertRange(value: number, originalRange: number[], targetRange: numbe * If only one rating is present, that rating is returned. * If neither rating is present, 0 is returned. * @param game The GameDto object containing the ratings. - * @returns The compound rating as a number between 0 and 100. + * @param scale The scale to convert the rating to (default is [0, 100]). + * @returns The compound rating. */ -export function compoundRating(game: GameDto): number { +export function compoundRating(game: GameDto, scale = [0, 100]): number { const weights = { critic: 0.4, user: 0.6 }; + const originalRange = [0, 100]; const criticRating = game.criticRating ?? 0; const userRating = game.userRating ?? 0; if (criticRating === 0 && userRating === 0) return 0; - if (criticRating === 0) return userRating; - if (userRating === 0) return criticRating; + if (criticRating === 0) return convertRange(userRating, originalRange, scale); + if (userRating === 0) return convertRange(criticRating, originalRange, scale); - return Math.round((criticRating * weights.critic + userRating * weights.user) * 10) / 10; + const avgRating = Math.round((criticRating * weights.critic + userRating * weights.user) * 10) / 10; + return convertRange(avgRating, originalRange, scale); } /** @@ -272,12 +276,11 @@ export function compoundRating(game: GameDto): number { * @param game The GameDto object containing the ratings. * @returns A string representing the star rating out of 5, or "N/A" if no ratings are available. */ -export function gameRatingInStars(game: GameDto) { - const originalRange = [0, 100]; +export function starRatingAsString(game: GameDto) { const starRange = [1, 5]; - const rating = compoundRating(game); + const rating = compoundRating(game, starRange); if (rating === 0) return "N/A"; - return convertRange(rating, originalRange, starRange).toFixed(1); + return rating.toFixed(1); } \ No newline at end of file diff --git a/app/src/main/frontend/views/GameView.tsx b/app/src/main/frontend/views/GameView.tsx index 92cc8f4..ae5dea8 100644 --- a/app/src/main/frontend/views/GameView.tsx +++ b/app/src/main/frontend/views/GameView.tsx @@ -5,7 +5,7 @@ import {GameCover} from "Frontend/components/general/covers/GameCover"; import ComboButton, {ComboButtonOption} from "Frontend/components/general/input/ComboButton"; import ImageCarousel from "Frontend/components/general/covers/ImageCarousel"; import {Accordion, AccordionItem, addToast, Button, Chip, Link, Tooltip, useDisclosure} from "@heroui/react"; -import {gameRatingInStars, humanFileSize, isAdmin, toTitleCase} from "Frontend/util/utils"; +import {humanFileSize, isAdmin, starRatingAsString, toTitleCase} from "Frontend/util/utils"; import {DownloadEndpoint} from "Frontend/endpoints/endpoints"; import {gameState} from "Frontend/state/GameState"; import {useSnapshot} from "valtio/react"; @@ -108,7 +108,7 @@ export default function GameView() {