From 68941d3341a06e1f9c0671d15d4548720c227709 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Mon, 26 Oct 2026 01:12:59 +0300 Subject: [PATCH] mobile-api: a session's own review; a new game from a move of another (play on from here) Co-Authored-By: Claude Opus 5.5 --- crates/mobile-api/src/session.rs | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/crates/mobile-api/src/session.rs b/crates/mobile-api/src/session.rs index e14d317..a7c768d 100644 --- a/crates/mobile-api/src/session.rs +++ b/crates/mobile-api/src/session.rs @@ -210,6 +210,36 @@ impl GameSession { pub fn pgn(&self) -> String { self.with(|g| g.to_pgn()) } + + /// The analysis of the game as it is now (its report), with earlier results from + /// the cache folder `cache_dir` ("" for none). + pub fn review(&self, cache_dir: String) -> Arc { + crate::GameReview::new(self.read(|g| g.clone()), cache_dir) + } + + /// A new game of the moves from the start to the move at place `at` (to play on from + /// there, android-play.md ยง31.6): the same start position, no tags but those of the + /// position. Refused on a null move on the way (a position the board cannot reach). + pub fn play_from(&self, at: u32) -> Result, ApiError> { + self.read(|g| { + let id = node(g, at)?; + let mut game = Game::from_start(g.start().clone()); + let mut cur = NodeId::ROOT; + for &n in &g.path(id)[1..] { + match g.game_move(n).map(|m| &m.kind) { + Some(chess_core::MoveKind::Normal(m)) => cur = game.add_move(cur, *m)?, + _ => { + return Err(ApiError::Edit { + detail: "a null move on the way".into(), + }); + } + } + } + game.forget_history(); + game.mark_saved(); + Ok(GameSession::wrap(game)) + }) + } } // ----- own databases ---------------------------------------------------------------- @@ -325,6 +355,25 @@ mod tests { assert!(back.data.nodes[0].children.contains(&v.cursor)); } + #[test] + fn a_game_goes_on_from_a_move_of_another() { + let s = + GameSession::from_pgn("[White \"A\"]\n\n1. e4 e5 (1... c5) 2. Nf3 *".into()).unwrap(); + let c5 = s.data().nodes.iter().find(|n| n.san == "c5").unwrap().id; + let p = s.play_from(c5).unwrap(); + assert_eq!(san_line(&p.data()), ["e4", "c5"]); + assert!(!p.is_modified()); + assert!( + !p.data() + .tags + .iter() + .any(|t| t.name == "White" && t.value == "A") + ); + let n = GameSession::from_pgn("1. e4 -- 2. d4 *".into()).unwrap(); + let d4 = n.data().nodes.iter().find(|x| x.san == "d4").unwrap().id; + assert!(n.play_from(d4).is_err()); + } + #[test] fn an_illegal_move_changes_nothing() { let s = GameSession::new_game(String::new());