great moves (task 27): the background pass over a list, the marks index beside the analysis cache, totals by category and player

This commit is contained in:
vrubelroman 2026-11-27 16:03:04 +03:00
parent b6d457bcd0
commit c3a06653d6
4 changed files with 1101 additions and 5 deletions

View file

@ -0,0 +1,744 @@
//! The search for brilliant and great moves (!!, !) in the games of a filtered list
//! (task 27, docs/design/task-27-brilliant-search-2026-11-27.md): a background pass of
//! the game analysis's engine over at most [`LIMIT`] games, the index of what each
//! analysed record holds (kept beside the analysis cache, one file per database), and
//! the totals of a pass by category. Games already analysed come from the analysis
//! cache, the engine is not asked again; the source database is only read.
use std::collections::BTreeMap;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use chess_core::Game;
use chess_import::Database;
use game_review::{CacheStore, GameAnalysis, JobEvent, JobFailure, JobSettings, Judgement};
use serde::{Deserialize, Serialize};
/// A pass takes at most this many games (the owner's limit, inclusive).
pub const LIMIT: usize = 100;
/// Why a pass over `n` listed games cannot start (the button is disabled with this).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Blocked {
NoGames,
TooMany,
}
/// Whether a pass over a list of `n` games may start: 1 to [`LIMIT`] inclusive.
pub fn can_start(n: usize) -> Result<(), Blocked> {
match n {
0 => Err(Blocked::NoGames),
n if n > LIMIT => Err(Blocked::TooMany),
_ => Ok(()),
}
}
/// Engine judgements of one side of a game.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Counts {
pub brilliant: u32,
pub great: u32,
pub inaccuracies: u32,
pub mistakes: u32,
pub blunders: u32,
}
/// One side of an analysed game: its judgements, average centipawn loss and accuracy.
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct SideMarks {
#[serde(flatten)]
pub counts: Counts,
#[serde(default)]
pub acpl: Option<u32>,
#[serde(default)]
pub accuracy: Option<f64>,
}
impl SideMarks {
fn of(s: &game_review::SideStats) -> SideMarks {
SideMarks {
counts: Counts {
brilliant: s.brilliant,
great: s.great,
inaccuracies: s.inaccuracies,
mistakes: s.mistakes,
blunders: s.blunders,
},
acpl: s.acpl,
accuracy: s.accuracy,
}
}
}
impl Counts {
pub fn add(&mut self, o: &Counts) {
self.brilliant += o.brilliant;
self.great += o.great;
self.inaccuracies += o.inaccuracies;
self.mistakes += o.mistakes;
self.blunders += o.blunders;
}
}
/// A move of note: its ply in the main line (0: the first move), who made it, its
/// number and SAN (Latin letters, as in PGN), and whether it is brilliant (else great).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Found {
pub ply: u16,
pub white: bool,
pub number: u32,
pub san: String,
pub brilliant: bool,
}
impl Found {
/// «21.Rxf7!!», «34…Nf3+!!» in the notation of the settings.
pub fn label(&self) -> String {
let dots = if self.white { "." } else { "…" };
let mark = if self.brilliant { "!!" } else { "!" };
format!("{}{dots}{}{mark}", self.number, crate::san::show(&self.san))
}
}
/// Why a game is not analysed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Skip {
NoMoves,
Chess960,
/// A training null move (`--`) in the main line.
NullMove,
Unreadable,
}
impl Skip {
pub fn reason(self) -> &'static str {
crate::i18n::tr(match self {
Skip::NoMoves => "no moves",
Skip::Chess960 => "Chess960",
Skip::NullMove => "a null move in the main line",
Skip::Unreadable => "the game cannot be read",
})
}
}
/// What the index knows of a record.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Entry {
/// The main line's name in the analysis cache: the entry holds only while the record
/// still has this main line.
pub line: String,
#[serde(default)]
pub white: SideMarks,
#[serde(default)]
pub black: SideMarks,
/// Every !! and the first three !.
#[serde(default)]
pub found: Vec<Found>,
/// Analysed with the engine settings of before (counted as analysed).
#[serde(default)]
pub stale: bool,
#[serde(default)]
pub skip: Option<Skip>,
}
impl Entry {
/// The !! and ! of the sides that count.
pub fn of_sides(&self, white: bool, black: bool) -> Counts {
let mut c = Counts::default();
if white {
c.add(&self.white.counts);
}
if black {
c.add(&self.black.counts);
}
c
}
}
/// The record of an analysis of `game`: its marks per side and its moves of note.
pub fn entry_of(analysis: &GameAnalysis, game: &Game) -> Entry {
let report = &analysis.report;
let line = analysis.line();
let mut found = Vec::new();
let mut great = 0;
for (ply, j) in report.judgements.iter().enumerate() {
let brilliant = match j {
Some(Judgement::Brilliant) => true,
Some(Judgement::Great) if great < 3 => {
great += 1;
false
}
_ => continue,
};
let Some(&node) = line.get(ply + 1) else {
continue;
};
let (Some(m), Some((number, color))) = (game.game_move(node), game.move_number(node))
else {
continue;
};
found.push(Found {
ply: ply as u16,
white: color == chess_core::shakmaty::Color::White,
number,
san: m.san.clone(),
brilliant,
});
}
Entry {
line: analysis.cache_key(),
white: SideMarks::of(&report.white),
black: SideMarks::of(&report.black),
found,
stale: analysis.stale(),
skip: None,
}
}
/// Why `game` is not analysed, if it is not.
pub fn skip_of(game: &Game, chess960: bool) -> Option<Skip> {
if chess960 {
return Some(Skip::Chess960);
}
let line = game.mainline();
if line.is_empty() {
return Some(Skip::NoMoves);
}
if line
.iter()
.any(|id| game.game_move(*id).is_none_or(|m| m.is_null()))
{
return Some(Skip::NullMove);
}
None
}
#[derive(Serialize, Deserialize)]
struct IndexFile {
version: u32,
records: Vec<(u32, Entry)>,
}
const INDEX_VERSION: u32 = 1;
/// The marks of a database's records, by record number, in a file of their own. No
/// file (or one of another version) is an empty index: nothing found yet.
#[derive(Debug, Default)]
pub struct Index {
file: Option<PathBuf>,
records: BTreeMap<u32, Entry>,
/// Changes since it was read (the table re-checks what it shows).
pub generation: u64,
}
/// The folder of the indexes: beside the analysis cache.
fn index_dir() -> Option<PathBuf> {
crate::settings::Settings::cache_dir().map(|d| d.join("marks"))
}
/// The index file of the database at `db`: named by a hash of its real path.
pub fn index_file(dir: &Path, db: &Path) -> PathBuf {
let real = std::fs::canonicalize(db).unwrap_or_else(|_| db.to_path_buf());
let mut h = std::collections::hash_map::DefaultHasher::new();
real.hash(&mut h);
dir.join(format!("{:016x}.json", h.finish()))
}
impl Index {
/// The index of the database at `db`, from the program's cache.
pub fn of(db: &Path) -> Index {
match index_dir() {
Some(dir) => Index::load(index_file(&dir, db)),
None => Index::default(),
}
}
/// The index kept in `file`.
pub fn load(file: PathBuf) -> Index {
let records = match crate::platform::fs::read(&file) {
Ok(bytes) => match serde_json::from_slice::<IndexFile>(&bytes) {
Ok(f) if f.version == INDEX_VERSION => f.records.into_iter().collect(),
Ok(f) => {
eprintln!("marks: {}: version {}", file.display(), f.version);
BTreeMap::new()
}
Err(e) => {
eprintln!("marks: {}: {e}", file.display());
BTreeMap::new()
}
},
Err(_) => BTreeMap::new(),
};
Index {
file: Some(file),
records,
generation: 0,
}
}
pub fn get(&self, record: u32) -> Option<&Entry> {
self.records.get(&record)
}
pub fn set(&mut self, record: u32, entry: Entry) {
self.records.insert(record, entry);
self.generation += 1;
}
/// 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 {
match self.records.get(&record) {
None => 4,
Some(e) if e.skip.is_some() => 3,
Some(e) => {
let c = e.of_sides(true, true);
if c.brilliant > 0 {
0
} else if c.great > 0 {
1
} else {
2
}
}
}
}
#[cfg(test)]
pub fn len(&self) -> usize {
self.records.len()
}
/// Written whole into a temporary file and renamed over the old one.
pub fn save(&self) -> std::io::Result<()> {
let Some(file) = &self.file else {
return Ok(());
};
let out = IndexFile {
version: INDEX_VERSION,
records: self.records.iter().map(|(r, e)| (*r, e.clone())).collect(),
};
let bytes = serde_json::to_vec(&out).map_err(std::io::Error::other)?;
if let Some(dir) = file.parent() {
std::fs::create_dir_all(dir)?;
}
crate::platform::fs::write_atomic(file, &bytes)
}
}
/// How far a pass is: games done, and the record being analysed (its number + 1; 0:
/// none).
#[derive(Debug, Default)]
pub struct Progress {
pub done: AtomicUsize,
pub current: AtomicUsize,
}
/// How a pass ended.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Outcome {
Finished,
Stopped,
EngineFailed(String),
SourceChanged,
}
/// What a pass did.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PassEnd {
pub outcome: Outcome,
/// Records analysed (by the engine now, or from the cache), in the pass's order.
pub reviewed: Vec<u32>,
/// Of them, taken from the cache without the engine.
pub from_cache: usize,
pub skipped: Vec<(u32, Skip)>,
}
/// The engine of a pass: started for each game that needs it.
pub struct Engine {
pub config: uci::EngineConfig,
pub settings: JobSettings,
/// Whether the engine, by the name it gives, is still the one checked.
pub fits: Box<dyn Fn(&str) -> bool + Send + Sync>,
/// A pause before each game (`CP_BRILLIANT_SLOW`: the progress can be photographed).
pub pause: std::time::Duration,
}
/// Analyses `records` of `db` in this order: each from the analysis cache in `cache`
/// when it is there (also one made with the engine settings of before), else with
/// `engine`; after each game the cache and `index` are saved, so a stop or a crash
/// keeps what was done. `cancel` stops it within the current search.
pub fn run_pass(
db: &Database,
records: &[u32],
index: &Mutex<Index>,
cache: &dyn CacheStore,
engine: &Engine,
progress: &Progress,
cancel: &AtomicBool,
) -> PassEnd {
let mut end = PassEnd {
outcome: Outcome::Finished,
reviewed: Vec::new(),
from_cache: 0,
skipped: Vec::new(),
};
for &record in records {
if cancel.load(Ordering::Relaxed) {
end.outcome = Outcome::Stopped;
break;
}
if let Some(pgn) = db.as_pgn()
&& pgn.source_changed().unwrap_or(true)
{
end.outcome = Outcome::SourceChanged;
break;
}
progress
.current
.store(record as usize + 1, Ordering::Relaxed);
if !engine.pause.is_zero() {
std::thread::sleep(engine.pause);
if cancel.load(Ordering::Relaxed) {
end.outcome = Outcome::Stopped;
break;
}
}
let index_skip = |skip: Skip, line: String| {
if let Ok(mut i) = index.lock() {
i.set(
record,
Entry {
line,
white: SideMarks::default(),
black: SideMarks::default(),
found: Vec::new(),
stale: false,
skip: Some(skip),
},
);
let _ = i.save();
}
};
let Some(parsed) = db.load_game(record as usize) else {
end.skipped.push((record, Skip::Unreadable));
index_skip(Skip::Unreadable, String::new());
progress.done.fetch_add(1, Ordering::Relaxed);
continue;
};
let game = parsed.game;
let chess960 = db.row(record as usize).chess960;
let mut analysis = GameAnalysis::with_store(&game, Some(cache));
if let Some(skip) = skip_of(&game, chess960) {
end.skipped.push((record, skip));
index_skip(skip, analysis.cache_key());
progress.done.fetch_add(1, Ordering::Relaxed);
continue;
}
let by_engine = analysis.has_data() && analysis.meta.as_ref().is_some_and(|m| m.nodes > 0);
// An analysis with every position evaluated is one, also of the engine settings
// of before (the owner's decision; `begin` would drop that one): it is taken as
// it is, the engine is not asked.
let targets = if by_engine && (analysis.stale() || analysis.missing() == 0) {
Vec::new()
} else {
analysis.begin(&game, !by_engine)
};
if targets.is_empty() && by_engine {
end.from_cache += 1;
} else {
let mut changed = false;
let result = game_review::run(
engine.config.clone(),
&engine.settings,
targets,
cancel,
&mut |e| {
if changed {
return;
}
if let JobEvent::Engine(name) = &e
&& !(engine.fits)(name)
{
changed = true;
return;
}
analysis.apply(e);
},
);
if changed {
end.outcome = Outcome::EngineFailed(
crate::i18n::tr("the engine is no longer the Stockfish 19 that was checked")
.to_owned(),
);
break;
}
if let Err(e) = result {
end.outcome = Outcome::EngineFailed(match e {
JobFailure::Start(e) | JobFailure::Engine(e) => e,
JobFailure::Terminated(r) => crate::analysis::termination_message(&r),
});
break;
}
if cancel.load(Ordering::Relaxed) {
// The evaluations made are kept for later; the game is not counted.
let _ = analysis.save_to(cache);
end.outcome = Outcome::Stopped;
break;
}
if let Err(e) = analysis.save_to(cache) {
eprintln!("review cache: {e}");
}
}
analysis.refresh(&game);
if let Ok(mut i) = index.lock() {
i.set(record, entry_of(&analysis, &game));
if let Err(e) = i.save() {
eprintln!("marks: {e}");
}
}
end.reviewed.push(record);
progress.done.fetch_add(1, Ordering::Relaxed);
}
progress.current.store(0, Ordering::Relaxed);
end
}
/// Whose moves count in the totals of each record: the player of the filter's sides
/// (`white`, `black`), or both when the filter names no player.
#[derive(Clone, Debug, Default)]
pub struct Sides {
/// Records where the filter's player has White, and Black; `None`: no player.
pub player: Option<(Vec<u32>, Vec<u32>)>,
}
impl Sides {
fn of(&self, record: u32) -> (bool, bool) {
match &self.player {
None => (true, true),
Some((w, b)) => (
w.binary_search(&record).is_ok(),
b.binary_search(&record).is_ok(),
),
}
}
}
/// The totals of a pass: games analysed, those with !! and with !, every category
/// summed over the sides that count, the averages over those sides, and the !! moves.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Totals {
pub games: usize,
pub with_brilliant: usize,
pub with_great: usize,
pub counts: Counts,
/// Sides counted (a game counts twice when both of its sides do).
pub sides: usize,
/// Average over the sides of their average centipawn loss, and of their accuracy,
/// with the least and the greatest accuracy.
pub acpl: Option<f64>,
pub accuracy: Option<f64>,
pub accuracy_range: Option<(f64, f64)>,
/// Of them, analysed with the engine settings of before.
pub stale: usize,
/// Records where the filter's player was found on neither side.
pub player_missing: Vec<u32>,
/// Every !! of the sides that count: record and move, in the records' order.
pub brilliant: Vec<(u32, Found)>,
}
/// Sums the index's entries of `records` over the sides that count (`sides`).
pub fn totals(index: &Index, records: &[u32], sides: &Sides) -> Totals {
let mut t = Totals::default();
let (mut acpl, mut acc) = (Vec::new(), Vec::new());
for &r in records {
let Some(e) = index.get(r).filter(|e| e.skip.is_none()) else {
continue;
};
let (white, black) = sides.of(r);
if !white && !black {
t.player_missing.push(r);
}
let c = e.of_sides(white, black);
t.games += 1;
t.stale += usize::from(e.stale);
t.with_brilliant += usize::from(c.brilliant > 0);
t.with_great += usize::from(c.great > 0);
t.counts.add(&c);
for (counted, side) in [(white, &e.white), (black, &e.black)] {
if counted {
t.sides += 1;
acpl.extend(side.acpl.map(f64::from));
acc.extend(side.accuracy);
}
}
t.brilliant.extend(
e.found
.iter()
.filter(|f| f.brilliant && if f.white { white } else { black })
.map(|f| (r, f.clone())),
);
}
let mean = |v: &[f64]| (!v.is_empty()).then(|| v.iter().sum::<f64>() / v.len() as f64);
t.acpl = mean(&acpl);
t.accuracy = mean(&acc);
t.accuracy_range = acc.iter().copied().fold(None, |r: Option<(f64, f64)>, a| {
Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))
});
t
}
/// The records of `view` (a pass's list) where the filter's player has White and Black:
/// the same filter with the side fixed, for `Sides`.
pub fn player_sides(
db: &Database,
filter: &chess_import::chessbase::base::Filter,
cancel: &AtomicBool,
) -> Sides {
use chess_import::chessbase::base::{Side, SortKey};
if !filter.player.is_set() {
return Sides::default();
}
let side = |s: Side| {
let f = chess_import::chessbase::base::Filter {
side: s,
..filter.clone()
};
let mut v = db
.query(&f, SortKey::Number, true, &AtomicUsize::new(0), cancel)
.unwrap_or_default();
v.sort_unstable();
v
};
let (w, b) = match filter.side {
Side::White => (side(Side::White), Vec::new()),
Side::Black => (Vec::new(), side(Side::Black)),
_ => (side(Side::White), side(Side::Black)),
};
Sides {
player: Some((w, b)),
}
}
/// What the app keeps of a pass for its summary: the filter, the words for its
/// conditions, and the games that could not be analysed.
pub type PassMeta = (
chess_import::chessbase::base::Filter,
String,
Vec<(u32, Skip)>,
);
/// A pass under way, on a thread of its own: its source, its list, how far it is, and
/// how it ended once it did.
pub struct PassJob {
pub source: PathBuf,
pub records: Vec<u32>,
progress: std::sync::Arc<Progress>,
cancel: std::sync::Arc<AtomicBool>,
rx: std::sync::mpsc::Receiver<(PassEnd, Sides)>,
pub stopping: bool,
pub started: std::time::Instant,
}
impl PassJob {
/// Starts the pass over `records` of `db` (the database at `source`); the sides of
/// the filter's player are found first, in the same thread.
#[allow(clippy::too_many_arguments)]
pub fn start(
ctx: &egui::Context,
source: PathBuf,
db: std::sync::Arc<Database>,
records: Vec<u32>,
filter: chess_import::chessbase::base::Filter,
index: std::sync::Arc<Mutex<Index>>,
cache: PathBuf,
engine: Engine,
) -> std::io::Result<PassJob> {
let progress = std::sync::Arc::new(Progress::default());
let cancel = std::sync::Arc::new(AtomicBool::new(false));
let (tx, rx) = std::sync::mpsc::channel();
let (p, c, list, ctx) = (
progress.clone(),
cancel.clone(),
records.clone(),
ctx.clone(),
);
std::thread::Builder::new()
.name("brilliant-search".into())
.spawn(move || {
let sides = player_sides(&db, &filter, &c);
let store = crate::review::Cache(cache);
let end = run_pass(&db, &list, &index, &store, &engine, &p, &c);
let _ = tx.send((end, sides));
ctx.request_repaint();
})?;
Ok(PassJob {
source,
records,
progress,
cancel,
rx,
stopping: false,
started: std::time::Instant::now(),
})
}
pub fn done(&self) -> usize {
self.progress.done.load(Ordering::Relaxed)
}
/// The record being analysed.
pub fn current(&self) -> Option<u32> {
match self.progress.current.load(Ordering::Relaxed) {
0 => None,
n => Some(n as u32 - 1),
}
}
/// «Stop»: the engines stop within their search; what was analysed stays.
pub fn stop(&mut self) {
self.cancel.store(true, Ordering::Relaxed);
self.stopping = true;
}
/// How it ended, once it did.
pub fn poll(&self) -> Option<(PassEnd, Sides)> {
self.rx.try_recv().ok()
}
/// Stops and waits up to `timeout` (closing the program).
pub fn shutdown(&mut self, timeout: std::time::Duration) {
self.stop();
let _ = self.rx.recv_timeout(timeout);
}
/// The time left, from the pace so far after three games, else from the moves.
pub fn left_secs(&self, estimate: impl Fn(&[u32]) -> f64) -> f64 {
let done = self.done();
let rest = &self.records[done.min(self.records.len())..];
if done >= 3 {
self.started.elapsed().as_secs_f64() / done as f64 * rest.len() as f64
} else {
estimate(rest)
}
}
}
impl Drop for PassJob {
fn drop(&mut self) {
self.cancel.store(true, Ordering::Relaxed);
}
}
/// Seconds an engine takes for games of these many plies: one search per position and a
/// second one for some, about 0.94 s per position in one process
/// (docs/decisions.md, «Analysis with fixed parameters»), over `processes`.
pub fn estimate_secs(plies: impl Iterator<Item = usize>, processes: usize) -> f64 {
plies.map(|p| (p + 1) as f64 * 0.94).sum::<f64>() / processes.max(1) as f64
}
#[cfg(test)]
#[path = "brilliant_tests.rs"]
mod tests;

View file

@ -0,0 +1,346 @@
use std::sync::Arc;
use game_review::CacheDir;
use super::*;
struct Dir(PathBuf);
impl Dir {
fn new(name: &str) -> Dir {
let path = std::env::temp_dir().join(format!("cp-brilliant-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(path.join("cache")).unwrap();
Dir(path)
}
fn cache(&self) -> PathBuf {
self.0.join("cache")
}
}
impl Drop for Dir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
/// The fake engine of the `uci` crate (`cargo build -p uci --bin fake_uci`).
fn fake() -> PathBuf {
let fake = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/debug/fake_uci");
assert!(
fake.exists(),
"build the fake engine first: cargo build -p uci --bin fake_uci"
);
fake
}
fn engine_at(path: &Path) -> Engine {
Engine {
config: uci::EngineConfig {
// The second search of !! and ! (`searchmoves`), as Stockfish makes it.
args: vec!["--searchmoves".into()],
..uci::EngineConfig::new(path)
},
settings: JobSettings::new(2, None),
fits: Box::new(|_| true),
pause: std::time::Duration::ZERO,
}
}
/// An engine that cannot start: a pass that asks for it fails.
fn no_engine(dir: &Dir) -> Engine {
engine_at(&dir.0.join("no-such-engine"))
}
const GAMES: &str = "[White \"Carlsen\"]\n[Black \"Kasparov\"]\n[Result \"*\"]\n\n1. e4 e5 2. Nf3 Nc6 *\n\n\
[White \"Kasparov\"]\n[Black \"Carlsen\"]\n[Result \"*\"]\n\n1. d4 d5 2. c4 *\n\n\
[White \"Anand\"]\n[Black \"Topalov\"]\n[Result \"*\"]\n\n1. e4 -- 2. d4 *\n\n\
[White \"Anand\"]\n[Black \"Kramnik\"]\n[Result \"*\"]\n\n*\n";
fn source(dir: &Dir) -> (PathBuf, Database) {
let path = dir.0.join("games.pgn");
std::fs::write(&path, GAMES).unwrap();
let db = chess_import::open(&path, &mut |_| {}, &AtomicBool::new(false))
.unwrap()
.db;
(path, db)
}
fn pass(
db: &Database,
records: &[u32],
index: &Mutex<Index>,
dir: &Dir,
engine: &Engine,
) -> PassEnd {
let cache = dir.cache();
run_pass(
db,
records,
index,
&CacheDir(&cache),
engine,
&Progress::default(),
&AtomicBool::new(false),
)
}
#[test]
fn the_limit_is_one_hundred_games_inclusive() {
assert_eq!(can_start(0), Err(Blocked::NoGames));
assert_eq!(can_start(1), Ok(()));
assert_eq!(can_start(LIMIT), Ok(()));
assert_eq!(LIMIT, 100);
assert_eq!(can_start(101), Err(Blocked::TooMany));
}
#[test]
fn a_pass_analyses_keeps_the_index_and_takes_the_cache_next_time() {
let dir = Dir::new("pass");
let (path, db) = source(&dir);
let before = std::fs::read(&path).unwrap();
let file = dir.0.join("marks.json");
let index = Mutex::new(Index::load(file.clone()));
let end = pass(&db, &[0, 1, 2, 3], &index, &dir, &engine_at(&fake()));
assert_eq!(end.outcome, Outcome::Finished);
assert_eq!(end.reviewed, vec![0, 1]);
assert_eq!(end.from_cache, 0);
assert_eq!(end.skipped, vec![(2, Skip::NullMove), (3, Skip::NoMoves)]);
// The index lives on: read anew (as after a restart) it has every record.
let again = Index::load(file.clone());
assert_eq!(again.len(), 4);
let entry = again.get(0).unwrap();
let game = db.load_game(0).unwrap().game;
assert_eq!(entry.line, GameAnalysis::new(&game, None).cache_key());
assert_eq!(again.get(2).unwrap().skip, Some(Skip::NullMove));
// Again: from the cache, the engine is not asked (there is none now).
let index = Mutex::new(again);
let end = pass(&db, &[1, 0], &index, &dir, &no_engine(&dir));
assert_eq!(end.outcome, Outcome::Finished);
assert_eq!((end.reviewed, end.from_cache), (vec![1, 0], 2));
// The source is only read.
assert_eq!(std::fs::read(&path).unwrap(), before);
}
#[test]
fn an_analysis_of_the_settings_of_before_counts_as_made() {
let dir = Dir::new("stale");
let (_, db) = source(&dir);
let index = Mutex::new(Index::load(dir.0.join("marks.json")));
pass(&db, &[0], &index, &dir, &engine_at(&fake()));
// The cached analysis made «before the fixed search».
for f in std::fs::read_dir(dir.cache()).unwrap() {
let f = f.unwrap().path();
let mut v: serde_json::Value = serde_json::from_slice(&std::fs::read(&f).unwrap()).unwrap();
v["meta"]["search"] = serde_json::json!(0);
std::fs::write(&f, serde_json::to_vec(&v).unwrap()).unwrap();
}
let end = pass(&db, &[0], &index, &dir, &no_engine(&dir));
assert_eq!((end.outcome, end.from_cache), (Outcome::Finished, 1));
assert!(index.lock().unwrap().get(0).unwrap().stale);
}
#[test]
fn a_stopped_pass_keeps_what_it_analysed() {
let dir = Dir::new("stop");
let (_, db) = source(&dir);
let file = dir.0.join("marks.json");
let index = Mutex::new(Index::load(file.clone()));
// Stopped when the second game's engine starts: the first one is kept.
let cancel = Arc::new(AtomicBool::new(false));
let starts = Arc::new(AtomicUsize::new(0));
let (c, s) = (cancel.clone(), starts.clone());
let engine = Engine {
fits: Box::new(move |_| {
if s.fetch_add(1, Ordering::Relaxed) >= 1 {
c.store(true, Ordering::Relaxed);
}
true
}),
..engine_at(&fake())
};
let cache = dir.cache();
let end = run_pass(
&db,
&[0, 1],
&index,
&CacheDir(&cache),
&engine,
&Progress::default(),
&cancel,
);
assert_eq!(end.outcome, Outcome::Stopped);
assert_eq!(end.reviewed, vec![0]);
let kept = Index::load(file);
assert!(kept.get(0).is_some() && kept.get(1).is_none());
}
#[test]
fn starting_and_stopping_at_once_never_breaks_the_index() {
let dir = Dir::new("race");
let (_, db) = source(&dir);
let db = Arc::new(db);
let file = dir.0.join("marks.json");
for round in 0..10 {
let index = Arc::new(Mutex::new(Index::load(file.clone())));
let cancel = Arc::new(AtomicBool::new(false));
let (d, i, c, cache) = (db.clone(), index.clone(), cancel.clone(), dir.cache());
let worker = std::thread::spawn(move || {
run_pass(
&d,
&[0, 1, 2, 3],
&i,
&CacheDir(&cache),
&engine_at(&fake()),
&Progress::default(),
&c,
)
});
if round % 2 == 0 {
std::thread::yield_now();
}
cancel.store(true, Ordering::Relaxed);
let end = worker.join().unwrap();
assert!(matches!(end.outcome, Outcome::Stopped | Outcome::Finished));
// The file is whole: it reads.
if file.exists() {
let bytes = std::fs::read(&file).unwrap();
assert!(serde_json::from_slice::<serde_json::Value>(&bytes).is_ok());
}
}
}
#[test]
fn a_broken_index_is_an_empty_one() {
let dir = Dir::new("broken");
let file = dir.0.join("marks.json");
std::fs::write(&file, b"{not json").unwrap();
assert_eq!(Index::load(file.clone()).len(), 0);
std::fs::write(&file, br#"{"version":99,"records":[]}"#).unwrap();
assert_eq!(Index::load(file).len(), 0);
assert_eq!(Index::load(dir.0.join("none.json")).len(), 0);
}
fn side(brilliant: u32, great: u32, blunders: u32, acpl: u32, accuracy: f64) -> SideMarks {
SideMarks {
counts: Counts {
brilliant,
great,
blunders,
..Counts::default()
},
acpl: Some(acpl),
accuracy: Some(accuracy),
}
}
fn found(ply: u16, white: bool, number: u32, san: &str, brilliant: bool) -> Found {
Found {
ply,
white,
number,
san: san.into(),
brilliant,
}
}
#[test]
fn totals_count_only_the_filters_players_moves() {
let mut index = Index::default();
let entry = |white, black, found| Entry {
line: String::new(),
white,
black,
found,
stale: false,
skip: None,
};
index.set(
0,
entry(
side(1, 0, 0, 20, 90.0),
side(0, 2, 1, 40, 70.0),
vec![
found(40, true, 21, "Rxf7", true),
found(13, false, 7, "Nd5", false),
],
),
);
index.set(
1,
entry(
side(0, 0, 3, 60, 60.0),
side(1, 1, 0, 10, 96.0),
vec![found(67, false, 34, "Nf3+", true)],
),
);
index.set(
2,
Entry {
skip: Some(Skip::Chess960),
..entry(SideMarks::default(), SideMarks::default(), Vec::new())
},
);
let all = totals(&index, &[0, 1, 2], &Sides::default());
assert_eq!(
(all.games, all.sides, all.with_brilliant, all.with_great),
(2, 4, 2, 2)
);
assert_eq!(
all.counts,
Counts {
brilliant: 2,
great: 3,
blunders: 4,
..Counts::default()
}
);
assert_eq!(all.acpl, Some(32.5));
assert_eq!(all.accuracy_range, Some((60.0, 96.0)));
assert_eq!(all.brilliant.len(), 2);
// The player had White in game 0 and Black in game 1: only those sides count.
let player = Sides {
player: Some((vec![0], vec![1])),
};
let t = totals(&index, &[0, 1, 2], &player);
assert_eq!(
(t.games, t.sides, t.with_brilliant, t.with_great),
(2, 2, 2, 1)
);
assert_eq!(t.counts.blunders, 0);
assert_eq!(t.acpl, Some(15.0));
assert_eq!(t.accuracy, Some(93.0));
assert_eq!(
t.brilliant
.iter()
.map(|(r, f)| (*r, f.label()))
.collect::<Vec<_>>(),
[(0, "21.Rxf7!!".to_owned()), (1, "34…Nf3+!!".to_owned())]
);
// A player found on neither side is said.
let nobody = Sides {
player: Some((Vec::new(), Vec::new())),
};
assert_eq!(totals(&index, &[0, 1], &nobody).player_missing, vec![0, 1]);
}
#[test]
fn the_players_sides_come_from_the_filter() {
let dir = Dir::new("sides");
let (_, 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));
assert_eq!(sides.player, Some((vec![0], vec![1])));
assert!(
player_sides(&db, &Default::default(), &AtomicBool::new(false))
.player
.is_none()
);
}

View file

@ -194,13 +194,13 @@ pub struct Review {
}
/// The folder of the cached analyses.
fn cache_dir() -> Option<PathBuf> {
pub fn cache_dir() -> Option<PathBuf> {
Settings::cache_dir().map(|d| d.join("analysis"))
}
/// The cached analyses in the program's files (the platform's: a folder of the disk, or
/// the browser's storage).
struct Cache(PathBuf);
pub struct Cache(pub PathBuf);
impl game_review::CacheStore for Cache {
fn read(&self, name: &str) -> Option<Vec<u8>> {
@ -870,7 +870,7 @@ pub const DEFAULT_NPS: f64 = 700_000.0;
/// ChessPenguin's Stockfish at `path` for an analysis job: below the program (nice 10),
/// so that the program stays smooth while every CPU works.
#[cfg(not(web))]
fn engine_config(path: &Path) -> uci::EngineConfig {
pub fn engine_config(path: &Path) -> uci::EngineConfig {
uci::EngineConfig {
nice: 10,
..uci::EngineConfig::new(path)
@ -936,7 +936,7 @@ pub fn judgement_name(j: Judgement) -> &'static str {
}
}
fn judgement_plural(j: Judgement) -> &'static str {
pub fn judgement_plural(j: Judgement) -> &'static str {
match j {
Judgement::Inaccuracy => tr("Inaccuracies"),
Judgement::Mistake => tr("Mistakes"),
@ -947,7 +947,7 @@ fn judgement_plural(j: Judgement) -> &'static str {
}
/// Accuracy in the colors of the move marks: green, blue, orange, red.
fn accuracy_color(a: f64, dark: bool) -> Color32 {
pub fn accuracy_color(a: f64, dark: bool) -> Color32 {
let j = if a >= 85.0 {
Judgement::Great
} else if a >= 70.0 {

View file

@ -585,6 +585,12 @@ impl GameAnalysis {
// ----- cache -----------------------------------------------------------------
/// The name of the cache file of this main line.
/// The name of this main line's file in the cache: equal for equal main lines (the
/// same positions), whatever the rest of the game.
pub fn cache_key(&self) -> String {
self.cache_name()
}
fn cache_name(&self) -> String {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for k in &self.keys {