online-games, play, prep: what needs processes or blocking requests is not built for a browser

online-games: the network part (lookups, probes, downloads into the database,
with their tests) moves to net.rs, built where a request may block a thread;
ureq is a dependency there only. The names, paths and states of the online
databases stay for every target.

play: the engines that play (Rodent) or judge (Maia's resignations) run as
processes driven from threads: not built for WebAssembly. getrandom has its
browser backend there.

prep: analyse (the job in engine processes) is native.

Nothing changes on the computer or Android; the tests pass as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-30 14:34:19 +03:00
parent 2703e5de38
commit 1e0fb082e5
8 changed files with 1033 additions and 981 deletions

View file

@ -1,12 +1,17 @@
//! A game against the computer (docs/design/play-mode.md, android-play.md §31): how it
//! ends, how long the opponent "thinks", its resignation judged by an engine, and Rodent
//! (`rodent`). Shared by the desktop and the Android apps; nothing here draws.
//! (`rodent`). Shared by the desktop and the Android apps; nothing here draws. The
//! engines that play or judge run as processes, each driven from a thread: not built
//! for a browser.
pub mod rodent;
mod rodent4_files;
#[cfg(not(target_family = "wasm"))]
use std::sync::mpsc::{Receiver, Sender, channel};
use std::time::{Duration, Instant};
use std::time::Duration;
#[cfg(not(target_family = "wasm"))]
use std::time::Instant;
use shakmaty::zobrist::Zobrist64;
use shakmaty::{Chess, Color, EnPassantMode, Position};
@ -242,6 +247,7 @@ pub fn hopeless(score: uci::Score) -> bool {
}
}
#[cfg(not(target_family = "wasm"))]
/// The engine (Stockfish, unseen by the user) judging the positions where Maia is to
/// move, in its own thread for the whole game: the newest request is searched, older
/// ones are dropped; an engine that does not start or dies leaves the game without
@ -256,6 +262,7 @@ pub struct Judge {
refused_at: Option<usize>,
}
#[cfg(not(target_family = "wasm"))]
impl Judge {
/// The engine of `config` with `options` set (an Android Stockfish: its `EvalFile`);
/// `wake` is called when an answer comes (the app draws again).

View file

@ -4,11 +4,15 @@
//! thread of its own. Shared by the desktop and the Android apps; nothing here draws.
use std::collections::BTreeMap;
use std::sync::mpsc::{Sender, channel};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::time::{Duration, Instant};
use std::time::Duration;
#[cfg(not(target_family = "wasm"))]
use std::{
sync::mpsc::{Sender, channel},
sync::{Arc, Condvar, Mutex, MutexGuard},
time::Instant,
};
use shakmaty::{Chess, Move};
use shakmaty::Move;
pub use crate::rodent4_files::{COMMIT, FILES, PERSONALITY_BOOKS};
@ -556,17 +560,19 @@ pub enum Failure {
Search(String),
}
#[cfg(not(target_family = "wasm"))]
/// A move asked for: the game for the engine, its position now, the pause.
struct Request {
generation: u64,
pos: uci::EnginePosition,
now: Chess,
now: shakmaty::Chess,
pause: Duration,
}
/// Rodent's move, or none (no legal move it gave), and the time it took.
pub type Answer = (Option<Move>, Duration);
#[cfg(not(target_family = "wasm"))]
#[derive(Default)]
struct State {
/// The request asked last (bumped by `forget`).
@ -577,12 +583,14 @@ struct State {
failure: Option<Failure>,
}
#[cfg(not(target_family = "wasm"))]
#[derive(Default)]
struct Shared {
state: Mutex<State>,
changed: Condvar,
}
#[cfg(not(target_family = "wasm"))]
impl Shared {
fn lock(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(|e| e.into_inner())
@ -594,6 +602,7 @@ impl Shared {
}
}
#[cfg(not(target_family = "wasm"))]
/// Rodent playing one game, in its own thread (a UCI child process): it thinks during
/// the pause a move should take (a game's own clock is not its business: Maia's pause
/// rule paces it) and answers after it. Only the newest request counts; the answer to
@ -603,6 +612,7 @@ pub struct RodentPlayer {
shared: Arc<Shared>,
}
#[cfg(not(target_family = "wasm"))]
impl RodentPlayer {
/// The engine of `config` (its folder, or its home in the environment: Android)
/// playing `style_id` at `uci_elo` (none: full strength), with its opening book when
@ -709,7 +719,7 @@ impl RodentPlayer {
/// Asks for a move in `now` (`pos`: the game to it for the engine) after `pause`;
/// the request's number, for [`RodentPlayer::wait`].
pub fn ask(&self, pos: uci::EnginePosition, now: Chess, pause: Duration) -> u64 {
pub fn ask(&self, pos: uci::EnginePosition, now: shakmaty::Chess, pause: Duration) -> u64 {
let generation = {
let mut s = self.shared.lock();
s.generation += 1;
@ -842,14 +852,14 @@ mod tests {
let start = || uci::EnginePosition::startpos(Vec::<String>::new());
let pause = Duration::from_millis(400);
// Asked twice: the first is passed over, the second answered with a legal move.
let first = player.ask(start(), Chess::default(), pause);
let second = player.ask(start(), Chess::default(), pause);
let first = player.ask(start(), shakmaty::Chess::default(), pause);
let second = player.ask(start(), shakmaty::Chess::default(), pause);
assert_eq!(player.wait(first), None);
let (m, spent) = player.wait(second).expect("an answer");
assert!(m.is_some(), "{:?}", player.failure());
assert!(spent >= pause);
// A request forgotten ends its wait at once.
let third = player.ask(start(), Chess::default(), pause);
let third = player.ask(start(), shakmaty::Chess::default(), pause);
player.forget();
assert_eq!(player.wait(third), None);
assert_eq!(player.failure(), None);