[aisleriot] sol: Use string instead of number for storing the score in the frontend



commit 2dd98c2d7d539c20954792482599bf5ea2bcf9e9
Author: Christian Persch <chpe gnome org>
Date:   Sat Dec 3 23:57:57 2011 +0100

    sol: Use string instead of number for storing the score in the frontend
    
    https://bugzilla.gnome.org/show_bug.cgi?id=556359

 games/api.scm |    7 ++++---
 src/game.c    |   39 +++++++++++++++++++++++++++++----------
 src/game.h    |    2 ++
 src/window.c  |   12 ++----------
 4 files changed, 37 insertions(+), 23 deletions(-)
---
diff --git a/games/api.scm b/games/api.scm
index b033e1c..f38ad1f 100644
--- a/games/api.scm
+++ b/games/api.scm
@@ -16,7 +16,7 @@
 
 (define-module (aisleriot api))
 
-(use-modules (aisleriot interface) (ice-9 format))
+(use-modules (aisleriot interface) (ice-9 format) (ice-9 i18n))
 
 ;; Feature masks:
 (define-public droppable-feature 1)
@@ -78,7 +78,7 @@
   (set! BOTTOM-SLOTS '())
   (set! LEFT-SLOTS '())
   (set! RIGHT-SLOTS '())
-  (set! SCORE 0))
+  (set-score! 0))
 
 ; Use this instead of define for variables which determine the state of
 ; the game. i.e. anything that isn't a constant. This is so undo/redo
@@ -624,7 +624,8 @@
 (define-public (set-score! value)
   (begin
     (set! SCORE value)
-    (update-score SCORE)))
+    (update-score (number->locale-string SCORE))
+    SCORE))
 
 (define-public (get-score) 
   SCORE)
diff --git a/src/game.c b/src/game.c
index a9c49d2..e568f2d 100644
--- a/src/game.c
+++ b/src/game.c
@@ -106,7 +106,7 @@ struct _AisleriotGame
   GTimer *timer;
 
   int timeout;
-  int score;
+  char *score;
 
   double width;
   double height;
@@ -985,13 +985,16 @@ static SCM
 scm_update_score (SCM new_score)
 {
   AisleriotGame *game = app_game;
-  int score;
+  char *score;
 
-  score = scm_to_int (new_score);
-  if (score != game->score) {
+  score = scm_to_locale_string (new_score);
+  if (g_strcmp0 (score, game->score) != 0) {
+    free (game->score);
     game->score = score;
 
     g_object_notify (G_OBJECT (game), "score");
+  } else {
+    free (score);
   }
 
   return new_score;
@@ -1178,7 +1181,7 @@ aisleriot_game_init (AisleriotGame *game)
   game->timer = g_timer_new ();
 
   game->timeout = 60 * 60;
-  game->score = 0;
+  game->score = NULL;
 }
 
 static GObject *
@@ -1222,6 +1225,8 @@ aisleriot_game_finalize (GObject *object)
   if (game->saved_rand)
     g_rand_free (game->saved_rand);
 
+  free (game->score);
+
   app_game = NULL;
 
   G_OBJECT_CLASS (aisleriot_game_parent_class)->finalize (object);
@@ -1249,7 +1254,7 @@ aisleriot_game_get_property (GObject *object,
       g_value_set_string (value, game->game_module);
       break;
     case PROP_SCORE:
-      g_value_set_int (value, game->score);
+      g_value_set_string (value, game->score);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -1366,10 +1371,10 @@ aisleriot_game_class_init (AisleriotGameClass *klass)
   g_object_class_install_property
     (gobject_class,
      PROP_SCORE,
-     g_param_spec_int ("score", NULL, NULL,
-                       G_MININT, G_MAXINT, 0,
-                       G_PARAM_READABLE |
-                       G_PARAM_STATIC_STRINGS));
+     g_param_spec_string ("score", NULL, NULL,
+                          NULL,
+                          G_PARAM_READABLE |
+                          G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property
     (gobject_class,
@@ -2562,6 +2567,20 @@ aisleriot_game_deal_cards (AisleriotGame *game)
   aisleriot_game_test_end_of_game (game);
 }
 
+/**
+ * aisleriot_game_get_score:
+ * @game:
+ * 
+ * Returns: (transfer none): the current score as a string
+ */
+const char *
+aisleriot_game_get_score (AisleriotGame *game)
+{
+  g_return_val_if_fail (AISLERIOT_IS_GAME (game), NULL);
+
+  return game->score ? game->score : "";
+}
+
 #ifdef HAVE_CLUTTER
 
 void
diff --git a/src/game.h b/src/game.h
index 1d96a4f..4238569 100644
--- a/src/game.h
+++ b/src/game.h
@@ -243,6 +243,8 @@ void aisleriot_game_get_card_offset (ArSlot *slot,
 
 void aisleriot_game_reset_old_cards (ArSlot *slot);
 
+const char *aisleriot_game_get_score (AisleriotGame *game);
+
 char **ar_get_game_modules (void);
 
 G_END_DECLS
diff --git a/src/window.c b/src/window.c
index 0196370..2a721da 100644
--- a/src/window.c
+++ b/src/window.c
@@ -1505,17 +1505,9 @@ sync_game_score (AisleriotGame *game,
                  AisleriotWindow *window)
 {
   AisleriotWindowPrivate *priv = window->priv;
-  guint score = 0;
-  char str[64];
 
-  g_object_get (game, "score", &score, NULL);
-
-  /* Translators: if you want to use localised digits for the game score,
-   * then translate this string to "%I6d", else to "%6d".
-   * Do not translate it to anything else!
-   */
-  g_snprintf (str, sizeof (str), C_("score", "%6d"), score);
-  gtk_label_set_text (GTK_LABEL (priv->score_label), str);
+  gtk_label_set_text (GTK_LABEL (priv->score_label),
+                      aisleriot_game_get_score (game));
 }
 
 static void



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