Opponent prep: which games by period or number, saved preparations
The report takes all the player's games, those of the last 1/2/5 years before his newest game, or his last 50/100/200 of each colour; every choice shows how many games it takes. Undated games count only with all. «💾 Save» keeps a preparation with its errors in the program's data; «📑 Preparations» in the library opens, renames and deletes them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1ac5645ef7
commit
57fa98241b
12 changed files with 1334 additions and 37 deletions
|
|
@ -31,6 +31,7 @@ mod book;
|
|||
mod errors;
|
||||
mod games;
|
||||
pub mod openings;
|
||||
mod period;
|
||||
mod players;
|
||||
mod tree;
|
||||
|
||||
|
|
@ -40,6 +41,7 @@ pub use errors::{EXACT, ErrorCounts, OpeningError, PositionCache, PositionEval,
|
|||
pub use game_review::{Eval, Judgement};
|
||||
pub use games::{BookExit, GameInfo, Outcome, Tally, move_label};
|
||||
pub use openings::Opening;
|
||||
pub use period::{Chosen, Period, PoolGame};
|
||||
pub use players::{Candidate, Player, candidates, games_of};
|
||||
pub use tree::{Node, Tree};
|
||||
|
||||
|
|
@ -55,6 +57,8 @@ pub struct Options {
|
|||
pub plies: usize,
|
||||
/// The player's colour(s).
|
||||
pub side: Side,
|
||||
/// Which of his games.
|
||||
pub period: Period,
|
||||
}
|
||||
|
||||
impl Default for Options {
|
||||
|
|
@ -62,6 +66,7 @@ impl Default for Options {
|
|||
Options {
|
||||
plies: DEFAULT_PLIES,
|
||||
side: Side::Any,
|
||||
period: Period::All,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -138,7 +143,8 @@ pub struct BookInfo {
|
|||
pub left: u32,
|
||||
}
|
||||
|
||||
const REPORT_VERSION: u32 = 3;
|
||||
// 4: the period and the pool of the player's games.
|
||||
const REPORT_VERSION: u32 = 4;
|
||||
|
||||
/// Everything the preparation knows about the player.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
|
|
@ -152,6 +158,13 @@ pub struct PrepReport {
|
|||
pub plies: usize,
|
||||
pub side_white: bool,
|
||||
pub side_black: bool,
|
||||
/// Which of the player's games the report took.
|
||||
#[serde(default)]
|
||||
pub period: Period,
|
||||
/// All the player's games of the side(s), before the period's choice (for the
|
||||
/// counts of the other periods).
|
||||
#[serde(default)]
|
||||
pub pool: Vec<PoolGame>,
|
||||
pub games: Vec<GameInfo>,
|
||||
/// Games left out: from a set-up position, or unreadable.
|
||||
pub skipped: u32,
|
||||
|
|
@ -224,7 +237,16 @@ pub fn build(
|
|||
cancel: &AtomicBool,
|
||||
) -> Option<PrepReport> {
|
||||
let found = games_of(db, player, options.side, cancel)?;
|
||||
let collected = games::collect(db, &found, options.plies, progress, cancel)?;
|
||||
let pool: Vec<PoolGame> = found
|
||||
.iter()
|
||||
.map(|&(index, white)| PoolGame {
|
||||
index,
|
||||
white,
|
||||
date: chess_import::pgn_catalog::date_number(&db.row(index as usize).date),
|
||||
})
|
||||
.collect();
|
||||
let chosen = period::choose(&pool, options.period);
|
||||
let collected = games::collect(db, &chosen, options.plies, progress, cancel)?;
|
||||
let mut openings = collected.openings;
|
||||
let games = collected.games;
|
||||
let mut evals = HashMap::new();
|
||||
|
|
@ -270,6 +292,8 @@ pub fn build(
|
|||
plies: options.plies,
|
||||
side_white: options.side != Side::Black,
|
||||
side_black: options.side != Side::White,
|
||||
period: options.period,
|
||||
pool,
|
||||
games,
|
||||
skipped: collected.skipped,
|
||||
openings: openings.list,
|
||||
|
|
@ -354,6 +378,20 @@ impl PrepReport {
|
|||
}
|
||||
}
|
||||
|
||||
/// The options the report was built with.
|
||||
pub fn options(&self) -> Options {
|
||||
Options {
|
||||
plies: self.plies,
|
||||
side: self.side(),
|
||||
period: self.period,
|
||||
}
|
||||
}
|
||||
|
||||
/// How many games `period` would take of the colour(s) `side`.
|
||||
pub fn count(&self, period: Period, side: Side) -> Chosen {
|
||||
period::count(&self.pool, period, side)
|
||||
}
|
||||
|
||||
pub fn color(&self, white: bool) -> &ColorReport {
|
||||
if white { &self.white } else { &self.black }
|
||||
}
|
||||
|
|
@ -564,6 +602,18 @@ impl PrepReport {
|
|||
Side::White => 1,
|
||||
Side::Black => 2,
|
||||
});
|
||||
// All the games: as before periods were chosen (the reports cached then).
|
||||
match options.period {
|
||||
Period::All => {}
|
||||
Period::Months(m) => {
|
||||
bytes.push(1);
|
||||
bytes.extend(u32::from(m).to_le_bytes());
|
||||
}
|
||||
Period::Last(n) => {
|
||||
bytes.push(2);
|
||||
bytes.extend(n.to_le_bytes());
|
||||
}
|
||||
}
|
||||
dir.join(format!("{:016x}.json", fnv(bytes.iter())))
|
||||
}
|
||||
|
||||
|
|
@ -581,7 +631,8 @@ impl PrepReport {
|
|||
&& report.fingerprint == fp
|
||||
&& report.player == *player
|
||||
&& report.plies == options.plies
|
||||
&& report.side() == options.side;
|
||||
&& report.side() == options.side
|
||||
&& report.period == options.period;
|
||||
if !same {
|
||||
return None;
|
||||
}
|
||||
|
|
@ -598,10 +649,7 @@ impl PrepReport {
|
|||
|
||||
/// Keeps the report in `dir` (atomically).
|
||||
pub fn save(&self, dir: &Path) -> std::io::Result<()> {
|
||||
let options = Options {
|
||||
plies: self.plies,
|
||||
side: self.side(),
|
||||
};
|
||||
let options = self.options();
|
||||
let path = Self::cache_path(dir, self.fingerprint, &self.player, &options);
|
||||
let bytes = serde_json::to_vec(self).map_err(std::io::Error::other)?;
|
||||
errors::write_atomic(&path, &bytes)
|
||||
|
|
@ -816,6 +864,7 @@ mod tests {
|
|||
&Options {
|
||||
plies: 4,
|
||||
side: Side::White,
|
||||
period: Period::All,
|
||||
},
|
||||
&AtomicUsize::new(0),
|
||||
&never,
|
||||
|
|
@ -825,6 +874,26 @@ mod tests {
|
|||
assert!(white_only.black.games.is_empty());
|
||||
assert!(white_only.games.iter().all(|g| g.ucis.len() <= 4));
|
||||
assert_eq!(white_only.side(), Side::White);
|
||||
// The last two months before his newest game (2026.04.01): from 2026.02.01.
|
||||
let recent = build(
|
||||
&db,
|
||||
&player,
|
||||
&Options {
|
||||
period: Period::Months(2),
|
||||
..Options::default()
|
||||
},
|
||||
&AtomicUsize::new(0),
|
||||
&never,
|
||||
)
|
||||
.expect("report");
|
||||
let dates: Vec<&str> = recent.games.iter().map(|g| g.date.as_str()).collect();
|
||||
assert_eq!(dates, ["2026.02.11", "2026.03.12", "2026.04.01"]);
|
||||
// The pool: every game of his, a set-up one too (skipped from the report).
|
||||
let all = report.games.len() + report.skipped as usize;
|
||||
assert_eq!(recent.pool.len(), all);
|
||||
assert_eq!(recent.count(Period::All, Side::Any).games, all);
|
||||
assert_eq!(recent.count(Period::Months(2), Side::Any).games, 3);
|
||||
assert_eq!(recent.count(Period::Last(1), Side::White).games, 1);
|
||||
// Cancelled.
|
||||
let cancelled = AtomicBool::new(true);
|
||||
assert!(
|
||||
|
|
@ -1037,7 +1106,8 @@ mod tests {
|
|||
&player,
|
||||
&Options {
|
||||
plies: 10,
|
||||
side: Side::Any
|
||||
side: Side::Any,
|
||||
period: Period::All,
|
||||
}
|
||||
)
|
||||
.is_none()
|
||||
|
|
|
|||
165
crates/prep/src/period.rs
Normal file
165
crates/prep/src/period.rs
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
//! Which of the player's games a report takes: all, those of the last months (counted
|
||||
//! back from his newest game, so an old database is not left empty), or his last games
|
||||
//! of each colour. Games without a date are taken by «all» only; by number they count
|
||||
//! as the oldest, in the order of the database (so a database without dates gives its
|
||||
//! last records).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use chess_import::chessbase::base::Side;
|
||||
|
||||
/// The games a report takes.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum Period {
|
||||
#[default]
|
||||
All,
|
||||
/// Played within this many months before the player's newest game.
|
||||
Months(u16),
|
||||
/// The newest this many games of each colour.
|
||||
Last(u32),
|
||||
}
|
||||
|
||||
/// One game of the player before the choice.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PoolGame {
|
||||
/// Record index in the database.
|
||||
pub index: u32,
|
||||
pub white: bool,
|
||||
/// `yyyymmdd` (month or day 0 when unknown); 0: no date.
|
||||
pub date: u32,
|
||||
}
|
||||
|
||||
/// What a period takes of the games of the colour(s) looked at.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Chosen {
|
||||
pub games: usize,
|
||||
/// Of all the games: undated ones a period by months leaves out.
|
||||
pub undated_out: usize,
|
||||
}
|
||||
|
||||
/// The newest date of the games (0: none dated).
|
||||
pub fn newest(pool: &[PoolGame]) -> u32 {
|
||||
pool.iter().map(|g| g.date).max().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// The first date within `months` before `newest` (`yyyymmdd`).
|
||||
fn cutoff(newest: u32, months: u16) -> u32 {
|
||||
let (y, month, d) = (newest / 10000, newest / 100 % 100, newest % 100);
|
||||
let total = (y * 12 + month.max(1) - 1).saturating_sub(u32::from(months));
|
||||
let (y, m) = (total / 12, total % 12 + 1);
|
||||
// A newest date without a month: the whole year it was in.
|
||||
let (m, d) = if month == 0 { (0, 0) } else { (m, d) };
|
||||
y * 10000 + m * 100 + d
|
||||
}
|
||||
|
||||
/// The games `period` takes: (record, the player had White), in record order.
|
||||
pub fn choose(pool: &[PoolGame], period: Period) -> Vec<(u32, bool)> {
|
||||
let mut out: Vec<(u32, bool)> = match period {
|
||||
Period::All => pool.iter().map(|g| (g.index, g.white)).collect(),
|
||||
Period::Months(months) => {
|
||||
let newest = newest(pool);
|
||||
if newest == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let from = cutoff(newest, months);
|
||||
pool.iter()
|
||||
.filter(|g| g.date != 0 && g.date >= from)
|
||||
.map(|g| (g.index, g.white))
|
||||
.collect()
|
||||
}
|
||||
Period::Last(n) => {
|
||||
let mut out = Vec::new();
|
||||
for white in [true, false] {
|
||||
let mut games: Vec<&PoolGame> = pool.iter().filter(|g| g.white == white).collect();
|
||||
// Newest first; the undated (0) last, later records counted newer.
|
||||
games.sort_by(|a, b| b.date.cmp(&a.date).then(b.index.cmp(&a.index)));
|
||||
out.extend(games.iter().take(n as usize).map(|g| (g.index, g.white)));
|
||||
}
|
||||
out
|
||||
}
|
||||
};
|
||||
out.sort_unstable();
|
||||
out
|
||||
}
|
||||
|
||||
/// How many games of `side` `period` takes, and the undated ones it leaves out.
|
||||
pub fn count(pool: &[PoolGame], period: Period, side: Side) -> Chosen {
|
||||
let of_side = |white: bool| match side {
|
||||
Side::Any => true,
|
||||
Side::White => white,
|
||||
Side::Black => !white,
|
||||
};
|
||||
let games = choose(pool, period)
|
||||
.iter()
|
||||
.filter(|(_, w)| of_side(*w))
|
||||
.count();
|
||||
let undated_out = match period {
|
||||
Period::Months(_) => pool
|
||||
.iter()
|
||||
.filter(|g| g.date == 0 && of_side(g.white))
|
||||
.count(),
|
||||
_ => 0,
|
||||
};
|
||||
Chosen { games, undated_out }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn g(index: u32, white: bool, date: u32) -> PoolGame {
|
||||
PoolGame { index, white, date }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn months_count_back_from_the_newest_game() {
|
||||
let pool = [
|
||||
g(0, true, 20200105),
|
||||
g(1, false, 20240301),
|
||||
g(2, true, 20250228),
|
||||
g(3, true, 20250301),
|
||||
g(4, false, 0),
|
||||
g(5, true, 20260301),
|
||||
];
|
||||
assert_eq!(cutoff(20260301, 12), 20250301);
|
||||
assert_eq!(cutoff(20260115, 1), 20251215);
|
||||
assert_eq!(cutoff(20260000, 24), 20240000);
|
||||
assert_eq!(
|
||||
choose(&pool, Period::Months(12)),
|
||||
vec![(3, true), (5, true)]
|
||||
);
|
||||
assert_eq!(
|
||||
choose(&pool, Period::Months(24)),
|
||||
vec![(1, false), (2, true), (3, true), (5, true)]
|
||||
);
|
||||
assert_eq!(choose(&pool, Period::All).len(), 6);
|
||||
let c = count(&pool, Period::Months(24), Side::Black);
|
||||
assert_eq!((c.games, c.undated_out), (1, 1));
|
||||
assert_eq!(count(&pool, Period::All, Side::Any).undated_out, 0);
|
||||
// Nothing dated: a period takes nothing.
|
||||
assert!(choose(&[g(0, true, 0)], Period::Months(12)).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_last_games_of_each_colour() {
|
||||
let pool = [
|
||||
g(0, true, 20200101),
|
||||
g(1, true, 0),
|
||||
g(2, true, 20230101),
|
||||
g(3, false, 20240101),
|
||||
g(4, true, 20220101),
|
||||
g(5, false, 20190101),
|
||||
];
|
||||
assert_eq!(
|
||||
choose(&pool, Period::Last(2)),
|
||||
vec![(2, true), (3, false), (4, true), (5, false)]
|
||||
);
|
||||
// Without dates: the last records.
|
||||
let undated = [g(0, true, 0), g(1, true, 0), g(2, true, 0)];
|
||||
assert_eq!(
|
||||
choose(&undated, Period::Last(2)),
|
||||
vec![(1, true), (2, true)]
|
||||
);
|
||||
assert_eq!(count(&pool, Period::Last(1), Side::Any).games, 2);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue