From 8c81f20dd299e4ec26b45581b35fce3b6e302f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Thu, 11 May 2023 18:35:51 +0200 Subject: [PATCH] [FIX] Use individual relative stat percentage, not 255 --- main.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/main.ts b/main.ts index 530291a..c0394bc 100644 --- a/main.ts +++ b/main.ts @@ -124,16 +124,17 @@ function renderPokemonIndex(pokemons: Array): string { ${ability.description} `; }) - .join('\n'); - const getStatBar = (value: number, maxValue: number) => { - const percentage = Math.round((value / maxValue) * 100); - const greenThreshold = 35; - const yellowThreshold = 20; + .join('\n'); + const getStatBar = (value: number, sumValues: number, maxValue: number) => { + const absolute_percentage = Math.round((value / maxValue) * 100); + const relative_percentage = Math.round((value / sumValues) * 100); + const greenThreshold = 100 / (6 - 1); // 6: because there are 6 stats + const yellowThreshold = 100 / (6 + 1); // +-1: to move a bit more/less from 6 let backgroundColor; - if (percentage >= greenThreshold) { + if (relative_percentage >= greenThreshold) { backgroundColor = 'limegreen'; - } else if (percentage >= yellowThreshold) { + } else if (relative_percentage >= yellowThreshold) { backgroundColor = 'gold'; } else { backgroundColor = 'tomato'; @@ -142,20 +143,22 @@ function renderPokemonIndex(pokemons: Array): string { return `
-
+
`; }; const statsTableRows = pokemon.stats - .map(stat => ` + .map(stat => { + const sum_stats = pokemon.stats.reduce((a, b) => a.value + b.value); + return ` ${(stat.name === 'hp') ? 'HP': stat.name.charAt(0).toUpperCase() + stat.name.slice(1)}: ${stat.value} - ${getStatBar(stat.value, 255)} - `) - .join('\n'); + ${getStatBar(stat.value, sum_stats, 255)} + `; + }).join('\n'); return `