From c0f5ee0cf74d0f60ab06d6e70649cbd2b10eeceb Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 15 Apr 2022 13:04:37 +0100 Subject: [PATCH] dice: Set cooldown_expiry to None when we can This will avoid pointless calls to Instant::now, which would otherwise be needed to discover that the cooldown time has expired (perhaps long ago). Signed-off-by: Ian Jackson --- src/dice.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/dice.rs b/src/dice.rs index 7e583ffc..a5b0f8ba 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -228,6 +228,22 @@ impl Die { / self.cooldown_time .as_secs_f64() } + + /// Possible stores None, saving us calling Instant::now in the future + #[throws(IE)] + pub fn cooldown_cleanup(&self, state: &mut State) { + if self.cooldown_remaining(state)? == Duration::default() { + state.cooldown_expires = None; + } + } + + #[throws(IE)] + pub fn cooldown_cleanup_hook(&self, gpieces: &mut GPieces, piece: PieceId) { + let state = gpieces + .byid_mut(piece).context("load hook")? + .xdata.get_mut_exp()?; + self.cooldown_cleanup(state)?; + } } #[dyn_upcast] @@ -275,6 +291,22 @@ impl PieceTrait for Die { vpid: VisiblePieceId) { self.svg(f, vpid, gpc.face, &gpc.xdata)? } + + #[throws(IE)] + fn held_change_hook(&self, + _: &InstanceRef, + gpieces: &mut GPieces, + piece: PieceId, + _was_held: Option) + -> UnpreparedUpdates { + self.cooldown_cleanup_hook(gpieces, piece)?; + None + } + + #[throws(IE)] + fn loaded_hook(&self, piece: PieceId, gs: &mut GameState, _: &InstanceRef) { + self.cooldown_cleanup_hook(&mut gs.pieces, piece)?; + } } #[typetag::serde(name="Die")] -- 2.30.2