Web version, stage 2: Stockfish 19 in workers, live analysis, game review on N workers, My games, files, keys
game-review: what the job does for one target (the full search, the second best without the move played, the shallow ones) becomes a machine without I/O (job/work.rs), driven by the engine processes on a computer (job/engines.rs, the same searches as before) and by Web Workers in a browser (job/workers.rs: a dispatcher polled from the frames, never blocking). The analysis cache goes through a CacheStore (a folder, or the program's files of the platform). The browser's engine: Stockfish.js 19.0.1 (npm stockfish, GPL-3.0), its single-threaded build served beside /app/ (engine/, build.sh checks the package's sha512), its 99 MB wasm downloaded only through the engine offer, checked against its SHA-256 and kept in Cache Storage; workers get it as a blob: address. Live analysis (lines, arrows, the bar) and the review run on it; the review uses one worker per logical CPU, at most 8. My games (the web version): a PGN in the browser's storage, always on the shelf; Ctrl+S puts a game there (its own record, or a new one); deleting with undo. chess-core's store gives the bytes of append/replace/remove/restore, so the platform writes them into the browser's files; on a computer the files are written as before. Settings: Browser data (used and free space, keeping the data for good, Download all as PGN, deleting the engine). Files: a file of the last visit offers Choose the file again; Keep a copy in the browser (refused with the room it needs when the browser gives too little); files over 1 GB are not opened, with a word on why. No reference tab and no search by position in the web version. Keys: Alt+Left, plain 1-6, Ctrl+C, N in the library (the browser keeps Ctrl+W/N/1-6/Shift+C). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4ccf8099a5
commit
61ccd763bc
34 changed files with 2063 additions and 253 deletions
|
|
@ -11,9 +11,72 @@ const DATABASES: [&str; 10] = [
|
|||
"pgn", "PGN", "cbh", "CBH", "cbv", "CBV", "zip", "ZIP", "7z", "7Z",
|
||||
];
|
||||
|
||||
/// Asks the user for databases to open; `then` gets their paths (nothing when the
|
||||
/// Files a browser does not open: larger than this (decisions «Веб-версия»: a warning
|
||||
/// above 1 GB — the program for the computer opens them).
|
||||
pub const TOO_BIG: u64 = 1 << 30;
|
||||
|
||||
/// What the user chose or dropped: the databases to open, and the files left out
|
||||
/// because they are too big for a browser (name, bytes).
|
||||
#[derive(Default)]
|
||||
pub struct Picked {
|
||||
pub paths: Vec<PathBuf>,
|
||||
pub too_big: Vec<(String, u64)>,
|
||||
}
|
||||
|
||||
/// A file the browser holds for this visit only (one the user chose or dropped): after
|
||||
/// a reload it is gone, the user chooses it again or keeps a copy. Never on a computer.
|
||||
pub fn session_only(path: &std::path::Path) -> bool {
|
||||
#[cfg(web)]
|
||||
return path.starts_with(super::web::vfs::OPENED);
|
||||
#[cfg(not(web))]
|
||||
{
|
||||
let _ = path;
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Keeps a copy of the session file `path` in the browser's storage, where it stays
|
||||
/// between visits; the copy's path. Refused when the browser gives the site too little
|
||||
/// room (the size and what is free, for the message).
|
||||
#[cfg(web)]
|
||||
pub fn keep_copy(path: &std::path::Path) -> Result<PathBuf, KeepCopy> {
|
||||
let bytes = super::fs::read(path).map_err(|e| KeepCopy::Failed(e.to_string()))?;
|
||||
let need = bytes.len() as u64;
|
||||
if let Some(free) = super::storage_free()
|
||||
&& need + need / 10 > free
|
||||
{
|
||||
return Err(KeepCopy::NoRoom { need, free });
|
||||
}
|
||||
let name = path.file_name().map(|n| n.to_owned()).unwrap_or_default();
|
||||
let dir = super::data_dir().unwrap_or_default().join("copies");
|
||||
let mut copy = dir.join(&name);
|
||||
let mut n = 1;
|
||||
while super::fs::exists(©) {
|
||||
n += 1;
|
||||
let stem = std::path::Path::new(&name)
|
||||
.file_stem()
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
let ext = std::path::Path::new(&name)
|
||||
.extension()
|
||||
.map(|e| e.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
copy = dir.join(format!("{stem} ({n}).{ext}"));
|
||||
}
|
||||
super::fs::write_atomic(©, &bytes).map_err(|e| KeepCopy::Failed(e.to_string()))?;
|
||||
Ok(copy)
|
||||
}
|
||||
|
||||
/// Why no copy was kept.
|
||||
#[cfg(web)]
|
||||
pub enum KeepCopy {
|
||||
NoRoom { need: u64, free: u64 },
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
/// Asks the user for databases to open; `then` gets what was chosen (nothing when the
|
||||
/// user cancelled). The dialog does not hold the screen: `then` comes later.
|
||||
pub fn pick_databases(then: impl FnOnce(Vec<PathBuf>) + Send + 'static) {
|
||||
pub fn pick_databases(then: impl FnOnce(Picked) + Send + 'static) {
|
||||
#[cfg(not(web))]
|
||||
{
|
||||
use crate::i18n::tr;
|
||||
|
|
@ -26,7 +89,10 @@ pub fn pick_databases(then: impl FnOnce(Vec<PathBuf>) + Send + 'static) {
|
|||
.add_filter("PGN", &["pgn", "PGN"])
|
||||
.add_filter(tr("All files"), &["*"])
|
||||
.pick_files();
|
||||
then(picked.unwrap_or_default());
|
||||
then(Picked {
|
||||
paths: picked.unwrap_or_default(),
|
||||
too_big: Vec::new(),
|
||||
});
|
||||
});
|
||||
}
|
||||
#[cfg(web)]
|
||||
|
|
@ -37,21 +103,14 @@ pub fn pick_databases(then: impl FnOnce(Vec<PathBuf>) + Send + 'static) {
|
|||
.map(|e| format!(".{e}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
super::web::page::pick_files(&accept, move |files| {
|
||||
then(
|
||||
files
|
||||
.into_iter()
|
||||
.map(|(name, bytes)| super::web::vfs::add_opened(&name, bytes))
|
||||
.collect(),
|
||||
);
|
||||
});
|
||||
super::web::page::pick_files(&accept, move |files| then(opened(files)));
|
||||
}
|
||||
}
|
||||
|
||||
/// The files dropped on the window in this frame, as databases to open: `then` gets
|
||||
/// their paths once read (in a browser they are kept for the session like chosen
|
||||
/// ones). The desktop program does not take dropped files: nothing comes there.
|
||||
pub fn take_dropped(ctx: &egui::Context, then: impl FnOnce(Vec<PathBuf>) + Send + 'static) {
|
||||
pub fn take_dropped(ctx: &egui::Context, then: impl FnOnce(Picked) + Send + 'static) {
|
||||
#[cfg(not(web))]
|
||||
{
|
||||
let _ = (ctx, then);
|
||||
|
|
@ -69,16 +128,24 @@ pub fn take_dropped(ctx: &egui::Context, then: impl FnOnce(Vec<PathBuf>) + Send
|
|||
return;
|
||||
}
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let read = super::web::page::read_files(files).await;
|
||||
then(
|
||||
read.into_iter()
|
||||
.map(|(name, bytes)| super::web::vfs::add_opened(&name, bytes))
|
||||
.collect(),
|
||||
);
|
||||
then(opened(super::web::page::read_files(files).await));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// The files read, kept for the session.
|
||||
#[cfg(web)]
|
||||
fn opened(files: super::web::page::Read) -> Picked {
|
||||
Picked {
|
||||
paths: files
|
||||
.read
|
||||
.into_iter()
|
||||
.map(|(name, bytes)| super::web::vfs::add_opened(&name, bytes))
|
||||
.collect(),
|
||||
too_big: files.too_big,
|
||||
}
|
||||
}
|
||||
|
||||
/// Files are dragged over the window now (the page shows where they go).
|
||||
pub fn dragging(ctx: &egui::Context) -> bool {
|
||||
cfg!(web) && ctx.input(|i| !i.raw.hovered_files.is_empty())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue