fix: rewinding and replaying a move now overwrites the line instead of appending

doMove only truncated moves/frames on the first divergence (forkPoint===null).
Subsequent rewind+move appended to the end of the line, corrupting the game
state irrecoverably. Always truncate to the current point before pushing.
Drop the dead forkedMoves state.
This commit is contained in:
Michael Pilosov 2026-06-25 19:29:27 +00:00
parent 1d8d8cec14
commit 94e890f3ca
2 changed files with 7 additions and 10 deletions

View File

@ -12,7 +12,6 @@
let board = null;
let forkPoint = null; // index where fork happened, null if on original line
let originalData = null; // saved original game before fork
let forkedMoves = []; // UCI moves made after fork point
let showPieces = true;
let showBoard = false;
let selectedSquare = null; // tap-to-move: currently selected source square
@ -157,16 +156,17 @@
.then((r) => r.ok ? r.json() : null)
.then((data) => {
if (!data) return;
// First divergence from the originally-loaded line: snapshot it so "Reset to original" works.
if (forkPoint === null && currentIndex < gameData.frames.length - 1) {
originalData = JSON.parse(JSON.stringify(gameData));
forkPoint = currentIndex;
gameData.moves = gameData.moves.slice(0, currentIndex);
gameData.frames = gameData.frames.slice(0, currentIndex + 1);
forkedMoves = [];
}
// Always overwrite any continuation past the current point, so rewinding and
// playing a different move replaces the old line instead of appending to it.
gameData.moves = gameData.moves.slice(0, currentIndex);
gameData.frames = gameData.frames.slice(0, currentIndex + 1);
gameData.moves.push({ san: data.san, uci: data.uci, ply: currentIndex + 1 });
gameData.frames.push(data.frame);
if (forkPoint !== null) forkedMoves.push(data.uci);
goTo(gameData.frames.length - 1);
updateForkUI();
});
@ -318,7 +318,6 @@
gameData = originalData;
originalData = null;
forkPoint = null;
forkedMoves = [];
goTo(0);
updateForkUI();
}
@ -330,7 +329,6 @@
gameData = await r.json();
forkPoint = null;
originalData = null;
forkedMoves = [];
currentIndex = 0;
goTo(0);
updateGameInfo();
@ -350,7 +348,6 @@
gameData = await r.json();
forkPoint = null;
originalData = null;
forkedMoves = [];
currentIndex = 0;
goTo(0);
updateGameInfo();

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess Pressure</title>
<link rel="stylesheet" href="/static/style.css?v=22">
<link rel="stylesheet" href="/static/style.css?v=23">
<link rel="stylesheet" href="https://unpkg.com/@chrisoakman/chessboardjs@1.0.0/dist/chessboard-1.0.0.min.css">
</head>
<body>
@ -124,6 +124,6 @@
<script src="https://unpkg.com/@chrisoakman/chessboardjs@1.0.0/dist/chessboard-1.0.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.3/chess.min.js"></script>
<script src="/static/gif.js"></script>
<script src="/static/app.js?v=22"></script>
<script src="/static/app.js?v=23"></script>
</body>
</html>