[gnome-games/glchess-vala] Only use game clock if configured



commit 160ac2094036399ea7472f8bc8c00f8de491fd1e
Author: Robert Ancell <robert ancell canonical com>
Date:   Sun Jan 9 14:25:33 2011 +1100

    Only use game clock if configured

 glchess/src/chess-game.vala |   26 +++++++++++++++++------
 glchess/src/chess-pgn.vala  |   12 ++++++++++-
 glchess/src/glchess.vala    |   46 ++++++++++++++++++++++++++++++++++--------
 3 files changed, 67 insertions(+), 17 deletions(-)
---
diff --git a/glchess/src/chess-game.vala b/glchess/src/chess-game.vala
index 0b475e0..47d5dde 100644
--- a/glchess/src/chess-game.vala
+++ b/glchess/src/chess-game.vala
@@ -787,7 +787,6 @@ public enum ChessRule
 public class ChessGame
 {
     public bool is_started;
-    public ChessClock clock;
     public ChessResult result;
     public ChessRule rule;
     public List<ChessState> move_stack;
@@ -809,11 +808,20 @@ public class ChessGame
     {
         get { return move_stack.data.current_player; }
     }
+    private ChessClock? _clock;
+    public ChessClock? clock
+    {
+        get { return _clock; }
+        set
+        {
+            if (is_started)
+                return;
+            _clock = value;
+        }
+    }
 
     public ChessGame (string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
     {
-        clock = new ChessClock (60, 60);
-
         is_started = false;
         move_stack.prepend (new ChessState (fen));
         result = ChessResult.IN_PROGRESS;
@@ -869,7 +877,8 @@ public class ChessGame
             state.last_move.moved_rook.moved ();
         moved (state.last_move);
 
-        clock.active_color = current_player.color;
+        if (_clock != null)
+            _clock.active_color = current_player.color;
         current_player.start_turn ();
         turn_started (current_player);
 
@@ -909,9 +918,12 @@ public class ChessGame
 
         reset ();
 
-        clock.expired.connect (clock_expired_cb);
-        clock.active_color = current_player.color;
-        clock.start ();
+        if (_clock != null)
+        {
+            _clock.expired.connect (clock_expired_cb);
+            _clock.active_color = current_player.color;
+            _clock.start ();
+        }
 
         started ();
         current_player.start_turn ();
diff --git a/glchess/src/chess-pgn.vala b/glchess/src/chess-pgn.vala
index 3c16fc0..a4cf04b 100644
--- a/glchess/src/chess-pgn.vala
+++ b/glchess/src/chess-pgn.vala
@@ -69,6 +69,11 @@ public class PGNGame
         get { return tags.lookup ("Date"); }
         set { tags.insert ("Date", value); }
     }
+    public string time
+    {
+        get { return tags.lookup ("Time"); }
+        set { tags.insert ("Time", value); }
+    }
     public string round
     {
         get { return tags.lookup ("Round"); }
@@ -94,6 +99,11 @@ public class PGNGame
         get { return tags.lookup ("Annotator"); }
         set { tags.insert ("Annotator", value); }
     }
+    public string? time_control
+    {
+        get { return tags.lookup ("TimeControl"); }
+        set { tags.insert ("TimeControl", value); }
+    }
     public bool set_up
     {
         get { string? v = tags.lookup ("SetUp"); return v != null && v == "1" ? true : false; }
@@ -108,7 +118,7 @@ public class PGNGame
     {
         get { return tags.lookup ("Termination"); }
         set { tags.insert ("Termination", value); }
-    }   
+    }
 
     public PGNGame ()
     {
diff --git a/glchess/src/glchess.vala b/glchess/src/glchess.vala
index 3d5c888..a061fe4 100644
--- a/glchess/src/glchess.vala
+++ b/glchess/src/glchess.vala
@@ -200,11 +200,24 @@ public class Application
         else
             game = new ChessGame ();
 
+        if (pgn_game.time_control != null)
+        {
+            var controls = pgn_game.time_control.split (":");
+            foreach (var control in controls)
+            {
+                /* We only support simple timeouts */
+                var duration = control.to_int ();
+                if (duration > 0)
+                    game.clock = new ChessClock (duration, duration);
+            }
+        }
+
         game.started.connect (game_start_cb);
         game.turn_started.connect (game_turn_cb);
         game.moved.connect (game_move_cb);
         game.ended.connect (game_end_cb);
-        game.clock.tick.connect (game_clock_tick_cb);
+        if (game.clock != null)
+            game.clock.tick.connect (game_clock_tick_cb);
 
         var model = (Gtk.ListStore) history_combo.model;
         model.clear ();
@@ -630,7 +643,7 @@ public class Application
         double fg[3] = { 0.0, 0.0, 0.0 };
         double bg[3] = { 1.0, 1.0, 1.0 };
 
-        draw_time (widget, c, (int) (game.clock.white_duration / 1000 - game.clock.white_used_in_seconds), fg, bg);
+        draw_time (widget, c, make_clock_text (game.clock, Color.WHITE), fg, bg);
         return false;
     }
 
@@ -640,19 +653,30 @@ public class Application
         double fg[3] = { 1.0, 1.0, 1.0 };
         double bg[3] = { 0.0, 0.0, 0.0 };
 
-        draw_time (widget, c, (int) (game.clock.black_duration / 1000 - game.clock.black_used_in_seconds), fg, bg);
+        draw_time (widget, c, make_clock_text (game.clock, Color.BLACK), fg, bg);
         return false;
     }
 
-    private void draw_time (Gtk.Widget widget, Cairo.Context c, int used, double[] fg, double[] bg)
+    private string make_clock_text (ChessClock? clock, Color color)
     {
-        double alpha = 1.0;
+        if (clock == null)
+            return "â??";
+
+        int used;
+        if (color == Color.WHITE)
+            used = (int) (game.clock.white_duration / 1000 - game.clock.white_used_in_seconds);
+        else
+            used = (int) (game.clock.black_duration / 1000 - game.clock.black_used_in_seconds);
 
-        string text = "â??";
         if (used >= 60)
-            text = "%d:%02d".printf (used / 60, used % 60);
-        else if (used >= 0)
-            text = ":%02d".printf (used);
+            return "%d:%02d".printf (used / 60, used % 60);
+        else
+            return ":%02d".printf (used);
+    }
+
+    private void draw_time (Gtk.Widget widget, Cairo.Context c, string text, double[] fg, double[] bg)
+    {
+        double alpha = 1.0;
 
         if (widget.get_state () == Gtk.StateType.INSENSITIVE)
             alpha = 0.5;
@@ -1222,6 +1246,10 @@ public class Application
         pgn_game = new PGNGame ();
         var now = new DateTime.now_local ();
         pgn_game.date = now.format ("%Y.%m.%d");
+        pgn_game.time = now.format ("%H:%M:%S");
+        var duration = settings.get_int ("duration");
+        if (duration > 0)
+            pgn_game.time_control = "%d".printf (duration);
         start_game ();
     }
 



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