Skip to content

Commit

Permalink
allow for reversed game analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Nov 10, 2023
1 parent 616e78b commit 927444a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ pub struct AnalysisOptions {
pub fen: String,
pub annotate_novelties: bool,
pub reference_db: Option<PathBuf>,
pub reversed: bool,
}

#[tauri::command]
Expand Down Expand Up @@ -452,6 +453,10 @@ pub async fn analyze_game(
fens.push(Fen::from_position(chess.clone(), EnPassantMode::Legal));
});

if options.reversed {
fens.reverse();
}

let mut novelty_found = false;

for (i, fen) in fens.iter().enumerate() {
Expand All @@ -463,7 +468,6 @@ pub async fn analyze_game(
finished: false,
},
)?;
let query = PositionQuery::exact_from_fen(&fen.to_string())?;

process
.set_options(EngineOptions {
Expand All @@ -490,19 +494,28 @@ pub async fn analyze_game(
_ => {}
}
}
analysis.push(current_analysis);
}

if options.reversed {
analysis.reverse();
fens.reverse();
}

for (i, analysis) in analysis.iter_mut().enumerate() {
let fen = &fens[i];
let query = PositionQuery::exact_from_fen(&fen.to_string())?;

if options.annotate_novelties && !novelty_found {
if let Some(reference) = options.reference_db.clone() {
current_analysis.novelty =
!is_position_in_db(reference, query, state.clone()).await?;
if current_analysis.novelty {
analysis.novelty = !is_position_in_db(reference, query, state.clone()).await?;
if analysis.novelty {
novelty_found = true;
}
} else {
return Err(Error::MissingReferenceDatabase);
}
}
analysis.push(current_analysis);
}
app.emit_all(
"report_progress",
Expand Down
2 changes: 1 addition & 1 deletion src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bestMovesPayload: "plugin:tauri-specta:best-moves-payload"

/** user-defined types **/

export type AnalysisOptions = { fen: string; annotateNovelties: boolean; referenceDb: string | null }
export type AnalysisOptions = { fen: string; annotateNovelties: boolean; referenceDb: string | null; reversed: boolean }
export type BestMoves = { depth: number; score: Score; uciMoves: string[]; sanMoves: string[]; multipv: number; nps: number }
export type BestMovesPayload = { bestLines: BestMoves[]; engine: string; tab: string }
export type EngineOption = { name: string; value: string }
Expand Down
8 changes: 8 additions & 0 deletions src/components/panels/analysis/ReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function ReportModal({
engine: engines[0]?.path ?? "",
millisecondsPerMove: 500,
novelty: true,
reversed: true
},

validate: {
Expand Down Expand Up @@ -70,6 +71,7 @@ function ReportModal({
annotateNovelties: form.values.novelty,
fen: initialFen,
referenceDb,
reversed: form.values.reversed
}
)
.then((analysis) => {
Expand Down Expand Up @@ -111,6 +113,12 @@ function ReportModal({
{...form.getInputProps("millisecondsPerMove")}
/>

<Checkbox
label="Reversed analysis"
description="Analyze the game in starting from the last move."
{...form.getInputProps("reversed", { type: "checkbox" })}
/>

<Checkbox
label="Annotate Novelties"
description="Add a comment to the first position that is not in the reference database."
Expand Down

0 comments on commit 927444a

Please sign in to comment.