diff --git a/apps/desktop/src/app.rs b/apps/desktop/src/app.rs index a1552a7..04e4232 100644 --- a/apps/desktop/src/app.rs +++ b/apps/desktop/src/app.rs @@ -306,6 +306,8 @@ pub struct App { brilliant_start: Option, brilliant_pass: Option, brilliant_summary: Option, + /// A pass asked for while Stockfish 19 is fetched or checked: it starts after. + brilliant_waiting: Option, applied_dark: Option, debug: DebugStartup, } @@ -580,6 +582,7 @@ impl App { brilliant_start: None, brilliant_pass: None, brilliant_summary: None, + brilliant_waiting: None, applied_dark: None, debug: DebugStartup::from_env(), } @@ -2043,10 +2046,11 @@ impl App { }; let Some(crate::engines::EngineLocation::Program(engine)) = crate::review_engine::engine() else { - self.toast( - ctx, - tr("The search needs Stockfish 19: start «Analyse game» once to get it"), - ); + // The offer of «Analyse game»: download Stockfish 19 (or check the user's); + // the pass starts after it. + dialog.counted = Some(counted); + self.brilliant_waiting = Some(dialog); + self.need_review_engine(ctx, crate::engine_offer::Pending::GreatMoves); return; }; let (Some(db), Some(cache)) = ( @@ -2224,13 +2228,9 @@ impl App { self.library.choose_only(&source, record as usize); } SummaryAction::Sort => self.library.sort_by_marks(&source), - SummaryAction::Continue(rest) => { - let filter = self - .brilliant_summary - .take() - .map(|_| chess_import::chessbase::base::Filter::default()) - .unwrap_or_default(); - self.ask_great_moves(ctx, source, rest, filter); + SummaryAction::Continue { records, filter } => { + self.brilliant_summary = None; + self.ask_great_moves(ctx, source, records, *filter); } SummaryAction::Open { record, ply } => { self.brilliant_summary = None; @@ -2241,6 +2241,9 @@ impl App { }); self.library.choose_only(&source, record as usize); self.open_game(ctx, source, record as usize, at); + if let Some(g) = self.game.as_mut() { + g.show_report(); + } } } } @@ -3554,6 +3557,7 @@ impl App { Some( crate::engine_offer::Pending::GameAnalysis { .. } | crate::engine_offer::Pending::OpeningErrors { .. } + | crate::engine_offer::Pending::GreatMoves ) ); if !own_search || self.settings.active_engine().is_none() { @@ -3653,6 +3657,11 @@ impl App { } } } + Some(crate::engine_offer::Pending::GreatMoves) => { + if let Some(dialog) = self.brilliant_waiting.take() { + self.start_great_moves(ctx, dialog); + } + } Some(crate::engine_offer::Pending::OpeningErrors { prep }) => { let actions = match self.prep.as_mut().filter(|p| p.serial == prep) { Some(p) => p.start_after_engine(ctx), @@ -4642,6 +4651,12 @@ impl eframe::App for App { LibraryAction::OpenGame { path, index, at } => { self.open_game(&ctx, path, index, at) } + LibraryAction::OpenGameReport { path, index, at } => { + self.open_game(&ctx, path, index, at); + if let Some(g) = self.game.as_mut() { + g.show_report(); + } + } LibraryAction::Sound(s) => self.play(s), LibraryAction::CopyText(text, msg) => { ctx.copy_text(text); @@ -5053,6 +5068,8 @@ fn still_waiting( Pending::GameAnalysis { game: g, .. } => Some(*g) == game, Pending::OpeningErrors { prep: p } => Some(*p) == prep, Pending::LiveAnalysis => false, + // The app keeps its question (`brilliant_waiting`) until it starts. + Pending::GreatMoves => true, } } diff --git a/apps/desktop/src/brilliant.rs b/apps/desktop/src/brilliant.rs index 2eacbfb..8c33633 100644 --- a/apps/desktop/src/brilliant.rs +++ b/apps/desktop/src/brilliant.rs @@ -144,6 +144,23 @@ pub struct Entry { } impl Entry { + /// The entry with only the marks of the sides that count (the filter's player): the + /// row's sign and hint show only them. + pub fn for_sides(&self, white: bool, black: bool) -> Entry { + let keep = |on: bool, s: &SideMarks| if on { *s } else { SideMarks::default() }; + Entry { + white: keep(white, &self.white), + black: keep(black, &self.black), + found: self + .found + .iter() + .filter(|f| if f.white { white } else { black }) + .cloned() + .collect(), + ..self.clone() + } + } + /// The !! and ! of the sides that count. pub fn of_sides(&self, white: bool, black: bool) -> Counts { let mut c = Counts::default(); @@ -289,12 +306,13 @@ impl Index { /// Where a record goes when the table is sorted by its marks: !! first, then !, /// analysed without them, not analysable, not analysed. - pub fn rank(&self, record: u32) -> u8 { + pub fn rank(&self, record: u32, sides: &Sides) -> u8 { match self.records.get(&record) { None => 4, Some(e) if e.skip.is_some() => 3, Some(e) => { - let c = e.of_sides(true, true); + let (w, b) = sides.of(record); + let c = e.of_sides(w, b); if c.brilliant > 0 { 0 } else if c.great > 0 { @@ -515,7 +533,8 @@ pub struct Sides { } impl Sides { - fn of(&self, record: u32) -> (bool, bool) { + /// Whether White's and Black's moves of `record` count. + pub fn of(&self, record: u32) -> (bool, bool) { match &self.player { None => (true, true), Some((w, b)) => ( diff --git a/apps/desktop/src/brilliant_tests.rs b/apps/desktop/src/brilliant_tests.rs index 768018c..6b05489 100644 --- a/apps/desktop/src/brilliant_tests.rs +++ b/apps/desktop/src/brilliant_tests.rs @@ -344,3 +344,86 @@ fn the_players_sides_come_from_the_filter() { .is_none() ); } + +/// White found a !! and Black a !: the row of a filter on the Black player shows !. +fn two_sided() -> Index { + let mut index = Index::default(); + index.set( + 0, + Entry { + line: String::new(), + white: side(1, 0, 0, 10, 95.0), + black: side(0, 1, 2, 40, 70.0), + found: vec![ + found(20, true, 11, "Rxf7", true), + found(23, false, 12, "Nd5", false), + ], + stale: false, + skip: None, + }, + ); + index.set( + 1, + Entry { + found: Vec::new(), + ..index.get(0).unwrap().clone() + }, + ); + index +} + +#[test] +fn the_column_counts_only_the_filters_players_moves() { + let index = two_sided(); + let both = Sides::default(); + let black = Sides { + player: Some((Vec::new(), vec![0])), + }; + assert_eq!(index.rank(0, &both), 0); + assert_eq!(index.rank(0, &black), 1); + // A game where the player is on neither side has nothing of his. + assert_eq!(index.rank(1, &black), 2); + let e = index.get(0).unwrap().for_sides(false, true); + assert_eq!(e.of_sides(true, true).brilliant, 0); + assert_eq!(e.of_sides(true, true).great, 1); + assert_eq!( + e.found.iter().map(Found::label).collect::>(), + ["12…Nd5!"] + ); +} + +#[test] +fn the_summary_keeps_the_player_for_continue() { + let dir = Dir::new("summary"); + let (path, db) = source(&dir); + let filter = chess_import::chessbase::base::Filter { + player: chess_import::chessbase::base::NameFilter { + text: "carlsen".into(), + exact: None, + }, + ..Default::default() + }; + let sides = player_sides(&db, &filter, &AtomicBool::new(false)); + let index = two_sided(); + // Record 0: Carlsen has White (its !!); record 1: Carlsen has Black (a !). + let summary = crate::brilliant_ui::Summary::new( + &path, + &db, + &index, + &[0, 1, 2], + &sides, + &filter, + String::new(), + Some((2, 3)), + Vec::new(), + ); + assert_eq!(summary.filter, filter); + assert_eq!(summary.rest, vec![2]); + assert_eq!( + (summary.totals.counts.brilliant, summary.totals.counts.great), + (1, 1) + ); + assert_eq!(summary.totals.counts.blunders, 2); + assert_eq!(summary.player, Some(("carlsen".to_owned(), 1, 1))); + assert_eq!(summary.rows.len(), 1); +} diff --git a/apps/desktop/src/brilliant_ui.rs b/apps/desktop/src/brilliant_ui.rs index 89a776b..884ddb8 100644 --- a/apps/desktop/src/brilliant_ui.rs +++ b/apps/desktop/src/brilliant_ui.rs @@ -235,6 +235,8 @@ pub struct Summary { pub skipped: Vec<(u32, Skip)>, pub rows: Vec, pub selected: Option, + /// The pass's filter: «Continue» goes on with it (its player's moves only). + pub filter: Filter, } pub enum SummaryAction { @@ -245,7 +247,10 @@ pub enum SummaryAction { record: u32, ply: u16, }, - Continue(Vec), + Continue { + records: Vec, + filter: Box, + }, /// Sort the table by the «!!» column. Sort, Close, @@ -311,6 +316,7 @@ impl Summary { skipped, rows, selected: None, + filter: filter.clone(), } } @@ -603,7 +609,10 @@ impl Summary { && !self.rest.is_empty() && ui.button(tr("Continue")).clicked() { - out = Some(SummaryAction::Continue(self.rest.clone())); + out = Some(SummaryAction::Continue { + records: self.rest.clone(), + filter: Box::new(self.filter.clone()), + }); } }); }); diff --git a/apps/desktop/src/engine_offer.rs b/apps/desktop/src/engine_offer.rs index 866e0cd..c218ced 100644 --- a/apps/desktop/src/engine_offer.rs +++ b/apps/desktop/src/engine_offer.rs @@ -18,6 +18,8 @@ pub const DOWNLOAD_INDICATOR: &str = "download_indicator"; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Pending { LiveAnalysis, + /// The search for great moves in a database's list (task 27), waiting in the app. + GreatMoves, /// The analysis of the game on the screen whose `serial` is `game` (no other game's); /// `full`: anew, over evaluations read from the PGN. GameAnalysis { @@ -162,6 +164,9 @@ impl EngineOffer { Some(Pending::OpeningErrors { .. }) => { tr("Stockfish installed — the search for opening errors started") } + Some(Pending::GreatMoves) => { + tr("Stockfish installed — the search for great moves started") + } None => tr("Stockfish installed"), } .to_string(), @@ -394,6 +399,9 @@ impl EngineOffer { Some(Pending::OpeningErrors { .. }) => { tr("The search for opening errors will start by itself when the download finishes.") } + Some(Pending::GreatMoves) => { + tr("The search for great moves will start by itself when the download finishes.") + } _ => tr("Analysis will start by itself when the download finishes."), }; ui.label(RichText::new(after).weak()); diff --git a/apps/desktop/src/game_view.rs b/apps/desktop/src/game_view.rs index a1fee50..a845606 100644 --- a/apps/desktop/src/game_view.rs +++ b/apps/desktop/src/game_view.rs @@ -5576,6 +5576,15 @@ fn paste_line_full(line: &[(u32, Color, String)]) -> String { .join(" ") } +impl GameView { + /// The report tab and the graph shown (a game opened at a move the search for great + /// moves found). + pub fn show_report(&mut self) { + self.review.visible = true; + self.side_tab = SideTab::Report; + } +} + /// «📈 Analyse game» in the colour of «!!» (task 26): the report it makes marks its finds /// with it. Hover and press stay visible (egui's own states, shaded); a disabled button /// keeps the usual look. diff --git a/apps/desktop/src/i18n.rs b/apps/desktop/src/i18n.rs index 54c2a7f..c900aa2 100644 --- a/apps/desktop/src/i18n.rs +++ b/apps/desktop/src/i18n.rs @@ -20634,6 +20634,28 @@ const ROWS: &[[&str; 9]] = &[ "Partiyani ochish", "Партияны ашу", ], + [ + "Stockfish installed — the search for great moves started", + "Stockfish установлен — поиск замечательных ходов начат", + "Stockfish installé — la recherche de très bons coups a commencé", + "Stockfish installiert — die Suche nach starken Zügen läuft", + "Stockfish instalado: la búsqueda de jugadas notables ha empezado", + "Stockfish installato — la ricerca di mosse notevoli è iniziata", + "Stockfish instalado — a pesquisa de lances notáveis começou", + "Stockfish o‘rnatildi — ajoyib yurishlarni qidirish boshlandi", + "Stockfish орнатылды — тамаша жүрістерді іздеу басталды", + ], + [ + "The search for great moves will start by itself when the download finishes.", + "Поиск замечательных ходов начнётся сам, когда загрузка закончится.", + "La recherche de très bons coups commencera d’elle-même à la fin du téléchargement.", + "Die Suche nach starken Zügen beginnt von selbst, wenn der Download fertig ist.", + "La búsqueda de jugadas notables empezará sola cuando termine la descarga.", + "La ricerca di mosse notevoli partirà da sola al termine del download.", + "A pesquisa de lances notáveis começa sozinha quando a transferência terminar.", + "Yuklab olish tugagach, ajoyib yurishlarni qidirish o‘zi boshlanadi.", + "Жүктеу аяқталғанда тамаша жүрістерді іздеу өзі басталады.", + ], ]; #[cfg(test)] diff --git a/apps/desktop/src/large_db.rs b/apps/desktop/src/large_db.rs index e5dc7f5..d3da970 100644 --- a/apps/desktop/src/large_db.rs +++ b/apps/desktop/src/large_db.rs @@ -359,6 +359,10 @@ pub struct LargeView { shown_marks: std::cell::RefCell<(u64, HashMap>)>, /// The table sorted by the «!!» column. marks_sort: bool, + /// Whose moves the «!!» column counts: the filter's player's (found in the background + /// for that filter), else both sides'. + marks_sides: (Option, crate::brilliant::Sides), + sides_job: Option<(Filter, Receiver)>, /// The search under way (here or in another database), set by the library. pub pass: Option, /// Why the search cannot start now (the engine is busy), set by the app. @@ -499,6 +503,8 @@ impl LargeView { marks: None, shown_marks: std::cell::RefCell::new((u64::MAX, HashMap::new())), marks_sort: false, + marks_sides: (None, Default::default()), + sides_job: None, pass: None, engine_busy: None, pass_error: None, @@ -671,6 +677,49 @@ impl LargeView { self.selected = Some(index); } + /// The filter's player's sides of each record, for the «!!» column: found again in + /// the background when the filter changes; both sides without a player. + fn update_marks_sides(&mut self, db: &Arc, ctx: &egui::Context) { + let filter = self.form.filter(); + if let Some((f, rx)) = &self.sides_job + && let Ok(sides) = rx.try_recv() + { + self.marks_sides = (Some(f.clone()), sides); + self.sides_job = None; + self.shown_marks.borrow_mut().1.clear(); + if self.marks_sort { + self.sort_by_marks(); + } + } + let wanted = self.marks_sides.0.as_ref() == Some(&filter) + || self.sides_job.as_ref().is_some_and(|(f, _)| *f == filter); + if wanted { + return; + } + if !filter.player.is_set() { + self.marks_sides = (Some(filter), Default::default()); + self.sides_job = None; + self.shown_marks.borrow_mut().1.clear(); + return; + } + let (tx, rx) = channel(); + let (db, f, ctx) = (db.clone(), filter.clone(), ctx.clone()); + let spawned = std::thread::Builder::new() + .name("marks-sides".into()) + .spawn(move || { + let sides = crate::brilliant::player_sides( + &db, + &f, + &std::sync::atomic::AtomicBool::new(false), + ); + let _ = tx.send(sides); + ctx.request_repaint(); + }); + if spawned.is_ok() { + self.sides_job = Some((filter, rx)); + } + } + /// The table sorted by its «!!» column (the summary's «Sort»). pub fn sort_marks_on(&mut self) { self.marks_sort = true; @@ -685,7 +734,8 @@ impl LargeView { let Ok(index) = index.lock() else { return; }; - self.view.sort_by_key(|&r| (index.rank(r), r)); + let sides = &self.marks_sides.1; + self.view.sort_by_key(|&r| (index.rank(r, sides), r)); self.view_gen += 1; } @@ -694,6 +744,7 @@ impl LargeView { fn shown_mark( marks: &Option>>, shown: &std::cell::RefCell<(u64, HashMap>)>, + sides: &crate::brilliant::Sides, db: &Database, record: u32, ) -> Option { @@ -714,6 +765,10 @@ impl LargeView { (entry.line == line).then_some(entry) }) .clone() + .map(|e| { + let (w, b) = sides.of(record); + e.for_sides(w, b) + }) } /// «!! Find great moves» (the button, Shift+A): the library asks first. @@ -2626,6 +2681,7 @@ impl LargeView { (true, _) => {} } } + self.update_marks_sides(db, ui.ctx()); // `CP_MARKS_DEMO=1`: made-up marks for the first rows (screenshots of the column). if std::env::var_os("CP_MARKS_DEMO").is_some() && self.shown.is_some() @@ -2777,7 +2833,8 @@ impl LargeView { .as_ref() .filter(|p| p.here) .and_then(|p| p.current); - let (marks_ref, shown_ref) = (&self.marks, &self.shown_marks); + let (marks_ref, shown_ref, sides_ref) = + (&self.marks, &self.shown_marks, &self.marks_sides.1); let view = &self.view; let selected = self.selected; let marked = &self.marked; @@ -2887,7 +2944,7 @@ impl LargeView { (_, Some(SortKey::BlackElo)) => RichText::new(elo(r.black_elo)), (_, Some(SortKey::Result)) => RichText::new(result), ("!!", None) => { - marks_cell(ui, Self::shown_mark(marks_ref, shown_ref, db, index as u32), pass_current == Some(index as u32), is_marked); + marks_cell(ui, Self::shown_mark(marks_ref, shown_ref, sides_ref, db, index as u32), pass_current == Some(index as u32), is_marked); return; } (_, Some(SortKey::Moves)) => RichText::new(match r.moves { @@ -2957,7 +3014,7 @@ impl LargeView { ui.close(); } // The first !! (else !) the search found: the game at it. - if let Some(f) = Self::shown_mark(marks_ref, shown_ref, db, index as u32) + if let Some(f) = Self::shown_mark(marks_ref, shown_ref, sides_ref, db, index as u32) .and_then(|e| { let first = e.found.iter().find(|f| f.brilliant).or(e.found.first()); first.cloned() @@ -2970,7 +3027,7 @@ impl LargeView { let node = *p.game.mainline().get(f.ply as usize)?; Some(p.game.node(node)?.position().clone()) }); - actions.push(LibraryAction::OpenGame { + actions.push(LibraryAction::OpenGameReport { path: path.to_path_buf(), index, at, diff --git a/apps/desktop/src/library.rs b/apps/desktop/src/library.rs index 0e49422..f4312f2 100644 --- a/apps/desktop/src/library.rs +++ b/apps/desktop/src/library.rs @@ -94,6 +94,13 @@ pub enum LibraryAction { }, /// A short message (a toast). Notice(String), + /// Open a game at the position `at` with its report shown (a move the search for + /// great moves found). + OpenGameReport { + path: PathBuf, + index: usize, + at: Option, + }, /// «!! Find great moves» over the list `records` of `path` (in the table's order), /// found by `filter` (task 27). FindGreatMoves {