[gnome-mines/wip/libgames-support: 7/8] Import old scores from 3.18



commit 2c61465f7c77f4476ae687adc6b48c275ae0a7fa
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Sun Feb 14 20:47:23 2016 -0600

    Import old scores from 3.18

 src/gnome-mines.vala |   35 +++++++++++++++++++++++++++++------
 1 files changed, 29 insertions(+), 6 deletions(-)
---
diff --git a/src/gnome-mines.vala b/src/gnome-mines.vala
index 31924c4..0da0e14 100644
--- a/src/gnome-mines.vala
+++ b/src/gnome-mines.vala
@@ -311,12 +311,13 @@ public class Mines : Gtk.Application
         /* Initialize Custom Game Screen */
         startup_custom_game_screen (ui_builder);
 
-        context = new Games.Scores.Context ("gnome-mines",
-                                            /* Label on the scores dialog */
-                                            _("Minefield:"),
-                                            window,
-                                            create_category_from_key,
-                                            Games.Scores.Style.TIME_LESS_IS_BETTER);
+        context = new Games.Scores.Context.with_importer ("gnome-mines",
+                                                          /* Label on the scores dialog */
+                                                          _("Minefield:"),
+                                                          window,
+                                                          create_category_from_key,
+                                                          Games.Scores.Style.TIME_LESS_IS_BETTER,
+                                                          new Games.Scores.HistoryFileImporter 
(parse_old_score));
 
         flag_label = (Gtk.Label) ui_builder.get_object ("flag_label");
         clock_label = (Gtk.Label) ui_builder.get_object ("clock_label");
@@ -351,6 +352,28 @@ public class Mines : Gtk.Application
                                                          num_mines).printf (width, height, num_mines));
     }
 
+    private void parse_old_score (string line, out Games.Scores.Score score, out Games.Scores.Category 
category)
+    {
+        score = null;
+        category = null;
+
+        var tokens = line.split (" ");
+        if (tokens.length != 5)
+            return;
+
+        var date = Games.Scores.HistoryFileImporter.parse_date (tokens[0]);
+        var width = int.parse (tokens[1]);
+        var height = int.parse (tokens[2]);
+        var num_mines = int.parse (tokens[3]);
+        var seconds = int.parse (tokens[4]);
+
+        if (date <= 0 || width <= 0 || height <= 0 || num_mines <= 0 || seconds < 0)
+            return;
+
+        score = new Games.Scores.Score (seconds, date);
+        category = create_category_from_key (@"$width-$height-$num_mines");
+    }
+
     private void startup_new_game_screen (Gtk.Builder builder)
     {
         new_game_screen =  (Gtk.AspectFrame) builder.get_object ("new_game_screen");


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