//! LISTENING WITH A SONG PLAYING (Cotton Music, 5 Nov 2026; iOS's //! `ListeningCard.dealt`, answered by the core for a shell that only draws, //! 12 Nov 2026): `shell.deck { …, view: "music/listening", playing: { song, //! face, drawn?, said? } }` deals the player in front of the albums played //! lately. //! //! `song` is the core's player's `now` as `shell.player` answered it, `face` //! the face it wears (its `face`), `said` why the song cannot play here (the //! player's words), `drawn` the player faces this phone draws. The album the //! song plays from is drawn on the player's face and moved to the front, the //! song's words on it: its title, artist, album (or `said` in the album's //! place) and length, the album's cover (else the song's). The card is still //! the album's record, so opening it opens the album. A song on no album is //! its own single: the player card stands for the song itself, its cover the //! song's, and opens nothing. A song whose album this page does not hold //! stands in front as its single does, its record still its album's. //! //! The face worn is the player's own where this phone draws it; a phone that //! draws only some of the ring wears the first of the ring it draws //! ([`worn`]). Only the first page is dealt so, and only on Listening. use serde_json::{json, Map, Value}; use crate::methods::MethodError; use crate::phone::shell_player::FACES; /// The view the player stands in front of. pub const VIEW: &str = "music/listening"; /// The card's place naming the song it plays. pub const MARK: &str = "now_playing"; fn bad(why: &str) -> MethodError { MethodError::new("bad_request", why.to_string()) } /// The face the player card wears: `face` where `drawn` holds it (or no /// `drawn` is named), else the first of the player's ring this phone draws. pub fn worn(face: &str, drawn: Option<&[String]>) -> Result { let Some(drawn) = drawn else { return Ok(face.to_string()) }; if drawn.iter().any(|one| one == face) { return Ok(face.to_string()); } FACES .iter() .map(|(id, _)| *id) .find(|id| drawn.iter().any(|one| one == id)) .map(str::to_string) .ok_or_else(|| bad("playing.drawn names none of the player's faces")) } /// The deck's answer with the player dealt in front, where the request asks. pub fn deal(answer: &mut Value, request: &Value) -> Result<(), MethodError> { let playing = match request.get("playing") { None | Some(Value::Null) => return Ok(()), Some(Value::Object(playing)) => playing, Some(_) => return Err(bad("playing is { song, face, drawn?, said? }")), }; if request["view"].as_str() != Some(VIEW) || request["cursor"].as_u64().unwrap_or(0) != 0 { return Ok(()); } let song = playing.get("song").and_then(Value::as_object).ok_or_else(|| bad("playing names the song the player holds (shell.player's now)"))?; if song.get("kind").and_then(Value::as_str).is_some_and(|kind| kind != "song") { return Ok(()); } let id = song.get("id").and_then(Value::as_str).ok_or_else(|| bad("playing.song is shell.player's now, with its id"))?; let face = playing.get("face").and_then(Value::as_str).ok_or_else(|| bad("playing names the face the player wears"))?; let drawn: Option> = match playing.get("drawn") { None | Some(Value::Null) => None, Some(Value::Array(faces)) => Some(faces.iter().map(|face| face.as_str().map(str::to_string).ok_or_else(|| bad("playing.drawn is a list of face ids"))).collect::>()?), Some(_) => return Err(bad("playing.drawn is a list of face ids")), }; let face = worn(face, drawn.as_deref())?; let said = playing.get("said").and_then(Value::as_str).filter(|said| !said.is_empty()); let album = song.get("album").and_then(Value::as_str); let words = |key: &str| song.get(key).and_then(Value::as_str).filter(|text| !text.is_empty()); let cards = answer["cards"].as_array_mut().ok_or_else(|| MethodError::new("internal", "a deck answers its cards".to_string()))?; // Where each place stands ("1 / 14"), kept for the order the player deals. let positions: Vec = cards.iter().map(|card| card["position"].clone()).collect(); let held = album.and_then(|album| cards.iter().position(|card| card["id"] == album)).map(|at| cards.remove(at)); let (mut card, single) = match held { Some(card) => (card, false), None => { let mut card = json!({ "id": album.unwrap_or(id), "slots": {}, "tap": {}, "acts": [] }); card["record"] = json!({ "table": if album.is_some() { "music_album" } else { "song" }, "id": album.unwrap_or(id) }); (card, album.is_none()) } }; let old_title = card["slots"]["title"].as_str().map(str::to_string); let old_photo = card["slots"]["photo"].as_str().filter(|photo| !photo.is_empty()).map(str::to_string); let mut slots: Map = card["slots"].as_object().cloned().unwrap_or_default(); slots.insert(MARK.into(), json!(id)); for (slot, key) in [("title", "title"), ("artist", "artist"), ("subtitle", "artist")] { match words(key) { Some(text) => slots.insert(slot.into(), json!(text)), None => slots.remove(slot), }; } match said.or(words("album_title")).map(str::to_string).or(if album.is_some() { old_title } else { None }) { Some(text) => slots.insert("album".into(), json!(text)), None => slots.remove("album"), }; if let Some(duration) = song.get("duration_s").and_then(Value::as_f64).filter(|duration| *duration > 0.0) { slots.insert("duration".into(), json!(duration)); } match old_photo.or(words("cover").map(str::to_string)) { Some(photo) => slots.insert("photo".into(), json!(photo)), None => slots.remove("photo"), }; card["slots"] = Value::Object(slots); card["face"] = json!(face); card["player"] = json!({ "song": id, "single": single, "said": said }); cards.insert(0, card); // The album moved to the front stands first: each place says where it now stands. if positions.len() == cards.len() { for (card, position) in cards.iter_mut().zip(positions) { card["position"] = position; } } Ok(()) } #[cfg(test)] #[path = "shell_deck_player_tests.rs"] mod tests;