fix(savegames): variations at the present no longer load as played moves
public
fix(savegames): variations at the present no longer load as played moves A variation or takeback hanging after the last played move was written as the main continuation and came back as played moves, e.g. after an autosave right after a takeback. Such variations now go into the header SchachDanach (a null move as placeholder, the variations beside it), which read_variations reads back; other programs ignore it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 7ebf6fe8583f59568e67f6e70dc38b73d3d41e0c |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 10b46625c441 |
| Children | 4963ba2572c3 |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/savegames.py +47 −8
| @@ -112,14 +112,46 @@ def to_pgn(state): | ||
| 112 | 112 | for ply, shapes in state.shapes.items(): |
| 113 | 113 | if ply in played and shapes: |
| 114 | 114 | _set_shapes(played[ply], shapes) |
| 115 | + last = len(state.board.move_stack) - 1 | |
| 116 | + ahead = [] # hinter der Gegenwart: im Zugtext würden sie zur Hauptlinie, also gespielt | |
| 115 | 117 | for table in (state.variations, state.takebacks): |
| 116 | 118 | for ply, variations in sorted(table.items()): |
| 117 | - if ply in played: | |
| 119 | + if ply == last: | |
| 120 | + ahead.extend(variations) | |
| 121 | + elif ply in played: | |
| 118 | 122 | for variation in variations: |
| 119 | 123 | _add_variation(played[ply], variation) |
| 124 | + if ahead: | |
| 125 | + game.headers[AHEAD_HEADER] = _ahead_text(state.board, ahead) | |
| 120 | 126 | return str(game) + "\n" |
| 121 | 127 | |
| 122 | 128 | |
| 129 | +AHEAD_HEADER = "SchachDanach" | |
| 130 | + | |
| 131 | + | |
| 132 | +def _ahead_text(board, variations): | |
| 133 | + """Varianten an der Gegenwart (z. B. dritte Hinweisstufe, gerade Zurückgenommenes) als Zugtext | |
| 134 | + für den Header AHEAD_HEADER: ein Nullzug als Platzhalter, die Varianten als Klammern daneben | |
| 135 | + (so behält auch die erste ihren Namen). Andere Programme ignorieren den Header.""" | |
| 136 | + ahead = chess.pgn.Game() | |
| 137 | + ahead.setup(board) | |
| 138 | + ahead.add_variation(chess.Move.null()) | |
| 139 | + for variation in variations: | |
| 140 | + _add_variation(ahead, variation) | |
| 141 | + text = ahead.accept(chess.pgn.StringExporter(headers=False, columns=None)) | |
| 142 | + return " ".join(text.replace('"', "'").split()) | |
| 143 | + | |
| 144 | + | |
| 145 | +def _read_ahead(board, text): | |
| 146 | + """Gegenstück zu _ahead_text: [Variante] an der Stellung ``board``.""" | |
| 147 | + if not text: | |
| 148 | + return [] | |
| 149 | + ahead = chess.pgn.read_game(io.StringIO(f'[FEN "{board.fen()}"]\n[SetUp "1"]\n\n{text}')) | |
| 150 | + if ahead is None: | |
| 151 | + return [] | |
| 152 | + return [_pack(node) for node in ahead.variations if node.move] | |
| 153 | + | |
| 154 | + | |
| 123 | 155 | # Pfeilfarben als PGN-Farben ([%cal Bd2d4]); Spur der Vorführung: eigene Züge blau, Gegner gelb |
| 124 | 156 | SHAPE_COLOR = {"green": "green", "red": "red", "blue": "blue", "yellow": "yellow"} |
| 125 | 157 | PGN_COLOR = {"hint": "blue", "threat": "yellow", **SHAPE_COLOR} |
| @@ -177,19 +209,26 @@ def split_takebacks(variations): | ||
| 177 | 209 | return side, back |
| 178 | 210 | |
| 179 | 211 | |
| 212 | +def _pack(node): | |
| 213 | + """Variante ab ``node`` im Format von GameState.variations.""" | |
| 214 | + shapes = read_shapes(node) | |
| 215 | + comment = " ".join(COMMAND_RE.sub(" ", node.comment or "").split()) | |
| 216 | + name = " ".join((node.starting_comment or "").split()) | |
| 217 | + return (node.move, comment, shapes, [_pack(child) for child in node.variations], name) | |
| 218 | + | |
| 219 | + | |
| 180 | 220 | def read_variations(game): |
| 181 | 221 | """Nebenvarianten einer PGN-Partie im Format von GameState.variations (Kommentare ohne [%…], |
| 182 | - außer der Marke [%zurueck …] für Zurückgenommenes, siehe ``split_takebacks``).""" | |
| 183 | - def pack(node): | |
| 184 | - shapes = read_shapes(node) | |
| 185 | - comment = " ".join(COMMAND_RE.sub(" ", node.comment or "").split()) | |
| 186 | - name = " ".join((node.starting_comment or "").split()) | |
| 187 | - return (node.move, comment, shapes, [pack(child) for child in node.variations], name) | |
| 222 | + außer der Marke [%zurueck …] für Zurückgenommenes, siehe ``split_takebacks``). Dazu die Varianten | |
| 223 | + an der Gegenwart aus dem Header AHEAD_HEADER.""" | |
| 188 | 224 | found, node, ply = {}, game, -1 |
| 189 | 225 | while node.variations: |
| 190 | 226 | if len(node.variations) > 1: |
| 191 | - found[ply] = [pack(side) for side in node.variations[1:]] | |
| 227 | + found[ply] = [_pack(side) for side in node.variations[1:]] | |
| 192 | 228 | node, ply = node.variations[0], ply + 1 |
| 229 | + ahead = _read_ahead(node.board(), game.headers.get(AHEAD_HEADER, "")) | |
| 230 | + if ahead: | |
| 231 | + found.setdefault(ply, []).extend(ahead) | |
| 193 | 232 | return found |
| 194 | 233 | |
| 195 | 234 | |
modified tests/test_takebacks.py +35 −0
| @@ -72,6 +72,7 @@ class PgnTest(unittest.TestCase): | ||
| 72 | 72 | self.assertEqual(list(loaded.variations), [3]) |
| 73 | 73 | self.assertEqual(loaded.variations[3][0][1], "Idee") |
| 74 | 74 | |
| 75 | + | |
| 75 | 76 | def test_export_switch(self): |
| 76 | 77 | with_back = export.to_pgn(self.state, export.Options(scope="game")) |
| 77 | 78 | without = export.to_pgn(self.state, export.Options(scope="game", takebacks=False)) |
| @@ -79,5 +80,39 @@ class PgnTest(unittest.TestCase): | ||
| 79 | 80 | self.assertNotIn("zurueck", without) |
| 80 | 81 | |
| 81 | 82 | |
| 83 | +class AheadTest(unittest.TestCase): | |
| 84 | + """Gefunden am 24.11.2026: Varianten an der Gegenwart (gerade Zurückgenommenes, die dritte | |
| 85 | + Hinweisstufe als Nebenvariante) standen im Zugtext als Hauptlinie und kamen beim Laden als | |
| 86 | + gespielte Züge zurück, etwa nach einem Autosave direkt nach einer Rücknahme.""" | |
| 87 | + | |
| 88 | + def setUp(self): | |
| 89 | + self.state = _state(["e4", "e5", "Nf3"]) | |
| 90 | + savegames.note_takeback(self.state.takebacks, [_move("b8c6")], 3, "selbst") # gerade zurückgenommen | |
| 91 | + self.state.variations = {2: [(_move("d7d6"), "Ruhig.", (), [(_move("d2d4"), "", (), [], "")], "Hinweis")]} | |
| 92 | + | |
| 93 | + def test_moves_stay_as_played(self): | |
| 94 | + loaded = savegames.from_pgn(savegames.to_pgn(self.state)) | |
| 95 | + self.assertEqual([m.uci() for m in loaded.board.move_stack], ["e2e4", "e7e5", "g1f3"]) | |
| 96 | + | |
| 97 | + def test_variations_and_takebacks_come_back(self): | |
| 98 | + loaded = savegames.from_pgn(savegames.to_pgn(self.state)) | |
| 99 | + self.assertEqual(loaded.variations, {2: [(_move("d7d6"), "Ruhig.", (), [(_move("d2d4"), "", (), [], "")], | |
| 100 | + "Hinweis")]}) | |
| 101 | + self.assertEqual(savegames.takeback_by(loaded.takebacks[2][0]), "selbst") | |
| 102 | + | |
| 103 | + def test_not_in_the_move_text(self): | |
| 104 | + text = savegames.to_pgn(self.state) | |
| 105 | + moves = text.split("\n\n", 1)[1] | |
| 106 | + self.assertNotIn("d6", moves) | |
| 107 | + self.assertIn(f'[{savegames.AHEAD_HEADER} "', text) | |
| 108 | + | |
| 109 | + def test_from_the_start(self): | |
| 110 | + state = _state([]) | |
| 111 | + state.variations = {-1: [(_move("e2e4"), "", (), [], "Hinweis")]} | |
| 112 | + loaded = savegames.from_pgn(savegames.to_pgn(state)) | |
| 113 | + self.assertEqual(loaded.board.move_stack, []) | |
| 114 | + self.assertEqual(list(loaded.variations), [-1]) | |
| 115 | + | |
| 116 | + | |
| 82 | 117 | if __name__ == "__main__": |
| 83 | 118 | unittest.main() |