Skip to content

Commit

Permalink
finish tablebase info
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Feb 5, 2024
1 parent 0a7337b commit 9e37d52
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/components/engines/EnginesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ function EngineSettings({
} else {
newSettings.push({ name, value });
}
console.log(name, value, def);
if (value !== def || requiredEngineSettings.includes(name)) {
setEngine({
...engine,
Expand Down
68 changes: 43 additions & 25 deletions src/components/panels/analysis/TablebaseInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TreeDispatchContext } from "@/components/common/TreeStateContext";
import { getTablebaseInfo } from "@/utils/lichess";
import { TablebaseCategory, getTablebaseInfo } from "@/utils/lichess";
import {
Accordion,
Badge,
Expand Down Expand Up @@ -56,7 +56,7 @@ function TablebaseInfo({
{isLoading && <Text ta="center">Loading...</Text>}
{error && <Text ta="center">Error: {error.message}</Text>}
{data && (
<OutcomeBadge outcome={data.category} turn={turn} wins />
<OutcomeBadge category={data.category} turn={turn} wins />
)}
</Group>
</Accordion.Control>
Expand All @@ -82,9 +82,10 @@ function TablebaseInfo({
{m.san}
</Text>
<OutcomeBadge
outcome={m.category}
category={m.category}
dtz={m.dtz}
dtm={m.dtm}
turn={turn === "white" ? "black" : "white"}
wins={false}
/>
</Group>
</Paper>
Expand All @@ -100,38 +101,55 @@ function TablebaseInfo({
}

function OutcomeBadge({
outcome,
category,
turn,
wins,
dtz,
dtm,
}: {
outcome: "win" | "loss" | "draw" | "unknown";
category: TablebaseCategory;
turn: "white" | "black";
wins: boolean;
wins?: boolean;
dtz?: number;
dtm?: number;
}) {
const color = match(outcome)
const normalizedCategory = match(category)
.with("win", () => (turn === "white" ? "White wins" : "Black wins"))
.with("loss", () => (turn === "white" ? "Black wins" : "White wins"))
.with("draw", () => "draw")
.with("blessed-loss", () => "draw")
.with("cursed-win", () => "draw")
.with("maybe-loss", () => "unknown")
.with("maybe-win", () => "unknown")
.with("unknown", () => "unknown")
.exhaustive();

const color = match(category)
.with("win", () => (turn === "white" ? "white" : "black"))
.with("loss", () => (turn === "white" ? "black" : "white"))
.with("draw", () => "gray")
.with("unknown", () => "gray")
.exhaustive();
const label = match(outcome)
.with(
"win",
() => (turn === "white" ? "White" : "Black") + (wins ? " wins" : ""),
)
.with(
"loss",
() => (turn === "white" ? "Black" : "White") + (wins ? " wins" : ""),
)
.with("draw", () => "Draw")
.with("unknown", () => "Unknown")
.exhaustive();
.otherwise(() => "gray");

const label = wins
? normalizedCategory
: match(category)
.with("draw", () => "Draw")
.with("unknown", () => "Unknown")
.otherwise(() => (dtm ? `DTM ${Math.abs(dtm)}` : `DTZ ${dtz}`));

return (
<Stack px="xs" py="xs" align="center" justify="center">
<Group p="xs">
<Badge autoContrast color={color}>
{label}
</Badge>
</Stack>
{["blessed-loss", "cursed-win", "maybe-win", "maybe-loss"].includes(
category,
) &&
wins && (
<Text c="dimmed" fz="xs">
*due to the 50-move rule
</Text>
)}
</Group>
);
}

Expand Down
16 changes: 13 additions & 3 deletions src/utils/lichess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const baseURL = "https://lichess.org/api";
const explorerURL = "https://explorer.lichess.ovh";
const tablebaseURL = "https://tablebase.lichess.ovh";

export type TablebaseCategory =
| "win"
| "unknown"
| "maybe-win"
| "cursed-win"
| "draw"
| "blessed-loss"
| "maybe-loss"
| "loss";

type TablebaseData = {
checkmate: boolean;
stalemate: boolean;
Expand All @@ -33,11 +43,11 @@ type TablebaseData = {
dtz: number;
precise_dtz: number;
dtm: number;
category: "win" | "loss" | "draw" | "unknown";
category: TablebaseCategory;
moves: TablebaseMove[];
};

type TablebaseMove = {
export type TablebaseMove = {
uci: string;
san: string;
zeroing: boolean;
Expand All @@ -49,7 +59,7 @@ type TablebaseMove = {
dtz: number;
precise_dtz: number;
dtm: number;
category: "win" | "loss";
category: TablebaseCategory;
};

type LichessPerf = {
Expand Down

0 comments on commit 9e37d52

Please sign in to comment.