From 1193374eb17c2e4fd34a6cf0980981d899eb1e93 Mon Sep 17 00:00:00 2001 From: Francisco Salgueiro Date: Mon, 2 Oct 2023 18:25:55 +0100 Subject: [PATCH] allow report for setup position --- src-tauri/src/chess.rs | 11 ++++---- src/components/boards/BoardAnalysis.tsx | 26 ++++++++++++------- .../panels/analysis/ReportModal.tsx | 25 +++++------------- src/utils/chess.ts | 12 +++++++++ 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src-tauri/src/chess.rs b/src-tauri/src/chess.rs index 698171ca..23a5c9d6 100644 --- a/src-tauri/src/chess.rs +++ b/src-tauri/src/chess.rs @@ -315,7 +315,8 @@ pub struct MoveAnalysis { #[tauri::command] pub async fn analyze_game( - moves: String, + fen: String, + moves: Vec, annotate_novelties: bool, engine: String, move_time: usize, @@ -330,8 +331,9 @@ pub async fn analyze_game( let mut child = start_engine(path)?; let (mut stdin, mut stdout) = get_handles(&mut child)?; + let fen = Fen::from_ascii(fen.as_bytes())?; - let mut chess = Chess::default(); + let mut chess: Chess = fen.into_position(CastlingMode::Standard)?; send_command( &mut stdin, @@ -344,12 +346,11 @@ pub async fn analyze_game( ) .await; - let splitted_moves: Vec<&str> = moves.split_whitespace().collect(); - let len_moves = splitted_moves.len(); + let len_moves = moves.len(); let mut novelty_found = false; - for (i, m) in splitted_moves.iter().enumerate() { + for (i, m) in moves.iter().enumerate() { app.emit_all( "report_progress", ProgressPayload { diff --git a/src/components/boards/BoardAnalysis.tsx b/src/components/boards/BoardAnalysis.tsx index 6e1641f3..ad9da383 100644 --- a/src/components/boards/BoardAnalysis.tsx +++ b/src/components/boards/BoardAnalysis.tsx @@ -8,7 +8,7 @@ import { } from "@tabler/icons-react"; import { useCallback, useContext, useEffect, useRef, useState } from "react"; import BoardLayout from "@/layouts/BoardLayout"; -import { getPGN } from "@/utils/chess"; +import { getMainLine } from "@/utils/chess"; import { getBoardSize } from "@/utils/misc"; import { invoke } from "@/utils/invoke"; @@ -27,7 +27,11 @@ import BoardPlay from "./BoardPlay"; import EditingCard from "./EditingCard"; import GameNotation from "./GameNotation"; import { useAtom, useAtomValue } from "jotai"; -import { autoSaveAtom, currentTabAtom, currentTabSelectedAtom } from "@/atoms/atoms"; +import { + autoSaveAtom, + currentTabAtom, + currentTabSelectedAtom, +} from "@/atoms/atoms"; import { saveToFile } from "@/utils/tabs"; function BoardAnalysis() { @@ -84,17 +88,15 @@ function BoardAnalysis() { useHotkeys([["Ctrl+S", () => saveFile()]]); - const [currentTabSelected, setCurrentTabSelected] = useAtom(currentTabSelectedAtom); + const [currentTabSelected, setCurrentTabSelected] = useAtom( + currentTabSelectedAtom + ); return ( <> 1000 ? boardSize : 600 + notationExpanded + ? 1750 + : window.innerWidth > 1000 + ? boardSize + : 600 } setNotationExpanded={setNotationExpanded} notationExpanded={notationExpanded} diff --git a/src/components/panels/analysis/ReportModal.tsx b/src/components/panels/analysis/ReportModal.tsx index 95c1afc6..0792ef34 100644 --- a/src/components/panels/analysis/ReportModal.tsx +++ b/src/components/panels/analysis/ReportModal.tsx @@ -9,8 +9,7 @@ import { Text, } from "@mantine/core"; import { useForm } from "@mantine/form"; -import { Chess } from "chess.js"; -import { memo, useContext, useMemo } from "react"; +import { memo, useContext } from "react"; import { MoveAnalysis } from "@/utils/chess"; import { useEngines } from "@/utils/engines"; import { formatDuration } from "@/utils/format"; @@ -18,15 +17,16 @@ import { invoke } from "@/utils/invoke"; import { TreeDispatchContext } from "@/components/common/TreeStateContext"; import { useAtomValue } from "jotai"; import { referenceDbAtom } from "@/atoms/atoms"; -import { error } from "tauri-plugin-log-api"; function ReportModal({ + initialFen, moves, reportingMode, toggleReportingMode, setInProgress, }: { - moves: string; + initialFen: string; + moves: string[]; reportingMode: boolean; toggleReportingMode: () => void; setInProgress: React.Dispatch>; @@ -35,18 +35,6 @@ function ReportModal({ const { engines } = useEngines(); const dispatch = useContext(TreeDispatchContext); - const uciMoves = useMemo(() => { - const chess = new Chess(); - let uciMoves: string[] = []; - try { - chess.loadPgn(moves); - uciMoves = chess.history(); - } catch (e) { - error(e as string); - } - return uciMoves; - }, [moves]); - const form = useForm({ initialValues: { engine: "", @@ -72,7 +60,8 @@ function ReportModal({ setInProgress(true); toggleReportingMode(); invoke("analyze_game", { - moves: uciMoves.join(" "), + fen: initialFen, + moves, engine: form.values.engine, annotateNovelties: form.values.novelty, moveTime: form.values.millisecondsPerMove, @@ -123,7 +112,7 @@ function ReportModal({ Estimated time:{" "} - {formatDuration(uciMoves.length * form.values.millisecondsPerMove)} + {formatDuration(moves.length * form.values.millisecondsPerMove)} diff --git a/src/utils/chess.ts b/src/utils/chess.ts index cca02f61..fbd7fb2c 100644 --- a/src/utils/chess.ts +++ b/src/utils/chess.ts @@ -190,6 +190,18 @@ export function getMoveText( return moveText; } +export function getMainLine(root: TreeNode): string[] { + const moves = []; + let node = root; + while (node.children.length > 0) { + node = node.children[0]; + if (node.move) { + moves.push(node.move.san); + } + } + return moves; +} + export function getPGN( tree: TreeNode, {