diff --git a/crates/chess-core/src/game.rs b/crates/chess-core/src/game.rs index 8ff7767..436cb99 100644 --- a/crates/chess-core/src/game.rs +++ b/crates/chess-core/src/game.rs @@ -373,6 +373,12 @@ impl Game { self.nodes.len() } + /// Every node ever made, by id (detached ones too: an id never changes and is never + /// reused, so it can name a move across edits). + pub fn all_nodes(&self) -> impl Iterator + '_ { + (0..self.nodes.len()).map(|i| NodeId(i as u32)) + } + /// Ids of all attached nodes in depth-first PGN order (root first). pub fn attached_nodes(&self) -> Vec { let mut out = Vec::new(); diff --git a/crates/mobile-api/src/lib.rs b/crates/mobile-api/src/lib.rs index 4455438..6490999 100644 --- a/crates/mobile-api/src/lib.rs +++ b/crates/mobile-api/src/lib.rs @@ -404,17 +404,11 @@ impl Database { } } -/// Node ids in tree order (the root first, each line before its variations' tails), -/// mapped to their places in [`GameData::nodes`]. +/// The nodes of [`GameData::nodes`]: every node the game ever had, by its id. A move +/// keeps its place when the game is changed (new moves come at the end, a removed one +/// stays as an unreachable entry), so a cursor or a report's marks stay on their moves. fn order(game: &Game) -> Vec { - let mut order = vec![NodeId::ROOT]; - let mut i = 0; - while i < order.len() { - let id = order[i]; - order.extend(game.children(id).iter().copied()); - i += 1; - } - order + game.all_nodes().collect() } /// The place in [`GameData::nodes`] of every node of the game. @@ -497,10 +491,16 @@ fn game_data(game: &Game, diagnostics: &[chess_core::Diagnostic]) -> GameData { let nags: Vec = node .map(|n| n.nags().iter().map(|g| g.0).collect()) .unwrap_or_default(); + // A removed move: kept in its place, reached from nowhere. + let attached = game.is_attached(id); MoveNode { id: place(id).unwrap_or(0), - parent: game.parent(id).and_then(place), - children: game.children(id).iter().filter_map(|c| place(*c)).collect(), + parent: game.parent(id).filter(|_| attached).and_then(place), + children: if attached { + game.children(id).iter().filter_map(|c| place(*c)).collect() + } else { + Vec::new() + }, san: mv.map(|m| m.san.clone()).unwrap_or_default(), move_number, white: color == shakmaty::Color::White, diff --git a/crates/mobile-api/src/session.rs b/crates/mobile-api/src/session.rs index 980507e..2d112ea 100644 --- a/crates/mobile-api/src/session.rs +++ b/crates/mobile-api/src/session.rs @@ -300,6 +300,24 @@ mod tests { assert!(s.is_modified()); } + #[test] + fn a_move_keeps_its_id_when_the_game_changes() { + let s = GameSession::from_pgn("1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 *".into()).unwrap(); + let before = s.data(); + let nf3 = before.nodes.iter().find(|n| n.san == "Nf3").unwrap().id; + // A variation at the first move, then removed: Nf3 stays where it was. + let v = s.play(0, "d2d4".into()).unwrap(); + assert_eq!(v.data.nodes[nf3 as usize].san, "Nf3"); + let gone = s.delete_from(v.cursor).unwrap(); + assert_eq!(gone.data.nodes[nf3 as usize].san, "Nf3"); + // The removed move is reached from nowhere. + assert!(gone.data.nodes[v.cursor as usize].parent.is_none()); + assert!(!gone.data.nodes[0].children.contains(&v.cursor)); + // Undo brings it back under the same id. + let back = s.undo().unwrap(); + assert!(back.data.nodes[0].children.contains(&v.cursor)); + } + #[test] fn an_illegal_move_changes_nothing() { let s = GameSession::new_game(String::new());