Skip to content

Commit

Permalink
score formatting: truncate optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekatwikz committed May 15, 2024
1 parent a4bc707 commit 4fa9e87
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/boards/EvalBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function EvalBar({
<Tooltip
position="right"
color={score && score.value < 0 ? "dark" : undefined}
label={score ? formatScore(score) : undefined}
label={score ? formatScore(score, false) : undefined}
disabled={!score}
>
<Box
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/EvalChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function EvalChart(props: EvalChartProps) {
function getEvalText(node: TreeNode, type: "cp" | "wdl"): string {
if (node.score) {
if (type === "cp") {
return `Advantage: ${formatScore(node.score.value)}`;
return `Advantage: ${formatScore(node.score.value, false)}`;
}
if (type === "wdl" && node.score.wdl) {
return `
Expand Down
2 changes: 1 addition & 1 deletion src/components/panels/analysis/ScoreBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function ScoreBubble({
<Tooltip
position="left"
color={score.value.value < 0 ? "dark" : undefined}
label={formatScore(score.value)}
label={formatScore(score.value, false)}
>
<Text
fw={700}
Expand Down
15 changes: 10 additions & 5 deletions src/utils/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ export const INITIAL_SCORE: Score = {

const CP_CEILING = 1000;

export function formatScore(score: ScoreValue): string {
export function formatScore(score: ScoreValue, truncate = true): string {
let scoreText = match(score.type)
// yoinked from: https://github.com/lichess-org/lila/blob/e110605c138c1f6fec417ab9317968b32a928a16/ui/ceval/src/util.ts#L8
.with("cp", () =>
Math.min(Math.round(Math.abs(score.value) / 10) / 10, 99).toFixed(1),
)
.with("cp", () => {
const absScore = Math.abs(score.value);
return (
truncate
? // idea from: https://github.com/lichess-org/lila/blob/e110605c138c1f6fec417ab9317968b32a928a16/ui/ceval/src/util.ts#L8
Math.min(Math.round(absScore / 10) / 10, 99).toFixed(1)
: (absScore / 100).toFixed(2)
).toString();
})
.with("mate", () => `M${Math.abs(score.value)}`)
.with("dtz", () => `DTZ${Math.abs(score.value)}`)
.exhaustive();
Expand Down

0 comments on commit 4fa9e87

Please sign in to comment.