[gnome-robots] Extract highscores into a class



commit 0da270ba30ac7d85f94925a6a1fc5c4d56961389
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Oct 4 13:05:01 2020 +0200

    Extract highscores into a class

 src/meson.build |  1 +
 src/robots.vala | 74 ++++++--------------------------------------
 src/scores.vala | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 65 deletions(-)
---
diff --git a/src/meson.build b/src/meson.build
index 929e551..d55a553 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -29,6 +29,7 @@ sources = files(
     'game-area.vala',
     'bubble.vala',
     'window-size.vala',
+    'scores.vala',
     'robots.vala',
 )
 
diff --git a/src/robots.vala b/src/robots.vala
index d1527df..d6becb5 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -19,9 +19,6 @@
 
 using Gtk;
 using Cairo;
-using Games;
-
-Games.Scores.Context highscores;
 
 public class RobotsWindow : ApplicationWindow {
 
@@ -30,6 +27,7 @@ public class RobotsWindow : ApplicationWindow {
     private GameArea game_area;
     private EventControllerKey key_controller;
     private Properties properties;
+    private RobotsScoresContext highscores;
 
     public RobotsWindow (Gtk.Application app,
                          Properties properties,
@@ -83,16 +81,9 @@ public class RobotsWindow : ApplicationWindow {
         key_controller = new EventControllerKey (this);
         key_controller.key_pressed.connect (keyboard_cb);
 
+        highscores = new RobotsScoresContext (this);
         game_area.add_score.connect ((game_type, score) => {
-            string name = category_name_from_key (game_type);
-            var category = new Scores.Category (game_type, name);
-            highscores.add_score.begin (score, category, null, (ctx, res) => {
-                try {
-                    highscores.add_score.end (res);
-                } catch (Error error) {
-                    warning ("Failed to add score: %s", error.message);
-                }
-            });
+            highscores.add_game_score (game_type, score);
         });
     }
 
@@ -198,52 +189,12 @@ public class RobotsWindow : ApplicationWindow {
     public void start_new_game () {
         game_area.start_new_game ();
     }
-}
 
-public string? category_name_from_key (string key) {
-    switch (key) {
-    case "classic_robots":
-        return N_("Classic robots");
-    case "classic_robots-safe":
-        return N_("Classic robots with safe moves");
-    case "classic_robots-super-safe":
-        return N_("Classic robots with super-safe moves");
-    case "nightmare":
-        return N_("Nightmare");
-    case "nightmare-safe":
-        return N_("Nightmare with safe moves");
-    case "nightmare-super-safe":
-        return N_("Nightmare with super-safe moves");
-    case "robots2":
-        return N_("Robots2");
-    case "robots2-safe":
-        return N_("Robots2 with safe moves");
-    case "robots2-super-safe":
-        return N_("Robots2 with super-safe moves");
-    case "robots2_easy":
-        return N_("Robots2 easy");
-    case "robots2_easy-safe":
-        return N_("Robots2 easy with safe moves");
-    case "robots2_easy-super-safe":
-        return N_("Robots2 easy with super-safe moves");
-    case "robots_with_safe_teleport":
-        return N_("Robots with safe teleport");
-    case "robots_with_safe_teleport-safe":
-        return N_("Robots with safe teleport with safe moves");
-    case "robots_with_safe_teleport-super-safe":
-        return N_("Robots with safe teleport with super-safe moves");
-    default:
-        return null;
+    public void show_highscores () {
+        highscores.run_dialog ();
     }
 }
 
-Games.Scores.Category? create_category_from_key (string key) {
-    string name = category_name_from_key (key);
-    if (name == null)
-        return null;
-    return new Games.Scores.Category (key, name);
-}
-
 class RobotsApplication : Gtk.Application {
 
     private Properties properties;
@@ -323,16 +274,6 @@ class RobotsApplication : Gtk.Application {
             quit ();
         }
 
-        var importer = new Games.Scores.DirectoryImporter ();
-        highscores = new Games.Scores.Context.with_importer_and_icon_name ("gnome-robots",
-                                                                           /* Label on the scores dialog, 
next to map type dropdown */
-                                                                           _("Game Type:"),
-                                                                           window,
-                                                                           create_category_from_key,
-                                                                           
Games.Scores.Style.POINTS_GREATER_IS_BETTER,
-                                                                           importer,
-                                                                           "org.gnome.Robots");
-
         window.show_all ();
     }
 
@@ -367,7 +308,10 @@ class RobotsApplication : Gtk.Application {
     }
 
     private void scores_cb () {
-        highscores.run_dialog ();
+        var window = get_active_window () as RobotsWindow;
+        if (window != null) {
+            window.show_highscores ();
+        }
     }
 
     private void help_cb () {
diff --git a/src/scores.vala b/src/scores.vala
new file mode 100644
index 0000000..576dd84
--- /dev/null
+++ b/src/scores.vala
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2020 Andrey Kutejko <andy128k gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * For more details see the file COPYING.
+ */
+
+using Gtk;
+using Cairo;
+using Games;
+
+public class RobotsScoresContext : Games.Scores.Context {
+
+    public RobotsScoresContext (Window game_window) {
+        base.with_importer_and_icon_name ("gnome-robots",
+                                          /* Label on the scores dialog, next to map type dropdown */
+                                          _("Game Type:"),
+                                          game_window,
+                                          create_category_from_game_type,
+                                          Scores.Style.POINTS_GREATER_IS_BETTER,
+                                          new Scores.DirectoryImporter (),
+                                          "org.gnome.Robots");
+    }
+
+    public void add_game_score (string game_type, int score) {
+        var category = create_category_from_game_type (game_type);
+        if (category == null) {
+            warning ("Failed to add score for unknown game '%s'.", game_type);
+        }
+        add_score.begin (score, category, null, (ctx, res) => {
+            try {
+                add_score.end (res);
+            } catch (Error error) {
+                warning ("Failed to add score: %s", error.message);
+            }
+        });
+    }
+
+    private static string? category_name_from_game_type (string game_type) {
+        switch (game_type) {
+        case "classic_robots":
+            return N_("Classic robots");
+        case "classic_robots-safe":
+            return N_("Classic robots with safe moves");
+        case "classic_robots-super-safe":
+            return N_("Classic robots with super-safe moves");
+        case "nightmare":
+            return N_("Nightmare");
+        case "nightmare-safe":
+            return N_("Nightmare with safe moves");
+        case "nightmare-super-safe":
+            return N_("Nightmare with super-safe moves");
+        case "robots2":
+            return N_("Robots2");
+        case "robots2-safe":
+            return N_("Robots2 with safe moves");
+        case "robots2-super-safe":
+            return N_("Robots2 with super-safe moves");
+        case "robots2_easy":
+            return N_("Robots2 easy");
+        case "robots2_easy-safe":
+            return N_("Robots2 easy with safe moves");
+        case "robots2_easy-super-safe":
+            return N_("Robots2 easy with super-safe moves");
+        case "robots_with_safe_teleport":
+            return N_("Robots with safe teleport");
+        case "robots_with_safe_teleport-safe":
+            return N_("Robots with safe teleport with safe moves");
+        case "robots_with_safe_teleport-super-safe":
+            return N_("Robots with safe teleport with super-safe moves");
+        default:
+            return null;
+        }
+    }
+
+    private static Scores.Category? create_category_from_game_type (string game_type) {
+        string name = category_name_from_game_type (game_type);
+        if (name == null)
+            return null;
+        return new Games.Scores.Category (game_type, name);
+    }
+}
+


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