From a5b07e9409d2b59106fbe4b725a984164176226e Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Fri, 27 Nov 2026 01:51:22 +0300 Subject: [PATCH] opening trainer: starter repertoires (Spanish for White, Najdorf for Black) on the first opening --- .../assets/repertoires/sicilian-black.pgn | 11 +++ .../assets/repertoires/spanish-white.pgn | 13 ++++ apps/desktop/src/i18n.rs | 22 ++++++ apps/desktop/src/repertoire_ui.rs | 69 ++++++++++++++++++- apps/desktop/src/repertoire_ui_tests.rs | 40 +++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 apps/desktop/assets/repertoires/sicilian-black.pgn create mode 100644 apps/desktop/assets/repertoires/spanish-white.pgn diff --git a/apps/desktop/assets/repertoires/sicilian-black.pgn b/apps/desktop/assets/repertoires/sicilian-black.pgn new file mode 100644 index 0000000..a050f88 --- /dev/null +++ b/apps/desktop/assets/repertoires/sicilian-black.pgn @@ -0,0 +1,11 @@ +[Event "ChessPenguin: Sicilian Najdorf for Black"] +[White "?"] +[Black "Repertoire"] +[Result "*"] +[ECO "B90"] + +{Sicilian Defence, Najdorf Variation for Black, with answers to 6.Be3, 6.Bg5 and the Alapin 2.c3.} +1. e4 c5 2. Nf3 (2. c3 {Alapin Variation.} 2... Nf6 3. e5 Nd5 4. d4 cxd4 5. Nf3 Nc6 6. cxd4 d6 {Black attacks the e5 pawn at once.}) +2... d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 a6 {The Najdorf: ...a6 controls b5 and prepares ...e5 or ...b5.} +6. Be3 (6. Bg5 e6 7. f4 Be7 8. Qf3 Qc7 9. O-O-O Nbd7 {Black keeps the position solid and prepares ...b5.}) +6... e5 7. Nb3 Be6 8. f3 Be7 9. Qd2 O-O 10. O-O-O Nbd7 11. g4 b5 {English Attack: both sides race on opposite wings.} * diff --git a/apps/desktop/assets/repertoires/spanish-white.pgn b/apps/desktop/assets/repertoires/spanish-white.pgn new file mode 100644 index 0000000..c1d8739 --- /dev/null +++ b/apps/desktop/assets/repertoires/spanish-white.pgn @@ -0,0 +1,13 @@ +[Event "ChessPenguin: Spanish Opening for White"] +[White "Repertoire"] +[Black "?"] +[Result "*"] +[ECO "C84"] + +{Spanish Opening (Ruy Lopez) for White: the Closed main line, the Marshall Attack and the Berlin Defence.} +1. e4 e5 2. Nf3 Nc6 3. Bb5 {The Spanish bishop pressures the knight that defends e5.} +3... a6 (3... Nf6 {Berlin Defence.} 4. O-O Nxe4 5. d4 Nd6 6. Bxc6 dxc6 7. dxe5 Nf5 +8. Qxd8+ Kxd8 {The Berlin endgame: White has the healthier pawn majority.}) +4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 (7... O-O 8. c3 d5 {Marshall Attack.} +9. exd5 Nxd5 10. Nxe5 Nxe5 11. Rxe5 c6 12. d4 {White returns material later if needed, but keeps the extra pawn for now.}) +8. c3 O-O 9. h3 {Stops ...Bg4 and prepares d4.} 9... Na5 10. Bc2 c5 11. d4 Qc7 12. Nbd2 {Chigorin Variation: the knight heads for f1 and g3.} * diff --git a/apps/desktop/src/i18n.rs b/apps/desktop/src/i18n.rs index b5e6efd..70dd1c9 100644 --- a/apps/desktop/src/i18n.rs +++ b/apps/desktop/src/i18n.rs @@ -18015,6 +18015,28 @@ const ROWS: &[[&str; 9]] = &[ "Bo‘sh repertuar yarating yoki PGN import qiling. Shuningdek, istalgan partiyada yurishni tanlab “Repertuariga qo‘shish…”ni tanlashingiz mumkin.", "Бос репертуар жасаңыз немесе PGN импорттаңыз. Сондай-ақ, кез келген партияда жүрісті таңдап, «Репертуарға қосу…» таңдауыңызға болады.", ], + [ + "Spanish Opening", + "Испанская партия", + "Partie espagnole", + "Spanische Partie", + "Apertura española", + "Partita spagnola", + "Abertura Espanhola", + "Ispan o'yini", + "Испан партиясы", + ], + [ + "Sicilian Defence: Najdorf", + "Сицилианская защита: Найдорф", + "Défense sicilienne : Najdorf", + "Sizilianische Verteidigung: Najdorf", + "Defensa siciliana: Najdorf", + "Difesa siciliana: Najdorf", + "Defesa Siciliana: Najdorf", + "Sitsiliya himoyasi: Naydorf", + "Сицилия қорғанысы: Найдорф", + ], [ "New repertoire", "Новый репертуар", diff --git a/apps/desktop/src/repertoire_ui.rs b/apps/desktop/src/repertoire_ui.rs index 3236ef2..c2aadfa 100644 --- a/apps/desktop/src/repertoire_ui.rs +++ b/apps/desktop/src/repertoire_ui.rs @@ -292,12 +292,21 @@ pub struct RepertoireUi { debug_start: Option, debug_pending_moves: Option, debug_menu: bool, + /// Colours of the starter repertoires created on the first opening, written to the + /// settings by the next `show`. + starter_sides: Vec<(RepertoireId, LearningSide)>, } impl RepertoireUi { pub fn new() -> Self { let (library_store, learning_store) = stores(); - Self::with_stores(library_store, learning_store) + let starter = seed_starter(&library_store); + let mut this = Self::with_stores(library_store, learning_store); + match starter { + Ok(sides) => this.starter_sides = sides, + Err(error) => this.show_message(error, true), + } + this } fn with_stores(library_store: LibraryStore, learning_store: LearningStore) -> Self { @@ -341,6 +350,7 @@ impl RepertoireUi { debug_start: None, debug_pending_moves: None, debug_menu: false, + starter_sides: Vec::new(), }; this.selected = this.sorted_ids().first().cloned(); this.reindex(); @@ -1075,6 +1085,12 @@ impl RepertoireUi { pub fn show(&mut self, ui: &mut Ui, settings: &mut Settings) -> Vec { self.poll(settings); let mut actions = Vec::new(); + if !self.starter_sides.is_empty() { + for (id, side) in self.starter_sides.drain(..) { + layout::set_side(settings, &id, side); + } + actions.push(RepertoireAction::SettingsChanged); + } let available = ui.available_rect_before_wrap(); let background = ui.visuals().window_fill(); ui.painter_at(available) @@ -2798,6 +2814,57 @@ fn read_import( }) } +const STARTER_REPERTOIRES: [(&str, &str, LearningSide); 2] = [ + ( + "Spanish Opening", + include_str!("../assets/repertoires/spanish-white.pgn"), + LearningSide::White, + ), + ( + "Sicilian Defence: Najdorf", + include_str!("../assets/repertoires/sicilian-black.pgn"), + LearningSide::Black, + ), +]; + +/// The first opening of the section, before any repertoire library file exists, gets +/// two example repertoires. An existing file, even an empty one, is never touched, so +/// examples deleted by the user do not come back. +fn seed_starter(store: &LibraryStore) -> Result, String> { + if store.path().exists() { + return Ok(Vec::new()); + } + let mut library = Library::default(); + let mut sides = Vec::new(); + let cancel = AtomicBool::new(false); + for (name, pgn, side) in STARTER_REPERTOIRES { + let db = PgnDatabase::from_bytes(pgn.as_bytes().to_vec(), None, &mut |_| {}, &cancel) + .map_err(|e| e.to_string())?; + let mut id = None; + for index in 0..db.len() { + let Some(parsed) = db.load_game(index) else { + continue; + }; + let current = match &id { + Some(id) => id, + None => id.insert( + library + .create(tr(name), &Game::from_start(parsed.game.start().clone())) + .map_err(|e| e.to_string())?, + ), + }; + library + .add_game(current, &parsed.game) + .map_err(|e| e.to_string())?; + } + if let Some(id) = id { + sides.push((id, side)); + } + } + store.save(&mut library).map_err(|e| e.to_string())?; + Ok(sides) +} + fn load_library(store: &LibraryStore) -> Result { let mut library = store.load().map_err(|error| error.to_string())?; // The depth limit of 2.2 is not a user's choice: trained lines go to their ends. diff --git a/apps/desktop/src/repertoire_ui_tests.rs b/apps/desktop/src/repertoire_ui_tests.rs index a2611ab..fee0c71 100644 --- a/apps/desktop/src/repertoire_ui_tests.rs +++ b/apps/desktop/src/repertoire_ui_tests.rs @@ -525,3 +525,43 @@ fn a_session_needs_no_question_to_go_back_and_counts_refresh_after_it() { let stats = ui.stats.get(&id).unwrap().as_ref().unwrap(); assert_eq!((stats.unseen, stats.due), (2, 1)); } + +#[test] +fn starter_repertoires_are_created_once_with_their_colours() { + let dir = TestDir::new("starter"); + let (store, _) = dir.stores(); + let sides = seed_starter(&store).unwrap(); + let library = store.load().unwrap(); + assert_eq!(library.repertoires.len(), 2); + assert_eq!( + sides.iter().map(|(_, side)| *side).collect::>(), + [LearningSide::White, LearningSide::Black] + ); + for ((id, _), lines) in sides.iter().zip([3, 3]) { + let repertoire = library.repertoire(id).unwrap(); + assert_eq!(layout::lines(&repertoire.game(), |_| false).len(), lines); + } + assert_eq!(library.repertoires[0].name, "Spanish Opening"); + assert_eq!(library.repertoires[1].name, "Sicilian Defence: Najdorf"); + + let mut settings = Settings::default(); + let mut ui = dir.ui(); + ui.starter_sides = sides; + egui::__run_test_ui(|egui_ui| { + ui.show(egui_ui, &mut settings); + }); + let black = &library.repertoires[1].id; + assert_eq!(layout::side_of(&settings, black), LearningSide::Black); + + assert!(seed_starter(&store).unwrap().is_empty()); + assert_eq!(store.load().unwrap().repertoires.len(), 2); +} + +#[test] +fn starter_repertoires_do_not_return_to_an_existing_library() { + let dir = TestDir::new("starter-existing"); + let (store, _) = dir.stores(); + store.save(&mut Library::default()).unwrap(); + assert!(seed_starter(&store).unwrap().is_empty()); + assert!(store.load().unwrap().repertoires.is_empty()); +}