diff --git a/public/analysis_icon.png b/public/analysis_icon.png deleted file mode 100644 index c1dc093..0000000 Binary files a/public/analysis_icon.png and /dev/null differ diff --git a/public/back.png b/public/back.png deleted file mode 100644 index 358de30..0000000 Binary files a/public/back.png and /dev/null differ diff --git a/public/back_to_start.png b/public/back_to_start.png deleted file mode 100644 index 9baf527..0000000 Binary files a/public/back_to_start.png and /dev/null differ diff --git a/public/best.png b/public/best.png deleted file mode 100644 index f9bc8af..0000000 Binary files a/public/best.png and /dev/null differ diff --git a/public/black_bishop.svg b/public/black_bishop.svg deleted file mode 100644 index e88c406..0000000 --- a/public/black_bishop.svg +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/public/black_king.svg b/public/black_king.svg deleted file mode 100644 index ba2ac9f..0000000 --- a/public/black_king.svg +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/public/black_knight.svg b/public/black_knight.svg deleted file mode 100644 index 04541a8..0000000 --- a/public/black_knight.svg +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - diff --git a/public/black_pawn.svg b/public/black_pawn.svg deleted file mode 100644 index b534de8..0000000 --- a/public/black_pawn.svg +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/public/black_queen.svg b/public/black_queen.svg deleted file mode 100644 index e557734..0000000 --- a/public/black_queen.svg +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/public/black_rook.svg b/public/black_rook.svg deleted file mode 100644 index 4eec43c..0000000 --- a/public/black_rook.svg +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/public/blunder.png b/public/blunder.png deleted file mode 100644 index 783af84..0000000 Binary files a/public/blunder.png and /dev/null differ diff --git a/public/book.png b/public/book.png deleted file mode 100644 index 65c8331..0000000 Binary files a/public/book.png and /dev/null differ diff --git a/public/brilliant.png b/public/brilliant.png deleted file mode 100644 index 20fa69d..0000000 Binary files a/public/brilliant.png and /dev/null differ diff --git a/public/capture.mp3 b/public/capture.mp3 deleted file mode 100644 index c71b93f..0000000 Binary files a/public/capture.mp3 and /dev/null differ diff --git a/public/castle.mp3 b/public/castle.mp3 deleted file mode 100644 index 0fe64ec..0000000 Binary files a/public/castle.mp3 and /dev/null differ diff --git a/public/check.mp3 b/public/check.mp3 deleted file mode 100644 index 82cce21..0000000 Binary files a/public/check.mp3 and /dev/null differ diff --git a/public/discord.png b/public/discord.png deleted file mode 100644 index 94bcf4a..0000000 Binary files a/public/discord.png and /dev/null differ diff --git a/public/excellent.png b/public/excellent.png deleted file mode 100644 index 3e61fe1..0000000 Binary files a/public/excellent.png and /dev/null differ diff --git a/public/flip.png b/public/flip.png deleted file mode 100644 index eaf9750..0000000 Binary files a/public/flip.png and /dev/null differ diff --git a/public/forced.png b/public/forced.png deleted file mode 100644 index 18006b6..0000000 Binary files a/public/forced.png and /dev/null differ diff --git a/public/game_end.mp3 b/public/game_end.mp3 deleted file mode 100644 index d5efe4e..0000000 Binary files a/public/game_end.mp3 and /dev/null differ diff --git a/public/go_to_end.png b/public/go_to_end.png deleted file mode 100644 index 915b343..0000000 Binary files a/public/go_to_end.png and /dev/null differ diff --git a/public/good.png b/public/good.png deleted file mode 100644 index 0028e0f..0000000 Binary files a/public/good.png and /dev/null differ diff --git a/public/great.png b/public/great.png deleted file mode 100644 index b3dcfdc..0000000 Binary files a/public/great.png and /dev/null differ diff --git a/public/inaccuracy.png b/public/inaccuracy.png deleted file mode 100644 index 46f8ad0..0000000 Binary files a/public/inaccuracy.png and /dev/null differ diff --git a/public/mistake.png b/public/mistake.png deleted file mode 100644 index 3151cab..0000000 Binary files a/public/mistake.png and /dev/null differ diff --git a/public/move.mp3 b/public/move.mp3 deleted file mode 100644 index e678ff7..0000000 Binary files a/public/move.mp3 and /dev/null differ diff --git a/public/next.png b/public/next.png deleted file mode 100644 index b39cce5..0000000 Binary files a/public/next.png and /dev/null differ diff --git a/public/promote.mp3 b/public/promote.mp3 deleted file mode 100644 index 25242a5..0000000 Binary files a/public/promote.mp3 and /dev/null differ diff --git a/public/save.png b/public/save.png deleted file mode 100644 index ec26b43..0000000 Binary files a/public/save.png and /dev/null differ diff --git a/public/white_bishop.svg b/public/white_bishop.svg deleted file mode 100644 index 3a8eaa2..0000000 --- a/public/white_bishop.svg +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/public/white_king.svg b/public/white_king.svg deleted file mode 100644 index 632ca1a..0000000 --- a/public/white_king.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/public/white_knight.svg b/public/white_knight.svg deleted file mode 100644 index a5f31c6..0000000 --- a/public/white_knight.svg +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - diff --git a/public/white_pawn.svg b/public/white_pawn.svg deleted file mode 100644 index b265fe1..0000000 --- a/public/white_pawn.svg +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/public/white_queen.svg b/public/white_queen.svg deleted file mode 100644 index 8df7c8f..0000000 --- a/public/white_queen.svg +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/public/white_rook.svg b/public/white_rook.svg deleted file mode 100644 index 0574ca6..0000000 --- a/public/white_rook.svg +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/lib/engine/helpers/parseResults.ts b/src/lib/engine/helpers/parseResults.ts index 3f8cef7..0427604 100644 --- a/src/lib/engine/helpers/parseResults.ts +++ b/src/lib/engine/helpers/parseResults.ts @@ -57,7 +57,7 @@ export const parseEvaluationResults = ( return parsedResults; }; -const sortLines = (a: LineEval, b: LineEval): number => { +export const sortLines = (a: LineEval, b: LineEval): number => { if (a.mate !== undefined && b.mate !== undefined) { return a.mate - b.mate; } diff --git a/src/lib/engine/uciEngine.ts b/src/lib/engine/uciEngine.ts index e9ad36a..b45a22d 100644 --- a/src/lib/engine/uciEngine.ts +++ b/src/lib/engine/uciEngine.ts @@ -7,6 +7,7 @@ import { import { parseEvaluationResults } from "./helpers/parseResults"; import { computeAccuracy } from "./helpers/accuracy"; import { getWhoIsCheckmated } from "../chess"; +import { getLichessEval } from "../lichess"; export abstract class UciEngine { private worker: Worker; @@ -143,6 +144,14 @@ export abstract class UciEngine { private async evaluatePosition(fen: string, depth = 16): Promise { console.log(`Evaluating position: ${fen}`); + const lichessEval = await getLichessEval(fen, this.multiPv); + if ( + lichessEval.lines.length >= this.multiPv && + lichessEval.lines[0].depth >= depth + ) { + return lichessEval; + } + const results = await this.sendCommands( [`position fen ${fen}`, `go depth ${depth}`], "bestmove" @@ -161,6 +170,8 @@ export abstract class UciEngine { }: EvaluatePositionWithUpdateParams): Promise { this.throwErrorIfNotReady(); + const lichessEvalPromise = getLichessEval(fen, multiPv); + await this.stopSearch(); await this.setMultiPv(multiPv); @@ -172,6 +183,16 @@ export abstract class UciEngine { }; console.log(`Evaluating position: ${fen}`); + + const lichessEval = await lichessEvalPromise; + if ( + lichessEval.lines.length >= multiPv && + lichessEval.lines[0].depth >= depth + ) { + setPartialEval(lichessEval); + return; + } + await this.sendCommands( [`position fen ${fen}`, `go depth ${depth}`], "bestmove", diff --git a/src/lib/lichess.ts b/src/lib/lichess.ts new file mode 100644 index 0000000..bd687ba --- /dev/null +++ b/src/lib/lichess.ts @@ -0,0 +1,51 @@ +import { LineEval, MoveEval } from "@/types/eval"; +import { sortLines } from "./engine/helpers/parseResults"; +import { + LichessError, + LichessEvalBody, + LichessResponse, +} from "@/types/lichess"; + +export const getLichessEval = async ( + fen: string, + multiPv = 1 +): Promise => { + try { + const res = await fetch( + `https://lichess.org/api/cloud-eval?fen=${fen}&multiPv=${multiPv}` + ); + + const data: LichessResponse = await res.json(); + + if ("error" in data) { + if (data.error === LichessError.NotFound) { + return { + bestMove: "", + lines: [], + }; + } + throw new Error(data.error); + } + + const lines: LineEval[] = data.pvs.map((pv, index) => ({ + pv: pv.moves.split(" "), + cp: pv.cp, + mate: pv.mate, + depth: data.depth, + multiPv: index + 1, + })); + + lines.sort(sortLines); + + return { + bestMove: lines[0].pv[0], + lines: lines.slice(0, multiPv), + }; + } catch (error) { + console.error(error); + return { + bestMove: "", + lines: [], + }; + } +}; diff --git a/src/sections/analysis/reviewPanelBody/lineEvaluation.tsx b/src/sections/analysis/reviewPanelBody/lineEvaluation.tsx index 003a2cb..329e3a1 100644 --- a/src/sections/analysis/reviewPanelBody/lineEvaluation.tsx +++ b/src/sections/analysis/reviewPanelBody/lineEvaluation.tsx @@ -47,7 +47,7 @@ export default function LineEvaluation({ line }: Props) { maxWidth={{ xs: "15em", sm: "25em", md: "30em", lg: "25em" }} > {showSkeleton ? ( - + ) : ( line.pv.map(moveLineUciToSan(board.fen())).join(", ") )} diff --git a/src/types/lichess.ts b/src/types/lichess.ts new file mode 100644 index 0000000..b151448 --- /dev/null +++ b/src/types/lichess.ts @@ -0,0 +1,18 @@ +export interface LichessErrorBody { + error: string | LichessError; +} + +export interface LichessEvalBody { + depth: number; + pvs: { + moves: string; + cp?: number; + mate?: number; + }[]; +} + +export type LichessResponse = T | LichessErrorBody; + +export enum LichessError { + NotFound = "Not found", +}