diff --git a/apps/desktop/src/game_view.rs b/apps/desktop/src/game_view.rs index 25b36f8..4d2a1ee 100644 --- a/apps/desktop/src/game_view.rs +++ b/apps/desktop/src/game_view.rs @@ -2109,16 +2109,6 @@ impl GameView { { self.board.set_premove(from, to); } - let maia = format!("Maia {}", start.strength); - let you = if start.user_name.is_empty() { - "?".to_owned() - } else { - start.user_name.clone() - }; - let (white, black) = match start.user { - Color::White => (you, maia), - Color::Black => (maia, you), - }; let event = match start.clock { Some(tc) => trf( "Game vs computer, {speed}", @@ -2127,30 +2117,16 @@ impl GameView { None => tr("Game vs computer").to_owned(), }; let g = &mut self.game; - g.set_tag("Event", &event); - g.set_tag("Site", "ChessPenguin"); - g.set_tag("White", &white); - g.set_tag("Black", &black); - let (we, be) = match start.user { - Color::White => (start.user_elo, Some(start.strength)), - Color::Black => (Some(start.strength), start.user_elo), - }; - if let Some(e) = we { - g.set_tag("WhiteElo", &e.to_string()); - } - if let Some(e) = be { - g.set_tag("BlackElo", &e.to_string()); - } - let (wt, bt) = match start.user { - Color::White => ("human", "program"), - Color::Black => ("program", "human"), - }; - g.set_tag("WhiteType", wt); - g.set_tag("BlackType", bt); - g.set_tag( - "TimeControl", + for (name, value) in ::play::game_tags( + &event, + start.user, + &start.user_name, + start.user_elo, + start.strength, &start.clock.map_or_else(|| "-".to_owned(), |tc| tc.pgn()), - ); + ) { + g.set_tag(&name, &value); + } // From a position of another game: its moves are already here; after the last // of them, where the game comes from (a comment of the game, written in PGN). let floor = match &start.from { diff --git a/crates/mobile-api/src/lib.rs b/crates/mobile-api/src/lib.rs index 6d9d298..64fa558 100644 --- a/crates/mobile-api/src/lib.rs +++ b/crates/mobile-api/src/lib.rs @@ -25,7 +25,9 @@ pub use online::{ AccountLookup, DownloadState, OnlineAccount, OnlineDatabase, OnlineDownload, OnlineFailure, OnlineProblem, OnlineTarget, Site, }; -pub use play::{GameEnding, MaiaPlayer, ResignJudge, hopeless_turns, maia_pause_ms, maia_range}; +pub use play::{ + GameEnding, MaiaPlayer, ResignJudge, hopeless_turns, maia_game_tags, maia_pause_ms, maia_range, +}; pub use review::{GameReview, MoveJudgement, ReviewMove, ReviewPoint, ReviewState, SideReview}; pub use session::{Edited, GameSession, own_create, own_remove, own_save}; pub use train::{TrainState, TrainStats, TrainVerdict, Trainer, trainer_set_file}; diff --git a/crates/mobile-api/src/play.rs b/crates/mobile-api/src/play.rs index 22d63ef..40cf86b 100644 --- a/crates/mobile-api/src/play.rs +++ b/crates/mobile-api/src/play.rs @@ -60,6 +60,26 @@ pub fn maia_pause_ms(ply: u32, only_move: bool) -> u64 { ::play::pause(None, ply as usize, only_move).as_millis() as u64 } +/// The tags of a game against Maia on the phone (no clocks): `event` in the app's +/// language; the user plays White when `user_white`. +#[uniffi::export] +pub fn maia_game_tags( + event: String, + user_white: bool, + user_name: String, + strength: u32, +) -> Vec { + let user = if user_white { + shakmaty::Color::White + } else { + shakmaty::Color::Black + }; + ::play::game_tags(&event, user, &user_name, None, strength, "-") + .into_iter() + .map(|(name, value)| crate::Tag { name, value }) + .collect() +} + /// Maia's turns in a row with her position hopeless before she offers to resign. #[uniffi::export] pub fn hopeless_turns() -> u32 { diff --git a/crates/play/src/lib.rs b/crates/play/src/lib.rs index 7b2c513..2bc0bfa 100644 --- a/crates/play/src/lib.rs +++ b/crates/play/src/lib.rs @@ -126,6 +126,47 @@ pub fn pause(clock: Option<(Duration, u32)>, ply: usize, only_move: bool) -> Dur Duration::from_secs_f32(secs) } +// ----- the game's tags ----------------------------------------------------------------- + +/// The tags of a game against Maia (as Lichess writes a game against a program): +/// `event` in the app's language, the user's name ("?" unknown) and rating, Maia's +/// strength, the time control in PGN ("-": none). The rest (date, result) is the game's. +pub fn game_tags( + event: &str, + user: Color, + user_name: &str, + user_elo: Option, + strength: u32, + time_control: &str, +) -> Vec<(String, String)> { + let maia = format!("Maia {strength}"); + let you = if user_name.trim().is_empty() { + "?".to_owned() + } else { + user_name.trim().to_owned() + }; + let (white, black, we, be, wt, bt) = match user { + Color::White => (you, maia, user_elo, Some(strength), "human", "program"), + Color::Black => (maia, you, Some(strength), user_elo, "program", "human"), + }; + let mut tags = vec![ + ("Event".to_owned(), event.to_owned()), + ("Site".to_owned(), "ChessPenguin".to_owned()), + ("White".to_owned(), white), + ("Black".to_owned(), black), + ]; + if let Some(e) = we { + tags.push(("WhiteElo".to_owned(), e.to_string())); + } + if let Some(e) = be { + tags.push(("BlackElo".to_owned(), e.to_string())); + } + tags.push(("WhiteType".to_owned(), wt.to_owned())); + tags.push(("BlackType".to_owned(), bt.to_owned())); + tags.push(("TimeControl".to_owned(), time_control.to_owned())); + tags +} + // ----- Maia resigns -------------------------------------------------------------------- /// Nodes the judge searches a position: a tenth of a second or two, enough to see that