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>
| Changeset | 0fe81fd805d6e9cbbfa932c7220548d8b6e42c81 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | b6e74594080d |
| Children | f31f51fae676 |
| 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 | ||
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | 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 | + """ | |
| 25 | 29 | found = [] |
| 30 | + expanded = set() | |
| 26 | 31 | |
| 27 | - def walk(board, path, seen): | |
| 32 | + def walk(board, path): | |
| 28 | 33 | options = course.options(board) |
| 29 | 34 | here = opening.key(board) |
| 30 | - if not options or here in seen: | |
| 35 | + if not options or here in expanded: | |
| 31 | 36 | found.append((" ".join(m.uci() for m in path), list(path))) |
| 32 | 37 | return |
| 38 | + expanded.add(here) | |
| 33 | 39 | for option in options: |
| 34 | 40 | board.push(option.move) |
| 35 | - walk(board, path + [option.move], seen | {here}) | |
| 41 | + walk(board, path + [option.move]) | |
| 36 | 42 | board.pop() |
| 37 | 43 | |
| 38 | - walk(course.start(), [], set()) | |
| 44 | + walk(course.start(), []) | |
| 39 | 45 | return found |
| 40 | 46 | |
| 41 | 47 | |
| @@ -73,17 +79,41 @@ class Progress: | ||
| 73 | 79 | def card(self, course, line_id): |
| 74 | 80 | return self.data.get(course.title, {}).get(line_id) |
| 75 | 81 | |
| 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 | + | |
| 76 | 104 | def _order(self, course, today): |
| 77 | 105 | """Linien nach Dringlichkeit. |
| 78 | 106 | |
| 79 | 107 | Gruppen: 0 fällig, 1 neu (bis zum Tageslimit), 2 noch nicht fällig, 3 neu über dem Limit. |
| 80 | 108 | Gruppe 2 und 3 sind freiwilliges Weiterüben, wenn für heute alles geschafft ist. |
| 81 | 109 | """ |
| 110 | + all_lines = lines(course) | |
| 111 | + self._migrate(course, {line_id for line_id, _m in all_lines}) | |
| 82 | 112 | cards = self.data.get(course.title, {}) |
| 83 | 113 | started = sum(1 for c in cards.values() if c.get("first") == today.isoformat()) |
| 84 | 114 | allowance = max(0, NEW_PER_DAY - started) |
| 85 | 115 | ranked = [] |
| 86 | - for index, (line_id, moves) in enumerate(lines(course)): | |
| 116 | + for index, (line_id, moves) in enumerate(all_lines): | |
| 87 | 117 | card = cards.get(line_id) |
| 88 | 118 | if card is None: |
| 89 | 119 | group = 1 if allowance > 0 else 3 |
| @@ -141,6 +171,8 @@ class Progress: | ||
| 141 | 171 | def played_line(course, moves, target=None): |
| 142 | 172 | """Zu welcher Linie gehört eine gespielte Zugfolge? Bevorzugt die Ziellinie.""" |
| 143 | 173 | 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) | |
| 144 | 176 | all_lines = lines(course) |
| 145 | 177 | ids = [line_id for line_id, _m in all_lines] |
| 146 | 178 | if played in ids: |
modified tests/test_repetition.py +31 −0
| @@ -38,6 +38,37 @@ class LinesTest(unittest.TestCase): | ||
| 38 | 38 | self.assertEqual(repetition.played_line(self.course, [], "target"), "target") |
| 39 | 39 | |
| 40 | 40 | |
| 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 | + | |
| 41 | 72 | class ProgressTest(unittest.TestCase): |
| 42 | 73 | def setUp(self): |
| 43 | 74 | self.course = opening.parse_course(COURSE) |