mobile-api: a move's id is its node id, kept across edits
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run

New moves come at the end and a removed one stays unreachable, so the
cursor and the report's marks stay on their moves when the game changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-26 00:33:55 +03:00
parent 68a9a37757
commit d9f37f1283
3 changed files with 36 additions and 12 deletions

View file

@ -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<Item = NodeId> + '_ {
(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<NodeId> {
let mut out = Vec::new();

View file

@ -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<NodeId> {
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<u8> = 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,

View file

@ -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());