[gnome-games] gnotravex: Stop using GamesScores



commit 73aa710f0d2e57116688545d649e6bf7cd4e794a
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Sep 27 12:09:44 2012 +1200

    gnotravex: Stop using GamesScores

 gnotravex/Makefile.am        |   12 ---
 gnotravex/src/Makefile.am    |   12 +--
 gnotravex/src/gnotravex.vala |  202 ++++++++++++++++++++++++++++++++++++------
 gnotravex/src/history.vala   |  102 +++++++++++++++++++++
 4 files changed, 280 insertions(+), 48 deletions(-)
---
diff --git a/gnotravex/Makefile.am b/gnotravex/Makefile.am
index c424197..b14967d 100644
--- a/gnotravex/Makefile.am
+++ b/gnotravex/Makefile.am
@@ -1,15 +1,3 @@
 SUBDIRS = data src help
 
-SCOREFILES = 2 3 4 5 6
-
-install-scorefiles-local:
-	-$(mkinstalldirs) $(DESTDIR)$(scoredir)
-	-for i in ${SCOREFILES} ; do \
-	   touch $(DESTDIR)$(scoredir)/gnotravex."$$i"x"$$i".scores; \
-	   chown $(scores_user):$(scores_group) $(DESTDIR)$(scoredir)/gnotravex."$$i"x"$$i".scores; \
-	   chmod 664 $(DESTDIR)$(scoredir)/gnotravex."$$i"x"$$i".scores; \
-	 done
-
-install-data-local: install-scorefiles-local
-
 -include $(top_srcdir)/git.mk
diff --git a/gnotravex/src/Makefile.am b/gnotravex/src/Makefile.am
index c2e4957..ca53824 100644
--- a/gnotravex/src/Makefile.am
+++ b/gnotravex/src/Makefile.am
@@ -3,18 +3,16 @@ bin_PROGRAMS = gnotravex
 gnotravex_SOURCES = \
 	config.vapi \
 	gnotravex.vala \
+	history.vala \
 	puzzle.vala \
 	puzzle-view.vala \
 	theme.vala
 
 gnotravex_VALAFLAGS = \
 	--pkg posix \
-	--pkg gtk+-3.0 \
-	--vapidir $(top_srcdir)/libgames-support \
-	--pkg GnomeGamesSupport-1.0
+	--pkg gtk+-3.0
 
 gnotravex_CFLAGS = \
-	-I$(top_srcdir)/libgames-support \
 	-DVERSION=\"$(VERSION)\" \
 	-DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \
 	-DDATA_DIRECTORY=\"$(datadir)/gnotravex\" \
@@ -25,16 +23,10 @@ gnotravex_CFLAGS = \
 	-Wno-unused
 
 gnotravex_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)/gnotravex && chmod 2555 $(DESTDIR)$(bindir)/gnotravex ;\
-	fi
-
 CLEANFILES = \
 	$(patsubst %.vala,%.c,$(filter %.vala, $(SOURCES))) \
 	*_vala.stamp
diff --git a/gnotravex/src/gnotravex.vala b/gnotravex/src/gnotravex.vala
index 04152b9..a76cc90 100644
--- a/gnotravex/src/gnotravex.vala
+++ b/gnotravex/src/gnotravex.vala
@@ -6,17 +6,9 @@ public class Gnotravex : Gtk.Application
 
     private Puzzle puzzle;
     private Gtk.Label clock_label;
-    private GnomeGamesSupport.Scores highscores;
+    private History history;
 
     private PuzzleView view;
-    private const GnomeGamesSupport.ScoresCategory scorecats[] =
-    {
-        { "2x2", N_("2Ã2") },
-        { "3x3", N_("3Ã3") },
-        { "4x4", N_("4Ã4") },
-        { "5x5", N_("5Ã5") },
-        { "6x6", N_("6Ã6") }
-    };
 
     private Gtk.Window window;
     private int window_width;
@@ -76,7 +68,8 @@ public class Gnotravex : Gtk.Application
 
         settings = new Settings ("org.gnome.gnotravex");
 
-        highscores = new GnomeGamesSupport.Scores ("gnotravex", scorecats, null, null, 0, GnomeGamesSupport.ScoreStyle.TIME_ASCENDING);
+        history = new History (Path.build_filename (Environment.get_user_data_dir (), "gnotravex", "history"));
+        history.load ();
 
         window = new Gtk.ApplicationWindow (this);
         window.title = _("Tetravex");
@@ -210,7 +203,6 @@ public class Gnotravex : Gtk.Application
             SignalHandler.disconnect_by_func (puzzle, null, this);
 
         var size = settings.get_int (KEY_GRID_SIZE);
-        highscores.set_category (scorecats[size - 2].key);
         puzzle = new Puzzle (size);
         puzzle.tick.connect (tick_cb);
         puzzle.solved.connect (solved_cb);
@@ -234,20 +226,28 @@ public class Gnotravex : Gtk.Application
 
     private void solved_cb (Puzzle puzzle)
     {
-        var seconds = (int) (puzzle.elapsed + 0.5);
-        var pos = highscores.add_time_score ((seconds / 60) * 1.0 + (seconds % 60) / 100.0);
+        var date = new DateTime.now_local ();
+        var duration = (uint) (puzzle.elapsed + 0.5);
+        var entry = new HistoryEntry (date, puzzle.size, duration);
+        history.add (entry);
+        history.save ();
 
-        var scores_dialog = new GnomeGamesSupport.ScoresDialog (window, highscores, _("Tetravex Scores"));
-        scores_dialog.set_category_description (_("Size:"));
-        scores_dialog.set_hilight (pos);
-        if (pos > 0)
-            scores_dialog.set_message ("<b>%s</b>\n\n%s".printf (_("Congratulations!"), pos == 1 ? _("Your score is the best!") : _("Your score has made the top ten.")));
-        scores_dialog.set_buttons (GnomeGamesSupport.ScoresButtons.QUIT_BUTTON | GnomeGamesSupport.ScoresButtons.NEW_GAME_BUTTON);
-        if (scores_dialog.run () == Gtk.ResponseType.REJECT)
+        if (show_scores (entry, true) == Gtk.ResponseType.CLOSE)
             window.destroy ();
         else
             new_game ();
-        scores_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 new_game_cb ()
@@ -262,10 +262,7 @@ public class Gnotravex : Gtk.Application
 
     private void scores_cb ()
     {
-        var scores_dialog = new GnomeGamesSupport.ScoresDialog (window, highscores, _("Tetravex Scores"));
-        scores_dialog.set_category_description (_("Size:"));
-        scores_dialog.run ();
-        scores_dialog.destroy ();
+        show_scores ();    
     }
 
     private bool view_button_press_event (Gtk.Widget widget, Gdk.EventButton event)
@@ -388,8 +385,161 @@ public class Gnotravex : Gtk.Application
         Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
         Intl.textdomain (GETTEXT_PACKAGE);
 
-        GnomeGamesSupport.scores_startup ();
         var app = new Gnotravex ();
         return app.run (args);
     }
 }
+
+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 (int));
+
+        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_size (uint size)
+    {
+        score_model.clear ();
+
+        var entries = history.entries.copy ();
+        entries.sort (compare_entries);
+
+        foreach (var entry in entries)
+        {
+            if (entry.size != size)
+                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)
+    {
+        if (a.size != b.size)
+            return (int) a.size - (int) b.size;
+        if (a.duration != b.duration)
+            return (int) a.duration - (int) b.duration;
+        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;
+
+        int size;
+        combo.model.get (iter, 1, out size);
+        set_size ((uint) size);
+    }
+
+    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
+            {
+                int size, height, n_mines;
+                size_model.get (iter, 1, out size, 2, out height, 3, out n_mines);
+                if (size == entry.size)
+                {
+                    have_size_entry = true;
+                    break;
+                }
+            } while (size_model.iter_next (ref iter));
+        }
+
+        if (!have_size_entry)
+        {
+            var label = "%u à %u".printf (entry.size, entry.size);
+
+            size_model.append (out iter);
+            size_model.set (iter, 0, label, 1, entry.size);
+    
+            /* 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.size == selected_entry.size)
+                size_combo.set_active_iter (iter);
+        }
+    }
+}
diff --git a/gnotravex/src/history.vala b/gnotravex/src/history.vala
new file mode 100644
index 0000000..03f55fb
--- /dev/null
+++ b/gnotravex/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 size = int.parse (tokens[1]);
+            var duration = int.parse (tokens[2]);
+
+            add (new HistoryEntry (date, size, duration));
+        }
+    }
+
+    public void save ()
+    {
+        var contents = "";
+
+        foreach (var entry in entries)
+        {
+            var line = "%s %u %u\n".printf (entry.date.to_string (), entry.size, 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 uint size;
+    public uint duration;
+
+    public HistoryEntry (DateTime date, uint size, uint duration)
+    {
+        this.date = date;
+        this.size = size;
+        this.duration = duration;
+    }
+}



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