feat(kurse): restart a failed line just before the slip
public
feat(kurse): restart a failed line just before the slip Replaying the first 5-7 shared moves on every "Nochmal" is pure overlearning and wears kids out. After an "again" grade the line restarts the same day one own move before the first slip (wrong move or hint); the moves up to there are auto-played at 350 ms each. Next day the line runs in full again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | c6a58c80691ff024e82bb9e6fa1057cee639a47d |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | c6390964f0a9 |
| Children | 63e835c7a558 |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/repetition.py +19 −2
| @@ -153,11 +153,24 @@ class Progress: | ||
| 153 | 153 | new += rank[0] == 1 |
| 154 | 154 | return due, new, len(ranked) |
| 155 | 155 | |
| 156 | - def record(self, course, line_id, grade, now=None): | |
| 156 | + def retry_from(self, course, line_id, today=None): | |
| 157 | + """Halbzug, ab dem die Linie heute weiterläuft – oder None für „von vorn“. | |
| 158 | + | |
| 159 | + Nach „Nochmal“ beginnt die Wiederholung am selben Tag einen eigenen Zug vor dem | |
| 160 | + ersten Ausrutscher, statt die längst sicheren ersten Züge noch einmal zu verlangen. | |
| 161 | + """ | |
| 162 | + today = today or datetime.date.today() | |
| 163 | + retry = (self.card(course, line_id) or {}).get("retry") | |
| 164 | + if not retry or retry.get("day") != today.isoformat(): | |
| 165 | + return None | |
| 166 | + return max(0, retry["slip"] - 2) | |
| 167 | + | |
| 168 | + def record(self, course, line_id, grade, now=None, slip=None): | |
| 157 | 169 | """Ergebnis eintragen (GOOD, HARD, AGAIN oder True/False); Tage bis zur nächsten Wiederholung. |
| 158 | 170 | |
| 159 | 171 | Gut: eine Stufe höher. Schwer: Stufe bleibt, gleicher Abstand wie zuletzt (neu: 1 Tag). |
| 160 | - Nochmal: zurück auf Anfang und gleich wieder fällig (0 Tage). | |
| 172 | + Nochmal: zurück auf Anfang und gleich wieder fällig (0 Tage). ``slip`` ist der Halbzug | |
| 173 | + des ersten Ausrutschers; nach „Nochmal“ beginnt die Linie heute kurz davor (``retry_from``). | |
| 161 | 174 | """ |
| 162 | 175 | grade = GOOD if grade is True else AGAIN if grade is False else grade |
| 163 | 176 | now = now or datetime.datetime.now() |
| @@ -173,6 +186,10 @@ class Progress: | ||
| 173 | 186 | card["step"] = -1 |
| 174 | 187 | card["lapses"] = card.get("lapses", 0) + 1 |
| 175 | 188 | days = 0 |
| 189 | + if grade == AGAIN and slip is not None: | |
| 190 | + card["retry"] = {"slip": slip, "day": now.date().isoformat()} | |
| 191 | + else: | |
| 192 | + card.pop("retry", None) | |
| 176 | 193 | card["due"] = (now.date() + datetime.timedelta(days=days)).isoformat() |
| 177 | 194 | card["seen"] = now.isoformat(timespec="seconds") |
| 178 | 195 | self.save() |
modified schach/training.py +41 −4
| @@ -23,6 +23,7 @@ from .engines import maia_candidates # | ||
| 23 | 23 | |
| 24 | 24 | OPPONENT_DELAY_MS = 650 |
| 25 | 25 | TAKE_BACK_MS = 1600 |
| 26 | +SKIP_MS = 350 # Vorspielen der sicheren ersten Züge nach „Nochmal“ | |
| 26 | 27 | |
| 27 | 28 | _resources = {} |
| 28 | 29 | |
| @@ -208,7 +209,7 @@ def square_at(label, x, y): | ||
| 208 | 209 | class OpeningMixin: |
| 209 | 210 | def _opening_init(self): |
| 210 | 211 | self.course = None |
| 211 | - self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0} | |
| 212 | + self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0, "slip": None} | |
| 212 | 213 | self.course_line = None # (id, Züge): die Linie, die heute dran ist (Lernkarte) |
| 213 | 214 | self.course_recorded = False |
| 214 | 215 | self.opening_busy = False # wartet auf Gegnerzug, Bewertung oder Zurücknahme |
| @@ -852,7 +853,7 @@ class OpeningMixin: | ||
| 852 | 853 | self._leave_special() |
| 853 | 854 | self.mode = "opening" |
| 854 | 855 | self.course = course |
| 855 | - self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0} | |
| 856 | + self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0, "slip": None} | |
| 856 | 857 | self.course_line = progress().pick(course) |
| 857 | 858 | self.course_recorded = False |
| 858 | 859 | self.human = course.color |
| @@ -867,7 +868,36 @@ class OpeningMixin: | ||
| 867 | 868 | self._coach(f"{welcome}\n<b>{_escape(self._opening_line_text())}</b>", shapes=course.welcome_shapes) |
| 868 | 869 | self._changed() |
| 869 | 870 | self._refresh() |
| 870 | - self._opening_next_step() | |
| 871 | + start = progress().retry_from(course, self.course_line[0]) | |
| 872 | + if start: | |
| 873 | + self._opening_skip(self.course_line[1][:start]) | |
| 874 | + else: | |
| 875 | + self._opening_next_step() | |
| 876 | + | |
| 877 | + def _opening_skip(self, moves): | |
| 878 | + """Nach „Nochmal“: die sicheren ersten Züge zügig vorspielen, dann ist man wieder dran.""" | |
| 879 | + self.opening_busy = True | |
| 880 | + self._refresh_chrome() | |
| 881 | + | |
| 882 | + def step(gen, rest): | |
| 883 | + if gen != self.gen or self.mode != "opening" or not self.opening_busy: | |
| 884 | + return False | |
| 885 | + if not rest: | |
| 886 | + self.opening_busy = False | |
| 887 | + number = self.board.fullmove_number | |
| 888 | + self._coach(f"Wir machen gleich kurz vor der Stelle von eben weiter, bei Zug {number}.") | |
| 889 | + self._refresh() | |
| 890 | + self._opening_next_step() | |
| 891 | + return False | |
| 892 | + move = rest[0] | |
| 893 | + self.view.animate_move(self.board, move) | |
| 894 | + self.board.push(move) | |
| 895 | + self._changed() | |
| 896 | + self._refresh() | |
| 897 | + GLib.timeout_add(SKIP_MS, step, self.gen, rest[1:]) | |
| 898 | + return False | |
| 899 | + | |
| 900 | + GLib.timeout_add(SKIP_MS, step, self.gen, list(moves)) | |
| 871 | 901 | |
| 872 | 902 | def _opening_leave(self): |
| 873 | 903 | self.demo = None |
| @@ -976,6 +1006,7 @@ class OpeningMixin: | ||
| 976 | 1006 | # nicht im Kurs: erklären und zurücknehmen |
| 977 | 1007 | self.opening_busy = True |
| 978 | 1008 | self.course_stats["errors"] += 1 |
| 1009 | + self._opening_slip(ply) | |
| 979 | 1010 | self._changed() |
| 980 | 1011 | self._refresh() |
| 981 | 1012 | if verdict.kind == "book": |
| @@ -1031,6 +1062,12 @@ class OpeningMixin: | ||
| 1031 | 1062 | target = self._opening_target(options) # Hinweis folgt der Linie von heute |
| 1032 | 1063 | self.hint_move = (target or options[0]).move |
| 1033 | 1064 | self.course_stats["hints"] += 1 |
| 1065 | + self._opening_slip(len(self.board.move_stack)) | |
| 1066 | + | |
| 1067 | + def _opening_slip(self, ply): | |
| 1068 | + """Erster Ausrutscher (Fehlversuch oder Hinweis) – dort setzt ein „Nochmal“ heute wieder an.""" | |
| 1069 | + if self.course_stats.get("slip") is None: | |
| 1070 | + self.course_stats["slip"] = ply | |
| 1034 | 1071 | |
| 1035 | 1072 | def _opening_finished(self): |
| 1036 | 1073 | self.opening_busy = False |
| @@ -1054,7 +1091,7 @@ class OpeningMixin: | ||
| 1054 | 1091 | if line is not None: |
| 1055 | 1092 | level = repetition.grade(errors, repetition.hint_weight(hints, reveals), |
| 1056 | 1093 | self.course_stats["blunders"]) |
| 1057 | - days = progress().record(self.course, line, level) | |
| 1094 | + days = progress().record(self.course, line, level, slip=self.course_stats["slip"]) | |
| 1058 | 1095 | summary = f"{summary} {repetition.next_text(days, level)}" |
| 1059 | 1096 | self.final_text = summary |
| 1060 | 1097 | self._refresh() |
added tests/test_opening_retry.py +104 −0
| @@ -0,0 +1,104 @@ | ||
| 1 | +"""Kursmodus nach „Nochmal“: Die Linie beginnt kurz vor dem ersten Ausrutscher.""" | |
| 2 | + | |
| 3 | +import datetime | |
| 4 | +import unittest | |
| 5 | +from unittest import mock | |
| 6 | + | |
| 7 | +import chess | |
| 8 | + | |
| 9 | +from schach import opening, repetition, training | |
| 10 | + | |
| 11 | +COURSE = """[Event "Mini"] | |
| 12 | +[SchachColor "white"] | |
| 13 | + | |
| 14 | +1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 4. c3 Nf6 * | |
| 15 | +""" | |
| 16 | + | |
| 17 | + | |
| 18 | +class _View: | |
| 19 | + flipped = False | |
| 20 | + | |
| 21 | + def animate_move(self, _board, _move): | |
| 22 | + pass | |
| 23 | + | |
| 24 | + | |
| 25 | +class _Toggle: | |
| 26 | + def set_active(self, _value): | |
| 27 | + pass | |
| 28 | + | |
| 29 | + | |
| 30 | +class FakeWindow(training.OpeningMixin): | |
| 31 | + def __init__(self): | |
| 32 | + self._opening_init() | |
| 33 | + self.mode = "opening" | |
| 34 | + self.gen = 0 | |
| 35 | + self.view = _View() | |
| 36 | + self.list_btn = _Toggle() | |
| 37 | + self.messages = [] | |
| 38 | + | |
| 39 | + def _leave_special(self): | |
| 40 | + pass | |
| 41 | + | |
| 42 | + def _reset_game_tables(self): | |
| 43 | + pass | |
| 44 | + | |
| 45 | + def _notation(self): | |
| 46 | + return "de" | |
| 47 | + | |
| 48 | + def _coach(self, markup, ply=None, replace=False, shapes=(), demos=()): | |
| 49 | + self.messages.append(markup) | |
| 50 | + | |
| 51 | + def _changed(self): | |
| 52 | + self.gen += 1 | |
| 53 | + | |
| 54 | + def _refresh(self): | |
| 55 | + pass | |
| 56 | + | |
| 57 | + def _refresh_chrome(self): | |
| 58 | + pass | |
| 59 | + | |
| 60 | + | |
| 61 | +def _now(*_args, **_kwargs): | |
| 62 | + return datetime.datetime.now() | |
| 63 | + | |
| 64 | + | |
| 65 | +class RetryTest(unittest.TestCase): | |
| 66 | + def setUp(self): | |
| 67 | + self.course = opening.parse_course(COURSE) | |
| 68 | + self.progress = repetition.Progress() | |
| 69 | + self.line = repetition.lines(self.course)[0][0] | |
| 70 | + patches = [ | |
| 71 | + mock.patch.object(training, "progress", lambda: self.progress), | |
| 72 | + # Zeitgeber sofort ausführen | |
| 73 | + mock.patch.object(training.GLib, "timeout_add", lambda _ms, fn, *a: fn(*a)), | |
| 74 | + ] | |
| 75 | + for patch in patches: | |
| 76 | + patch.start() | |
| 77 | + self.addCleanup(patch.stop) | |
| 78 | + self.window = FakeWindow() | |
| 79 | + | |
| 80 | + def test_fresh_line_starts_at_the_beginning(self): | |
| 81 | + self.window.start_opening(self.course) | |
| 82 | + self.assertEqual(len(self.window.board.move_stack), 0) | |
| 83 | + | |
| 84 | + def test_again_starts_one_own_move_before_the_slip(self): | |
| 85 | + # Ausrutscher bei 4. c3 (Halbzug 6): weiter ab 3. Lc4 (Halbzug 4) | |
| 86 | + self.progress.record(self.course, self.line, repetition.AGAIN, slip=6) | |
| 87 | + self.window.start_opening(self.course) | |
| 88 | + board = self.window.board | |
| 89 | + self.assertEqual([m.uci() for m in board.move_stack], ["e2e4", "e7e5", "g1f3", "b8c6"]) | |
| 90 | + self.assertEqual(board.turn, chess.WHITE) | |
| 91 | + self.assertFalse(self.window.opening_busy) | |
| 92 | + self.assertIn("Zug 3", self.window.messages[-1]) | |
| 93 | + | |
| 94 | + def test_first_slip_is_remembered(self): | |
| 95 | + self.window.start_opening(self.course) | |
| 96 | + self.window.board.push_san("e4") | |
| 97 | + self.window.board.push_san("e5") | |
| 98 | + self.window._opening_slip(2) | |
| 99 | + self.window._opening_slip(4) # späterer Ausrutscher zählt nicht | |
| 100 | + self.assertEqual(self.window.course_stats["slip"], 2) | |
| 101 | + | |
| 102 | + | |
| 103 | +if __name__ == "__main__": | |
| 104 | + unittest.main() | |
modified tests/test_repetition.py +23 −0
| @@ -146,6 +146,29 @@ class ProgressTest(unittest.TestCase): | ||
| 146 | 146 | self.assertEqual(self.progress.record(self.course, self.main, repetition.HARD, at(4)), 3) |
| 147 | 147 | self.assertEqual(self.progress.record(self.course, self.main, repetition.GOOD, at(7)), 7) |
| 148 | 148 | |
| 149 | + def test_again_starts_one_own_move_before_the_slip(self): | |
| 150 | + # Ausrutscher beim 3. Zug von Weiß (Halbzug 4): neu ab dem 2. Zug von Weiß (Halbzug 2) | |
| 151 | + self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=4) | |
| 152 | + self.assertEqual(self.progress.retry_from(self.course, self.main, TODAY), 2) | |
| 153 | + self.assertIsNone(self.progress.retry_from(self.course, self.second, TODAY)) | |
| 154 | + | |
| 155 | + def test_again_on_first_move_starts_at_the_beginning(self): | |
| 156 | + self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=0) | |
| 157 | + self.assertEqual(self.progress.retry_from(self.course, self.main, TODAY), 0) | |
| 158 | + | |
| 159 | + def test_retry_only_on_the_same_day(self): | |
| 160 | + self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=4) | |
| 161 | + self.assertIsNone(self.progress.retry_from(self.course, self.main, TODAY + datetime.timedelta(days=1))) | |
| 162 | + | |
| 163 | + def test_retry_cleared_after_next_attempt(self): | |
| 164 | + self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=4) | |
| 165 | + self.progress.record(self.course, self.main, repetition.GOOD, at(0)) | |
| 166 | + self.assertIsNone(self.progress.retry_from(self.course, self.main, TODAY)) | |
| 167 | + | |
| 168 | + def test_hard_does_not_shorten(self): | |
| 169 | + self.progress.record(self.course, self.main, repetition.HARD, at(0), slip=4) | |
| 170 | + self.assertIsNone(self.progress.retry_from(self.course, self.main, TODAY)) | |
| 171 | + | |
| 149 | 172 | def test_saved_and_loaded(self): |
| 150 | 173 | with tempfile.TemporaryDirectory() as tmp: |
| 151 | 174 | path = os.path.join(tmp, "sub", "training.json") |