[gnome-games] quadrapassel: Stop using GamesScores



commit 7f142ab39c14f4df7e924062618e89871aeadeef
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Sep 28 11:25:46 2012 +1200

    quadrapassel: Stop using GamesScores

 quadrapassel/Makefile.am           |    6 --
 quadrapassel/src/Makefile.am       |   14 +---
 quadrapassel/src/history.vala      |   99 ++++++++++++++++++++++++++
 quadrapassel/src/quadrapassel.vala |  135 +++++++++++++++++++++++++++---------
 4 files changed, 203 insertions(+), 51 deletions(-)
---
diff --git a/quadrapassel/Makefile.am b/quadrapassel/Makefile.am
index f0343ca..40c0ea9 100644
--- a/quadrapassel/Makefile.am
+++ b/quadrapassel/Makefile.am
@@ -1,9 +1,3 @@
 SUBDIRS = src data help
 
-install-data-local:
-	-$(mkinstalldirs) $(DESTDIR)$(scoredir)
-	-touch $(DESTDIR)$(scoredir)/quadrapassel.scores
-	-chown $(scores_user):$(scores_group) $(DESTDIR)$(scoredir)/quadrapassel.scores
-	-chmod 664 $(DESTDIR)$(scoredir)/quadrapassel.scores
-
 -include $(top_srcdir)/git.mk
diff --git a/quadrapassel/src/Makefile.am b/quadrapassel/src/Makefile.am
index f7aedc4..051143e 100644
--- a/quadrapassel/src/Makefile.am
+++ b/quadrapassel/src/Makefile.am
@@ -5,10 +5,10 @@ quadrapassel_SOURCES = \
 	quadrapassel.vala \
 	preview.vala \
 	game.vala \
-	game-view.vala
+	game-view.vala \
+	history.vala
 
 quadrapassel_CFLAGS = \
-	-I$(top_srcdir)/libgames-support \
 	-DVERSION=\"$(VERSION)\" \
 	-DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \
 	-DDATA_DIRECTORY=\"$(datadir)/quadrapassel\" \
@@ -29,12 +29,9 @@ quadrapassel_VALAFLAGS = \
 	--pkg clutter-gtk-1.0 \
 	--pkg cogl-1.0 \
 	--pkg libcanberra \
-	--pkg libcanberra-gtk \
-	--vapidir $(top_srcdir)/libgames-support \
-	--pkg GnomeGamesSupport-1.0
+	--pkg libcanberra-gtk
 
 quadrapassel_LDADD = \
-	$(top_builddir)/libgames-support/libgames-support.la \
 	$(CANBERRA_GTK_LIBS) \
 	$(CLUTTER_GTK_LIBS) \
 	$(CLUTTER_LIBS) \
@@ -42,11 +39,6 @@ quadrapassel_LDADD = \
 	$(RSVG_LIBS) \
 	$(INTLLIBS)
 
-install-exec-hook:
-	-if test "$(setgid)" = "true"; then \
-	  chgrp $(scores_group) $(DESTDIR)$(bindir)/quadrapassel && chmod 2555 $(DESTDIR)$(bindir)/quadrapassel ;\
-	fi
-
 CLEANFILES = \
 	$(patsubst %.vala,%.c,$(filter %.vala, $(SOURCES))) \
 	*_vala.stamp
diff --git a/quadrapassel/src/history.vala b/quadrapassel/src/history.vala
new file mode 100644
index 0000000..a73ea4a
--- /dev/null
+++ b/quadrapassel/src/history.vala
@@ -0,0 +1,99 @@
+public class History
+{
+    public string filename;
+    public List<HistoryEntry> entries;
+
+    public signal void entry_added (HistoryEntry entry);
+
+    public History (string filename)
+    {
+        this.filename = filename;
+        entries = new List<HistoryEntry> ();
+    }
+
+    public void add (HistoryEntry entry)
+    {
+        entries.append (entry);
+        entry_added (entry);
+    }
+
+    public void load ()
+    {
+        entries = new List<HistoryEntry> ();
+
+        var contents = "";
+        try
+        {
+            FileUtils.get_contents (filename, out contents);
+        }
+        catch (FileError e)
+        {
+            if (!(e is FileError.NOENT))
+                warning ("Failed to load history: %s", e.message);
+            return;
+        }
+
+        foreach (var line in contents.split ("\n"))
+        {
+            var tokens = line.split (" ");
+            if (tokens.length != 2)
+                continue;
+
+            var date = parse_date (tokens[0]);
+            if (date == null)
+                continue;
+            var score = int.parse (tokens[1]);
+
+            add (new HistoryEntry (date, score));
+        }
+    }
+
+    public void save ()
+    {
+        var contents = "";
+
+        foreach (var entry in entries)
+        {
+            var line = "%s %i\n".printf (entry.date.to_string (), entry.score);
+            contents += line;
+        }
+
+        try
+        {
+            DirUtils.create_with_parents (Path.get_dirname (filename), 0775);
+            FileUtils.set_contents (filename, contents);
+        }
+        catch (FileError e)
+        {
+            warning ("Failed to save history: %s", e.message);
+        }    
+    }
+
+    private DateTime? parse_date (string date)
+    {
+        if (date.length < 19 || date[4] != '-' || date[7] != '-' || date[10] != 'T' || date[13] != ':' || date[16] != ':')
+            return null;
+
+        var year = int.parse (date.substring (0, 4));
+        var month = int.parse (date.substring (5, 2));
+        var day = int.parse (date.substring (8, 2));
+        var hour = int.parse (date.substring (11, 2));
+        var minute = int.parse (date.substring (14, 2));
+        var seconds = int.parse (date.substring (17, 2));
+        var timezone = date.substring (19);
+
+        return new DateTime (new TimeZone (timezone), year, month, day, hour, minute, seconds);
+    }
+}
+
+public class HistoryEntry
+{
+    public DateTime date;
+    public int score;
+
+    public HistoryEntry (DateTime date, int score)
+    {
+        this.date = date;
+        this.score = score;
+    }
+}
diff --git a/quadrapassel/src/quadrapassel.vala b/quadrapassel/src/quadrapassel.vala
index e5ae9d4..26efad9 100644
--- a/quadrapassel/src/quadrapassel.vala
+++ b/quadrapassel/src/quadrapassel.vala
@@ -28,7 +28,7 @@ public class Quadrapassel : Gtk.Application
     /* Label showing the current level */
     private Gtk.Label level_label;
 
-    private GnomeGamesSupport.Scores high_scores;
+    private History history;
 
     private SimpleAction pause_action;
 
@@ -198,12 +198,8 @@ public class Quadrapassel : Gtk.Application
         level_label.show ();
         score_grid.attach (level_label, 1, 2, 1, 1);
 
-        high_scores = new GnomeGamesSupport.Scores ("quadrapassel",
-                                                    new GnomeGamesSupport.ScoresCategory[0],
-                                                    null,
-                                                    null,
-                                                    0,
-                                                    GnomeGamesSupport.ScoreStyle.PLAIN_DESCENDING);
+        history = new History (Path.build_filename (Environment.get_user_data_dir (), "quadrapassel", "history"));
+        history.load ();
 
         pause_action.set_enabled (false);
     }
@@ -252,7 +248,12 @@ public class Quadrapassel : Gtk.Application
 
         /* Record the score if the game isn't over. */
         if (game != null && game.score > 0)
-            high_scores.add_plain_score (game.score);
+        {
+            var date = new DateTime.now_local ();
+            var entry = new HistoryEntry (date, game.score);
+            history.add (entry);
+            history.save ();
+        }
     }
 
     protected override void activate ()
@@ -691,32 +692,30 @@ public class Quadrapassel : Gtk.Application
         pause_action.set_enabled (false);
         if (game.score > 0)
         {
-            var pos = high_scores.add_plain_score (game.score);
-            var dialog = new GnomeGamesSupport.ScoresDialog (window, high_scores, _("Quadrapassel Scores"));
-            var title = _("Puzzle solved!");
-            var message = _("You didn't make the top ten, better luck next time.");
-            if (pos == 1)
-                message = _("Your score is the best!");
-            else if (pos > 1)
-                message = _("Your score has made the top ten.");
-            dialog.set_message ("<b>%s</b>\n\n%s".printf (title, message));
-            dialog.set_buttons (GnomeGamesSupport.ScoresButtons.QUIT_BUTTON | GnomeGamesSupport.ScoresButtons.NEW_GAME_BUTTON);
-            if (pos > 0)
-                dialog.set_hilight (pos);
-
-            switch (dialog.run ())
-            {
-            case Gtk.ResponseType.REJECT:
+            var date = new DateTime.now_local ();
+            var entry = new HistoryEntry (date, game.score);
+            history.add (entry);
+            history.save ();
+
+            if (show_scores (entry, true) == Gtk.ResponseType.CLOSE)
                 window.destroy ();
-                break;
-            default:
+            else
                 new_game ();
-                break;
-            }
-            dialog.destroy ();
         }
     }
 
+    private int show_scores (HistoryEntry? selected_entry = null, bool show_quit = false)
+    {
+        var dialog = new ScoreDialog (history, selected_entry, show_quit);
+        dialog.modal = true;
+        dialog.transient_for = window;
+
+        var result = dialog.run ();
+        dialog.destroy ();
+
+        return result;
+    }
+
     private void update_score ()
     {
         var score = 0;
@@ -771,9 +770,7 @@ public class Quadrapassel : Gtk.Application
 
     private void scores_cb ()
     {
-        var dialog = new GnomeGamesSupport.ScoresDialog (window, high_scores, _("Quadrapassel Scores"));
-        dialog.run ();
-        dialog.destroy ();
+        show_scores ();
     }
 
     public static int main (string[] args)
@@ -783,8 +780,6 @@ public class Quadrapassel : Gtk.Application
         Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
         Intl.textdomain (GETTEXT_PACKAGE);
 
-        GnomeGamesSupport.scores_startup ();
-    
         var context = new OptionContext ("");
 
         context.add_group (Gtk.get_option_group (true));
@@ -821,3 +816,75 @@ public class Quadrapassel : Gtk.Application
         return app.run (args);
     }
 }
+
+public class ScoreDialog : Gtk.Dialog
+{
+    private History history;
+    private HistoryEntry? selected_entry = null;
+    private Gtk.ListStore score_model;
+
+    public ScoreDialog (History history, HistoryEntry? selected_entry = null, bool show_quit = false)
+    {
+        this.history = history;
+        history.entry_added.connect (entry_added_cb);
+        this.selected_entry = selected_entry;
+
+        if (show_quit)
+        {
+            add_button (Gtk.Stock.QUIT, Gtk.ResponseType.CLOSE);
+            add_button (_("New Game"), Gtk.ResponseType.OK);
+        }
+        else
+            add_button (Gtk.Stock.OK, Gtk.ResponseType.DELETE_EVENT);
+        set_size_request (200, 300);
+
+        var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
+        vbox.border_width = 6;
+        vbox.show ();
+        get_content_area ().pack_start (vbox, true, true, 0);
+
+        var scroll = new Gtk.ScrolledWindow (null, null);
+        scroll.shadow_type = Gtk.ShadowType.ETCHED_IN;
+        scroll.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
+        scroll.show ();
+        vbox.pack_start (scroll, true, true, 0);
+
+        score_model = new Gtk.ListStore (3, typeof (string), typeof (string), typeof (int));
+
+        var scores = new Gtk.TreeView ();
+        var renderer = new Gtk.CellRendererText ();
+        scores.insert_column_with_attributes (-1, _("Date"), renderer, "text", 0, "weight", 2);
+        renderer = new Gtk.CellRendererText ();
+        renderer.xalign = 1.0f;
+        scores.insert_column_with_attributes (-1, _("Score"), renderer, "text", 1, "weight", 2);
+        scores.model = score_model;
+        scores.show ();
+        scroll.add (scores);
+
+        var entries = history.entries.copy ();
+        entries.sort (compare_entries);
+        foreach (var entry in entries)
+            entry_added_cb (entry);
+    }
+
+    private static int compare_entries (HistoryEntry a, HistoryEntry b)
+    {
+        if (a.score != b.score)
+            return a.score - b.score;
+        return a.date.compare (b.date);
+    }
+
+    private void entry_added_cb (HistoryEntry entry)
+    {
+        var date_label = entry.date.format ("%d/%m/%Y");
+        var score_label = "%i".printf (entry.score);
+
+        int weight = Pango.Weight.NORMAL;
+        if (entry == selected_entry)
+            weight = Pango.Weight.BOLD;
+
+        Gtk.TreeIter iter;
+        score_model.append (out iter);
+        score_model.set (iter, 0, date_label, 1, score_label, 2, weight);
+    }
+}



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