Skip to content

Commit

Permalink
Merge pull request #19 from franciscoBSalgueiro/18-its-not-possible-t…
Browse files Browse the repository at this point in the history
…o-make-reports-of-games-that-start-from-a-specific-position

Allow report for setup position
  • Loading branch information
franciscoBSalgueiro authored Oct 2, 2023
2 parents c23b81f + 1193374 commit 3ba55fc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
11 changes: 6 additions & 5 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ pub struct MoveAnalysis {

#[tauri::command]
pub async fn analyze_game(
moves: String,
fen: String,
moves: Vec<String>,
annotate_novelties: bool,
engine: String,
move_time: usize,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
26 changes: 16 additions & 10 deletions src/components/boards/BoardAnalysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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() {
Expand Down Expand Up @@ -84,17 +88,15 @@ function BoardAnalysis() {

useHotkeys([["Ctrl+S", () => saveFile()]]);

const [currentTabSelected, setCurrentTabSelected] = useAtom(currentTabSelectedAtom);
const [currentTabSelected, setCurrentTabSelected] = useAtom(
currentTabSelectedAtom
);

return (
<>
<ReportModal
moves={getPGN(root, {
headers: null,
comments: false,
specialSymbols: false,
symbols: false,
})}
initialFen={root.fen}
moves={getMainLine(root)}
reportingMode={reportingMode}
toggleReportingMode={toggleReportingMode}
setInProgress={setInProgress}
Expand Down Expand Up @@ -169,7 +171,11 @@ function BoardAnalysis() {
<Stack>
<GameNotation
boardSize={
notationExpanded ? 1750 : window.innerWidth > 1000 ? boardSize : 600
notationExpanded
? 1750
: window.innerWidth > 1000
? boardSize
: 600
}
setNotationExpanded={setNotationExpanded}
notationExpanded={notationExpanded}
Expand Down
25 changes: 7 additions & 18 deletions src/components/panels/analysis/ReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ 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";
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<React.SetStateAction<boolean>>;
Expand All @@ -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: "",
Expand All @@ -72,7 +60,8 @@ function ReportModal({
setInProgress(true);
toggleReportingMode();
invoke<MoveAnalysis[]>("analyze_game", {
moves: uciMoves.join(" "),
fen: initialFen,
moves,
engine: form.values.engine,
annotateNovelties: form.values.novelty,
moveTime: form.values.millisecondsPerMove,
Expand Down Expand Up @@ -123,7 +112,7 @@ function ReportModal({

<Text size="sm">
Estimated time:{" "}
{formatDuration(uciMoves.length * form.values.millisecondsPerMove)}
{formatDuration(moves.length * form.values.millisecondsPerMove)}
</Text>

<Group position="right">
Expand Down
12 changes: 12 additions & 0 deletions src/utils/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down

0 comments on commit 3ba55fc

Please sign in to comment.