chiark / gitweb /
clock: Implement per_move, but brokenly
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 20:16:55 +0000 (20:16 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 21:53:14 +0000 (21:53 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/clock.rs

index 6c6539ea4b4069d4faf1d575a68c7447c1e05118..6e548bf3c927ae240108d279834b3b475f8a1bd3 100644 (file)
@@ -88,7 +88,7 @@ impl std::ops::Not for User {
 #[derive(Debug,Clone,Serialize,Deserialize)]
 pub struct ChessClock { // Spec
   time: Time,
-  #[serde(default)] per_move: usize,
+  #[serde(default)] per_move: Time,
 }
 
 #[derive(Debug,Serialize,Deserialize)]
@@ -239,6 +239,7 @@ impl State {
   fn do_start_or_stop(&mut self, piece: PieceId,
                       was_implied_running: Option<User>,
                       held: Option<PlayerId>,
+                      spec: &ChessClock,
                       ig: &InstanceRef) {
     let state = self;
     if state.implies_running(held) == was_implied_running { return }
@@ -253,6 +254,16 @@ impl State {
       }
     }
 
+    if_chain! {
+      if let Some(was_running_user) = was_implied_running;
+      if let Some(now_running_user) = state.implies_running(held);
+      if now_running_user != was_running_user;
+      then {
+        let remaining = &mut state.users[now_running_user].remaining;
+        *remaining = *remaining + TVL::seconds(spec.per_move.into());
+      }
+    }
+
     if_chain! {
       if let Some(now_running_user) = state.implies_running(held);
       then {
@@ -628,7 +639,7 @@ impl PieceTrait for Clock {
       }
     };
 
-    state.do_start_or_stop(piece, was_implied_running, held, ig)
+    state.do_start_or_stop(piece, was_implied_running, held, &self.spec, ig)
       .map_err(|e| APOE::ReportViaResponse(e.into()))?;
 
     let log = log_did_to_piece(ioccults, gpl, gpc, ipc, &did)
@@ -682,7 +693,7 @@ impl PieceTrait for Clock {
       }
     }
 
-    state.do_start_or_stop(piece, was_running, now_held, ig)?;
+    state.do_start_or_stop(piece, was_running, now_held, &self.spec, ig)?;
     unprepared_update(piece)
   }