[gnome-games/glchess-vala] Update history index when game saved, fix date for games



commit dfb87ceb0d026ff7324b0341e7f6b8ab871ee1bc
Author: Robert Ancell <robert ancell canonical com>
Date:   Sat Jan 1 14:58:25 2011 +1100

    Update history index when game saved, fix date for games

 glchess/src/chess-pgn.vala |    9 ++++++
 glchess/src/glchess.vala   |   62 ++++++++++++++++++++++---------------------
 glchess/src/history.vala   |   12 ++++++++-
 3 files changed, 52 insertions(+), 31 deletions(-)
---
diff --git a/glchess/src/chess-pgn.vala b/glchess/src/chess-pgn.vala
index e06d557..3c16fc0 100644
--- a/glchess/src/chess-pgn.vala
+++ b/glchess/src/chess-pgn.vala
@@ -45,6 +45,15 @@ public class PGNGame
     public static string RESULT_WHITE       = "1-0";
     public static string RESULT_BLACK       = "0-1";
 
+    public static string TERMINATE_ABANDONED        = "abandoned";
+    public static string TERMINATE_ADJUDICATION     = "adjudication";
+    public static string TERMINATE_DEATH            = "death";
+    public static string TERMINATE_EMERGENCY        = "emergency";
+    public static string TERMINATE_NORMAL           = "normal";
+    public static string TERMINATE_RULES_INFRACTION = "rules infraction";
+    public static string TERMINATE_TIME_FORFEIT     = "time forfeit";
+    public static string TERMINATE_UNTERMINATED     = "unterminated";
+
     public string event
     {
         get { return tags.lookup ("Event"); }
diff --git a/glchess/src/glchess.vala b/glchess/src/glchess.vala
index b602cf7..8623467 100644
--- a/glchess/src/glchess.vala
+++ b/glchess/src/glchess.vala
@@ -33,10 +33,10 @@ public class Application
     private Gtk.Label? save_dialog_error_label = null;
     private Gtk.AboutDialog? about_dialog = null;
 
+    private PGNGame pgn_game;
     private ChessGame game;
     private bool in_history;
     private File autosave_file;
-    private PGNGame pgn_game;
     private List<AIProfile> ai_profiles;
     private ChessPlayer? opponent = null;
     private ChessEngine? opponent_engine = null;
@@ -110,7 +110,9 @@ public class Application
         {
             try
             {
-                if (autosave_file == null)
+                if (autosave_file != null)
+                    history.update (autosave_file, "", pgn_game.result);
+                else
                     autosave_file = history.add (pgn_game.date, pgn_game.result);
                 pgn_game.write (autosave_file);
             }
@@ -177,9 +179,21 @@ public class Application
         update_history_panel ();
     }
 
-    private void start_game (ChessGame game)
+    private void start_game ()
     {
-        this.game = game;
+        if (pgn_game.set_up)
+        {
+            if (pgn_game.fen != null)
+                game = new ChessGame (pgn_game.fen);
+            else
+            {
+                warning ("Chess game has SetUp tag but no FEN tag");
+                game = new ChessGame ();            
+            }
+        }
+        else
+            game = new ChessGame ();
+
         game.started.connect (game_start_cb);
         game.turn_started.connect (game_turn_cb);
         game.moved.connect (game_move_cb);
@@ -212,6 +226,15 @@ public class Application
         }
 
         game.start ();
+        foreach (var move in pgn_game.moves)
+        {
+            debug ("Move: %s", move);
+            if (!game.current_player.move (move))
+            {
+                warning ("Invalid move: %s", move);
+                break;
+            }
+        }
     }
 
     private ChessEngine? get_engine (string name)
@@ -513,10 +536,12 @@ public class Application
         case ChessRule.ABANDONMENT:
             /* Message displayed when a game is abandoned */
             reason = "The game has been abandoned";
+            pgn_game.termination = PGNGame.TERMINATE_ABANDONED;
             break;
         case ChessRule.DEATH:
             /* Message displayed when the game ends due to a player dying */
             reason = "One of the players has died";
+            pgn_game.termination = PGNGame.TERMINATE_DEATH;
             break;
         }
 
@@ -1136,8 +1161,8 @@ public class Application
         in_history = true;
         pgn_game = new PGNGame ();
         var now = new DateTime.now_local ();
-        pgn_game.date = now.format ("%Y.%M.%d");
-        start_game (new ChessGame ());    
+        pgn_game.date = now.format ("%Y.%m.%d");
+        start_game ();
     }
 
     private void load_game (File file, bool in_history) throws Error
@@ -1145,31 +1170,8 @@ public class Application
         var pgn = new PGN.from_file (file);
         pgn_game = pgn.games.nth_data (0);
 
-        ChessGame game;
-        if (pgn_game.set_up)
-        {
-            if (pgn_game.fen != null)
-                game = new ChessGame (pgn_game.fen);
-            else
-            {
-                warning ("Chess game has SetUp tag but no FEN tag");
-                game = new ChessGame ();            
-            }
-        }
-        else
-            game = new ChessGame ();
-
         this.in_history = in_history;
-        start_game (game);
-        foreach (var move in pgn_game.moves)
-        {
-            debug ("Move: %s", move);
-            if (!game.current_player.move (move))
-            {
-                warning ("Invalid move: %s", move);
-                break;
-            }
-        }
+        start_game ();
     }
 }
 
diff --git a/glchess/src/history.vala b/glchess/src/history.vala
index ba22f70..e540847 100644
--- a/glchess/src/history.vala
+++ b/glchess/src/history.vala
@@ -8,7 +8,7 @@ public class History
     {
         history_dir = File.new_for_path (Path.build_filename (data_dir.get_path (), "history", null));
     }
-    
+
     public File add (string date, string result) throws Error
     {
         load ();
@@ -58,6 +58,16 @@ public class History
         return file;
     }
 
+    public void update (File file, string fen, string result)
+    {
+        var relative_path = history_dir.get_relative_path (file);
+
+        Sqlite.Statement statement;
+        assert (db.prepare_v2 ("UPDATE GameTable SET fen=\"%s\", result=\"%s\" WHERE path=\"%s\"".printf (fen, result, relative_path), -1, out statement) == Sqlite.OK);
+        if (statement.step () != Sqlite.DONE)
+            warning ("Failed to update game in history index: %s", db.errmsg ());
+    }
+
     public List<File> get_unfinished ()
     {
         load ();



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