Sample databases on the first start: 38 classic games and Alekhine's games
The library is no longer empty at the first start: Classic games (the four annotated samples plus 34 famous games from Steinitz to Carlsen, each with a short introduction of our own) and Alekhine (267 games, moves only, the book's commentary removed). They are built in, written once to the data directory and put on the shelf; a sample removed from the shelf does not come back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
97d38ec919
commit
673f447491
7 changed files with 5530 additions and 3 deletions
|
|
@ -97,7 +97,8 @@ impl App {
|
|||
let mut settings = Settings::load();
|
||||
crate::i18n::set_lang(settings.language);
|
||||
crate::engine_download::clean_leftovers();
|
||||
if crate::engines::first_run_setup(&mut settings) {
|
||||
let engines_found = crate::engines::first_run_setup(&mut settings);
|
||||
if crate::examples::first_run_setup(&mut settings) || engines_found {
|
||||
let _ = settings.save();
|
||||
}
|
||||
// PGN files passed on the command line (e.g. "Open with…") join the library, first one selected.
|
||||
|
|
|
|||
74
apps/desktop/src/examples.rs
Normal file
74
apps/desktop/src/examples.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
//! Sample databases for the first start, so the library is not empty: classic games
|
||||
//! with short introductions, and Alekhine's games (moves only). They are written once
|
||||
//! to `<local data dir>/examples/` and put on the library shelf; a sample removed from
|
||||
//! the shelf does not come back.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::settings::Settings;
|
||||
|
||||
const FILES: [(&str, &str); 2] = [
|
||||
("Classic games.pgn", include_str!("../../../examples/classics.pgn")),
|
||||
("Alekhine.pgn", include_str!("../../../examples/alekhine.pgn")),
|
||||
];
|
||||
|
||||
fn dir() -> Option<PathBuf> {
|
||||
directories::ProjectDirs::from("io.github", "vrubelroman", "ChessPenguin")
|
||||
.map(|d| d.data_local_dir().join("examples"))
|
||||
}
|
||||
|
||||
/// Adds the samples once; `true` when the settings changed. A new library starts with
|
||||
/// them (the classics open); an existing one gets them after its own databases.
|
||||
pub fn first_run_setup(settings: &mut Settings) -> bool {
|
||||
if settings.examples_added {
|
||||
return false;
|
||||
}
|
||||
settings.examples_added = true;
|
||||
let Some(dir) = dir() else {
|
||||
return true;
|
||||
};
|
||||
if let Err(e) = std::fs::create_dir_all(&dir) {
|
||||
eprintln!("examples: cannot create {}: {e}", dir.display());
|
||||
return true;
|
||||
}
|
||||
for (name, text) in FILES {
|
||||
let path = dir.join(name);
|
||||
if !path.exists()
|
||||
&& let Err(e) = std::fs::write(&path, text)
|
||||
{
|
||||
eprintln!("examples: cannot write {}: {e}", path.display());
|
||||
continue;
|
||||
}
|
||||
if !settings.databases.contains(&path) {
|
||||
settings.databases.push(path);
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn samples_are_valid_pgn() {
|
||||
for (name, text) in FILES {
|
||||
let dir = std::env::temp_dir().join(format!("cp-examples-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join(name);
|
||||
std::fs::write(&path, text).unwrap();
|
||||
let db = chess_core::PgnDatabase::open(
|
||||
&path,
|
||||
&mut |_| {},
|
||||
&std::sync::atomic::AtomicBool::new(false),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(db.len() >= 30, "{name}: {} games", db.len());
|
||||
for i in 0..db.len() {
|
||||
let parsed = db.load_game(i).unwrap();
|
||||
assert!(parsed.diagnostics.is_empty(), "{name} #{}: {:?}", i + 1, parsed.diagnostics);
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ mod board_search;
|
|||
mod engine_download;
|
||||
mod engine_offer;
|
||||
mod engines;
|
||||
mod examples;
|
||||
mod game_view;
|
||||
mod i18n;
|
||||
mod large_db;
|
||||
|
|
|
|||
|
|
@ -82,6 +82,8 @@ pub struct Settings {
|
|||
pub review: ReviewSettings,
|
||||
/// The first-start engine discovery has run (see `engines::first_run_setup`).
|
||||
pub engines_discovered: bool,
|
||||
/// The sample databases were put on the shelf (see `examples::first_run_setup`).
|
||||
pub examples_added: bool,
|
||||
}
|
||||
|
||||
/// Options of the whole-game analysis (the engine is the active one).
|
||||
|
|
@ -134,6 +136,7 @@ impl Default for Settings {
|
|||
databases: Vec::new(),
|
||||
review: ReviewSettings::default(),
|
||||
engines_discovered: false,
|
||||
examples_added: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue