feat(training): draw [%cal]/[%csl] arrows from course comments
public
feat(training): draw [%cal]/[%csl] arrows from course comments Arrows show while their coach comment is visible, also when paging back. Commands are stripped from the displayed text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | e867cf33e63e6acbba812a4e1b77ec56b4837c71 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | aa29a4f8fa94 |
| Children | 372bd4e661e8 |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/diagram.py +19 −0
| @@ -50,6 +50,9 @@ DEFAULT_SCHEME = "holz" | ||
| 50 | 50 | HINT = (0.21, 0.52, 0.89) |
| 51 | 51 | THREAT = (0.91, 0.36, 0.11) |
| 52 | 52 | CHECK = (0.88, 0.11, 0.14) |
| 53 | +# Farben der Kurs-Pfeile ([%cal]/[%csl]), wie bei Lichess | |
| 54 | +SHAPE_COLORS = {"green": (0.08, 0.55, 0.20), "red": (0.80, 0.13, 0.13), | |
| 55 | + "blue": (0.10, 0.35, 0.80), "yellow": (0.90, 0.60, 0.0)} | |
| 53 | 56 | BADGE = { |
| 54 | 57 | "!!": (0.10, 0.62, 0.55), |
| 55 | 58 | "!": (0.30, 0.52, 0.80), |
| @@ -124,6 +127,7 @@ class BoardPainter: | ||
| 124 | 127 | self.clocks = None # {Farbe: Millisekunden} |
| 125 | 128 | self.clock_running = None # Farbe, deren Uhr gerade läuft |
| 126 | 129 | self.turn_marker = None # Farbe, deren Name fett steht (wer am Zug ist, bei zwei Spielern) |
| 130 | + self.shapes = () # Pfeile und Felder aus Kurskommentaren: ((von, nach, Farbe), …) | |
| 127 | 131 | self.preview = None # Zug, der halb durchsichtig gezeigt wird (Explorer: Maus über einer Zeile) |
| 128 | 132 | self.editing = False # Stellung aufbauen: keine geschlagenen Figuren anzeigen |
| 129 | 133 | self.eval_bar = None # Gewinnchance Weiß in Prozent: Balken links neben dem Brett |
| @@ -253,6 +257,12 @@ class BoardPainter: | ||
| 253 | 257 | |
| 254 | 258 | if self.threat_from is not None and self.threat_to is not None: |
| 255 | 259 | self._draw_arrow(cr, self.threat_from, self.threat_to, sq, THREAT) |
| 260 | + for tail, head, color in self.shapes: | |
| 261 | + rgb = SHAPE_COLORS.get(color, SHAPE_COLORS["green"]) | |
| 262 | + if tail == head: | |
| 263 | + self._ring(cr, tail, sq, rgb) | |
| 264 | + else: | |
| 265 | + self._draw_arrow(cr, tail, head, sq, rgb) | |
| 256 | 266 | |
| 257 | 267 | if self.strips: |
| 258 | 268 | if not self.editing: |
| @@ -294,6 +304,15 @@ class BoardPainter: | ||
| 294 | 304 | cr.rectangle(x + inset, y + inset, sq - 2 * inset, sq - 2 * inset) |
| 295 | 305 | cr.stroke() |
| 296 | 306 | |
| 307 | + def _ring(self, cr, square, sq, rgb): | |
| 308 | + """Markiertes Feld: Kreis wie bei Lichess.""" | |
| 309 | + x, y = self._square_origin(square) | |
| 310 | + cr.new_path() | |
| 311 | + cr.set_source_rgba(*rgb, 0.85) | |
| 312 | + cr.set_line_width(sq * 0.08) | |
| 313 | + cr.arc(x + sq / 2, y + sq / 2, sq * 0.42, 0, 2 * math.pi) | |
| 314 | + cr.stroke() | |
| 315 | + | |
| 297 | 316 | def _draw_arrow(self, cr, src, dst, sq, rgb): |
| 298 | 317 | x0, y0 = self._square_origin(src) |
| 299 | 318 | x1, y1 = self._square_origin(dst) |
modified schach/opening.py +20 −4
| @@ -32,6 +32,7 @@ class Option: | ||
| 32 | 32 | move: chess.Move |
| 33 | 33 | comment: str |
| 34 | 34 | main: bool # Hauptvariante (sonst Nebenvariante) |
| 35 | + shapes: tuple = () # Pfeile und Felder aus [%cal]/[%csl]: ((von, nach, Farbe), …), Feld: von == nach | |
| 35 | 36 | |
| 36 | 37 | |
| 37 | 38 | @dataclasses.dataclass |
| @@ -39,11 +40,12 @@ class Course: | ||
| 39 | 40 | title: str |
| 40 | 41 | color: bool |
| 41 | 42 | intro: str |
| 42 | - welcome: str # Kommentar vor dem ersten Zug | |
| 43 | + welcome: str # Kommentar vor dem ersten Zug (ohne [%cal]/[%csl]) | |
| 43 | 44 | start_fen: str |
| 44 | 45 | positions: dict # Stellungs-Hash -> [Option], Hauptvariante zuerst |
| 45 | 46 | reached: set # alle Stellungs-Hashes, die im Kurs vorkommen (auch Endstellungen) |
| 46 | 47 | path: str = "" |
| 48 | + welcome_shapes: tuple = () | |
| 47 | 49 | |
| 48 | 50 | def start(self): |
| 49 | 51 | return chess.Board(self.start_fen) |
| @@ -58,6 +60,18 @@ class Course: | ||
| 58 | 60 | return key(board) in self.reached |
| 59 | 61 | |
| 60 | 62 | |
| 63 | +COMMAND_RE = re.compile(r"\[%[^\]]*\]") | |
| 64 | + | |
| 65 | + | |
| 66 | +def clean_comment(text): | |
| 67 | + """Kommentar ohne PGN-Befehle wie [%cal Gc4f7], Leerraum zusammengefasst.""" | |
| 68 | + return " ".join(COMMAND_RE.sub(" ", text or "").split()) | |
| 69 | + | |
| 70 | + | |
| 71 | +def shapes(node): | |
| 72 | + return tuple((a.tail, a.head, a.color) for a in node.arrows()) | |
| 73 | + | |
| 74 | + | |
| 61 | 75 | def parse_course(text, path=""): |
| 62 | 76 | game = chess.pgn.read_game(io.StringIO(text)) |
| 63 | 77 | if game is None: |
| @@ -73,11 +87,12 @@ def parse_course(text, path=""): | ||
| 73 | 87 | for index, child in enumerate(node.variations): |
| 74 | 88 | options = positions.setdefault(key(board), []) |
| 75 | 89 | existing = next((o for o in options if o.move == child.move), None) |
| 76 | - comment = " ".join(child.comment.split()) | |
| 90 | + comment = clean_comment(child.comment) | |
| 77 | 91 | if existing is None: |
| 78 | - options.append(Option(child.move, comment, index == 0)) | |
| 92 | + options.append(Option(child.move, comment, index == 0, shapes(child))) | |
| 79 | 93 | elif not existing.comment: |
| 80 | 94 | existing.comment = comment |
| 95 | + existing.shapes = shapes(child) | |
| 81 | 96 | walk(child) |
| 82 | 97 | |
| 83 | 98 | walk(game) |
| @@ -88,7 +103,8 @@ def parse_course(text, path=""): | ||
| 88 | 103 | title=headers.get("Event", "Kurs"), |
| 89 | 104 | color=color, |
| 90 | 105 | intro=headers.get("SchachIntro", ""), |
| 91 | - welcome=" ".join(game.comment.split()), | |
| 106 | + welcome=clean_comment(game.comment), | |
| 107 | + welcome_shapes=shapes(game), | |
| 92 | 108 | start_fen=game.board().fen(), |
| 93 | 109 | positions=positions, |
| 94 | 110 | reached=reached, |
modified schach/training.py +15 −6
| @@ -116,9 +116,13 @@ class OpeningMixin: | ||
| 116 | 116 | self.coach = Gtk.Revealer(child=card, transition_type=Gtk.RevealerTransitionType.SLIDE_UP) |
| 117 | 117 | return self.coach |
| 118 | 118 | |
| 119 | - def _coach(self, markup, ply=None, replace=False): | |
| 120 | - """Neue Rückmeldung zur Stellung nach Halbzug ``ply`` (Standard: aktuelle Stellung).""" | |
| 121 | - entry = {"ply": len(self.board.move_stack) - 1 if ply is None else ply, "markup": markup} | |
| 119 | + def _coach(self, markup, ply=None, replace=False, shapes=()): | |
| 120 | + """Neue Rückmeldung zur Stellung nach Halbzug ``ply`` (Standard: aktuelle Stellung). | |
| 121 | + | |
| 122 | + ``shapes``: Pfeile und Felder aus dem Kurskommentar, solange diese Rückmeldung zu sehen ist. | |
| 123 | + """ | |
| 124 | + entry = {"ply": len(self.board.move_stack) - 1 if ply is None else ply, "markup": markup, | |
| 125 | + "shapes": tuple(shapes)} | |
| 122 | 126 | if replace and self.coach_log: |
| 123 | 127 | self.coach_log[-1] = entry |
| 124 | 128 | else: |
| @@ -130,10 +134,13 @@ class OpeningMixin: | ||
| 130 | 134 | def _coach_show(self): |
| 131 | 135 | if not self.coach_log: |
| 132 | 136 | self.coach.set_reveal_child(False) |
| 137 | + self.view.shapes = () | |
| 133 | 138 | return |
| 134 | 139 | last = len(self.coach_log) - 1 |
| 135 | 140 | index = last if self.coach_index is None else self.coach_index |
| 136 | 141 | self.coach_label.set_markup(self.coach_log[index]["markup"]) |
| 142 | + self.view.shapes = self.coach_log[index].get("shapes", ()) | |
| 143 | + self.view.queue_draw() | |
| 137 | 144 | self.coach_nav.set_visible(last > 0) |
| 138 | 145 | self.coach_prev.set_sensitive(index > 0) |
| 139 | 146 | self.coach_next.set_sensitive(index < last) |
| @@ -255,7 +262,7 @@ class OpeningMixin: | ||
| 255 | 262 | self.list_btn.set_active(False) |
| 256 | 263 | self.coach_log = [] |
| 257 | 264 | welcome = _escape(notation.text(course.welcome or course.intro, self._notation())) |
| 258 | - self._coach(f"{welcome}\n<b>{_escape(self._opening_line_text())}</b>") | |
| 265 | + self._coach(f"{welcome}\n<b>{_escape(self._opening_line_text())}</b>", shapes=course.welcome_shapes) | |
| 259 | 266 | self._changed() |
| 260 | 267 | self._refresh() |
| 261 | 268 | self._opening_next_step() |
| @@ -265,6 +272,7 @@ class OpeningMixin: | ||
| 265 | 272 | self.opening_busy = False |
| 266 | 273 | self.coach_log = [] |
| 267 | 274 | self.coach_index = None |
| 275 | + self.view.shapes = () | |
| 268 | 276 | self.coach.set_reveal_child(False) |
| 269 | 277 | self.coach_button.set_visible(False) |
| 270 | 278 | |
| @@ -331,7 +339,7 @@ class OpeningMixin: | ||
| 331 | 339 | self.opening_busy = False |
| 332 | 340 | self._changed() |
| 333 | 341 | comment = f" – {_escape(notation.text(option.comment, self._notation()))}" if option.comment else "" |
| 334 | - self._coach(f"<b>{_escape(label)}</b>{comment}") | |
| 342 | + self._coach(f"<b>{_escape(label)}</b>{comment}", shapes=option.shapes) | |
| 335 | 343 | self._refresh() |
| 336 | 344 | if not self.course.options(self.board): |
| 337 | 345 | self._opening_finished() |
| @@ -352,7 +360,8 @@ class OpeningMixin: | ||
| 352 | 360 | comment = verdict.option.comment or "Genau so." |
| 353 | 361 | else: |
| 354 | 362 | comment = "Zugumstellung – auch so kommst du zu unserem Ziel." |
| 355 | - self._coach(f"✓ <b>{label}</b> – {_escape(notation.text(comment, self._notation()))}") | |
| 363 | + self._coach(f"✓ <b>{label}</b> – {_escape(notation.text(comment, self._notation()))}", | |
| 364 | + shapes=verdict.option.shapes if verdict.option else ()) | |
| 356 | 365 | self._refresh() |
| 357 | 366 | self._opening_next_step() |
| 358 | 367 | return |
modified tests/test_opening.py +9 −0
| @@ -34,6 +34,15 @@ class CourseTest(unittest.TestCase): | ||
| 34 | 34 | for course in self.courses.values(): |
| 35 | 35 | self.assertIn(opening.key(course.start()), course.positions) |
| 36 | 36 | |
| 37 | + def test_arrows_parsed_and_hidden_from_text(self): | |
| 38 | + course = opening.parse_course( | |
| 39 | + '[Event "T"]\n\n{ Start [%csl Re4] } 1. e4 { Zentrum! [%cal Gd1h5,Gf1c4] } *') | |
| 40 | + option = course.options(chess.Board())[0] | |
| 41 | + self.assertEqual(option.comment, "Zentrum!") | |
| 42 | + self.assertEqual(option.shapes, ((chess.D1, chess.H5, "green"), (chess.F1, chess.C4, "green"))) | |
| 43 | + self.assertEqual(course.welcome, "Start") | |
| 44 | + self.assertEqual(course.welcome_shapes, ((chess.E4, chess.E4, "red"),)) | |
| 45 | + | |
| 37 | 46 | def test_options_main_first(self): |
| 38 | 47 | italian = self.courses["Italienisch"] |
| 39 | 48 | options = italian.options(play("e4", "e5", "Nf3", "Nc6", "Bc4")) |