chiark / gitweb /
clock: Tidying, refactor, introduce initial()
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 21:37:45 +0000 (21:37 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 21:55:05 +0000 (21:55 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/clock.rs

index 3f3acd3925236c9cd657ed5d5952efe81032f7ba..44888a5fda7ec20b65ce37fca629aa685ee53b6e 100644 (file)
@@ -56,6 +56,15 @@ struct Running {
 impl Spec {
   fn initial_time(&self) -> TimeSpec { TVL::seconds(self.time.into()) }
   fn per_move(&self) -> TimeSpec { TVL::seconds(self.per_move.into()) }
+  fn initial(&self) -> [TimeSpec; N] {
+    // White is player Y, and they will ge to go first, so the clock
+    // will go from stopped to Y, and then later when it's X's turn
+    // X will get an extra per_move.  Y therefore needs per_move too.
+    [
+      self.initial_time(),
+      self.initial_time() + self.per_move(),
+    ]
+  }
 }
 
 impl State {
@@ -66,14 +75,9 @@ impl State {
   }
 
   fn reset(&mut self, spec: &Spec) {
-    for ust in &mut self.users {
-      ust.remaining = spec.initial_time();
+    for (ust, t) in izip!(&mut self.users, spec.initial().iter().copied()) {
+      ust.remaining = t;
     }
-    // White is player Y, and they will ge to go first, so the clock
-    // will go from stopped to Y, and then later when it's X's turn
-    // X will get an extra per_move.  Y therefore needs per_move too.
-    let y_remaining = &mut self.users[USERS[0]].remaining;
-    *y_remaining = *y_remaining + spec.per_move();
   }
 
   fn implies_running(&self, held: Option<PlayerId>) -> Option<User> {