chessbaseLinux/crates/prep/src/players.rs
vrubelroman d54f207889 Opponent prep: the prep crate (openings only)
A new crate shared by the apps, no UI: what a player of a database plays
in the opening and where in the opening he goes wrong.

- Player: candidates from the database's names (initials with a full
  stop work), one or several ids, White, Black or both.
- Lines: the first N plies (default 20) of the main lines, position keys,
  UCI/SAN, result from the player's side, full date, opponent's Elo;
  games from a set-up position are skipped.
- Repertoire tree per colour by move order with position keys:
  games (+/=/-), last date, average opponent Elo, game list,
  transpositions marked.
- Opening names from lichess-org/chess-openings (CC0; revision and
  SHA-256 pinned, files in crates/prep/data, THIRD_PARTY_NOTICES): the
  deepest named position of a line, families and variations with their
  first moves, grouped by name.
- Book: a second database (or the same one without the player's games)
  says where each game left it (first position with fewer than T games,
  T = 5) and whose move it was.
- Engine: every distinct position of the lines once through
  game_review::run, judged with the game analysis rules for the player's
  moves only; errors by opening and the repeated ones (same position,
  same move in two or more games); evaluations cached per position across
  reports, the report cached by database, player and plies.
- examples/prep_report.rs prints it all for a database and a player.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-28 12:32:39 +03:00

92 lines
3.1 KiB
Rust

//! The player: picked from the names of the database (never free text, so that
//! "Вафин" offers "Вафин Радель · 271" and "Вафина Раяна · 10" and the user chooses),
//! as one or several name ids when the database spells the same person in several
//! ways ("Kasparov, G." and "Kasparov, Garry"), and the player's games by colour.
use std::sync::atomic::{AtomicBool, AtomicUsize};
use chess_import::Database;
use chess_import::chessbase::base::{Filter, NameFilter, Side, SortKey};
use serde::{Deserialize, Serialize};
/// A name of the database that matches a query, with its number of games.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Candidate {
pub id: u32,
pub name: String,
pub games: u32,
}
/// Up to `limit` names containing every word of `query` (an initial with its full
/// stop finds the full name), names starting with the first word first, then by
/// number of games; and the number of all matches.
pub fn candidates(db: &Database, query: &str, limit: usize) -> (Vec<Candidate>, usize) {
let (list, total) = db.suggest_players(query, limit);
let list = list
.into_iter()
.map(|s| Candidate {
id: s.id,
name: s.name,
games: s.games,
})
.collect();
(list, total)
}
/// The player of a report: one or more name ids of the database.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Player {
pub ids: Vec<u32>,
}
impl Player {
pub fn new(ids: impl IntoIterator<Item = u32>) -> Player {
let mut ids: Vec<u32> = ids.into_iter().collect();
ids.sort_unstable();
ids.dedup();
Player { ids }
}
/// The names behind the ids, as the game list shows them.
pub fn names(&self, db: &Database) -> Vec<String> {
self.ids
.iter()
.map(|&id| db.player_name(id).to_owned())
.collect()
}
}
/// The player's games for `side`: (record index, the player had White), in record
/// order. `None` when cancelled.
pub fn games_of(
db: &Database,
player: &Player,
side: Side,
cancel: &AtomicBool,
) -> Option<Vec<(u32, bool)>> {
let mut out = Vec::new();
for &id in &player.ids {
for (s, white) in [(Side::White, true), (Side::Black, false)] {
if side != Side::Any && side != s {
continue;
}
let filter = Filter {
player: NameFilter {
text: String::new(),
exact: Some(id),
},
side: s,
games_only: true,
..Filter::default()
};
let found = db.query(&filter, SortKey::Number, true, &AtomicUsize::new(0), cancel)?;
out.extend(found.into_iter().map(|i| (i, white)));
}
}
// The same record with the same colour twice: two ids of the player on one side
// cannot happen, but two ids on both sides of one game can (a game against
// oneself in a spelling-merged list): it is kept once per colour.
out.sort_unstable();
out.dedup();
Some(out)
}