chiark / gitweb /
dice: Set cooldown_expiry to None when we can
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 12:04:37 +0000 (13:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 21:43:43 +0000 (22:43 +0100)
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 <ijackson@chiark.greenend.org.uk>
src/dice.rs

index 7e583ffc480bcb25248e1c5431477b5393975d73..a5b0f8ba1e510e3766e307c28a93028299ee6be1 100644 (file)
@@ -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<PlayerId>)
+                      -> 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")]