From 1dfbded9ccae9ff20fe2cad8989b181a6ccc5039 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 16 Feb 2021 20:51:37 +0000 Subject: [PATCH] recalculate_occultation: Add log callback NFC Signed-off-by: Ian Jackson --- daemon/api.rs | 19 ++++++++++++++----- src/hidden.rs | 29 ++++++++++++++++------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/daemon/api.rs b/daemon/api.rs index 413dc996..cbb00df8 100644 --- a/daemon/api.rs +++ b/daemon/api.rs @@ -286,11 +286,20 @@ api_route!{ logents); let update= - recalculate_occultation(gs, - who_by, - ipieces, - piece, - vanilla).map_err(|e| OnlineError::from(e))?; + recalculate_occultation( + gs, + who_by, + ipieces, + piece, + vanilla, + |who_by, old, new, show| vec![ LogEntry { html: Html(format!( + "{} moved {} from {} to {}", + &who_by.0, + if let Some(show) = show { &show.0 } else { "something" }, + &old.0, &new.0, + ))}], + + ).map_err(|e| OnlineError::from(e))?; update } diff --git a/src/hidden.rs b/src/hidden.rs index aba911bd..fb8dc598 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -210,12 +210,15 @@ pub fn massage_prep_piecestate( // xxx this means this only happens on ungrab I think ? #[throws(InternalError)] -pub fn recalculate_occultation( +pub fn recalculate_occultation< + L: FnOnce(&Html, Html, Html, Option<&Html>) -> Vec, + >( gs: &mut GameState, who_by: Html, ipieces: &PiecesLoaded, piece: PieceId, vanilla: PUFOS, + log_callback: L, ) -> PieceUpdate { @@ -316,6 +319,14 @@ pub fn recalculate_occultation( let most_obscure = most_obscure.unwrap_or(&OccK::Visible); // no players! + let call_log_callback = + |show| Ok::<_,IE>( + log_callback(&who_by, + describe_occulter(ONI::Old)?, + describe_occulter(ONI::New)?, + show) + ); + let log = match most_obscure { OccK::Visible => { vanilla.2 @@ -323,19 +334,11 @@ pub fn recalculate_occultation( OccK::Scrambled | OccK::Displaced{..} => { let face = ipc.nfaces() - 1; let show = ipc.describe_html(Some(face.into()), gpc)?; - vec![ LogEntry { html: Html(format!( - "{} moved {} from {} to {}", - who_by.0, &show.0, - describe_occulter(ONI::Old)?.0, - describe_occulter(ONI::New)?.0, - ))}] + call_log_callback(Some(&show))? + }, + OccK::Invisible => { + call_log_callback(None)? }, - OccK::Invisible => vec![ LogEntry { html: Html(format!( - "{} moved something from {} to {}", - who_by.0, - describe_occulter(ONI::Old)?.0, - describe_occulter(ONI::New)?.0, - ))}], }; let update = PieceUpdate { -- 2.30.2