feat(kurse): one card per course decision, auto-play what sits
public
feat(kurse): one card per course decision, auto-play what sits A per-line card failed the whole line for one wrong move at move 7 and made kids replay the shared first moves every time. A card is now a position with the player to move plus the course move, keyed by Zobrist hash, so transpositions share a card and course edits keep progress. Lines stay the unit of play: the one with the most urgent decision is picked, moves up to the first due or new decision are auto-played (both sides), later own moves that still sit are played for you. Nothing due on the line: player makes every move. Each decision is recorded when the course move is found, with its own slips and hints. Supersedes the restart-before-slip logic. Course dialog counts due moves and new lines; the new-line limit only counts decisions unique to a line (the shared first move would otherwise start every line). Migration: line cards hand their FSRS state to every decision on the line (earlier due wins) and stay as course_line_done with their log. Adds cards.content (FEN) to existing databases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 2457cf10d6673bf48a1906a35c7b23de41a19340 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 36c9bf3f9c8f |
| Children | 00427af512e9 ed3706c93b63 |
| Pushlog | by timo via ssh by timo via ssh |
modified ROADMAP.md +1 −1
| @@ -598,7 +598,7 @@ behält Reihenfolge und offene Fragen. | ||
| 598 | 598 | | Datei | Inhalt | Status | |
| 599 | 599 | | --- | --- | --- | |
| 600 | 600 | | [`roadmap/analysebrett.md`](roadmap/analysebrett.md) | Analysebrett mit Variantenbaum, „Warum?“ und „Warum nicht?“ | Stufen 1–4 umgesetzt | |
| 601 | -| [`roadmap/training.md`](roadmap/training.md) | Rätsel aus eigenen Partien, „Aus Fehlern lernen“, Wiederholung mit FSRS und SQLite | Schritte 1 und 2 umgesetzt (ohne Karte pro Entscheidung) | | |
| 601 | +| [`roadmap/training.md`](roadmap/training.md) | Rätsel aus eigenen Partien, „Aus Fehlern lernen“, Wiederholung mit FSRS und SQLite | Schritte 1 und 2 umgesetzt, zurückgenommene Züge im PGN | | |
| 602 | 602 | |
| 603 | 603 | ## Reihenfolge |
| 604 | 604 | |
modified roadmap/training.md +29 −15
| @@ -68,7 +68,7 @@ wie in `opening.py`). `training.json` wird einmal übernommen. | ||
| 68 | 68 | |
| 69 | 69 | | Tabelle | Inhalt | |
| 70 | 70 | | --- | --- | |
| 71 | -| `cards` | Art (Kurslinie, später Kursentscheidung, Rätsel A, Patzer B, Lichess-Rätsel), Herkunft (Kurs, Partiedatei und Halbzug, Lichess-ID), Schlüssel, später Inhalt als PGN-Text (FEN, Lösung, `[%cal]`, Kommentare), Themen, Wertung. Dazu der Lernstand für FSRS: Zustand, Stabilität, Schwierigkeit, fällig am, zuletzt, und „heute noch einmal“ mit dem Halbzug des Ausrutschers | | |
| 71 | +| `cards` | Art (Kursentscheidung, später Rätsel A, Patzer B, Lichess-Rätsel), Herkunft (Kurs, später Partiedatei und Halbzug, Lichess-ID), Schlüssel (bei Kursen der Stellungs-Hash), Inhalt (bei Kursen die FEN, später PGN-Text mit Lösung, `[%cal]`, Kommentaren), später Themen und Wertung. Dazu der Lernstand für FSRS: Zustand, Stabilität, Schwierigkeit, fällig am, zuletzt, und „heute noch einmal“ | | |
| 72 | 72 | | `reviews` | eine Zeile pro Versuch: Karte, Zeitpunkt, Bewertung, Fehler, Hinweise, Patzer, Dauer, später ob direkt oder über ein Geschwister | |
| 73 | 73 | |
| 74 | 74 | Der Inhalt bleibt PGN, so kann man eine Karte weiter mit Strg+C kopieren oder zum Kurs |
| @@ -161,15 +161,29 @@ Lichess gemeldet, sonst stimmt die Wertu | ||
| 161 | 161 | Bisher fing jede Wiederholung einer Linie bei Zug 1 an. Die ersten Züge sind in vielen |
| 162 | 162 | Linien gleich und sitzen längst. Sie noch einmal zu ziehen, bringt nichts mehr und nervt. |
| 163 | 163 | |
| 164 | -1. **Sofortiges Nochmal** (umgesetzt 22.11.2026): Wurde eine Linie heute mit „Nochmal“ | |
| 165 | - bewertet, beginnt sie beim nächsten Mal **einen eigenen Zug vor dem ersten | |
| 166 | - Ausrutscher** (Fehlversuch oder Hinweis). Die Züge bis dorthin spielt das Programm | |
| 167 | - zügig vor. Am nächsten Tag läuft die Linie wieder ganz. | |
| 168 | -2. **Eine Karte pro Entscheidung** (kommt mit SQLite und FSRS): Eine Karte ist eine | |
| 169 | - Stellung plus Kurszug, Zugumstellungen fallen über `opening.key` zusammen. Bei | |
| 170 | - einer Wiederholung zieht das Programm alle nicht fälligen Züge selbst, Florian zieht | |
| 171 | - nur die fälligen. Eine neue Linie und eine Linie, deren Züge alle sitzen, läuft | |
| 172 | - einmal ganz am Stück, weil man in der Partie auch alles hintereinander finden muss. | |
| 164 | +**Eine Karte pro Entscheidung** (umgesetzt 22.11.2026): Eine Karte ist eine Stellung, in | |
| 165 | +der man selbst am Zug ist, plus Kurszug. Schlüssel ist der Zobrist-Hash (`opening.key`), | |
| 166 | +Zugumstellungen fallen also zusammen, und Kursänderungen lassen den Lernstand stehen. | |
| 167 | + | |
| 168 | +- Gespielt wird weiter in Linien. Dran ist die Linie mit der dringendsten Entscheidung. | |
| 169 | +- Bis zur ersten fälligen oder neuen Entscheidung spielt das Programm zügig vor (350 ms | |
| 170 | + pro Halbzug, beide Seiten). Spätere eigene Züge, die noch sitzen, zieht es selbst | |
| 171 | + („sitzt, den spiele ich vor“). | |
| 172 | +- Ist auf der Linie nichts fällig und nichts neu (freiwilliges Üben), zieht man alles | |
| 173 | + selbst. | |
| 174 | +- Weicht man von der Linie ab (Zugumstellung), wird jede unbekannte Stellung gefragt. | |
| 175 | +- Eingetragen wird jede Entscheidung, sobald der Kurszug gefunden ist, mit ihren eigenen | |
| 176 | + Fehlversuchen, Hinweisen und Patzern. Am Ende sagt die Linie, wann ihr Schwächstes | |
| 177 | + wiederkommt („Fast. Dieser Zug kommt morgen.“). | |
| 178 | +- Nach „Nochmal“ ist die Entscheidung heute noch einmal fällig. Die Linie beginnt dann | |
| 179 | + direkt davor; das ersetzt den Neustart kurz vor dem Fehler vom Vormittag. | |
| 180 | +- Die Kursauswahl zählt fällige Züge und neue Linien („Heute dran: 2 Züge zum Wiederholen | |
| 181 | + und 1 neue Linie“). Linien zu zählen ginge nicht: Die ersten Züge gehören zu jeder Linie. | |
| 182 | +- Das Tageslimit für neue Linien zählt nur Entscheidungen, die es allein auf einer Linie | |
| 183 | + gibt. Sonst wären mit dem ersten Zug alle Linien „angefangen“. | |
| 184 | +- Übernahme: Die Linienkarten vom selben Tag vererben ihren Lernstand an jede Entscheidung | |
| 185 | + auf der Linie, bei mehreren die früher fällige. Sie bleiben als `course_line_done` samt | |
| 186 | + Protokoll liegen. | |
| 173 | 187 | |
| 174 | 188 | ## Zurückgenommene Züge im PGN |
| 175 | 189 | |
| @@ -191,11 +205,11 @@ Erklärung, was passiert ist, und Art B bekommt später genau diese Fehler als Rohstoff. | ||
| 191 | 205 | |
| 192 | 206 | ## Reihenfolge |
| 193 | 207 | |
| 194 | -1. Kurse: sofortiges Nochmal kurz vor dem Fehler. **Umgesetzt.** | |
| 195 | -2. SQLite und FSRS für die vorhandenen Kurslinien, Übernahme aus `training.json`, | |
| 196 | - Einstellung für den Zielwert. **Umgesetzt** (22.11.2026, `schach/repetition.py`, | |
| 197 | - `training.sqlite`; `training.json` bleibt als Sicherung liegen). Offen: eine Karte pro | |
| 198 | - Entscheidung. | |
| 208 | +1. Kurse: sofortiges Nochmal kurz vor dem Fehler. **Umgesetzt**, inzwischen aufgegangen in | |
| 209 | + der Karte pro Entscheidung. | |
| 210 | +2. SQLite und FSRS für die Kurse, Übernahme aus `training.json`, Einstellung für den | |
| 211 | + Zielwert, eine Karte pro Entscheidung. **Umgesetzt** (22.11.2026, `schach/repetition.py`, | |
| 212 | + `training.sqlite`; `training.json` bleibt als Sicherung liegen). | |
| 199 | 213 | 3. Fehlgeschlagene Lichess-Rätsel werden Karten (vorhandene Rätselansicht). |
| 200 | 214 | 4. Art A: Suche nach der Partie gegen Maia, mit Einzügern; aufheben als Karte. |
| 201 | 215 | 5. Art B: „Aus Fehlern lernen“ nach Lichess-Vorbild, schwere Fehler als Karte. |
modified schach/repetition.py +194 −154
| @@ -1,14 +1,16 @@ | ||
| 1 | -"""Wiederholung nach Fehlern – Lernkarten wie bei Anki, eine Karte pro Kurslinie. | |
| 1 | +"""Wiederholung nach Fehlern – Lernkarten wie bei Anki, eine Karte pro Entscheidung im Kurs. | |
| 2 | 2 | |
| 3 | -Eine Linie ist ein Weg durch den Kurs vom Start bis zum Ende einer Variante. Geplant wird | |
| 4 | -mit FSRS (``py-fsrs``, wie Anki seit 23.10): Gut, Schwer und Nochmal verändern Stabilität | |
| 5 | -und Schwierigkeit der Karte, daraus folgt der nächste Termin. Nach „Nochmal“ ist die Linie | |
| 6 | -zusätzlich heute noch einmal fällig – nach den anderen fälligen – und beginnt kurz vor | |
| 7 | -dem ersten Ausrutscher (``retry_from``). | |
| 3 | +Eine Entscheidung ist eine Stellung, in der man selbst am Zug ist und der Kurs einen Zug | |
| 4 | +kennt (Schlüssel: Zobrist-Hash, Zugumstellungen fallen zusammen). Gespielt wird weiter in | |
| 5 | +Linien – Wegen durch den Kurs vom Start bis zum Ende einer Variante. Dran ist die Linie | |
| 6 | +mit der dringendsten Entscheidung; Züge, die noch sitzen, spielt das Programm selbst | |
| 7 | +(``asked``). Geplant wird mit FSRS (``py-fsrs``, wie Anki seit 23.10): Gut, Schwer und | |
| 8 | +Nochmal verändern Stabilität und Schwierigkeit der Karte, daraus folgt der nächste Termin. | |
| 9 | +Nach „Nochmal“ ist die Entscheidung zusätzlich heute noch einmal fällig. | |
| 8 | 10 | |
| 9 | 11 | Reine Logik ohne GTK. Gespeichert in SQLite (Tabellen ``cards`` und ``reviews``, siehe |
| 10 | 12 | ``roadmap/training.md``), eine Datei pro Benutzer. Der alte Stand aus ``training.json`` |
| 11 | -wird einmal übernommen. | |
| 13 | +und die Linienkarten von vorher werden einmal übernommen. | |
| 12 | 14 | """ |
| 13 | 15 | |
| 14 | 16 | import datetime |
| @@ -16,6 +18,8 @@ import json | ||
| 16 | 18 | import os |
| 17 | 19 | import sqlite3 |
| 18 | 20 | |
| 21 | +import chess | |
| 22 | + | |
| 19 | 23 | from . import opening |
| 20 | 24 | |
| 21 | 25 | try: |
| @@ -25,7 +29,9 @@ except ImportError: # schach.sh holt es | ||
| 25 | 29 | |
| 26 | 30 | GOOD, HARD, AGAIN = "good", "hard", "again" |
| 27 | 31 | NEW_PER_DAY = 3 # neue Linien pro Kurs und Tag – Wiederholungen kommen immer dran |
| 28 | -KIND_LINE = "course_line" | |
| 32 | +KIND_MOVE = "course_move" # eine Entscheidung im Kurs | |
| 33 | +KIND_LINE = "course_line" # früher: eine Karte pro Linie; wird beim Öffnen des Kurses umgewandelt | |
| 34 | +KIND_LINE_DONE = "course_line_done" # umgewandelte Linienkarte, bleibt mit ihrem Protokoll erhalten | |
| 29 | 35 | MAX_DAYS = 60 # länger nicht: sonst merkt man sich eher die Stellung als das Muster |
| 30 | 36 | RETENTION = {"relaxed": 0.80, "balanced": 0.85, "thorough": 0.92} # Einstellung „Wiederholen“ |
| 31 | 37 | DEFAULT_RETENTION = "balanced" |
| @@ -34,9 +40,9 @@ LEGACY_LADDER = [1, 3, 7, 14, 30, 60, 12 | ||
| 34 | 40 | SCHEMA = """ |
| 35 | 41 | CREATE TABLE IF NOT EXISTS cards ( |
| 36 | 42 | id INTEGER PRIMARY KEY, |
| 37 | - kind TEXT NOT NULL, -- course_line; später auch Rätsel, Patzer, Lichess-Rätsel | |
| 43 | + kind TEXT NOT NULL, -- course_move; später auch Rätsel, Patzer, Lichess-Rätsel | |
| 38 | 44 | source TEXT NOT NULL, -- Kurstitel |
| 39 | - key TEXT NOT NULL, -- Linie: UCI-Züge, mit Leerzeichen getrennt | |
| 45 | + key TEXT NOT NULL, -- Entscheidung: Zobrist-Hash der Stellung (hex) | |
| 40 | 46 | created TEXT NOT NULL, -- Ortsdatum des ersten Versuchs (Tageslimit für Neues) |
| 41 | 47 | state INTEGER NOT NULL, -- fsrs.State |
| 42 | 48 | step INTEGER, |
| @@ -45,7 +51,8 @@ CREATE TABLE IF NOT EXISTS cards ( | ||
| 45 | 51 | due TEXT NOT NULL, -- UTC, ISO 8601 |
| 46 | 52 | last_review TEXT, -- UTC, ISO 8601 |
| 47 | 53 | again_day TEXT, -- Ortsdatum eines „Nochmal“: heute noch einmal fällig |
| 48 | - slip INTEGER, -- Halbzug des ersten Ausrutschers dabei | |
| 54 | + slip INTEGER, -- nicht mehr benutzt (Halbzug des Ausrutschers bei Linienkarten) | |
| 55 | + content TEXT, -- zum Nachsehen: FEN der Stellung | |
| 49 | 56 | UNIQUE (kind, source, key) |
| 50 | 57 | ); |
| 51 | 58 | CREATE TABLE IF NOT EXISTS reviews ( |
| @@ -60,13 +67,14 @@ CREATE TABLE IF NOT EXISTS reviews ( | ||
| 60 | 67 | ); |
| 61 | 68 | CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT); |
| 62 | 69 | """ |
| 70 | +STATE_COLUMNS = ("state", "step", "stability", "difficulty", "due", "last_review", "again_day") | |
| 63 | 71 | |
| 64 | 72 | |
| 65 | 73 | def lines(course): |
| 66 | 74 | """Alle Linien des Kurses: [(id, [Zug, …])], Hauptvariante zuerst. |
| 67 | 75 | |
| 68 | 76 | Eine Linie endet, wo sie per Zugumstellung in eine Stellung kommt, die schon zu einer |
| 69 | - früheren Linie gehört – sonst stünde derselbe Rest des Kurses mehrfach als Karte da. | |
| 77 | + früheren Linie gehört – sonst stünde derselbe Rest des Kurses mehrfach da. | |
| 70 | 78 | """ |
| 71 | 79 | found = [] |
| 72 | 80 | expanded = set() |
| @@ -87,6 +95,27 @@ def lines(course): | ||
| 87 | 95 | return found |
| 88 | 96 | |
| 89 | 97 | |
| 98 | +def position_key(board): | |
| 99 | + return format(opening.key(board), "016x") | |
| 100 | + | |
| 101 | + | |
| 102 | +def decisions(course, moves): | |
| 103 | + """Eigene Entscheidungen entlang einer Zugfolge: [(Halbzug, Schlüssel)]. | |
| 104 | + | |
| 105 | + Das sind die Stellungen, in denen man selbst am Zug ist und der Kurs einen Zug kennt. | |
| 106 | + Bricht ab, wo ein Zug nicht mehr legal ist (alte Linie, geänderter Kurs). | |
| 107 | + """ | |
| 108 | + board = course.start() | |
| 109 | + found = [] | |
| 110 | + for ply, move in enumerate(moves): | |
| 111 | + if move not in board.legal_moves: | |
| 112 | + break | |
| 113 | + if board.turn == course.color and course.options(board): | |
| 114 | + found.append((ply, position_key(board))) | |
| 115 | + board.push(move) | |
| 116 | + return found | |
| 117 | + | |
| 118 | + | |
| 90 | 119 | def _date(text): |
| 91 | 120 | try: |
| 92 | 121 | return datetime.date.fromisoformat(text) |
| @@ -113,12 +142,12 @@ def _midnight(day): | ||
| 113 | 142 | return _utc(datetime.datetime.combine(day, datetime.time())) |
| 114 | 143 | |
| 115 | 144 | |
| 116 | -# GOOD/HARD/AGAIN → fsrs.Rating für Kurslinien. Ein falscher Kurszug heißt: Der Kurszug war nicht | |
| 117 | -# abrufbar – für FSRS ein Vergessen, auch wenn es nur einer war („Schwer“). FSRS rechnet dann die | |
| 118 | -# Stabilität nach dem Vergessen aus der alten: frische Linien kommen morgen wieder, gut sitzende erst | |
| 119 | -# nach einigen Tagen (fließend, ohne festen Wert). Heute noch einmal kommt nur „Nochmal“ (mehrere | |
| 120 | -# Ausrutscher oder ein Patzer), siehe again_day. FSRS-„Schwer“ hieße „gewusst, aber mühsam“ und ließe | |
| 121 | -# die Stabilität sogar wachsen – das passt zu einem Fehlversuch nicht. | |
| 145 | +# GOOD/HARD/AGAIN → fsrs.Rating für Kursentscheidungen. Ein falscher Kurszug heißt: Der Kurszug war | |
| 146 | +# nicht abrufbar – für FSRS ein Vergessen, auch wenn es nur einer war („Schwer“). FSRS rechnet dann | |
| 147 | +# die Stabilität nach dem Vergessen aus der alten: frische Züge kommen morgen wieder, gut sitzende | |
| 148 | +# erst nach einigen Tagen (fließend, ohne festen Wert). Heute noch einmal kommt nur „Nochmal“ | |
| 149 | +# (mehrere Ausrutscher oder ein Patzer), siehe again_day. FSRS-„Schwer“ hieße „gewusst, aber mühsam“ | |
| 150 | +# und ließe die Stabilität sogar wachsen – das passt zu einem Fehlversuch nicht. | |
| 122 | 151 | RATINGS = {} |
| 123 | 152 | if fsrs is not None: |
| 124 | 153 | RATINGS = {GOOD: fsrs.Rating.Good, HARD: fsrs.Rating.Again, AGAIN: fsrs.Rating.Again} |
| @@ -142,6 +171,9 @@ class Progress: | ||
| 142 | 171 | self.db = sqlite3.connect(path or ":memory:") |
| 143 | 172 | self.db.row_factory = sqlite3.Row |
| 144 | 173 | self.db.executescript(SCHEMA) |
| 174 | + columns = {row["name"] for row in self.db.execute("PRAGMA table_info(cards)")} | |
| 175 | + if "content" not in columns: # Datenbank vom ersten Tag mit FSRS | |
| 176 | + self.db.execute("ALTER TABLE cards ADD COLUMN content TEXT") | |
| 145 | 177 | self.fuzz = fuzz |
| 146 | 178 | self.set_retention(retention) |
| 147 | 179 | if legacy_path: |
| @@ -161,21 +193,17 @@ class Progress: | ||
| 161 | 193 | |
| 162 | 194 | # --- Speicher ------------------------------------------------------------ |
| 163 | 195 | |
| 164 | - def _rows(self, course): | |
| 196 | + def _rows(self, course, kind=KIND_MOVE): | |
| 165 | 197 | query = "SELECT * FROM cards WHERE kind = ? AND source = ?" |
| 166 | - return {row["key"]: row for row in self.db.execute(query, (KIND_LINE, course.title))} | |
| 198 | + return {row["key"]: row for row in self.db.execute(query, (kind, course.title))} | |
| 167 | 199 | |
| 168 | - def _row(self, course, line_id): | |
| 200 | + def _row(self, course, key): | |
| 169 | 201 | query = "SELECT * FROM cards WHERE kind = ? AND source = ? AND key = ?" |
| 170 | - return self.db.execute(query, (KIND_LINE, course.title, line_id)).fetchone() | |
| 171 | - | |
| 172 | - def _merge(self, keep, drop): | |
| 173 | - """Karte ``drop`` in ``keep`` aufgehen lassen; ihre Versuche bleiben erhalten.""" | |
| 174 | - self.db.execute("UPDATE reviews SET card_id = ? WHERE card_id = ?", (keep, drop)) | |
| 175 | - self.db.execute("DELETE FROM cards WHERE id = ?", (drop,)) | |
| 202 | + return self.db.execute(query, (KIND_MOVE, course.title, key)).fetchone() | |
| 176 | 203 | |
| 177 | 204 | def _import_json(self, path): |
| 178 | - """Lernstand aus ``training.json`` (feste Stufenleiter) einmal übernehmen. | |
| 205 | + """Lernstand aus ``training.json`` (feste Stufenleiter) einmal übernehmen – als Linienkarten, | |
| 206 | + die ``_convert_lines`` beim Öffnen des Kurses in Entscheidungen umwandelt. | |
| 179 | 207 | |
| 180 | 208 | Die Stabilität ist der letzte Abstand der Leiter, die Schwierigkeit mittel (5 von 10). |
| 181 | 209 | Fälligkeit und ein offenes „Nochmal“ von heute bleiben, wie sie waren. |
| @@ -198,21 +226,57 @@ class Progress: | ||
| 198 | 226 | retry = card.get("retry") or {} |
| 199 | 227 | self.db.execute( |
| 200 | 228 | "INSERT OR IGNORE INTO cards (kind, source, key, created, state, step, stability," |
| 201 | - " difficulty, due, last_review, again_day, slip) VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?)", | |
| 229 | + " difficulty, due, last_review, again_day) VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?)", | |
| 202 | 230 | (KIND_LINE, title, line_id, card.get("first") or datetime.date.today().isoformat(), |
| 203 | 231 | int(fsrs.State.Review), float(days), 5.0, due.isoformat(), |
| 204 | 232 | _utc(datetime.datetime.fromisoformat(seen)).isoformat() if seen else None, |
| 205 | - retry.get("day"), retry.get("slip")), | |
| 233 | + retry.get("day")), | |
| 206 | 234 | ) |
| 207 | 235 | self.db.execute("INSERT INTO meta (key, value) VALUES ('imported_json', ?)", |
| 208 | 236 | (datetime.datetime.now().isoformat(timespec="seconds"),)) |
| 209 | 237 | self.db.commit() |
| 210 | 238 | |
| 239 | + def _convert_lines(self, course): | |
| 240 | + """Linienkarten (vor der Karte pro Entscheidung) in Entscheidungen umwandeln. | |
| 241 | + | |
| 242 | + Jede Entscheidung auf der Linie erbt deren Lernstand. Liegt sie auf mehreren Linien, gilt | |
| 243 | + die früher fällige. Die Linienkarte bleibt als ``course_line_done`` samt Protokoll liegen. | |
| 244 | + """ | |
| 245 | + legacy = list(self.db.execute("SELECT * FROM cards WHERE kind = ? AND source = ?", | |
| 246 | + (KIND_LINE, course.title))) | |
| 247 | + if not legacy: | |
| 248 | + return | |
| 249 | + known = {key: (row["id"], row["due"]) for key, row in self._rows(course).items()} | |
| 250 | + for line in legacy: | |
| 251 | + try: | |
| 252 | + moves = [chess.Move.from_uci(uci) for uci in line["key"].split()] | |
| 253 | + except ValueError: | |
| 254 | + moves = [] | |
| 255 | + values = tuple(line[c] for c in STATE_COLUMNS) | |
| 256 | + board = course.start() | |
| 257 | + for ply, key in decisions(course, moves): | |
| 258 | + while len(board.move_stack) < ply: | |
| 259 | + board.push(moves[len(board.move_stack)]) | |
| 260 | + if key not in known: | |
| 261 | + cursor = self.db.execute( | |
| 262 | + "INSERT INTO cards (kind, source, key, created, content, " + ", ".join(STATE_COLUMNS) | |
| 263 | + + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", | |
| 264 | + (KIND_MOVE, course.title, key, line["created"], board.fen()) + values, | |
| 265 | + ) | |
| 266 | + known[key] = (cursor.lastrowid, line["due"]) | |
| 267 | + elif _instant(line["due"]) < _instant(known[key][1]): | |
| 268 | + self.db.execute("UPDATE cards SET " + ", ".join(f"{c} = ?" for c in STATE_COLUMNS) | |
| 269 | + + " WHERE id = ?", values + (known[key][0],)) | |
| 270 | + known[key] = (known[key][0], line["due"]) | |
| 271 | + self.db.execute("UPDATE cards SET kind = ? WHERE id = ?", (KIND_LINE_DONE, line["id"])) | |
| 272 | + self.db.commit() | |
| 273 | + | |
| 211 | 274 | # --- Abfragen ------------------------------------------------------------ |
| 212 | 275 | |
| 213 | - def card(self, course, line_id): | |
| 214 | - """Gespeicherte Karte als dict, ``due_day`` als Ortsdatum – oder None, wenn neu.""" | |
| 215 | - row = self._row(course, line_id) | |
| 276 | + def card(self, course, board): | |
| 277 | + """Gespeicherte Karte zur Stellung als dict, ``due_day`` als Ortsdatum – oder None, wenn neu.""" | |
| 278 | + self._convert_lines(course) | |
| 279 | + row = self._row(course, position_key(board)) | |
| 216 | 280 | if row is None: |
| 217 | 281 | return None |
| 218 | 282 | card = dict(row) |
| @@ -224,115 +288,111 @@ class Progress: | ||
| 224 | 288 | return today |
| 225 | 289 | return _local_day(row["due"]) |
| 226 | 290 | |
| 227 | - def _migrate(self, course, ids, today=None): | |
| 228 | - """Lernstand von Linien übertragen, deren Kennung sich geändert hat. | |
| 291 | + def _order(self, course, today): | |
| 292 | + """Linien nach Dringlichkeit ihrer Entscheidungen. | |
| 229 | 293 | |
| 230 | - Gekürzt: Früher liefen Linien nach einer Zugumstellung weiter; jetzt enden sie dort. | |
| 231 | - Mehrere alte Karten können auf dieselbe neue fallen – dann gilt die früher fällige. | |
| 232 | - Verlängert: Der Kurs führt eine Linie weiter. Die Karte geht auf die erste Fortsetzung | |
| 233 | - (die Hauptvariante) und ist gleich fällig, damit die neuen Züge bald drankommen. | |
| 234 | - ``ids`` in Kursreihenfolge. | |
| 294 | + Gruppen: 0 eine Entscheidung fällig, 1 neue Entscheidungen (bis zum Tageslimit an neuen | |
| 295 | + Linien), 2 nichts fällig, 3 neu über dem Limit. ``fresh``: Die Linie bringt heute Neues | |
| 296 | + (im Limit) – auch wenn sie wegen der ersten Züge schon fällig ist. Gruppe 2 und 3 sind freiwilliges | |
| 297 | + Weiterüben, wenn für heute alles geschafft ist. Innerhalb der Fälligen kommt zuerst, | |
| 298 | + was am längsten wartet – ein „Nochmal“ von eben also nach den anderen. | |
| 235 | 299 | """ |
| 300 | + self._convert_lines(course) | |
| 301 | + all_lines = lines(course) | |
| 236 | 302 | rows = self._rows(course) |
| 237 | - if not rows: | |
| 238 | - return | |
| 239 | - today = today or datetime.date.today() | |
| 240 | - changed = False | |
| 241 | - for old in [i for i in rows if i not in ids]: | |
| 242 | - row = rows[old] | |
| 243 | - target = max((i for i in ids if i and old.startswith(i + " ")), key=len, default=None) | |
| 244 | - if target is None: | |
| 245 | - target = next((i for i in ids if i.startswith(old + " ")), None) | |
| 246 | - if target is None or target in rows: | |
| 247 | - continue | |
| 248 | - due = min(row["due"], _midnight(today).isoformat()) | |
| 249 | - self.db.execute("UPDATE cards SET key = ?, due = ? WHERE id = ?", (target, due, row["id"])) | |
| 303 | + todays = today.isoformat() | |
| 304 | + found = [] | |
| 305 | + uses = {} # wie viele Linien durch eine Entscheidung gehen | |
| 306 | + for index, (line_id, moves) in enumerate(all_lines): | |
| 307 | + keys = [key for _ply, key in decisions(course, moves)] | |
| 308 | + for key in set(keys): | |
| 309 | + uses[key] = uses.get(key, 0) + 1 | |
| 310 | + found.append((index, line_id, moves, keys, [rows[k] for k in keys if k in rows])) | |
| 311 | + # Heute angefangen: eine Entscheidung, die es nur auf dieser Linie gibt, ist heute neu dazugekommen. | |
| 312 | + # Gemeinsame (die ersten Züge) zählen nicht – sonst wären mit ihnen alle Linien angefangen. | |
| 313 | + started = {line_id for _i, line_id, _m, _k, cards in found | |
| 314 | + if any(c["created"] == todays and uses[c["key"]] == 1 for c in cards)} | |
| 315 | + allowance = max(0, NEW_PER_DAY - len(started)) | |
| 316 | + ranked = [] | |
| 317 | + for index, line_id, moves, keys, cards in found: | |
| 318 | + due = sorted((self._due_day(c, today), c["last_review"] or "") for c in cards) | |
| 319 | + fresh = False # bringt heute Neues (im Tageslimit) | |
| 320 | + if len(cards) < len(keys): | |
| 321 | + fresh = line_id in started or allowance > 0 | |
| 322 | + allowance -= fresh and line_id not in started | |
| 323 | + if due and due[0][0] <= today: | |
| 324 | + rank = (0, due[0][0], due[0][1], index) | |
| 325 | + elif len(cards) < len(keys): | |
| 326 | + rank = (1 if fresh else 3, datetime.date.min, "", index) | |
| 250 | 327 | else: |
| 251 | - existing = rows.get(target) | |
| 252 | - if existing is None: | |
| 253 | - self.db.execute("UPDATE cards SET key = ? WHERE id = ?", (target, row["id"])) | |
| 254 | - elif _instant(row["due"]) < _instant(existing["due"]): | |
| 255 | - self._merge(row["id"], existing["id"]) | |
| 256 | - self.db.execute("UPDATE cards SET key = ? WHERE id = ?", (target, row["id"])) | |
| 257 | - else: | |
| 258 | - self._merge(existing["id"], row["id"]) | |
| 259 | - del rows[old] | |
| 260 | - rows = self._rows(course) | |
| 261 | - changed = True | |
| 262 | - if changed: | |
| 263 | - self.db.commit() | |
| 264 | - | |
| 265 | - def _order(self, course, today): | |
| 266 | - """Linien nach Dringlichkeit. | |
| 267 | - | |
| 268 | - Gruppen: 0 fällig, 1 neu (bis zum Tageslimit), 2 noch nicht fällig, 3 neu über dem Limit. | |
| 269 | - Gruppe 2 und 3 sind freiwilliges Weiterüben, wenn für heute alles geschafft ist. | |
| 270 | - Innerhalb der Fälligen kommt zuerst, was am längsten wartet – ein „Nochmal“ von eben | |
| 271 | - also nach den anderen. | |
| 272 | - """ | |
| 273 | - all_lines = lines(course) | |
| 274 | - self._migrate(course, [line_id for line_id, _m in all_lines], today) | |
| 275 | - rows = self._rows(course) | |
| 276 | - started = sum(1 for row in rows.values() if row["created"] == today.isoformat()) | |
| 277 | - allowance = max(0, NEW_PER_DAY - started) | |
| 278 | - ranked = [] | |
| 279 | - for index, (line_id, moves) in enumerate(all_lines): | |
| 280 | - row = rows.get(line_id) | |
| 281 | - if row is None: | |
| 282 | - group = 1 if allowance > 0 else 3 | |
| 283 | - allowance -= group == 1 | |
| 284 | - ranked.append(((group, datetime.date.min, "", index), line_id, moves)) | |
| 285 | - continue | |
| 286 | - due = self._due_day(row, today) | |
| 287 | - group = 0 if due <= today else 2 | |
| 288 | - ranked.append(((group, due, row["last_review"] or "", index), line_id, moves)) | |
| 328 | + rank = (2, due[0][0] if due else datetime.date.max, "", index) | |
| 329 | + ranked.append((rank, line_id, moves, fresh)) | |
| 289 | 330 | ranked.sort(key=lambda item: item[0]) |
| 290 | 331 | return ranked |
| 291 | 332 | |
| 292 | 333 | def pick(self, course, today=None): |
| 293 | - """Die fälligste Linie: (id, Züge).""" | |
| 334 | + """Die dringendste Linie: (id, Züge).""" | |
| 294 | 335 | today = today or datetime.date.today() |
| 295 | - _rank, line_id, moves = self._order(course, today)[0] | |
| 336 | + _rank, line_id, moves, _fresh = self._order(course, today)[0] | |
| 296 | 337 | return line_id, moves |
| 297 | 338 | |
| 298 | 339 | def summary(self, course, today=None): |
| 299 | - """(fällig, neu heute, gesamt) für die Kursauswahl.""" | |
| 340 | + """(fällige Züge, neue Linien heute, Linien gesamt) für die Kursauswahl. | |
| 341 | + | |
| 342 | + Gezählt werden fällige Entscheidungen, nicht Linien: Die ersten Züge gehören zu fast jeder | |
| 343 | + Linie, sonst wären mit ihnen gleich alle Linien „fällig“. | |
| 344 | + """ | |
| 300 | 345 | today = today or datetime.date.today() |
| 301 | - due = new = 0 | |
| 302 | 346 | ranked = self._order(course, today) |
| 303 | - for rank, _id, _moves in ranked: | |
| 304 | - due += rank[0] == 0 | |
| 305 | - new += rank[0] == 1 | |
| 347 | + keys = {key for _rank, _id, moves, _fresh in ranked for _ply, key in decisions(course, moves)} | |
| 348 | + rows = self._rows(course) | |
| 349 | + due = sum(1 for key in keys if key in rows and self._due_day(rows[key], today) <= today) | |
| 350 | + new = sum(1 for _rank, _id, _moves, fresh in ranked if fresh) | |
| 306 | 351 | return due, new, len(ranked) |
| 307 | 352 | |
| 308 | - def retry_from(self, course, line_id, today=None): | |
| 309 | - """Halbzug, ab dem die Linie heute weiterläuft – oder None für „von vorn“. | |
| 353 | + def asked(self, course, moves, today=None): | |
| 354 | + """Schlüssel der Entscheidungen, die man auf dieser Linie selbst ziehen soll. | |
| 310 | 355 | |
| 311 | - Nach „Nochmal“ beginnt die Wiederholung am selben Tag einen eigenen Zug vor dem | |
| 312 | - ersten Ausrutscher, statt die längst sicheren ersten Züge noch einmal zu verlangen. | |
| 356 | + Fällige und neue. Ist nichts davon dabei (freiwilliges Üben), alle – sonst spielte | |
| 357 | + das Programm die ganze Linie allein. | |
| 313 | 358 | """ |
| 314 | 359 | today = today or datetime.date.today() |
| 315 | - row = self._row(course, line_id) | |
| 316 | - if row is None or row["again_day"] != today.isoformat() or row["slip"] is None: | |
| 317 | - return None | |
| 318 | - return max(0, row["slip"] - 2) | |
| 360 | + self._convert_lines(course) | |
| 361 | + rows = self._rows(course) | |
| 362 | + keys = [key for _ply, key in decisions(course, moves)] | |
| 363 | + ask = {k for k in keys if k not in rows or self._due_day(rows[k], today) <= today} | |
| 364 | + return ask or set(keys) | |
| 365 | + | |
| 366 | + def line_kind(self, course, moves, today=None): | |
| 367 | + """„neu“, „Wiederholung“ oder „Üben“ für die Ansage zu Beginn.""" | |
| 368 | + today = today or datetime.date.today() | |
| 369 | + self._convert_lines(course) | |
| 370 | + rows = self._rows(course) | |
| 371 | + keys = [key for _ply, key in decisions(course, moves)] | |
| 372 | + if any(k not in rows for k in keys): | |
| 373 | + return "neu" | |
| 374 | + if any(self._due_day(rows[k], today) <= today for k in keys): | |
| 375 | + return "Wiederholung" | |
| 376 | + return "Üben" | |
| 319 | 377 | |
| 320 | 378 | # --- Eintragen ----------------------------------------------------------- |
| 321 | 379 | |
| 322 | - def record(self, course, line_id, grade, now=None, slip=None, | |
| 323 | - errors=None, hints=None, blunders=None, duration_ms=None): | |
| 324 | - """Ergebnis eintragen (GOOD, HARD, AGAIN oder True/False); Tage bis zur nächsten Wiederholung. | |
| 380 | + def record(self, course, board, grade, now=None, errors=None, hints=None, blunders=None, | |
| 381 | + duration_ms=None): | |
| 382 | + """Ergebnis einer Entscheidung eintragen (GOOD, HARD, AGAIN oder True/False) – ``board`` ist | |
| 383 | + die Stellung davor. Gibt die Tage bis zur nächsten Wiederholung zurück. | |
| 325 | 384 | |
| 326 | 385 | FSRS rechnet den Termin. Schwer zählt für FSRS als Vergessen (siehe ``RATINGS``), |
| 327 | - Nochmal ebenso und ist zusätzlich heute noch einmal fällig (0 Tage). | |
| 328 | - ``slip`` ist der Halbzug des ersten Ausrutschers; nach „Nochmal“ beginnt die Linie | |
| 329 | - heute kurz davor (``retry_from``). Fehler, Hinweise, Patzer und Dauer landen im | |
| 330 | - Protokoll (``reviews``), aus dem sich FSRS später nachjustieren lässt. | |
| 386 | + Nochmal ebenso und ist zusätzlich heute noch einmal fällig (0 Tage). Fehler, Hinweise, | |
| 387 | + Patzer und Dauer landen im Protokoll (``reviews``), aus dem sich FSRS später | |
| 388 | + nachjustieren lässt. | |
| 331 | 389 | """ |
| 332 | 390 | grade = GOOD if grade is True else AGAIN if grade is False else grade |
| 333 | 391 | now = now or datetime.datetime.now() |
| 334 | 392 | today = now.date() |
| 335 | - row = self._row(course, line_id) | |
| 393 | + self._convert_lines(course) | |
| 394 | + key = position_key(board) | |
| 395 | + row = self._row(course, key) | |
| 336 | 396 | if row is None: |
| 337 | 397 | card = fsrs.Card() |
| 338 | 398 | else: |
| @@ -344,23 +404,19 @@ class Progress: | ||
| 344 | 404 | card, _log = self.scheduler.review_card(card, RATINGS[grade], review_datetime=_utc(now), |
| 345 | 405 | review_duration=duration_ms) |
| 346 | 406 | again = grade == AGAIN |
| 347 | - state = (int(card.state), card.step, card.stability, card.difficulty, card.due.isoformat(), | |
| 348 | - card.last_review.isoformat(), today.isoformat() if again else None, | |
| 349 | - slip if again else None) | |
| 407 | + values = (int(card.state), card.step, card.stability, card.difficulty, card.due.isoformat(), | |
| 408 | + card.last_review.isoformat(), today.isoformat() if again else None) | |
| 350 | 409 | if row is None: |
| 351 | 410 | cursor = self.db.execute( |
| 352 | - "INSERT INTO cards (kind, source, key, created, state, step, stability, difficulty," | |
| 353 | - " due, last_review, again_day, slip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", | |
| 354 | - (KIND_LINE, course.title, line_id, today.isoformat()) + state, | |
| 411 | + "INSERT INTO cards (kind, source, key, created, content, " + ", ".join(STATE_COLUMNS) | |
| 412 | + + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", | |
| 413 | + (KIND_MOVE, course.title, key, today.isoformat(), board.fen()) + values, | |
| 355 | 414 | ) |
| 356 | 415 | card_id = cursor.lastrowid |
| 357 | 416 | else: |
| 358 | 417 | card_id = row["id"] |
| 359 | - self.db.execute( | |
| 360 | - "UPDATE cards SET state = ?, step = ?, stability = ?, difficulty = ?, due = ?," | |
| 361 | - " last_review = ?, again_day = ?, slip = ? WHERE id = ?", | |
| 362 | - state + (card_id,), | |
| 363 | - ) | |
| 418 | + self.db.execute("UPDATE cards SET " + ", ".join(f"{c} = ?" for c in STATE_COLUMNS) | |
| 419 | + + " WHERE id = ?", values + (card_id,)) | |
| 364 | 420 | self.db.execute( |
| 365 | 421 | "INSERT INTO reviews (card_id, at, rating, errors, hints, blunders, duration_ms)" |
| 366 | 422 | " VALUES (?, ?, ?, ?, ?, ?, ?)", |
| @@ -372,32 +428,6 @@ class Progress: | ||
| 372 | 428 | return max(1, (card.due.astimezone().date() - today).days) |
| 373 | 429 | |
| 374 | 430 | |
| 375 | -def played_line(course, moves, target=None): | |
| 376 | - """Zu welcher Linie gehört eine gespielte Zugfolge? Bevorzugt die Ziellinie.""" | |
| 377 | - played = " ".join(m.uci() for m in moves) | |
| 378 | - if target and (played == target or played.startswith(target + " ")): | |
| 379 | - return target # Ziellinie gespielt (nach ihrer Zugumstellung ging es im Kurs weiter) | |
| 380 | - all_lines = lines(course) | |
| 381 | - ids = [line_id for line_id, _m in all_lines] | |
| 382 | - if played in ids: | |
| 383 | - return played | |
| 384 | - # Zugumstellung: Linie mit derselben Endstellung | |
| 385 | - board = course.start() | |
| 386 | - for move in moves: | |
| 387 | - board.push(move) | |
| 388 | - end = opening.key(board) | |
| 389 | - matches = [] | |
| 390 | - for line_id, line_moves in all_lines: | |
| 391 | - probe = course.start() | |
| 392 | - for move in line_moves: | |
| 393 | - probe.push(move) | |
| 394 | - if opening.key(probe) == end: | |
| 395 | - matches.append(line_id) | |
| 396 | - if target in matches or not matches: | |
| 397 | - return target | |
| 398 | - return matches[0] | |
| 399 | - | |
| 400 | - | |
| 401 | 431 | def hint_weight(hints, reveals): |
| 402 | 432 | """Gewicht der Hinweise: nur die Figur zählt halb, Figur und Zielfeld ganz. |
| 403 | 433 | |
| @@ -418,12 +448,22 @@ def grade(errors, hints, blunders): | ||
| 418 | 448 | return GOOD |
| 419 | 449 | |
| 420 | 450 | |
| 451 | +def worst(grades): | |
| 452 | + """Schlechteste Stufe einer Linie (für die Rückmeldung am Ende).""" | |
| 453 | + for level in (AGAIN, HARD): | |
| 454 | + if level in grades: | |
| 455 | + return level | |
| 456 | + return GOOD | |
| 457 | + | |
| 458 | + | |
| 421 | 459 | def next_text(days, grade=None): |
| 460 | + """Rückmeldung am Ende einer Linie: wann das Schwächste daraus wiederkommt.""" | |
| 422 | 461 | lead = {HARD: "Fast.", AGAIN: "Das üben wir noch einmal."}.get(grade, "") # Gut: Lob steht schon davor |
| 462 | + subject = "Dieser Zug kommt" if grade in (HARD, AGAIN) else "Die nächste Wiederholung kommt" | |
| 423 | 463 | if days == 0: |
| 424 | - text = "Diese Linie kommt bald wieder dran." | |
| 464 | + text = "Den Zug wiederholen wir gleich." | |
| 425 | 465 | elif days == 1: |
| 426 | - text = "Diese Linie kommt morgen wieder." | |
| 466 | + text = f"{subject} morgen." | |
| 427 | 467 | else: |
| 428 | - text = f"Diese Linie kommt in {days} Tagen wieder." | |
| 468 | + text = f"{subject} in {days} Tagen." | |
| 429 | 469 | return f"{lead} {text}".strip() |
modified schach/training.py +66 −32
| @@ -84,9 +84,9 @@ def _due_text(course): | ||
| 84 | 84 | return f"Für heute geschafft! Du kannst trotzdem weiterüben ({total} Linien im Kurs)." |
| 85 | 85 | parts = [] |
| 86 | 86 | if due: |
| 87 | - parts.append(f"{due} zum Wiederholen") | |
| 87 | + parts.append("1 Zug zum Wiederholen" if due == 1 else f"{due} Züge zum Wiederholen") | |
| 88 | 88 | if new: |
| 89 | - parts.append(f"{new} neu") | |
| 89 | + parts.append("1 neue Linie" if new == 1 else f"{new} neue Linien") | |
| 90 | 90 | return f"Heute dran: {' und '.join(parts)} (von {total} Linien)." |
| 91 | 91 | |
| 92 | 92 | |
| @@ -219,9 +219,12 @@ def square_at(label, x, y): | ||
| 219 | 219 | class OpeningMixin: |
| 220 | 220 | def _opening_init(self): |
| 221 | 221 | self.course = None |
| 222 | - self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0, "slip": None} | |
| 223 | - self.course_line = None # (id, Züge): die Linie, die heute dran ist (Lernkarte) | |
| 224 | - self.course_recorded = False | |
| 222 | + self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0} | |
| 223 | + self.course_line = None # (id, Züge): die Linie, die heute dran ist | |
| 224 | + self.course_ask = set() # Entscheidungen (Stellungsschlüssel), die man heute selbst zieht | |
| 225 | + self.course_keys = set() # alle Entscheidungen der Linie; daneben (abgewichen) wird immer gefragt | |
| 226 | + self.course_records = [] # (Stufe, Tage) je eingetragener Entscheidung | |
| 227 | + self.decision = None # course_stats zu Beginn der Entscheidung, auf die gerade gewartet wird | |
| 225 | 228 | self.opening_busy = False # wartet auf Gegnerzug, Bewertung oder Zurücknahme |
| 226 | 229 | self._name_cache = {} |
| 227 | 230 | self.coach_log = [] # Rückmeldungen: {"ply": Halbzug der zugehörigen Stellung, "markup": Text} |
| @@ -863,9 +866,14 @@ class OpeningMixin: | ||
| 863 | 866 | self._leave_special() |
| 864 | 867 | self.mode = "opening" |
| 865 | 868 | self.course = course |
| 866 | - self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0, "slip": None} | |
| 869 | + self.course_stats = {"errors": 0, "hints": 0, "reveals": 0, "blunders": 0} | |
| 867 | 870 | self.course_line = progress().pick(course) |
| 868 | - self.course_recorded = False | |
| 871 | + line_moves = self.course_line[1] | |
| 872 | + line_decisions = repetition.decisions(course, line_moves) | |
| 873 | + self.course_ask = progress().asked(course, line_moves) | |
| 874 | + self.course_keys = {key for _ply, key in line_decisions} | |
| 875 | + self.course_records = [] | |
| 876 | + self.decision = None | |
| 869 | 877 | self.human = course.color |
| 870 | 878 | self.board = course.start() |
| 871 | 879 | self._reset_game_tables() |
| @@ -878,14 +886,15 @@ class OpeningMixin: | ||
| 878 | 886 | self._coach(f"{welcome}\n<b>{_escape(self._opening_line_text())}</b>", shapes=course.welcome_shapes) |
| 879 | 887 | self._changed() |
| 880 | 888 | self._refresh() |
| 881 | - start = progress().retry_from(course, self.course_line[0]) | |
| 889 | + # Was bis zur ersten fälligen oder neuen Entscheidung sitzt, spielt das Programm zügig vor | |
| 890 | + start = min((ply for ply, key in line_decisions if key in self.course_ask), default=0) | |
| 882 | 891 | if start: |
| 883 | - self._opening_skip(self.course_line[1][:start]) | |
| 892 | + self._opening_skip(line_moves[:start]) | |
| 884 | 893 | else: |
| 885 | 894 | self._opening_next_step() |
| 886 | 895 | |
| 887 | 896 | def _opening_skip(self, moves): |
| 888 | - """Nach „Nochmal“: die sicheren ersten Züge zügig vorspielen, dann ist man wieder dran.""" | |
| 897 | + """Die sicheren ersten Züge zügig vorspielen (beide Seiten), dann ist man selbst dran.""" | |
| 889 | 898 | self.opening_busy = True |
| 890 | 899 | self._refresh_chrome() |
| 891 | 900 | |
| @@ -895,7 +904,7 @@ class OpeningMixin: | ||
| 895 | 904 | if not rest: |
| 896 | 905 | self.opening_busy = False |
| 897 | 906 | number = self.board.fullmove_number |
| 898 | - self._coach(f"Wir machen gleich kurz vor der Stelle von eben weiter, bei Zug {number}.") | |
| 907 | + self._coach(f"Die Züge davor sitzen – weiter geht es bei Zug {number}.") | |
| 899 | 908 | self._refresh() |
| 900 | 909 | self._opening_next_step() |
| 901 | 910 | return False |
| @@ -926,6 +935,7 @@ class OpeningMixin: | ||
| 926 | 935 | self._opening_finished() |
| 927 | 936 | return |
| 928 | 937 | if self.board.turn == self.human: |
| 938 | + self._opening_my_turn(options) | |
| 929 | 939 | return |
| 930 | 940 | self.opening_busy = True |
| 931 | 941 | self._refresh_chrome() |
| @@ -951,6 +961,43 @@ class OpeningMixin: | ||
| 951 | 961 | |
| 952 | 962 | self.maia.submit(maia_candidates(board, self.elo, count=8), pick) |
| 953 | 963 | |
| 964 | + def _opening_my_turn(self, options): | |
| 965 | + """Selbst am Zug: Ist die Entscheidung heute dran, warten – sonst zieht das Programm den Kurszug.""" | |
| 966 | + key = repetition.position_key(self.board) | |
| 967 | + if key in self.course_ask or key not in self.course_keys: # daneben: unbekannt, also fragen | |
| 968 | + self.decision = dict(self.course_stats) | |
| 969 | + return | |
| 970 | + option = self._opening_target(options) or next((o for o in options if o.main), options[0]) | |
| 971 | + self.opening_busy = True | |
| 972 | + self._refresh_chrome() | |
| 973 | + GLib.timeout_add(SKIP_MS, self._opening_auto, self.gen, option) | |
| 974 | + | |
| 975 | + def _opening_auto(self, gen, option): | |
| 976 | + """Einen eigenen Kurszug vorspielen, der noch sitzt.""" | |
| 977 | + if gen != self.gen or self.mode != "opening" or option.move not in self.board.legal_moves: | |
| 978 | + return False | |
| 979 | + label = _escape(notation.label(self.board, option.move, self._notation())) | |
| 980 | + self.view.animate_move(self.board, option.move) | |
| 981 | + self.board.push(option.move) | |
| 982 | + self.opening_busy = False | |
| 983 | + self._changed() | |
| 984 | + self._coach(f"<b>{label}</b> – sitzt, den spiele ich vor.", shapes=option.shapes) | |
| 985 | + self._refresh() | |
| 986 | + self._opening_next_step() | |
| 987 | + return False | |
| 988 | + | |
| 989 | + def _opening_decided(self, before): | |
| 990 | + """Kurszug gefunden: die Entscheidung mit ihren Fehlversuchen und Hinweisen eintragen.""" | |
| 991 | + if self.decision is None: | |
| 992 | + return | |
| 993 | + base, self.decision = self.decision, None | |
| 994 | + delta = {k: self.course_stats[k] - base[k] for k in ("errors", "hints", "reveals", "blunders")} | |
| 995 | + hints = repetition.hint_weight(delta["hints"], delta["reveals"]) | |
| 996 | + level = repetition.grade(delta["errors"], hints, delta["blunders"]) | |
| 997 | + days = progress().record(self.course, before, level, errors=delta["errors"], hints=hints, | |
| 998 | + blunders=delta["blunders"]) | |
| 999 | + self.course_records.append((level, days)) | |
| 1000 | + | |
| 954 | 1001 | def _opening_line_text(self): |
| 955 | 1002 | """„Heute dran: Linie 3 von 13 (neu) – Italienisch: Giuoco Pianissimo“.""" |
| 956 | 1003 | line_id, moves = self.course_line |
| @@ -960,7 +1007,7 @@ class OpeningMixin: | ||
| 960 | 1007 | end.push(move) |
| 961 | 1008 | name = _names().name(end) |
| 962 | 1009 | name = notation.text(name, self._notation()) if name else "" |
| 963 | - kind = "neu" if progress().card(self.course, line_id) is None else "Wiederholung" | |
| 1010 | + kind = progress().line_kind(self.course, moves) | |
| 964 | 1011 | text = f"Heute dran: Linie {all_ids.index(line_id) + 1} von {len(all_ids)} ({kind})" |
| 965 | 1012 | return f"{text} – {name}" if name and name != self.course.title else text |
| 966 | 1013 | |
| @@ -987,8 +1034,7 @@ class OpeningMixin: | ||
| 987 | 1034 | comment = f" – {_escape(notation.text(option.comment, self._notation()))}" if option.comment else "" |
| 988 | 1035 | self._coach(f"<b>{_escape(label)}</b>{comment}", shapes=option.shapes, demos=why) |
| 989 | 1036 | self._refresh() |
| 990 | - if not self.course.options(self.board): | |
| 991 | - self._opening_finished() | |
| 1037 | + self._opening_next_step() # Linie zu Ende, oder man ist selbst dran | |
| 992 | 1038 | return False |
| 993 | 1039 | |
| 994 | 1040 | def _opening_play(self, move): |
| @@ -1002,6 +1048,7 @@ class OpeningMixin: | ||
| 1002 | 1048 | |
| 1003 | 1049 | if verdict.kind in ("course", "transposition"): |
| 1004 | 1050 | self._changed() |
| 1051 | + self._opening_decided(before) | |
| 1005 | 1052 | if verdict.option is not None: |
| 1006 | 1053 | comment = verdict.option.comment or "Genau so." |
| 1007 | 1054 | else: |
| @@ -1016,7 +1063,6 @@ class OpeningMixin: | ||
| 1016 | 1063 | # nicht im Kurs: erklären und zurücknehmen |
| 1017 | 1064 | self.opening_busy = True |
| 1018 | 1065 | self.course_stats["errors"] += 1 |
| 1019 | - self._opening_slip(ply) | |
| 1020 | 1066 | self._changed() |
| 1021 | 1067 | self._refresh() |
| 1022 | 1068 | if verdict.kind == "book": |
| @@ -1073,12 +1119,6 @@ class OpeningMixin: | ||
| 1073 | 1119 | target = self._opening_target(options) # Hinweis folgt der Linie von heute |
| 1074 | 1120 | self.hint_move = (target or options[0]).move |
| 1075 | 1121 | self.course_stats["hints"] += 1 |
| 1076 | - self._opening_slip(len(self.board.move_stack)) | |
| 1077 | - | |
| 1078 | - def _opening_slip(self, ply): | |
| 1079 | - """Erster Ausrutscher (Fehlversuch oder Hinweis) – dort setzt ein „Nochmal“ heute wieder an.""" | |
| 1080 | - if self.course_stats.get("slip") is None: | |
| 1081 | - self.course_stats["slip"] = ply | |
| 1082 | 1122 | |
| 1083 | 1123 | def _opening_finished(self): |
| 1084 | 1124 | self.opening_busy = False |
| @@ -1095,17 +1135,10 @@ class OpeningMixin: | ||
| 1095 | 1135 | if halves: |
| 1096 | 1136 | parts.append(f"{halves} halben Hinweis" + ("en" if halves > 1 else "")) |
| 1097 | 1137 | summary = f"Ziel erreicht – mit {' und '.join(parts)}." |
| 1098 | - if not self.course_recorded and self.course is not None: | |
| 1099 | - self.course_recorded = True # nach Zurücknehmen und erneutem Ende nicht doppelt zählen | |
| 1100 | - target = self.course_line[0] if self.course_line else None | |
| 1101 | - line = repetition.played_line(self.course, self.board.move_stack, target) | |
| 1102 | - if line is not None: | |
| 1103 | - level = repetition.grade(errors, repetition.hint_weight(hints, reveals), | |
| 1104 | - self.course_stats["blunders"]) | |
| 1105 | - days = progress().record(self.course, line, level, slip=self.course_stats["slip"], | |
| 1106 | - errors=errors, hints=repetition.hint_weight(hints, reveals), | |
| 1107 | - blunders=self.course_stats["blunders"]) | |
| 1108 | - summary = f"{summary} {repetition.next_text(days, level)}" | |
| 1138 | + if self.course_records: # eingetragen wurde schon je Entscheidung; hier nur die Rückmeldung | |
| 1139 | + level = repetition.worst([grade for grade, _days in self.course_records]) | |
| 1140 | + days = min(d for grade, d in self.course_records if grade == level) | |
| 1141 | + summary = f"{summary} {repetition.next_text(days, level)}" | |
| 1109 | 1142 | self.final_text = summary |
| 1110 | 1143 | self._refresh() |
| 1111 | 1144 | self._coach_show() |
| @@ -1132,6 +1165,7 @@ class OpeningMixin: | ||
| 1132 | 1165 | self.final_text = None |
| 1133 | 1166 | self.end_shown = False |
| 1134 | 1167 | self.opening_busy = False |
| 1168 | + self.decision = None # was jetzt noch einmal gezogen wird, ist schon eingetragen | |
| 1135 | 1169 | plies = len(self.board.move_stack) |
| 1136 | 1170 | self.coach_log = [e for e in self.coach_log if e["ply"] < plies] |
| 1137 | 1171 | self._coach("Zurückgenommen – du bist wieder am Zug.") |
modified tests/test_opening_retry.py +39 −16
| @@ -1,4 +1,4 @@ | ||
| 1 | -"""Kursmodus nach „Nochmal“: Die Linie beginnt kurz vor dem ersten Ausrutscher.""" | |
| 1 | +"""Kursmodus: Was sitzt, spielt das Programm vor; gefragt wird, was heute dran ist.""" | |
| 2 | 2 | |
| 3 | 3 | import datetime |
| 4 | 4 | import unittest |
| @@ -27,6 +27,11 @@ class _Toggle: | ||
| 27 | 27 | pass |
| 28 | 28 | |
| 29 | 29 | |
| 30 | +class _NoNames: | |
| 31 | + def name(self, _board): | |
| 32 | + return "" | |
| 33 | + | |
| 34 | + | |
| 30 | 35 | class FakeWindow(training.OpeningMixin): |
| 31 | 36 | def __init__(self): |
| 32 | 37 | self._opening_init() |
| @@ -37,6 +42,7 @@ class FakeWindow(training.OpeningMixin): | ||
| 37 | 42 | self.messages = [] |
| 38 | 43 | self.symbols = {} |
| 39 | 44 | self.takebacks = {} |
| 45 | + self.final_text = None | |
| 40 | 46 | self.badge_ply = None |
| 41 | 47 | self.browse_ply = None |
| 42 | 48 | |
| @@ -61,47 +67,64 @@ class FakeWindow(training.OpeningMixin): | ||
| 61 | 67 | def _refresh_chrome(self): |
| 62 | 68 | pass |
| 63 | 69 | |
| 64 | - | |
| 65 | -def _now(*_args, **_kwargs): | |
| 66 | - return datetime.datetime.now() | |
| 70 | + def _coach_show(self): | |
| 71 | + pass | |
| 67 | 72 | |
| 68 | 73 | |
| 69 | 74 | class RetryTest(unittest.TestCase): |
| 70 | 75 | def setUp(self): |
| 71 | 76 | self.course = opening.parse_course(COURSE) |
| 72 | 77 | self.progress = repetition.Progress(fuzz=False) |
| 73 | - self.line = repetition.lines(self.course)[0][0] | |
| 74 | 78 | patches = [ |
| 75 | 79 | mock.patch.object(training, "progress", lambda: self.progress), |
| 76 | 80 | # Zeitgeber sofort ausführen |
| 77 | 81 | mock.patch.object(training.GLib, "timeout_add", lambda _ms, fn, *a: fn(*a)), |
| 82 | + mock.patch.object(training, "_book", lambda: None), # Kurszüge braucht kein Buch | |
| 83 | + mock.patch.object(training, "_names", lambda: _NoNames()), | |
| 78 | 84 | ] |
| 79 | 85 | for patch in patches: |
| 80 | 86 | patch.start() |
| 81 | 87 | self.addCleanup(patch.stop) |
| 82 | 88 | self.window = FakeWindow() |
| 83 | 89 | |
| 90 | + def board_after(self, ucis=""): | |
| 91 | + board = self.course.start() | |
| 92 | + for uci in ucis.split(): | |
| 93 | + board.push_uci(uci) | |
| 94 | + return board | |
| 95 | + | |
| 84 | 96 | def test_fresh_line_starts_at_the_beginning(self): |
| 85 | 97 | self.window.start_opening(self.course) |
| 86 | 98 | self.assertEqual(len(self.window.board.move_stack), 0) |
| 99 | + self.assertIsNotNone(self.window.decision) # wartet auf den eigenen Zug | |
| 87 | 100 | |
| 88 | - def test_again_starts_one_own_move_before_the_slip(self): | |
| 89 | - # Ausrutscher bei 4. c3 (Halbzug 6): weiter ab 3. Lc4 (Halbzug 4) | |
| 90 | - self.progress.record(self.course, self.line, repetition.AGAIN, slip=6) | |
| 101 | + def test_known_moves_are_played_up_to_the_due_one(self): | |
| 102 | + # sitzt: 1. e4, 2. Sf3, 3. Lc4 – heute „Nochmal“ bei 4. c3 (Halbzug 6) | |
| 103 | + for ucis in ("", "e2e4 e7e5", "e2e4 e7e5 g1f3 b8c6"): | |
| 104 | + self.progress.record(self.course, self.board_after(ucis), repetition.GOOD) | |
| 105 | + self.progress.record(self.course, self.board_after("e2e4 e7e5 g1f3 b8c6 f1c4 f8c5"), repetition.AGAIN) | |
| 91 | 106 | self.window.start_opening(self.course) |
| 92 | 107 | board = self.window.board |
| 93 | - self.assertEqual([m.uci() for m in board.move_stack], ["e2e4", "e7e5", "g1f3", "b8c6"]) | |
| 108 | + self.assertEqual(len(board.move_stack), 6) # direkt vor dem Fehler von eben | |
| 94 | 109 | self.assertEqual(board.turn, chess.WHITE) |
| 95 | 110 | self.assertFalse(self.window.opening_busy) |
| 96 | - self.assertIn("Zug 3", self.window.messages[-1]) | |
| 111 | + self.assertIsNotNone(self.window.decision) | |
| 112 | + self.assertIn("Zug 4", self.window.messages[-1]) | |
| 97 | 113 | |
| 98 | - def test_first_slip_is_remembered(self): | |
| 114 | + def test_later_known_move_is_played_for_you_and_due_one_is_recorded(self): | |
| 115 | + long_ago = datetime.datetime.now() - datetime.timedelta(days=90) | |
| 116 | + for ucis in ("", "e2e4 e7e5", "e2e4 e7e5 g1f3 b8c6 f1c4 f8c5"): | |
| 117 | + self.progress.record(self.course, self.board_after(ucis), repetition.GOOD) | |
| 118 | + bc4 = self.board_after("e2e4 e7e5 g1f3 b8c6") | |
| 119 | + self.progress.record(self.course, bc4, repetition.GOOD, now=long_ago) # längst fällig | |
| 99 | 120 | self.window.start_opening(self.course) |
| 100 | - self.window.board.push_san("e4") | |
| 101 | - self.window.board.push_san("e5") | |
| 102 | - self.window._opening_slip(2) | |
| 103 | - self.window._opening_slip(4) # späterer Ausrutscher zählt nicht | |
| 104 | - self.assertEqual(self.window.course_stats["slip"], 2) | |
| 121 | + self.assertEqual(len(self.window.board.move_stack), 4) | |
| 122 | + self.window._opening_play(chess.Move.from_uci("f1c4")) | |
| 123 | + # Gegner 3… Lc5, dann 4. c3 vorgespielt (sitzt), 4… Sf6 – Linie zu Ende | |
| 124 | + self.assertEqual(len(self.window.board.move_stack), 8) | |
| 125 | + self.assertEqual([grade for grade, _days in self.window.course_records], [repetition.GOOD]) | |
| 126 | + self.assertIn("nächste Wiederholung", self.window.final_text) | |
| 127 | + self.assertGreater(self.progress.card(self.course, bc4)["last_review"], long_ago.isoformat()) | |
| 105 | 128 | |
| 106 | 129 | def test_course_takeback_is_kept_for_the_pgn(self): |
| 107 | 130 | self.window.start_opening(self.course) |
modified tests/test_repetition.py +129 −117
| @@ -1,11 +1,10 @@ | ||
| 1 | 1 | import datetime |
| 2 | 2 | import json |
| 3 | 3 | import os |
| 4 | +import sqlite3 | |
| 4 | 5 | import tempfile |
| 5 | 6 | import unittest |
| 6 | 7 | |
| 7 | -import chess | |
| 8 | - | |
| 9 | 8 | from schach import opening, repetition |
| 10 | 9 | |
| 11 | 10 | COURSE = """[Event "Mini"] |
| @@ -15,12 +14,20 @@ COURSE = """[Event "Mini"] | ||
| 15 | 14 | """ |
| 16 | 15 | |
| 17 | 16 | TODAY = datetime.date(2026, 11, 20) |
| 17 | +G, H, A = repetition.GOOD, repetition.HARD, repetition.AGAIN | |
| 18 | 18 | |
| 19 | 19 | |
| 20 | 20 | def at(day_offset): |
| 21 | 21 | return datetime.datetime.combine(TODAY + datetime.timedelta(days=day_offset), datetime.time(12)) |
| 22 | 22 | |
| 23 | 23 | |
| 24 | +def after(course, ucis=""): | |
| 25 | + board = course.start() | |
| 26 | + for uci in ucis.split(): | |
| 27 | + board.push_uci(uci) | |
| 28 | + return board | |
| 29 | + | |
| 30 | + | |
| 24 | 31 | class LinesTest(unittest.TestCase): |
| 25 | 32 | def setUp(self): |
| 26 | 33 | self.course = opening.parse_course(COURSE) |
| @@ -33,10 +40,14 @@ class LinesTest(unittest.TestCase): | ||
| 33 | 40 | for course in opening.load_courses(): |
| 34 | 41 | self.assertGreater(len(repetition.lines(course)), 1, course.title) |
| 35 | 42 | |
| 36 | - def test_played_line_prefers_exact_then_target(self): | |
| 37 | - moves = [chess.Move.from_uci(u) for u in "e2e4 c7c5 g1f3".split()] | |
| 38 | - self.assertEqual(repetition.played_line(self.course, moves, "x"), "e2e4 c7c5 g1f3") | |
| 39 | - self.assertEqual(repetition.played_line(self.course, [], "target"), "target") | |
| 43 | + def test_decisions_are_own_moves_shared_between_lines(self): | |
| 44 | + (_main_id, main), (_id2, second), (_id3, third) = repetition.lines(self.course) | |
| 45 | + found = repetition.decisions(self.course, main) | |
| 46 | + self.assertEqual([ply for ply, _key in found], [0, 2, 4]) | |
| 47 | + self.assertEqual(found[1][1], repetition.position_key(after(self.course, "e2e4 e7e5"))) | |
| 48 | + second_keys = [key for _ply, key in repetition.decisions(self.course, second)] | |
| 49 | + self.assertEqual(second_keys[:2], [key for _ply, key in found[:2]]) # gemeinsamer Anfang | |
| 50 | + self.assertEqual(len(repetition.decisions(self.course, third)), 2) | |
| 40 | 51 | |
| 41 | 52 | |
| 42 | 53 | TRANSPOSING = """[Event "Umstellung"] |
| @@ -47,191 +58,177 @@ TRANSPOSING = """[Event "Umstellung"] | ||
| 47 | 58 | |
| 48 | 59 | |
| 49 | 60 | class TranspositionTest(unittest.TestCase): |
| 50 | - def setUp(self): | |
| 51 | - self.course = opening.parse_course(TRANSPOSING) | |
| 52 | - | |
| 53 | 61 | def test_line_ends_at_transposition(self): |
| 54 | - ids = [i for i, _m in repetition.lines(self.course)] | |
| 62 | + course = opening.parse_course(TRANSPOSING) | |
| 63 | + ids = [i for i, _m in repetition.lines(course)] | |
| 55 | 64 | # 2…Sf6 3.Lc4 Sc6 ist dieselbe Stellung wie die Hauptvariante nach 3…Sf6: dort endet die Linie |
| 56 | 65 | self.assertEqual(ids, ["e2e4 e7e5 g1f3 b8c6 f1c4 g8f6 d2d3 f8e7", "e2e4 e7e5 g1f3 b8c6 f1c4 g8f6 f3g5", |
| 57 | 66 | "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6"]) |
| 58 | 67 | |
| 59 | - def test_old_long_ids_migrate(self): | |
| 60 | - progress = repetition.Progress(fuzz=False) | |
| 61 | - old = "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6 d2d3 f8e7" # so lief die Linie früher weiter | |
| 62 | - progress.record(self.course, old, repetition.GOOD, at(-1)) | |
| 63 | - stability = progress.card(self.course, old)["stability"] | |
| 64 | - progress.summary(self.course, TODAY) | |
| 65 | - self.assertEqual(progress.card(self.course, "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6")["stability"], stability) | |
| 66 | - self.assertIsNone(progress.card(self.course, old)) | |
| 67 | - | |
| 68 | - def test_extended_line_keeps_progress_and_is_due(self): | |
| 68 | + def test_transposed_position_is_the_same_card(self): | |
| 69 | + course = opening.parse_course(TRANSPOSING) | |
| 69 | 70 | progress = repetition.Progress(fuzz=False) |
| 70 | - old = "e2e4 e7e5 g1f3 b8c6 f1c4 g8f6" # früher endete die Hauptvariante hier, jetzt geht sie weiter | |
| 71 | - progress.record(self.course, old, repetition.GOOD, at(-1)) # eigentlich erst in ein paar Tagen | |
| 72 | - due, _new, _total = progress.summary(self.course, TODAY) | |
| 73 | - card = progress.card(self.course, "e2e4 e7e5 g1f3 b8c6 f1c4 g8f6 d2d3 f8e7") # erste Fortsetzung | |
| 74 | - self.assertEqual(card["due_day"], TODAY.isoformat()) | |
| 75 | - self.assertIsNone(progress.card(self.course, old)) | |
| 76 | - self.assertIsNone(progress.card(self.course, "e2e4 e7e5 g1f3 b8c6 f1c4 g8f6 f3g5")) # bleibt neu | |
| 77 | - self.assertEqual(due, 1) | |
| 78 | - | |
| 79 | - def test_played_line_beyond_transposition_counts_for_target(self): | |
| 80 | - target = "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6" | |
| 81 | - moves = [chess.Move.from_uci(u) for u in (target + " d2d3 f8e7").split()] | |
| 82 | - self.assertEqual(repetition.played_line(self.course, moves, target), target) | |
| 71 | + progress.record(course, after(course, "e2e4 e7e5 g1f3 b8c6 f1c4 g8f6"), G, at(0)) | |
| 72 | + self.assertIsNotNone(progress.card(course, after(course, "e2e4 e7e5 g1f3 g8f6 f1c4 b8c6"))) | |
| 83 | 73 | |
| 84 | 74 | |
| 85 | 75 | class ProgressTest(unittest.TestCase): |
| 86 | 76 | def setUp(self): |
| 87 | 77 | self.course = opening.parse_course(COURSE) |
| 88 | 78 | self.progress = repetition.Progress(fuzz=False) |
| 89 | - self.main, self.second, self.third = [i for i, _m in repetition.lines(self.course)] | |
| 79 | + (self.main, self.main_moves), (self.second, self.second_moves), _third = repetition.lines(self.course) | |
| 80 | + self.start = after(self.course) | |
| 81 | + self.e4e5 = after(self.course, "e2e4 e7e5") | |
| 82 | + self.nc6 = after(self.course, "e2e4 e7e5 g1f3 b8c6") | |
| 83 | + self.d6 = after(self.course, "e2e4 e7e5 g1f3 d7d6") | |
| 84 | + | |
| 85 | + def learn_main(self, day=0, grade=G): | |
| 86 | + for board in (self.start, self.e4e5, self.nc6): | |
| 87 | + self.progress.record(self.course, board, grade, at(day)) | |
| 90 | 88 | |
| 91 | 89 | def test_new_lines_in_course_order(self): |
| 92 | 90 | self.assertEqual(self.progress.pick(self.course, TODAY)[0], self.main) |
| 93 | 91 | self.assertEqual(self.progress.summary(self.course, TODAY), (0, 3, 3)) |
| 94 | 92 | |
| 93 | + def test_known_moves_are_played_for_you(self): | |
| 94 | + self.learn_main() | |
| 95 | + self.assertEqual(self.progress.pick(self.course, TODAY)[0], self.second) # main erst später | |
| 96 | + key = repetition.position_key(self.d6) | |
| 97 | + self.assertEqual(self.progress.asked(self.course, self.second_moves, TODAY), {key}) # nur das Neue | |
| 98 | + self.assertEqual(self.progress.line_kind(self.course, self.second_moves, TODAY), "neu") | |
| 99 | + | |
| 100 | + def test_nothing_due_asks_everything(self): | |
| 101 | + self.learn_main() | |
| 102 | + self.assertEqual(len(self.progress.asked(self.course, self.main_moves, TODAY)), 3) # freiwillig: alles | |
| 103 | + self.assertEqual(self.progress.line_kind(self.course, self.main_moves, TODAY), "Üben") | |
| 104 | + | |
| 95 | 105 | def test_success_grows_the_interval(self): |
| 96 | - first = self.progress.record(self.course, self.main, True, at(0)) | |
| 106 | + first = self.progress.record(self.course, self.nc6, G, at(0)) | |
| 107 | + second = self.progress.record(self.course, self.nc6, G, at(first)) | |
| 108 | + third = self.progress.record(self.course, self.nc6, G, at(first + second)) | |
| 97 | 109 | self.assertGreaterEqual(first, 1) |
| 98 | - self.assertEqual(self.progress.pick(self.course, TODAY)[0], self.second) # main erst später | |
| 99 | - second = self.progress.record(self.course, self.main, True, at(first)) | |
| 100 | - third = self.progress.record(self.course, self.main, True, at(first + second)) | |
| 101 | 110 | self.assertLess(first, second) |
| 102 | 111 | self.assertLess(second, third) |
| 103 | 112 | |
| 104 | 113 | def test_failure_is_due_again_but_after_other_due_lines(self): |
| 105 | - self.progress.record(self.course, self.second, True, at(-30)) # längst wieder fällig | |
| 106 | - self.assertEqual(self.progress.record(self.course, self.main, False, at(0)), 0) | |
| 114 | + self.progress.record(self.course, self.d6, G, at(-30)) # längst wieder fällig | |
| 115 | + self.learn_main() | |
| 116 | + self.assertEqual(self.progress.record(self.course, self.nc6, A, at(0)), 0) | |
| 107 | 117 | self.assertEqual(self.progress.pick(self.course, TODAY)[0], self.second) |
| 108 | - self.progress.record(self.course, self.second, True, at(0)) | |
| 118 | + self.progress.record(self.course, self.d6, G, at(0)) | |
| 109 | 119 | self.assertEqual(self.progress.pick(self.course, TODAY)[0], self.main) # vor neuen Linien |
| 120 | + asked = self.progress.asked(self.course, self.main_moves, TODAY) | |
| 121 | + self.assertEqual(asked, {repetition.position_key(self.nc6)}) # direkt zum Fehler von eben | |
| 110 | 122 | self.assertEqual(self.progress.summary(self.course, TODAY), (1, 1, 3)) |
| 111 | 123 | |
| 112 | 124 | def test_failure_only_today_then_fsrs_decides(self): |
| 113 | - self.progress.record(self.course, self.main, False, at(0)) | |
| 114 | - self.assertEqual(self.progress.card(self.course, self.main)["again_day"], TODAY.isoformat()) | |
| 125 | + self.progress.record(self.course, self.nc6, A, at(0)) | |
| 126 | + self.assertEqual(self.progress.card(self.course, self.nc6)["again_day"], TODAY.isoformat()) | |
| 115 | 127 | tomorrow = TODAY + datetime.timedelta(days=1) |
| 116 | 128 | self.assertEqual(self.progress.summary(self.course, tomorrow)[0], 1) # FSRS: morgen wieder |
| 117 | 129 | |
| 118 | - def test_failure_shrinks_the_interval(self): | |
| 130 | + def test_failure_shrinks_the_stability(self): | |
| 119 | 131 | day = 0 |
| 120 | 132 | for _ in range(3): |
| 121 | - day += self.progress.record(self.course, self.main, True, at(day)) | |
| 122 | - before = self.progress.card(self.course, self.main)["stability"] | |
| 123 | - self.progress.record(self.course, self.main, False, at(day)) | |
| 124 | - self.assertLess(self.progress.card(self.course, self.main)["stability"], before) | |
| 133 | + day += self.progress.record(self.course, self.nc6, G, at(day)) | |
| 134 | + before = self.progress.card(self.course, self.nc6)["stability"] | |
| 135 | + self.progress.record(self.course, self.nc6, A, at(day)) | |
| 136 | + self.assertLess(self.progress.card(self.course, self.nc6)["stability"], before) | |
| 125 | 137 | |
| 126 | 138 | def test_new_lines_limited_per_day(self): |
| 127 | 139 | course = opening.load_courses()[0] |
| 128 | 140 | progress = repetition.Progress(fuzz=False) |
| 129 | - ids = [i for i, _m in repetition.lines(course)] | |
| 130 | - self.assertGreater(len(ids), repetition.NEW_PER_DAY) | |
| 141 | + self.assertGreater(len(repetition.lines(course)), repetition.NEW_PER_DAY) | |
| 131 | 142 | self.assertEqual(progress.summary(course, TODAY)[1], repetition.NEW_PER_DAY) |
| 132 | - for line_id in ids[:repetition.NEW_PER_DAY]: | |
| 133 | - progress.record(course, line_id, True, at(0)) | |
| 143 | + for _ in range(repetition.NEW_PER_DAY): | |
| 144 | + _line_id, moves = progress.pick(course, TODAY) | |
| 145 | + plies = {ply for ply, _key in repetition.decisions(course, moves)} | |
| 146 | + board = course.start() | |
| 147 | + for ply, move in enumerate(moves): | |
| 148 | + if ply in plies: | |
| 149 | + progress.record(course, board, G, at(0)) | |
| 150 | + board.push(move) | |
| 134 | 151 | due, new, _total = progress.summary(course, TODAY) |
| 135 | 152 | self.assertEqual((due, new), (0, 0)) # für heute geschafft |
| 136 | - self.assertIn(progress.pick(course, TODAY)[0], ids[:repetition.NEW_PER_DAY]) # weiterüben: Bekanntes | |
| 137 | 153 | later = TODAY + datetime.timedelta(days=repetition.MAX_DAYS) |
| 138 | - self.assertEqual(progress.summary(course, later)[:2], (repetition.NEW_PER_DAY, repetition.NEW_PER_DAY)) | |
| 154 | + self.assertEqual(progress.summary(course, later)[1], repetition.NEW_PER_DAY) | |
| 139 | 155 | |
| 140 | 156 | def test_grade_from_history(self): |
| 141 | - self.assertEqual(repetition.grade(0, 0, 0), repetition.GOOD) | |
| 142 | - self.assertEqual(repetition.grade(1, 0, 0), repetition.HARD) | |
| 143 | - self.assertEqual(repetition.grade(0, 1, 0), repetition.HARD) | |
| 144 | - self.assertEqual(repetition.grade(1, 1, 0), repetition.AGAIN) | |
| 145 | - self.assertEqual(repetition.grade(1, 0, 1), repetition.AGAIN) # ein echter Patzer | |
| 157 | + self.assertEqual(repetition.grade(0, 0, 0), G) | |
| 158 | + self.assertEqual(repetition.grade(1, 0, 0), H) | |
| 159 | + self.assertEqual(repetition.grade(0, 1, 0), H) | |
| 160 | + self.assertEqual(repetition.grade(1, 1, 0), A) | |
| 161 | + self.assertEqual(repetition.grade(1, 0, 1), A) # ein echter Patzer | |
| 162 | + self.assertEqual(repetition.worst([G, H, G]), H) | |
| 163 | + self.assertEqual(repetition.worst([G, A, H]), A) | |
| 146 | 164 | |
| 147 | 165 | def test_piece_only_hint_counts_half(self): |
| 148 | 166 | half = repetition.hint_weight(1, 0) # nur die Figur gezeigt |
| 149 | - self.assertEqual(repetition.grade(0, half, 0), repetition.HARD) # Hilfe gebraucht ist nie „gut“ | |
| 150 | - self.assertEqual(repetition.grade(0, repetition.hint_weight(2, 0), 0), repetition.HARD) | |
| 151 | - self.assertEqual(repetition.grade(1, half, 0), repetition.AGAIN) | |
| 152 | - self.assertEqual(repetition.grade(0, repetition.hint_weight(1, 1), 0), repetition.HARD) | |
| 153 | - self.assertEqual(repetition.grade(0, repetition.hint_weight(2, 1), 0), repetition.AGAIN) | |
| 167 | + self.assertEqual(repetition.grade(0, half, 0), H) # Hilfe gebraucht ist nie „gut“ | |
| 168 | + self.assertEqual(repetition.grade(0, repetition.hint_weight(2, 0), 0), H) | |
| 169 | + self.assertEqual(repetition.grade(1, half, 0), A) | |
| 170 | + self.assertEqual(repetition.grade(0, repetition.hint_weight(1, 1), 0), H) | |
| 171 | + self.assertEqual(repetition.grade(0, repetition.hint_weight(2, 1), 0), A) | |
| 154 | 172 | |
| 155 | - def test_hard_is_not_repeated_today_and_comes_before_good(self): | |
| 156 | - hard = self.progress.record(self.course, self.main, repetition.HARD, at(0)) | |
| 157 | - good = self.progress.record(self.course, self.second, repetition.GOOD, at(0)) | |
| 158 | - self.assertGreaterEqual(hard, 1) | |
| 159 | - self.assertLessEqual(hard, good) | |
| 160 | - self.assertIsNone(self.progress.card(self.course, self.main)["again_day"]) | |
| 173 | + def test_hard_is_not_repeated_today(self): | |
| 174 | + self.assertGreaterEqual(self.progress.record(self.course, self.nc6, H, at(0)), 1) | |
| 175 | + self.assertIsNone(self.progress.card(self.course, self.nc6)["again_day"]) | |
| 161 | 176 | |
| 162 | - def test_hard_comes_back_sooner_the_fresher_the_line(self): | |
| 163 | - fresh = repetition.Progress(fuzz=False) | |
| 164 | - fresh.record(self.course, self.main, True, at(0)) | |
| 165 | - self.assertEqual(fresh.record(self.course, self.main, repetition.HARD, at(4)), 1) | |
| 166 | - | |
| 177 | + def test_hard_comes_back_sooner_the_fresher_the_move(self): | |
| 178 | + self.progress.record(self.course, self.start, G, at(0)) | |
| 179 | + self.assertEqual(self.progress.record(self.course, self.start, H, at(4)), 1) | |
| 167 | 180 | day = 0 |
| 168 | 181 | for _ in range(3): |
| 169 | - day += self.progress.record(self.course, self.main, True, at(day)) | |
| 170 | - before = self.progress.card(self.course, self.main)["stability"] | |
| 171 | - hard = self.progress.record(self.course, self.main, repetition.HARD, at(day)) | |
| 182 | + day += self.progress.record(self.course, self.nc6, G, at(day)) | |
| 183 | + before = self.progress.card(self.course, self.nc6)["stability"] | |
| 184 | + hard = self.progress.record(self.course, self.nc6, H, at(day)) | |
| 172 | 185 | self.assertGreater(hard, 1) # sitzt eigentlich: nicht stur morgen |
| 173 | 186 | self.assertLess(hard, 14) # aber bald |
| 174 | - self.assertLess(self.progress.card(self.course, self.main)["stability"], before) # FSRS: vergessen | |
| 175 | - self.assertIsNone(self.progress.card(self.course, self.main)["again_day"]) # nicht heute | |
| 187 | + self.assertLess(self.progress.card(self.course, self.nc6)["stability"], before) # FSRS: vergessen | |
| 176 | 188 | |
| 177 | 189 | def test_interval_capped(self): |
| 178 | 190 | day = 0 |
| 179 | 191 | for _ in range(12): |
| 180 | - days = self.progress.record(self.course, self.main, True, at(day)) | |
| 192 | + days = self.progress.record(self.course, self.nc6, G, at(day)) | |
| 181 | 193 | day += days |
| 182 | 194 | self.assertEqual(days, repetition.MAX_DAYS) |
| 183 | 195 | |
| 184 | 196 | def test_higher_retention_means_shorter_intervals(self): |
| 185 | 197 | relaxed = repetition.Progress(retention="relaxed", fuzz=False) |
| 186 | 198 | thorough = repetition.Progress(retention="thorough", fuzz=False) |
| 187 | - self.assertGreater(relaxed.record(self.course, self.main, True, at(0)), | |
| 188 | - thorough.record(self.course, self.main, True, at(0))) | |
| 199 | + self.assertGreater(relaxed.record(self.course, self.nc6, G, at(0)), | |
| 200 | + thorough.record(self.course, self.nc6, G, at(0))) | |
| 189 | 201 | thorough.set_retention("relaxed") |
| 190 | 202 | self.assertEqual(thorough.scheduler.desired_retention, repetition.RETENTION["relaxed"]) |
| 191 | 203 | |
| 192 | 204 | def test_every_attempt_is_logged(self): |
| 193 | - self.progress.record(self.course, self.main, repetition.HARD, at(0), errors=1, hints=0.5, blunders=0) | |
| 194 | - self.progress.record(self.course, self.main, repetition.GOOD, at(3)) | |
| 205 | + self.progress.record(self.course, self.nc6, H, at(0), errors=1, hints=0.5, blunders=0) | |
| 206 | + self.progress.record(self.course, self.nc6, G, at(3)) | |
| 195 | 207 | rows = self.progress.db.execute("SELECT rating, errors, hints FROM reviews ORDER BY id").fetchall() |
| 196 | 208 | self.assertEqual([tuple(r) for r in rows], [(1, 1, 0.5), (3, None, None)]) |
| 197 | - | |
| 198 | - def test_again_starts_one_own_move_before_the_slip(self): | |
| 199 | - # Ausrutscher beim 3. Zug von Weiß (Halbzug 4): neu ab dem 2. Zug von Weiß (Halbzug 2) | |
| 200 | - self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=4) | |
| 201 | - self.assertEqual(self.progress.retry_from(self.course, self.main, TODAY), 2) | |
| 202 | - self.assertIsNone(self.progress.retry_from(self.course, self.second, TODAY)) | |
| 203 | - | |
| 204 | - def test_again_on_first_move_starts_at_the_beginning(self): | |
| 205 | - self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=0) | |
| 206 | - self.assertEqual(self.progress.retry_from(self.course, self.main, TODAY), 0) | |
| 209 | + self.assertEqual(self.progress.card(self.course, self.nc6)["content"], self.nc6.fen()) | |
| 207 | 210 | |
| 208 | - def test_retry_only_on_the_same_day(self): | |
| 209 | - self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=4) | |
| 210 | - self.assertIsNone(self.progress.retry_from(self.course, self.main, TODAY + datetime.timedelta(days=1))) | |
| 211 | - | |
| 212 | - def test_retry_cleared_after_next_attempt(self): | |
| 213 | - self.progress.record(self.course, self.main, repetition.AGAIN, at(0), slip=4) | |
| 214 | - self.progress.record(self.course, self.main, repetition.GOOD, at(0)) | |
| 215 | - self.assertIsNone(self.progress.retry_from(self.course, self.main, TODAY)) | |
| 216 | - | |
| 217 | - def test_hard_does_not_shorten(self): | |
| 218 | - self.progress.record(self.course, self.main, repetition.HARD, at(0), slip=4) | |
| 219 | - self.assertIsNone(self.progress.retry_from(self.course, self.main, TODAY)) | |
| 211 | + def test_next_text(self): | |
| 212 | + self.assertEqual(repetition.next_text(4), "Die nächste Wiederholung kommt in 4 Tagen.") | |
| 213 | + self.assertEqual(repetition.next_text(1, H), "Fast. Dieser Zug kommt morgen.") | |
| 214 | + self.assertEqual(repetition.next_text(0, A), "Das üben wir noch einmal. Den Zug wiederholen wir gleich.") | |
| 220 | 215 | |
| 221 | 216 | def test_saved_and_loaded(self): |
| 222 | 217 | with tempfile.TemporaryDirectory() as tmp: |
| 223 | 218 | path = os.path.join(tmp, "sub", "training.sqlite") |
| 224 | 219 | progress = repetition.Progress(path, fuzz=False) |
| 225 | - days = progress.record(self.course, self.main, True, at(0)) | |
| 220 | + days = progress.record(self.course, self.nc6, G, at(0)) | |
| 226 | 221 | again = repetition.Progress(path) |
| 227 | 222 | expected = (TODAY + datetime.timedelta(days=days)).isoformat() |
| 228 | - self.assertEqual(again.card(self.course, self.main)["due_day"], expected) | |
| 223 | + self.assertEqual(again.card(self.course, self.nc6)["due_day"], expected) | |
| 229 | 224 | |
| 230 | 225 | |
| 231 | -class LegacyImportTest(unittest.TestCase): | |
| 226 | +class LegacyTest(unittest.TestCase): | |
| 227 | + """Übernahme: training.json (Stufenleiter) → Linienkarten → Entscheidungen.""" | |
| 228 | + | |
| 232 | 229 | def setUp(self): |
| 233 | 230 | self.course = opening.parse_course(COURSE) |
| 234 | - self.main, self.second, _third = [i for i, _m in repetition.lines(self.course)] | |
| 231 | + (self.main, _m), (self.second, _m2), _third = repetition.lines(self.course) | |
| 235 | 232 | self.tmp = tempfile.TemporaryDirectory() |
| 236 | 233 | self.addCleanup(self.tmp.cleanup) |
| 237 | 234 | self.json = os.path.join(self.tmp.name, "training.json") |
| @@ -245,16 +242,31 @@ class LegacyImportTest(unittest.TestCase | ||
| 245 | 242 | with open(self.json, "w", encoding="utf-8") as fh: |
| 246 | 243 | json.dump(legacy, fh) |
| 247 | 244 | |
| 248 | - def test_ladder_becomes_stability_and_due_stays(self): | |
| 245 | + def test_line_progress_goes_to_each_decision(self): | |
| 249 | 246 | progress = repetition.Progress(self.db, legacy_path=self.json) |
| 250 | - card = progress.card(self.course, self.main) | |
| 251 | - self.assertEqual((card["stability"], card["difficulty"], card["due_day"]), (7.0, 5.0, "2026-12-01")) | |
| 252 | - self.assertEqual(progress.retry_from(self.course, self.second, TODAY), 2) # offenes „Nochmal“ | |
| 247 | + bc4 = progress.card(self.course, after(self.course, "e2e4 e7e5 g1f3 b8c6")) | |
| 248 | + self.assertEqual((bc4["stability"], bc4["difficulty"], bc4["due_day"]), (7.0, 5.0, "2026-12-01")) | |
| 249 | + d4 = progress.card(self.course, after(self.course, "e2e4 e7e5 g1f3 d7d6")) | |
| 250 | + self.assertEqual(d4["again_day"], TODAY.isoformat()) # offenes „Nochmal“ von heute | |
| 251 | + shared = progress.card(self.course, after(self.course)) # auf beiden Linien: die früher fällige | |
| 252 | + self.assertEqual(shared["due_day"], TODAY.isoformat()) | |
| 253 | + kinds = {r[0] for r in progress.db.execute("SELECT kind FROM cards")} | |
| 254 | + self.assertEqual(kinds, {repetition.KIND_MOVE, repetition.KIND_LINE_DONE}) | |
| 253 | 255 | |
| 254 | 256 | def test_imported_only_once(self): |
| 255 | - repetition.Progress(self.db, legacy_path=self.json).record(self.course, self.main, True, at(20)) | |
| 257 | + board = after(self.course, "e2e4 e7e5 g1f3 b8c6") | |
| 258 | + repetition.Progress(self.db, legacy_path=self.json).record(self.course, board, G, at(20)) | |
| 256 | 259 | progress = repetition.Progress(self.db, legacy_path=self.json) |
| 257 | - self.assertNotEqual(progress.card(self.course, self.main)["due_day"], "2026-12-01") | |
| 260 | + self.assertNotEqual(progress.card(self.course, board)["due_day"], "2026-12-01") | |
| 261 | + | |
| 262 | + def test_old_database_gets_content_column(self): | |
| 263 | + db = sqlite3.connect(self.db) | |
| 264 | + db.executescript(repetition.SCHEMA.replace( | |
| 265 | + " content TEXT, -- zum Nachsehen: FEN der Stellung\n", "")) | |
| 266 | + db.close() | |
| 267 | + progress = repetition.Progress(self.db) | |
| 268 | + progress.record(self.course, after(self.course), G, at(0)) | |
| 269 | + self.assertIsNotNone(progress.card(self.course, after(self.course))["content"]) | |
| 258 | 270 | |
| 259 | 271 | |
| 260 | 272 | if __name__ == "__main__": |