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>
This commit is contained in:
vrubelroman 2026-10-30 15:09:17 +03:00
parent a4445d2c15
commit 3e69a5d57c
61 changed files with 2628 additions and 552 deletions

View file

@ -4,7 +4,8 @@
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::time::Duration;
use web_time::Instant;
use chess_core::shakmaty::{Chess, Color, Move, Position};
use serde::{Deserialize, Serialize};
@ -457,6 +458,26 @@ struct Request {
pause: Duration,
}
/// The engine judging a game (Stockfish at `exe`), waking `ctx` with its answers.
/// A browser starts no engine processes: there is no judge there, and the game goes
/// without resignations, as when the engine does not start.
pub fn start_judge(exe: std::path::PathBuf, ctx: &egui::Context) -> Option<Judge> {
#[cfg(not(web))]
{
let ctx = ctx.clone();
Some(Judge::start(
uci::EngineConfig::new(exe),
Vec::new(),
Box::new(move || ctx.request_repaint()),
))
}
#[cfg(web)]
{
let _ = (exe, ctx);
None
}
}
/// What computes the opponent's moves.
enum Worker {
/// Maia in a thread of her own; her error, if loading failed.
@ -466,7 +487,8 @@ enum Worker {
generation: u64,
error: Arc<Mutex<Option<String>>>,
},
/// Rodent, a UCI engine (the shared crate's player).
/// Rodent, a UCI engine (the shared crate's player): a process, not in a browser.
#[cfg(not(web))]
Rodent(::play::rodent::RodentPlayer),
}
@ -551,6 +573,7 @@ impl Opponent {
pause,
});
}
#[cfg(not(web))]
Worker::Rodent(player) => {
if let (Some(pos), Some(now)) = (engine_pos, positions.last().cloned()) {
player.ask(pos, now, pause);
@ -561,6 +584,7 @@ impl Opponent {
/// Rodent (`exe` in its folder) playing in `style` at `uci_elo` (none: full
/// strength), with its opening book.
#[cfg(not(web))]
pub fn start_rodent(
exe: std::path::PathBuf,
style: String,
@ -585,10 +609,28 @@ impl Opponent {
}
}
/// An opponent that did not start, for `why`: the game ends at once with it (in a
/// browser, which starts no engine processes, Rodent).
#[cfg(web)]
pub fn not_started(why: String) -> Opponent {
let (tx, _) = channel();
let (_, rx) = channel();
Opponent {
worker: Worker::Maia {
tx,
rx,
generation: 0,
error: Arc::new(Mutex::new(Some(why))),
},
thinking: false,
}
}
/// Forgets what was asked (its answer will be dropped).
pub fn forget(&mut self) {
match &mut self.worker {
Worker::Maia { generation, .. } => *generation += 1,
#[cfg(not(web))]
Worker::Rodent(player) => player.forget(),
}
self.thinking = false;
@ -607,6 +649,7 @@ impl Opponent {
}
}
}
#[cfg(not(web))]
Worker::Rodent(player) => {
if let Some((m, pause)) = player.answer() {
self.thinking = false;
@ -618,13 +661,13 @@ impl Opponent {
}
pub fn failed(&self) -> Option<String> {
use ::play::rodent::Failure;
match &self.worker {
Worker::Maia { error, .. } => error.lock().ok().and_then(|g| g.clone()),
#[cfg(not(web))]
Worker::Rodent(player) => player.failure().map(|f| match f {
Failure::Start(e) => start_failure(&e),
Failure::Stopped => tr("it stopped while playing").to_owned(),
Failure::Search(e) => e,
::play::rodent::Failure::Start(e) => start_failure(&e),
::play::rodent::Failure::Stopped => tr("it stopped while playing").to_owned(),
::play::rodent::Failure::Search(e) => e,
}),
}
}