[gnome-chess] Improve ChessClock



commit f4961bd57b243b0305b0fe771bd48f4b2a4a41bc
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Fri Jul 27 17:51:59 2018 -0500

    Improve ChessClock
    
    Make most of the members private and expose new properties to get the
    remaining time for each player. Much simpler to use.
    
    Credit to Alessandro Bruni for the initial version of this patch adding
    the new properties, and to Sahil Sareen for spotting a bug in that
    version.

 lib/chess-clock.vala | 89 +++++++++++++++++++++++++++-------------------------
 src/gnome-chess.vala | 17 +++-------
 2 files changed, 51 insertions(+), 55 deletions(-)
---
diff --git a/lib/chess-clock.vala b/lib/chess-clock.vala
index 04269a5..4761e73 100644
--- a/lib/chess-clock.vala
+++ b/lib/chess-clock.vala
@@ -51,60 +51,33 @@ public enum ClockType
 
 public class ChessClock : Object
 {
-    public int white_initial_seconds { get; private set; }
+    private int white_initial_seconds;
+    private int black_initial_seconds;
 
-    public int black_initial_seconds { get; private set; }
+    private int white_seconds_used = 0;
+    private int black_seconds_used = 0;
 
-    public int white_seconds_used { get; private set; default = 0; }
+    private int white_prev_move_seconds = 0;
+    private int black_prev_move_seconds = 0;
 
-    public int black_seconds_used { get; private set; default = 0; }
-
-    public ClockType clock_type { get; set; default = ClockType.SIMPLE; }
-
-    public int white_prev_move_seconds { get; private set; default = 0; }
-
-    public int black_prev_move_seconds { get; private set; default = 0; }
-
-    public int white_extra_seconds { get; private set; default = 0; }
-
-    public int black_extra_seconds { get; private set; default = 0; }
-
-    private Color _active_color = Color.WHITE;
+    private int white_extra_seconds = 0;
+    private int black_extra_seconds = 0;
 
     public int extra_seconds { get; set; default = 0; }
 
-    public void update_prev_move_time ()
+    public int white_remaining_seconds
     {
-        if (active_color == Color.WHITE)
-            black_prev_move_seconds = black_seconds_used;
-        else
-            white_prev_move_seconds = white_seconds_used;
+        get { return white_initial_seconds + white_extra_seconds - white_seconds_used; }
     }
 
-    public void update_extra_seconds ()
+    public int black_remaining_seconds
     {
-        int white_move_used = 0, black_move_used = 0;
-        switch (clock_type)
-        {
-        case ClockType.SIMPLE:
-            break;
-        case ClockType.FISCHER:
-            if (active_color == Color.WHITE)
-                white_extra_seconds += extra_seconds;
-            else
-                black_extra_seconds += extra_seconds;
-            break;
-        case ClockType.BRONSTEIN:
-            white_move_used = white_seconds_used - white_prev_move_seconds;
-            black_move_used = black_seconds_used - black_prev_move_seconds;
-            if (active_color != Color.WHITE)
-                white_extra_seconds += int.min (extra_seconds, white_move_used);
-            else
-                black_extra_seconds += int.min (extra_seconds, black_move_used);
-            break;
-        }
+        get { return black_initial_seconds + black_extra_seconds - black_seconds_used; }
     }
 
+    public ClockType clock_type { get; set; default = ClockType.SIMPLE; }
+
+    private Color _active_color = Color.WHITE;
     public Color active_color
     {
         get { return _active_color; }
@@ -219,4 +192,36 @@ public class ChessClock : Object
         Source.remove (tick_timeout_id);
         tick_timeout_id = 0;
     }
+
+    public void update_prev_move_time ()
+    {
+        if (active_color == Color.WHITE)
+            black_prev_move_seconds = black_seconds_used;
+        else
+            white_prev_move_seconds = white_seconds_used;
+    }
+
+    private void update_extra_seconds ()
+    {
+        int white_move_used = 0, black_move_used = 0;
+        switch (clock_type)
+        {
+        case ClockType.SIMPLE:
+            break;
+        case ClockType.FISCHER:
+            if (active_color == Color.WHITE)
+                white_extra_seconds += extra_seconds;
+            else
+                black_extra_seconds += extra_seconds;
+            break;
+        case ClockType.BRONSTEIN:
+            white_move_used = white_seconds_used - white_prev_move_seconds;
+            black_move_used = black_seconds_used - black_prev_move_seconds;
+            if (active_color != Color.WHITE)
+                white_extra_seconds += int.min (extra_seconds, white_move_used);
+            else
+                black_extra_seconds += int.min (extra_seconds, black_move_used);
+            break;
+        }
+    }
 }
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 6c5936d..2933b5a 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -1597,9 +1597,9 @@ Copyright © 2015–2016 Sahil Sareen""";
 
         int time;
         if (color == Color.WHITE)
-            time = game.clock.white_initial_seconds - game.clock.white_seconds_used + 
game.clock.white_extra_seconds;
+            time = game.clock.white_remaining_seconds;
         else
-            time = game.clock.black_initial_seconds - game.clock.black_seconds_used + 
game.clock.black_extra_seconds;
+            time = game.clock.black_remaining_seconds;
 
         if (time >= 60)
             return "%d∶\xE2\x80\x8E%02d".printf (time / 60, time % 60);
@@ -2282,17 +2282,8 @@ Copyright © 2015–2016 Sahil Sareen""";
         if (game.clock != null)
         {
             /* We currently only support simple timeouts */
-            uint white_initial_time = int.parse (pgn_game.white_time_left);
-            uint black_initial_time = int.parse (pgn_game.black_time_left);
-
-            uint white_used = game.clock.white_seconds_used;
-            uint black_used = game.clock.black_seconds_used;
-
-            uint white_extra = game.clock.white_extra_seconds;
-            uint black_extra = game.clock.black_extra_seconds;
-
-            pgn_game.white_time_left = (white_initial_time - white_used + white_extra).to_string ();
-            pgn_game.black_time_left = (black_initial_time - black_used + black_extra).to_string ();
+            pgn_game.white_time_left = game.clock.white_remaining_seconds.to_string ();
+            pgn_game.black_time_left = game.clock.black_remaining_seconds.to_string ();
         }
     }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]