[gnome-nibbles] Add multiple scoring categories



commit 727b8bbf21aea338ebf0c25c8d61e3059bcaa14d
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Fri Jul 31 20:00:06 2015 +0300

    Add multiple scoring categories

 src/gnome-nibbles.vala |   81 +++++++++++++++++++++++++++++++++---------------
 src/nibbles-game.vala  |    8 ++--
 2 files changed, 60 insertions(+), 29 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 909f48c..29aae3e 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -38,11 +38,9 @@ public class Nibbles : Gtk.Application
     private Gee.LinkedList<Gtk.ToggleButton> number_of_players_buttons;
     private Gtk.Revealer next_button_revealer;
 
+    /* Used for handling the game's scores */
     private Games.Scores.Context scores_context;
-    private Games.Scores.Category cat_beginner;
-    private Games.Scores.Category cat_slow;
-    private Games.Scores.Category cat_medium;
-    private Games.Scores.Category cat_fast;
+    private Gee.LinkedList<Games.Scores.Category> scorecats;
 
     private NibblesView? view;
     private NibblesGame? game = null;
@@ -341,29 +339,66 @@ public class Nibbles : Gtk.Application
         }
     }
 
+    /*\
+    * * Scoring
+    \*/
+
     public void create_scores ()
     {
-        stderr.printf("[Debug] Created\n");
         scores_context = new Games.Scores.Context ("gnome-nibbles", "", window, 
Games.Scores.Style.PLAIN_DESCENDING);
-        cat_beginner = new Games.Scores.Category ("beginner", "Beginner");
-        cat_slow = new Games.Scores.Category ("slow", "Slow");
-        cat_medium = new Games.Scores.Category ("medium", "Medium");
-        cat_fast = new Games.Scores.Category ("fast", "Fast");
+
+        scorecats = new Gee.LinkedList<Games.Scores.Category> ();
+        scorecats.add (new Games.Scores.Category ("beginner", "Beginner"));
+        scorecats.add (new Games.Scores.Category ("slow", "Slow"));
+        scorecats.add (new Games.Scores.Category ("medium", "Medium"));
+        scorecats.add (new Games.Scores.Category ("fast", "Fast"));
+        scorecats.add (new Games.Scores.Category ("beginner-fakes", "Beginner with Fakes"));
+        scorecats.add (new Games.Scores.Category ("slow-fakes", "Slow with Fakes"));
+        scorecats.add (new Games.Scores.Category ("medium-fakes", "Medium with Fakes"));
+        scorecats.add (new Games.Scores.Category ("fast-fakes", "Fast with Fakes"));
 
         scores_context.category_request.connect ((s, key) => {
-            if (key == "beginner")
-                return cat_beginner;
-            else if (key == "slow")
-                return cat_slow;
-            else if (key == "cat_medium")
-                return cat_medium;
-            else if (key == "cat_fast")
-                return cat_fast;
-            else
-                return null;
+
+            foreach (var cat in scorecats)
+            {
+                if (key == cat.key)
+                    return cat;
+            }
+            return null;
         });
     }
 
+    public Games.Scores.Category get_scores_category (int speed, bool fakes)
+    {
+        string key = null;
+        switch (speed)
+        {
+            case 1:
+                key = "fast";
+                break;
+            case 2:
+                key = "medium";
+                break;
+            case 3:
+                key = "slow";
+                break;
+            case 4:
+                key = "beginner";
+                break;
+        }
+
+        if (fakes)
+            key = key + "-fakes";
+
+        foreach (var cat in scorecats)
+        {
+            if (key == cat.key)
+                return cat;
+        }
+
+        return scorecats.first ();
+    }
+
     public void log_score_cb (int score)
     {
         if (game.numhumans != 1)
@@ -373,15 +408,11 @@ public class Nibbles : Gtk.Application
             return;
 
         if (score <= 0)
-        {
-            stderr.printf("[Debug] 0\n");
             return;
-        }
-        stderr.printf("[Debug] Here\n");
+
         try
         {
-            scores_context.add_score (score, cat_slow);
-
+            scores_context.add_score (score, get_scores_category (game.speed, game.fakes));
         }
         catch (GLib.Error e)
         {
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index eba4908..214fbf2 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -42,7 +42,7 @@ public class NibblesGame : Object
     public const int GAMEDELAY = 35;
     public const int BONUSDELAY = 100;
 
-    public const int NUMHUMANS = 2;
+    public const int NUMHUMANS = 1;
     public const int NUMAI = 0;
     public const int NUMWORMS = NUMHUMANS + NUMAI;
 
@@ -61,7 +61,7 @@ public class NibblesGame : Object
     public int numai = NUMAI;
     public int numworms = NUMHUMANS + NUMAI;
 
-    public int game_speed = 2;
+    public int speed = 1;
 
     public bool fakes = false;
 
@@ -91,10 +91,10 @@ public class NibblesGame : Object
     {
         add_bonus (true);
 
-        main_id = Timeout.add (GAMEDELAY * game_speed, main_loop_cb);
+        main_id = Timeout.add (GAMEDELAY * speed, main_loop_cb);
         Source.set_name_by_id (main_id, "[Nibbles] main_loop_cb");
 
-        add_bonus_id = Timeout.add (BONUSDELAY * game_speed, add_bonus_cb);
+        add_bonus_id = Timeout.add (BONUSDELAY * speed, add_bonus_cb);
         Source.set_name_by_id (add_bonus_id, "[Nibbles] add_bonus_cb");
     }
 


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