[gnome-klotski] Use libgames-support to handle high scores



commit f69bbc40353ce8a2af171004e534e69afb46b688
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Sat Feb 13 23:20:29 2016 -0600

    Use libgames-support to handle high scores

 configure.ac              |    2 +
 data/Makefile.am          |    1 -
 data/klotski-scores.ui    |  110 -------------------------------
 src/Makefile.am           |    4 +-
 src/gnome-klotski.vala    |    2 +-
 src/history.vala          |  112 --------------------------------
 src/klotski-window.vala   |   59 ++++++++++++-----
 src/klotski.gresource.xml |    1 -
 src/score-dialog.vala     |  156 ---------------------------------------------
 9 files changed, 47 insertions(+), 400 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index eac1863..2b4523b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,8 +23,10 @@ GTK_REQUIRED=3.19.0
 RSVG_REQUIRED=2.32.0
 
 PKG_CHECK_MODULES(GNOME_KLOTSKI, [
+  gee-0.8
   glib-2.0 >= $GLIB_REQUIRED
   gtk+-3.0 >= $GTK_REQUIRED
+  libgames-support-1.0
   librsvg-2.0 >= $RSVG_REQUIRED
 ])
 
diff --git a/data/Makefile.am b/data/Makefile.am
index ebd7084..85f5287 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -7,7 +7,6 @@ man_MANS = gnome-klotski.6
 
 dist_noinst_DATA = \
        klotski-menus.ui \
-       klotski-scores.ui \
        klotski.ui \
        klotski.css
 
diff --git a/src/Makefile.am b/src/Makefile.am
index e56d099..fd5a351 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,11 +5,9 @@ BUILT_SOURCES = klotski-resources.c
 gnome_klotski_SOURCES = \
        config.vapi \
        gnome-klotski.vala \
-       history.vala \
        klotski-window.vala \
        puzzle.vala \
        puzzle-view.vala \
-       score-dialog.vala \
        $(BUILT_SOURCES)
 
 gnome_klotski_CFLAGS = \
@@ -22,7 +20,9 @@ gnome_klotski_CFLAGS = \
 
 gnome_klotski_VALAFLAGS = \
        --pkg posix \
+       --pkg gee-0.8 \
        --pkg gtk+-3.0 \
+       --pkg libgames-support-1.0 \
        --pkg librsvg-2.0 \
        --gresources $(builddir)/klotski.gresource.xml \
        --target-glib $(GLIB_REQUIRED)
diff --git a/src/gnome-klotski.vala b/src/gnome-klotski.vala
index dfa06a0..93a2d85 100644
--- a/src/gnome-klotski.vala
+++ b/src/gnome-klotski.vala
@@ -88,7 +88,7 @@ public class Klotski : Gtk.Application
 
     private void scores_cb ()
     {
-        ((KlotskiWindow) get_active_window ()).show_scores (null);
+        ((KlotskiWindow) get_active_window ()).show_scores ();
     }
 
     private void help_cb ()
diff --git a/src/klotski-window.vala b/src/klotski-window.vala
index 05d93e1..cd0f49f 100644
--- a/src/klotski-window.vala
+++ b/src/klotski-window.vala
@@ -56,13 +56,16 @@ public class KlotskiWindow : ApplicationWindow
     private int current_pack = -1;
     private int current_level = -1;
 
-    private History history;
+    private Games.Scores.Context scores_context;
 
-    /* The "puzzle name" remarks provide context for translation. */
+    /* The "puzzle name" remarks provide context for translation. Add new
+     * puzzles at the end, or you'll mess up saved scores.
+     */
     private Gtk.ListStore liststore_huarong;
     private Gtk.ListStore liststore_challenge;
     private Gtk.ListStore liststore_skill;
     private TreeIter[] puzzles_items;
+    private Gee.List<Games.Scores.Category> score_categories;
     public const LevelInfo level[] =
     {
       /* puzzle name */
@@ -446,6 +449,17 @@ public class KlotskiWindow : ApplicationWindow
         {"start-game", start_puzzle_cb}
     };
 
+    private Games.Scores.Category? category_request (string key)
+    {
+        var i = int.parse (key);
+        // i will be 0 if parsing fails, but 0 is also a valid map key.
+        if (i == 0 && key != "0")
+            return null;
+        if (i < 0 || i > score_categories.size)
+            return null;
+        return score_categories[i];
+    }
+
     public KlotskiWindow ()
     {
         var css_provider = new CssProvider ();
@@ -464,10 +478,18 @@ public class KlotskiWindow : ApplicationWindow
         next_puzzle = lookup_action ("next-puzzle") as SimpleAction;
         start_game = lookup_action ("start-game") as SimpleAction;
 
-        string histfile = Path.build_filename (Environment.get_user_data_dir (), "gnome-klotski", "history");
+        score_categories = new Gee.ArrayList<Games.Scores.Category> ();
+        for (var i = 0; i < level.length; i++)
+        {
+            score_categories.add (new Games.Scores.Category (i.to_string (), _(level[i].name)));
+        }
 
-        history = new History (histfile);
-        history.load ();
+        scores_context = new Games.Scores.Context ("gnome-klotski",
+                                                   // Label on the scores dialog, next to dropdown */
+                                                   _("Puzzle"),
+                                                   this,
+                                                   category_request,
+                                                   Games.Scores.Style.PLAIN_ASCENDING);
 
         // name, active, puzzle number (or -1), sensitive=false CSS hack
         liststore_huarong = new Gtk.ListStore (4, typeof (string), typeof (bool), typeof (int), typeof 
(bool));
@@ -756,21 +778,24 @@ public class KlotskiWindow : ApplicationWindow
 
         puzzle_solved (puzzles_items[current_level], true);
 
-        var date = new DateTime.now_local ();
-        var entry = new HistoryEntry (date, current_level, puzzle.moves);
-        history.add (entry);
-        history.save ();
-
-        show_scores (entry);
+        scores_context.add_score.begin (puzzle.moves,
+                                        score_categories[current_level],
+                                        null,
+                                        (object, result) => {
+            try
+            {
+                scores_context.add_score.end (result);
+            }
+            catch (GLib.Error e)
+            {
+                warning ("Failed to add score: %s", e.message);
+            }
+        });
     }
 
-    public void show_scores (HistoryEntry? selected_entry = null)
+    public void show_scores ()
     {
-        var dialog = new ScoreDialog (history, selected_entry);
-        dialog.set_transient_for (this);
-
-        /* var result = */ dialog.run ();
-        dialog.destroy ();
+        scores_context.run_dialog ();
     }
 
     private string get_level_key (int level_number)
diff --git a/src/klotski.gresource.xml b/src/klotski.gresource.xml
index 71609ef..cf8c5eb 100644
--- a/src/klotski.gresource.xml
+++ b/src/klotski.gresource.xml
@@ -2,7 +2,6 @@
 <gresources>
   <gresource prefix="/org/gnome/klotski/ui">
     <file preprocess="xml-stripblanks" alias="klotski.ui">../data/klotski.ui</file>
-    <file preprocess="xml-stripblanks" alias="scores.ui">../data/klotski-scores.ui</file>
     <file alias="klotski.css">../data/klotski.css</file>
   </gresource>
   <gresource prefix="/org/gnome/klotski/gtk">


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