[gnome-games] gnome-mahjongg: Stop using GamesScores



commit a4e0007d297c3a37131ee8636bce702b6fbfa12c
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Sep 27 12:42:51 2012 +1200

    gnome-mahjongg: Stop using GamesScores

 gnome-mahjongg/data/Makefile.am        |   10 --
 gnome-mahjongg/src/Makefile.am         |   12 +--
 gnome-mahjongg/src/gnome-mahjongg.vala |  218 ++++++++++++++++++++++++++------
 gnome-mahjongg/src/history.vala        |  102 +++++++++++++++
 4 files changed, 281 insertions(+), 61 deletions(-)
---
diff --git a/gnome-mahjongg/data/Makefile.am b/gnome-mahjongg/data/Makefile.am
index 18a9bb7..f3074c9 100644
--- a/gnome-mahjongg/data/Makefile.am
+++ b/gnome-mahjongg/data/Makefile.am
@@ -30,16 +30,6 @@ EXTRA_DIST = \
 CLEANFILES = $(desktop_DATA) $(gsettings_SCHEMAS)
 DISTCLEANFILES = $(desktop_DATA) $(gsettings_SCHEMAS)
 
-install-scorefiles-local:
-	-$(mkinstalldirs) $(DESTDIR)$(scoredir)
-	-for i in easy difficult confounding pyramid tictactoe cloud dragon bridges ziggurat; do \
-		touch $(DESTDIR)$(scoredir)/gnome-mahjongg.$$i.scores; \
-		chown $(scores_user):$(scores_group) $(DESTDIR)$(scoredir)/gnome-mahjongg.$$i.scores; \
-		chmod 664 $(DESTDIR)$(scoredir)/gnome-mahjongg.$$i.scores; \
-	done
-
-install-data-local: install-scorefiles-local
-
 @INTLTOOL_DESKTOP_RULE@
 
 -include $(top_srcdir)/git.mk
diff --git a/gnome-mahjongg/src/Makefile.am b/gnome-mahjongg/src/Makefile.am
index 682abad..cff2b89 100644
--- a/gnome-mahjongg/src/Makefile.am
+++ b/gnome-mahjongg/src/Makefile.am
@@ -6,17 +6,15 @@ gnome_mahjongg_SOURCES = 	\
 	game.vala	\
 	game-view.vala	\
 	gnome-mahjongg.vala	\
+	history.vala	\
 	map.vala
 
 gnome_mahjongg_VALAFLAGS = \
 	--pkg posix \
 	--pkg gtk+-3.0 \
-	--pkg librsvg-2.0 \
-	--vapidir $(top_srcdir)/libgames-support \
-	--pkg GnomeGamesSupport-1.0
+	--pkg librsvg-2.0
 
 gnome_mahjongg_CFLAGS = \
-	-I$(top_srcdir)/libgames-support \
 	-DVERSION=\"$(VERSION)\" \
 	-DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \
 	-DDATA_DIRECTORY=\"$(datadir)/gnome-mahjongg\" \
@@ -25,16 +23,10 @@ gnome_mahjongg_CFLAGS = \
 	$(RSVG_CFLAGS)
 
 gnome_mahjongg_LDADD = \
-	$(top_builddir)/libgames-support/libgames-support.la \
 	$(GTK_LIBS)	\
 	$(RSVG_LIBS) \
 	$(INTLLIBS)
 
-install-exec-hook:
-	-if test "$(setgid)" = "true"; then \
-	  chgrp $(scores_group) $(DESTDIR)$(bindir)/gnome-mahjongg && chmod 2555 $(DESTDIR)$(bindir)/gnome-mahjongg ;\
-	fi
-
 CLEANFILES = \
 	$(patsubst %.vala,%.c,$(filter %.vala, $(SOURCES))) \
 	*_vala.stamp
diff --git a/gnome-mahjongg/src/gnome-mahjongg.vala b/gnome-mahjongg/src/gnome-mahjongg.vala
index c137e02..bb16937 100644
--- a/gnome-mahjongg/src/gnome-mahjongg.vala
+++ b/gnome-mahjongg/src/gnome-mahjongg.vala
@@ -2,7 +2,7 @@ public class Mahjongg : Gtk.Application
 {
     private Settings settings;
 
-    private GnomeGamesSupport.Scores highscores;
+    private History history;
 
     private List<Map> maps = null;
 
@@ -40,15 +40,8 @@ public class Mahjongg : Gtk.Application
 
         load_maps ();
 
-        highscores = new GnomeGamesSupport.Scores ("gnome-mahjongg",
-                                                   new GnomeGamesSupport.ScoresCategory[0],
-                                                   null, null, 0,
-                                                   GnomeGamesSupport.ScoreStyle.TIME_ASCENDING);
-        foreach (var map in maps)
-        {
-            var display_name = dpgettext2 (null, "mahjongg map name", map.name);
-            highscores.add_category (map.score_name, display_name);
-        }
+        history = new History (Path.build_filename (Environment.get_user_data_dir (), "gnome-mahjongg", "history"));
+        history.load ();
 
         window = new Gtk.ApplicationWindow (this);
         window.title = _("Mahjongg");
@@ -303,32 +296,16 @@ public class Mahjongg : Gtk.Application
 
         if (game_view.game.complete)
         {
-            var seconds = (int) (game_view.game.elapsed + 0.5);
-
-            var p = highscores.add_time_score ((seconds / 60) * 1.0 + (seconds % 60) / 100.0);
-            var scores_dialog = new GnomeGamesSupport.ScoresDialog (window, highscores, _("Mahjongg Scores"));
-            scores_dialog.set_category_description (_("Layout:"));
-            var title = _("Puzzle solved!");
-            var message = _("You didn't make the top ten, better luck next time.");
-            if (p == 1)
-                message = _("Your score is the best!");
-            else if (p > 1)
-                message = _("Your score has made the top ten.");
-            scores_dialog.set_message ("<b>%s</b>\n\n%s".printf (title, message));
-            scores_dialog.set_buttons (GnomeGamesSupport.ScoresButtons.QUIT_BUTTON | GnomeGamesSupport.ScoresButtons.NEW_GAME_BUTTON);
-            if (p > 0)
-                scores_dialog.set_hilight (p);
-
-            switch (scores_dialog.run ())
-            {
-            case Gtk.ResponseType.REJECT:
+            var date = new DateTime.now_local ();
+            var duration = (uint) (game_view.game.elapsed + 0.5);
+            var entry = new HistoryEntry (date, game_view.game.map.score_name, duration);
+            history.add (entry);
+            history.save ();
+
+            if (show_scores (entry, true) == Gtk.ResponseType.CLOSE)
                 window.destroy ();
-                break;
-            default:
+            else
                 new_game ();
-                break;
-            }
-            scores_dialog.destroy ();
         }
         else if (!game_view.game.can_move)
         {
@@ -359,6 +336,18 @@ public class Mahjongg : Gtk.Application
         }
     }
 
+    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 preferences_cb ()
     {
         if (preferences_dialog != null)
@@ -588,10 +577,7 @@ public class Mahjongg : Gtk.Application
 
     private void scores_cb ()
     {
-        var map_scores_dialog = new GnomeGamesSupport.ScoresDialog (window, highscores, _("Mahjongg Scores"));
-        map_scores_dialog.set_category_description (_("Layout:"));
-        map_scores_dialog.run ();
-        map_scores_dialog.destroy ();
+        show_scores ();
     }
 
     private void new_game_cb ()
@@ -648,7 +634,6 @@ public class Mahjongg : Gtk.Application
         game_view.game = new Game (map);
         game_view.game.moved.connect (moved_cb);
         game_view.game.tick.connect (tick_cb);
-        highscores.set_category (game_view.game.map.score_name);
 
         /* Set window title */
         var display_name = dpgettext2 (null, "mahjongg map name", game_view.game.map.name);
@@ -744,8 +729,6 @@ public class Mahjongg : Gtk.Application
         Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
         Intl.textdomain (GETTEXT_PACKAGE);
 
-        GnomeGamesSupport.scores_startup ();
-
         Gtk.init (ref args);
 
         var context = new OptionContext ("");
@@ -773,3 +756,156 @@ public class Mahjongg : Gtk.Application
         return result;
     }
 }
+
+public class ScoreDialog : Gtk.Dialog
+{
+    private History history;
+    private HistoryEntry? selected_entry = null;
+    private Gtk.ListStore size_model;
+    private Gtk.ListStore score_model;
+    private Gtk.ComboBox size_combo;
+
+    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 hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
+        hbox.show ();
+        vbox.pack_start (hbox, false, false, 0);
+
+        var label = new Gtk.Label (_("Size:"));
+        label.show ();
+        hbox.pack_start (label, false, false, 0);
+
+        size_model = new Gtk.ListStore (2, typeof (string), typeof (string));
+
+        size_combo = new Gtk.ComboBox ();
+        size_combo.changed.connect (size_changed_cb);
+        size_combo.model = size_model;
+        var renderer = new Gtk.CellRendererText ();
+        size_combo.pack_start (renderer, true);
+        size_combo.add_attribute (renderer, "text", 0);
+        size_combo.show ();
+        hbox.pack_start (size_combo, 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 ();
+        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, _("Time"), renderer, "text", 1, "weight", 2);
+        scores.model = score_model;
+        scores.show ();
+        scroll.add (scores);
+
+        foreach (var entry in history.entries)
+            entry_added_cb (entry);
+    }
+
+    public void set_map (string name)
+    {
+        score_model.clear ();
+
+        var entries = history.entries.copy ();
+        entries.sort (compare_entries);
+
+        foreach (var entry in entries)
+        {
+            if (entry.name != name)
+                continue;
+
+            var date_label = entry.date.format ("%d/%m/%Y");
+
+            var time_label = "%us".printf (entry.duration);
+            if (entry.duration >= 60)
+                time_label = "%um %us".printf (entry.duration / 60, entry.duration % 60);
+
+            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, time_label, 2, weight);
+        }
+    }
+
+    private static int compare_entries (HistoryEntry a, HistoryEntry b)
+    {
+        var d = strcmp (a.name, b.name);
+        if (d != 0)
+            return d;
+        return a.date.compare (b.date);
+    }
+
+    private void size_changed_cb (Gtk.ComboBox combo)
+    {
+        Gtk.TreeIter iter;
+        if (!combo.get_active_iter (out iter))
+            return;
+
+        string name;
+        combo.model.get (iter, 1, out name);
+        set_map (name);
+    }
+
+    private void entry_added_cb (HistoryEntry entry)
+    {
+        /* Ignore if already have an entry for this */
+        Gtk.TreeIter iter;
+        var have_size_entry = false;
+        if (size_model.get_iter_first (out iter))
+        {
+            do
+            {
+                string name;
+                size_model.get (iter, 1, out name);
+                if (name == entry.name)
+                {
+                    have_size_entry = true;
+                    break;
+                }
+            } while (size_model.iter_next (ref iter));
+        }
+
+        if (!have_size_entry)
+        {
+            var label = "%s".printf (entry.name);
+
+            size_model.append (out iter);
+            size_model.set (iter, 0, label, 1, entry.name);
+    
+            /* Select this entry if don't have any */
+            if (size_combo.get_active () == -1)
+                size_combo.set_active_iter (iter);
+
+            /* Select this entry if the same category as the selected one */
+            if (selected_entry != null && entry.name == selected_entry.name)
+                size_combo.set_active_iter (iter);
+        }
+    }
+}
diff --git a/gnome-mahjongg/src/history.vala b/gnome-mahjongg/src/history.vala
new file mode 100644
index 0000000..d64b604
--- /dev/null
+++ b/gnome-mahjongg/src/history.vala
@@ -0,0 +1,102 @@
+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 != 3)
+                continue;
+
+            var date = parse_date (tokens[0]);
+            if (date == null)
+                continue;
+            var name = tokens[1];
+            var duration = int.parse (tokens[2]);
+
+            add (new HistoryEntry (date, name, duration));
+        }
+    }
+
+    public void save ()
+    {
+        var contents = "";
+
+        foreach (var entry in entries)
+        {
+            var line = "%s %s %u\n".printf (entry.date.to_string (), entry.name, entry.duration);
+            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 string name;
+    public uint duration;
+
+    public HistoryEntry (DateTime date, string name, uint duration)
+    {
+        this.date = date;
+        this.name = name;
+        this.duration = duration;
+    }
+}



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