Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

fix(training): end a line where it transposes into a counted one

public

fix(training): end a line where it transposes into a counted one

Lines continued through the whole subtree after a transposition, so
4...b5 duplicated the Spanish main line as extra cards (39 instead
of 31; Italian 13 instead of 8). Stored progress of the old, longer
ids moves to the shortened id; when several collapse, the earlier
due date wins. A target line followed by more course moves still
counts for the target.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Changeset0fe81fd805d6e9cbbfa932c7220548d8b6e42c81
Author (claimed)Timo Stolz <timo.stolz@nullachtvierzehn.de>,
Parentsb6e74594080d
Childrenf31f51fae676
Pushlog
by timo via ssh
by timo via ssh

modified schach/repetition.py +38 −6

@@ -21,21 +21,27 @@ NEW_PER_DAY = 3 # neue Linien pro Kurs und Tag – Wiederholungen kommen immer dran
2121
 
2222
 
2323
 def lines(course):
24
-    """Alle Linien des Kurses: [(id, [Zug, …])], Hauptvariante zuerst."""
24
+    """Alle Linien des Kurses: [(id, [Zug, …])], Hauptvariante zuerst.
25
+
26
+    Eine Linie endet, wo sie per Zugumstellung in eine Stellung kommt, die schon zu einer
27
+    früheren Linie gehört – sonst stünde derselbe Rest des Kurses mehrfach als Karte da.
28
+    """
2529
     found = []
30
+    expanded = set()
2631
 
27
-    def walk(board, path, seen):
32
+    def walk(board, path):
2833
         options = course.options(board)
2934
         here = opening.key(board)
30
-        if not options or here in seen:
35
+        if not options or here in expanded:
3136
             found.append((" ".join(m.uci() for m in path), list(path)))
3237
             return
38
+        expanded.add(here)
3339
         for option in options:
3440
             board.push(option.move)
35
-            walk(board, path + [option.move], seen | {here})
41
+            walk(board, path + [option.move])
3642
             board.pop()
3743
 
38
-    walk(course.start(), [], set())
44
+    walk(course.start(), [])
3945
     return found
4046
 
4147
 
@@ -73,17 +79,41 @@ class Progress:
7379
     def card(self, course, line_id):
7480
         return self.data.get(course.title, {}).get(line_id)
7581
 
82
+    def _migrate(self, course, ids):
83
+        """Lernstand alter, längerer Linien-Kennungen auf die gekürzte Kennung übertragen.
84
+
85
+        Früher liefen Linien nach einer Zugumstellung weiter; jetzt enden sie dort. Mehrere
86
+        alte Karten können auf dieselbe neue fallen – dann gilt die früher fällige.
87
+        """
88
+        cards = self.data.get(course.title)
89
+        if not cards:
90
+            return
91
+        changed = False
92
+        for old in [i for i in cards if i not in ids]:
93
+            target = max((i for i in ids if i and old.startswith(i + " ")), key=len, default=None)
94
+            if target is None:
95
+                continue
96
+            card = cards.pop(old)
97
+            existing = cards.get(target)
98
+            if existing is None or (card.get("due", "") < existing.get("due", "")):
99
+                cards[target] = card
100
+            changed = True
101
+        if changed:
102
+            self.save()
103
+
76104
     def _order(self, course, today):
77105
         """Linien nach Dringlichkeit.
78106
 
79107
         Gruppen: 0 fällig, 1 neu (bis zum Tageslimit), 2 noch nicht fällig, 3 neu über dem Limit.
80108
         Gruppe 2 und 3 sind freiwilliges Weiterüben, wenn für heute alles geschafft ist.
81109
         """
110
+        all_lines = lines(course)
111
+        self._migrate(course, {line_id for line_id, _m in all_lines})
82112
         cards = self.data.get(course.title, {})
83113
         started = sum(1 for c in cards.values() if c.get("first") == today.isoformat())
84114
         allowance = max(0, NEW_PER_DAY - started)
85115
         ranked = []
86
-        for index, (line_id, moves) in enumerate(lines(course)):
116
+        for index, (line_id, moves) in enumerate(all_lines):
87117
             card = cards.get(line_id)
88118
             if card is None:
89119
                 group = 1 if allowance > 0 else 3
@@ -141,6 +171,8 @@ class Progress:
141171
 def played_line(course, moves, target=None):
142172
     """Zu welcher Linie gehört eine gespielte Zugfolge? Bevorzugt die Ziellinie."""
143173
     played = " ".join(m.uci() for m in moves)
174
+    if target and (played == target or played.startswith(target + " ")):
175
+        return target  # Ziellinie gespielt (nach ihrer Zugumstellung ging es im Kurs weiter)
144176
     all_lines = lines(course)
145177
     ids = [line_id for line_id, _m in all_lines]
146178
     if played in ids:

modified tests/test_repetition.py +31 −0

@@ -38,6 +38,37 @@ class LinesTest(unittest.TestCase):
3838
         self.assertEqual(repetition.played_line(self.course, [], "target"), "target")
3939
 
4040
 
41
+TRANSPOSING = """[Event "Umstellung"]
42
+[SchachColor "white"]
43
+
44
+1. e4 e5 2. Nf3 Nc6 (2... Nf6 3. Bc4 Nc6) 3. Bc4 Nf6 4. d3 (4. Ng5) 4... Be7 *
45
+"""
46
+
47
+
48
+class TranspositionTest(unittest.TestCase):
49
+    def setUp(self):
50
+        self.course = opening.parse_course(TRANSPOSING)
51
+
52
+    def test_line_ends_at_transposition(self):
53
+        ids = [i for i, _m in repetition.lines(self.course)]
54
+        # 2…Sf6 3.Lc4 Sc6 ist dieselbe Stellung wie die Hauptvariante nach 3…Sf6: dort endet die Linie
55
+        self.assertEqual(ids, ["e2e4 e7e5 g1f3 b8c6 f1c4 g8f6 d2d3 f8e7", "e2e4 e7e5 g1f3 b8c6 f1c4 g8f6 f3g5",
56
+                               "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6"])
57
+
58
+    def test_old_long_ids_migrate(self):
59
+        progress = repetition.Progress()
60
+        old = "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6 d2d3 f8e7"  # so lief die Linie früher weiter
61
+        progress.data["Umstellung"] = {old: {"step": 2, "due": "2026-12-01", "lapses": 0}}
62
+        progress.summary(self.course, TODAY)
63
+        self.assertEqual(progress.card(self.course, "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6")["step"], 2)
64
+        self.assertIsNone(progress.card(self.course, old))
65
+
66
+    def test_played_line_beyond_transposition_counts_for_target(self):
67
+        target = "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6"
68
+        moves = [chess.Move.from_uci(u) for u in (target + " d2d3 f8e7").split()]
69
+        self.assertEqual(repetition.played_line(self.course, moves, target), target)
70
+
71
+
4172
 class ProgressTest(unittest.TestCase):
4273
     def setUp(self):
4374
         self.course = opening.parse_course(COURSE)