[gnome-2048] Add scores management



commit af175b9fbd48e68b99e3ed33eb065967f5bc6c53
Author: Juan R. GarcĂ­a Blanco <juanrgar gmail com>
Date:   Fri Feb 20 23:14:53 2015 +0100

    Add scores management
    
        * data/menus.ui: Add 'Scores' menu entry.
        * src/Makefile.am: Build with games-scores.
        * src/application.vala: Create scores context and categories;
        add new score when Game::finished signal is triggered. Add
        _game_restored property to avoid showing scores dialog after
        restoring.
        * src/game.vala: Decouple ::finished signal emission from
        Clutter signal handler.

 data/menus.ui        |    4 ++++
 src/Makefile.am      |    3 ++-
 src/application.vala |   48 ++++++++++++++++++++++++++++++++++++++++++------
 src/game.vala        |   24 +++++++++++++-----------
 4 files changed, 61 insertions(+), 18 deletions(-)
---
diff --git a/data/menus.ui b/data/menus.ui
index 1828db9..034c83c 100644
--- a/data/menus.ui
+++ b/data/menus.ui
@@ -6,6 +6,10 @@
         <attribute name="label" translatable="yes">New Game</attribute>
         <attribute name="action">app.new-game</attribute>
       </item>
+      <item>
+        <attribute name="label" translatable="yes">Scores</attribute>
+        <attribute name="action">app.scores</attribute>
+      </item>
     </section>
     <section>
       <item>
diff --git a/src/Makefile.am b/src/Makefile.am
index abe353c..d139c2a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -25,7 +25,8 @@ gnome_2048_VALAFLAGS = \
        --pkg clutter-1.0 \
        --pkg clutter-gtk-1.0 \
        --pkg cogl-1.0 \
-       --pkg gee-0.8
+       --pkg gee-0.8 \
+       --pkg games-scores
 
 gnome_2048_LDADD = \
        $(GNOME_2048_LIBS) \
diff --git a/src/application.vala b/src/application.vala
index 584a48a..7679cc3 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -16,6 +16,8 @@
  * along with GNOME 2048; if not, see <http://www.gnu.org/licenses/>.
  */
 
+using Games;
+
 public class Application : Gtk.Application
 {
   private GLib.Settings _settings;
@@ -30,6 +32,12 @@ public class Application : Gtk.Application
   private Gtk.Label _score;
   private Gtk.ComboBoxText _grid_size_combo;
 
+  private Scores.Context _scores_ctx;
+  private Scores.Category _grid4_cat;
+  private Scores.Category _grid5_cat;
+
+  private bool _game_restored;
+
   private int _window_width;
   private int _window_height;
   private bool _window_maximized;
@@ -42,6 +50,7 @@ public class Application : Gtk.Application
   private const GLib.ActionEntry[] action_entries =
   {
     { "new-game",       new_game_cb       },
+    { "scores",         scores_cb         },
     { "about",          about_cb          },
     { "preferences",    preferences_cb    },
     { "quit",           quit_cb           },
@@ -77,8 +86,11 @@ public class Application : Gtk.Application
     _create_preferences_dialog (builder);
     _create_congrats_dialog (builder);
 
-    if (!_game.restore_game ())
-      _game.new_game ();
+    _create_scores ();
+
+    _game_restored = _game.restore_game ();
+    if (!_game_restored)
+      new_game_cb ();
   }
 
   protected override void shutdown ()
@@ -126,6 +138,16 @@ public class Application : Gtk.Application
     });
     _game.finished.connect ((s) => {
       _header_bar.subtitle = _("Game Over");
+
+      if (!_game_restored) {
+        try {
+          Scores.Category cat = (_settings.get_int ("rows") == 4) ? _grid4_cat : _grid5_cat;
+          _scores_ctx.add_score (_game.score, cat);
+        } catch (GLib.Error e) {
+          stderr.printf ("%s\n", e.message);
+        }
+      }
+
       debug ("finished");
     });
     _game.target_value_reached.connect ((s, v) => {
@@ -252,7 +274,7 @@ public class Application : Gtk.Application
 
       settings_changed = _game.reload_settings ();
       if (settings_changed)
-        _game.new_game ();
+        new_game_cb ();
       return _preferences_dialog.hide_on_delete ();
     });
 
@@ -272,7 +294,7 @@ public class Application : Gtk.Application
 
     _congrats_dialog.response.connect ((response_id) => {
       if (response_id == 0)
-        _game.new_game ();
+        new_game_cb ();
       _congrats_dialog.hide ();
     });
     _congrats_dialog.delete_event.connect ((response_id) => {
@@ -282,13 +304,26 @@ public class Application : Gtk.Application
     _congrats_message = builder.get_object ("messagelabel") as Gtk.Label;
   }
 
+  private void _create_scores ()
+  {
+    _scores_ctx = new Scores.Context ("gnome-2048", "", _window, Scores.Style.PLAIN_DESCENDING);
+    _grid4_cat = new Scores.Category ("grid4", "Grid 4 x 4");
+    _grid5_cat = new Scores.Category ("grid5", "Grid 5 x 5");
+  }
+
   private void new_game_cb ()
   {
     _header_bar.subtitle = null;
+    _game_restored = false;
 
     _game.new_game ();
   }
 
+  private void scores_cb ()
+  {
+    _scores_ctx.run_dialog ();
+  }
+
   private void about_cb ()
   {
     _about_dialog.present ();
@@ -297,10 +332,9 @@ public class Application : Gtk.Application
   private void preferences_cb ()
   {
     int grid_size;
-    int rows, cols;
+    int rows;
 
     rows = _settings.get_int ("rows");
-    cols = _settings.get_int ("cols");
 
     if (rows == 4) {
       grid_size = 0;
@@ -329,6 +363,8 @@ public class Application : Gtk.Application
 
   private bool key_press_event_cb (Gtk.Widget widget, Gdk.EventKey event)
   {
+    _game_restored = false;
+
     return _game.key_pressed (event);
   }
 
diff --git a/src/game.vala b/src/game.vala
index 6655fe7..39f38f9 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -624,8 +624,18 @@ public class Game : GLib.Object
       _foreground_cur[e.from.row,e.from.col] = null;
     }
 
-    _finish_move ();
+    GLib.Timeout.add (100, _finish_move);
+  }
+
+  private void _create_show_hide_transition ()
+  {
+    _show_hide_trans = new Clutter.TransitionGroup ();
+    _show_hide_trans.stopped.connect (_on_show_hide_trans_stopped);
+    _show_hide_trans.set_duration (100);
+  }
 
+  private bool _finish_move ()
+  {
     if (_state == GameState.SHOWING_FIRST_TILE) {
       _state = GameState.SHOWING_SECOND_TILE;
       debug ("state show second tile");
@@ -637,17 +647,7 @@ public class Game : GLib.Object
       _state = GameState.IDLE;
       debug ("state idle");
     }
-  }
 
-  private void _create_show_hide_transition ()
-  {
-    _show_hide_trans = new Clutter.TransitionGroup ();
-    _show_hide_trans.stopped.connect (_on_show_hide_trans_stopped);
-    _show_hide_trans.set_duration (100);
-  }
-
-  private void _finish_move ()
-  {
     foreach (var e in _to_move) {
       _foreground_cur[e.to.row,e.to.col] = _foreground_nxt[e.to.row,e.to.col];
       _foreground_nxt[e.to.row,e.to.col] = null;
@@ -668,5 +668,7 @@ public class Game : GLib.Object
 
     if (_grid.is_finished ())
       finished ();
+
+    return false;
   }
 }


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