2026-10-28 19:46:45 +03:00
|
|
|
//! Sample databases of the program, so the library is not empty: classic games with
|
|
|
|
|
//! short introductions, and Alekhine's games (moves only). At the first start they are
|
|
|
|
|
//! written, named in the interface language, to `<local data dir>/examples/` and put on
|
|
|
|
|
//! the library shelf; a sample removed from the shelf does not come back.
|
|
|
|
|
//!
|
|
|
|
|
//! The samples are read only, so their files are the program's: at every start a file
|
|
|
|
|
//! that is not the text built into this version (an older version of the sample, a file
|
|
|
|
|
//! damaged or edited outside) is replaced by it, whether on the shelf or not. Nothing
|
|
|
|
|
//! keeps a sample by its place in the file: the counts of the cards are of the file's
|
|
|
|
|
//! size and time, the marks of viewed games of the games' text.
|
2026-10-19 12:21:09 +03:00
|
|
|
|
2026-10-28 19:46:45 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
2026-10-19 12:21:09 +03:00
|
|
|
|
2026-10-28 19:46:45 +03:00
|
|
|
use crate::i18n::{all_languages, tr};
|
2026-10-19 12:21:09 +03:00
|
|
|
use crate::settings::Settings;
|
|
|
|
|
|
2026-10-28 19:46:45 +03:00
|
|
|
/// The samples: the English name (a key of the translations) and the text.
|
|
|
|
|
const SAMPLES: [(&str, &str); 2] = [
|
|
|
|
|
(
|
|
|
|
|
"Classic games",
|
|
|
|
|
include_str!("../../../examples/classics.pgn"),
|
|
|
|
|
),
|
|
|
|
|
("Alekhine", include_str!("../../../examples/alekhine.pgn")),
|
|
|
|
|
];
|
2026-10-19 12:21:09 +03:00
|
|
|
|
|
|
|
|
fn dir() -> Option<PathBuf> {
|
Web version, stage 1: the interface as a library over a platform layer; apps/web
apps/desktop becomes a library (chesspenguin_app: the interface) and a thin
binary; apps/web starts the same interface in a page's canvas (eframe's
WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser.
The platform layer (apps/desktop/src/platform): the program's folders, files,
background work, the files the user opens or saves, the clipboard and what
the platform offers (CAPS). On a computer it is the system as before; in a
browser the program's data lives in IndexedDB behind an in-memory file tree
loaded before the start, the files the user opens are read into it for the
session (only read), background jobs run on the page's thread after the
frame, the clipboard is read through the browser's API, sounds play through
audio elements, a game is saved as a download.
What a browser does not have is not built for it: updates, the online games
window and its probe, engine processes (analysis starts a Web Worker there;
the review and preparation jobs, Rodent and the judge are native), blocking
requests (engine, Maia and Rodent downloads report no download for the
platform, translation reports that it makes no requests). The desktop
program behaves as before; its build finds unused code (the web build
compiles parts it never shows).
apps/web: the entry point, the page /app/ (device check before anything large
is loaded: phones and tablets get the page about the apps in the site's nine
languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile
web), a static server and headless Firefox checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
|
|
|
crate::platform::examples_dir()
|
2026-10-19 12:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-25 01:39:19 +03:00
|
|
|
/// A sample database of the program: only read (games added there would end up in a
|
|
|
|
|
/// hidden data folder).
|
|
|
|
|
pub fn is_example(path: &std::path::Path) -> bool {
|
|
|
|
|
dir().is_some_and(|d| path.starts_with(d))
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 19:46:45 +03:00
|
|
|
/// Adds the samples at the first start and brings their files up to this version;
|
|
|
|
|
/// `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 setup(settings: &mut Settings) -> bool {
|
|
|
|
|
match dir() {
|
|
|
|
|
Some(dir) => setup_in(&dir, settings),
|
|
|
|
|
None => !std::mem::replace(&mut settings.examples_added, true),
|
2026-10-19 12:21:09 +03:00
|
|
|
}
|
2026-10-28 19:46:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn setup_in(dir: &Path, settings: &mut Settings) -> bool {
|
|
|
|
|
let first = !std::mem::replace(&mut settings.examples_added, true);
|
|
|
|
|
for (key, text) in SAMPLES {
|
|
|
|
|
let named = dir.join(format!("{}.pgn", tr(key)));
|
|
|
|
|
// The file is named in the language of the first start: the sample's file under
|
|
|
|
|
// any language's name is it.
|
|
|
|
|
for name in all_languages(key) {
|
|
|
|
|
let path = dir.join(format!("{name}.pgn"));
|
Web version, stage 1: the interface as a library over a platform layer; apps/web
apps/desktop becomes a library (chesspenguin_app: the interface) and a thin
binary; apps/web starts the same interface in a page's canvas (eframe's
WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser.
The platform layer (apps/desktop/src/platform): the program's folders, files,
background work, the files the user opens or saves, the clipboard and what
the platform offers (CAPS). On a computer it is the system as before; in a
browser the program's data lives in IndexedDB behind an in-memory file tree
loaded before the start, the files the user opens are read into it for the
session (only read), background jobs run on the page's thread after the
frame, the clipboard is read through the browser's API, sounds play through
audio elements, a game is saved as a download.
What a browser does not have is not built for it: updates, the online games
window and its probe, engine processes (analysis starts a Web Worker there;
the review and preparation jobs, Rodent and the judge are native), blocking
requests (engine, Maia and Rodent downloads report no download for the
platform, translation reports that it makes no requests). The desktop
program behaves as before; its build finds unused code (the web build
compiles parts it never shows).
apps/web: the entry point, the page /app/ (device check before anything large
is loaded: phones and tablets get the page about the apps in the site's nine
languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile
web), a static server and headless Firefox checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
|
|
|
let kept = crate::platform::fs::exists(&path) || settings.databases.contains(&path);
|
2026-10-28 19:46:45 +03:00
|
|
|
if (kept || (first && path == named))
|
|
|
|
|
&& let Err(e) = bring_up_to_date(&path, text)
|
|
|
|
|
{
|
|
|
|
|
eprintln!("examples: cannot write {}: {e}", path.display());
|
|
|
|
|
}
|
2026-10-19 12:21:09 +03:00
|
|
|
}
|
Web version, stage 1: the interface as a library over a platform layer; apps/web
apps/desktop becomes a library (chesspenguin_app: the interface) and a thin
binary; apps/web starts the same interface in a page's canvas (eframe's
WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser.
The platform layer (apps/desktop/src/platform): the program's folders, files,
background work, the files the user opens or saves, the clipboard and what
the platform offers (CAPS). On a computer it is the system as before; in a
browser the program's data lives in IndexedDB behind an in-memory file tree
loaded before the start, the files the user opens are read into it for the
session (only read), background jobs run on the page's thread after the
frame, the clipboard is read through the browser's API, sounds play through
audio elements, a game is saved as a download.
What a browser does not have is not built for it: updates, the online games
window and its probe, engine processes (analysis starts a Web Worker there;
the review and preparation jobs, Rodent and the judge are native), blocking
requests (engine, Maia and Rodent downloads report no download for the
platform, translation reports that it makes no requests). The desktop
program behaves as before; its build finds unused code (the web build
compiles parts it never shows).
apps/web: the entry point, the page /app/ (device check before anything large
is loaded: phones and tablets get the page about the apps in the site's nine
languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile
web), a static server and headless Firefox checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
|
|
|
if first && crate::platform::fs::exists(&named) && !settings.databases.contains(&named) {
|
2026-10-28 19:46:45 +03:00
|
|
|
settings.databases.push(named);
|
2026-10-19 12:21:09 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-28 19:46:45 +03:00
|
|
|
first
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Writes `text` to `path` unless the file holds it already. The new file takes the old
|
|
|
|
|
/// one's place at once (a program started meanwhile reads one or the other whole).
|
|
|
|
|
fn bring_up_to_date(path: &Path, text: &str) -> std::io::Result<()> {
|
Web version, stage 1: the interface as a library over a platform layer; apps/web
apps/desktop becomes a library (chesspenguin_app: the interface) and a thin
binary; apps/web starts the same interface in a page's canvas (eframe's
WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser.
The platform layer (apps/desktop/src/platform): the program's folders, files,
background work, the files the user opens or saves, the clipboard and what
the platform offers (CAPS). On a computer it is the system as before; in a
browser the program's data lives in IndexedDB behind an in-memory file tree
loaded before the start, the files the user opens are read into it for the
session (only read), background jobs run on the page's thread after the
frame, the clipboard is read through the browser's API, sounds play through
audio elements, a game is saved as a download.
What a browser does not have is not built for it: updates, the online games
window and its probe, engine processes (analysis starts a Web Worker there;
the review and preparation jobs, Rodent and the judge are native), blocking
requests (engine, Maia and Rodent downloads report no download for the
platform, translation reports that it makes no requests). The desktop
program behaves as before; its build finds unused code (the web build
compiles parts it never shows).
apps/web: the entry point, the page /app/ (device check before anything large
is loaded: phones and tablets get the page about the apps in the site's nine
languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile
web), a static server and headless Firefox checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
|
|
|
if crate::platform::fs::read(path).is_ok_and(|on_disk| on_disk == text.as_bytes()) {
|
2026-10-28 19:46:45 +03:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
Web version, stage 1: the interface as a library over a platform layer; apps/web
apps/desktop becomes a library (chesspenguin_app: the interface) and a thin
binary; apps/web starts the same interface in a page's canvas (eframe's
WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser.
The platform layer (apps/desktop/src/platform): the program's folders, files,
background work, the files the user opens or saves, the clipboard and what
the platform offers (CAPS). On a computer it is the system as before; in a
browser the program's data lives in IndexedDB behind an in-memory file tree
loaded before the start, the files the user opens are read into it for the
session (only read), background jobs run on the page's thread after the
frame, the clipboard is read through the browser's API, sounds play through
audio elements, a game is saved as a download.
What a browser does not have is not built for it: updates, the online games
window and its probe, engine processes (analysis starts a Web Worker there;
the review and preparation jobs, Rodent and the judge are native), blocking
requests (engine, Maia and Rodent downloads report no download for the
platform, translation reports that it makes no requests). The desktop
program behaves as before; its build finds unused code (the web build
compiles parts it never shows).
apps/web: the entry point, the page /app/ (device check before anything large
is loaded: phones and tablets get the page about the apps in the site's nine
languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile
web), a static server and headless Firefox checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
|
|
|
crate::platform::fs::write_atomic(path, text.as_bytes())
|
2026-10-19 12:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2026-10-28 19:46:45 +03:00
|
|
|
/// A folder of its own for each test, empty.
|
|
|
|
|
fn folder(name: &str) -> PathBuf {
|
|
|
|
|
let dir = std::env::temp_dir().join(format!("cp-examples-{name}-{}", std::process::id()));
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
dir
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn classics(dir: &Path) -> PathBuf {
|
|
|
|
|
dir.join(format!("{}.pgn", tr("Classic games")))
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 12:21:09 +03:00
|
|
|
#[test]
|
|
|
|
|
fn samples_are_valid_pgn() {
|
2026-10-28 19:46:45 +03:00
|
|
|
let dir = folder("valid");
|
|
|
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
|
|
|
for (name, text) in SAMPLES {
|
2026-10-19 12:24:40 +03:00
|
|
|
let path = dir.join(format!("{name}.pgn"));
|
2026-10-19 12:21:09 +03:00
|
|
|
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();
|
2026-10-19 12:28:20 +03:00
|
|
|
assert!(
|
|
|
|
|
parsed.diagnostics.is_empty(),
|
|
|
|
|
"{name} #{}: {:?}",
|
|
|
|
|
i + 1,
|
|
|
|
|
parsed.diagnostics
|
|
|
|
|
);
|
2026-10-19 12:21:09 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-28 19:46:45 +03:00
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn the_first_start_writes_the_samples_and_shelves_them() {
|
|
|
|
|
let dir = folder("first");
|
|
|
|
|
let mut settings = Settings::default();
|
|
|
|
|
assert!(setup_in(&dir, &mut settings));
|
|
|
|
|
assert!(settings.examples_added);
|
|
|
|
|
assert_eq!(settings.databases.len(), SAMPLES.len());
|
|
|
|
|
assert_eq!(settings.databases[0], classics(&dir));
|
|
|
|
|
for (path, (_, text)) in settings.databases.iter().zip(SAMPLES) {
|
|
|
|
|
assert_eq!(std::fs::read_to_string(path).unwrap(), text);
|
|
|
|
|
}
|
|
|
|
|
// The next start changes nothing.
|
|
|
|
|
let before = settings.databases.clone();
|
|
|
|
|
assert!(!setup_in(&dir, &mut settings));
|
|
|
|
|
assert_eq!(settings.databases, before);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn an_older_or_damaged_sample_is_replaced() {
|
|
|
|
|
let dir = folder("older");
|
|
|
|
|
let mut settings = Settings::default();
|
|
|
|
|
setup_in(&dir, &mut settings);
|
|
|
|
|
let path = classics(&dir);
|
|
|
|
|
let old: String = SAMPLES[0].1.chars().take(SAMPLES[0].1.len() / 3).collect();
|
|
|
|
|
std::fs::write(&path, old).unwrap();
|
|
|
|
|
assert!(!setup_in(&dir, &mut settings));
|
|
|
|
|
assert_eq!(std::fs::read_to_string(&path).unwrap(), SAMPLES[0].1);
|
|
|
|
|
// Edited outside: the same.
|
|
|
|
|
let mut edited = SAMPLES[0].1.to_owned();
|
|
|
|
|
edited.push_str("\n[Event \"mine\"]\n\n1. e4 *\n");
|
|
|
|
|
std::fs::write(&path, edited).unwrap();
|
|
|
|
|
setup_in(&dir, &mut settings);
|
|
|
|
|
assert_eq!(std::fs::read_to_string(&path).unwrap(), SAMPLES[0].1);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A program that started in another language named the file in it.
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_sample_named_in_another_language_is_replaced() {
|
|
|
|
|
let dir = folder("language");
|
|
|
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
|
|
|
let russian = dir.join("Классические партии.pgn");
|
|
|
|
|
std::fs::write(&russian, "[Event \"old\"]\n\n1. e4 *\n").unwrap();
|
|
|
|
|
let mut settings = Settings {
|
|
|
|
|
examples_added: true,
|
|
|
|
|
databases: vec![russian.clone()],
|
|
|
|
|
..Settings::default()
|
|
|
|
|
};
|
|
|
|
|
assert!(!setup_in(&dir, &mut settings));
|
|
|
|
|
assert_eq!(std::fs::read_to_string(&russian).unwrap(), SAMPLES[0].1);
|
|
|
|
|
// The samples of the other names are not made.
|
|
|
|
|
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1);
|
|
|
|
|
assert_eq!(settings.databases, vec![russian]);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_sample_taken_off_the_shelf_stays_off() {
|
|
|
|
|
let dir = folder("off");
|
|
|
|
|
let mut settings = Settings::default();
|
|
|
|
|
setup_in(&dir, &mut settings);
|
|
|
|
|
let path = classics(&dir);
|
|
|
|
|
settings.databases.retain(|p| *p != path);
|
|
|
|
|
std::fs::write(&path, "[Event \"old\"]\n\n1. e4 *\n").unwrap();
|
|
|
|
|
assert!(!setup_in(&dir, &mut settings));
|
|
|
|
|
assert!(!settings.databases.contains(&path));
|
|
|
|
|
// Its file is the new one, should the user open it again.
|
|
|
|
|
assert_eq!(std::fs::read_to_string(&path).unwrap(), SAMPLES[0].1);
|
|
|
|
|
// Deleted as well: not made again.
|
|
|
|
|
std::fs::remove_file(&path).unwrap();
|
|
|
|
|
setup_in(&dir, &mut settings);
|
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
2026-10-19 12:21:09 +03:00
|
|
|
}
|
|
|
|
|
}
|