Thanks Ajay and Ariel.<div>I filled sl #4373 to follow this issue.</div><div><br></div><div>Comments:</div><div>* Now you save the game data and the game session data every time. There are small games, like aritmetic or letters, but can be big if have images or sounds attached. Should be good save the data only if is a custom game.  </div>
<div><div>* I think is better use json to persist a structure like a list instead of str/eval. </div><div><br></div><div>I haven't tested this yet.</div><div><br></div><div>Gonzalo</div><div><br></div><div><br><div class="gmail_quote">
On Thu, Jan 10, 2013 at 11:44 AM, Ariel Calzada <span dir="ltr"><<a href="mailto:ariel.calzada@gmail.com" target="_blank">ariel.calzada@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
From: Ajay Garg <<a href="mailto:ajay@activitycentral.com">ajay@activitycentral.com</a>><br>
<br>
The approach followed has been simply to save the current-game image<br>
when closing the game. The game resumes from that image next time<br>
onwards.<br>
<br>
Tests conducted ::<br>
<br>
a)<br>
<br>
   (i)<br>
   User exits, without finishing the current game.<br>
<br>
   (ii)<br>
   When she next starts, she will proceed from the same game, from the same stage.<br>
<br>
b)<br>
<br>
   (i)<br>
   User exits, without finishing the current game.<br>
<br>
   (ii)<br>
   She then transfers the journal-entry to another XO.<br>
<br>
   (iii)<br>
   Tries to resume the game, from the (transferred) journal entry.<br>
<br>
   (iv)<br>
   Game resumes, from the same stage.<br>
<br>
<br>
Signed-off-by: Ajay Garg <<a href="mailto:ajay@activitycentral.com">ajay@activitycentral.com</a>><br>
---<br>
 activity.py        | 16 ++++++++++++++--<br>
 cardlist.py        |  3 ++-<br>
 game.py            | 14 ++++++++++++--<br>
 memorizetoolbar.py |  2 +-<br>
 model.py           | 12 +++++++++++-<br>
 5 files changed, 40 insertions(+), 7 deletions(-)<br>
<br>
diff --git a/activity.py b/activity.py<br>
index 8bfd758..8bc679d 100644<br>
--- a/activity.py<br>
+++ b/activity.py<br>
@@ -107,7 +107,7 @@ class MemorizeActivity(Activity):<br>
         # Play game mode<br>
         self.table = cardtable.CardTable()<br>
         self.scoreboard = scoreboard.Scoreboard()<br>
-        self.cardlist = cardlist.CardList()<br>
+        self.cardlist = cardlist.CardList(self)<br>
         self.createcardpanel = createcardpanel.CreateCardPanel()<br>
         self.cardlist.connect('pair-selected',<br>
                 self.createcardpanel.pair_selected)<br>
@@ -127,7 +127,7 @@ class MemorizeActivity(Activity):<br>
                 self._memorizeToolbarBuilder.reset)<br>
         self._createToolbarBuilder.connect('create_equal_pairs',<br>
                 self.change_equal_pairs)<br>
-        self.game = game.MemorizeGame()<br>
+        self.game = game.MemorizeGame(self)<br>
<br>
         self._edit_button.connect('toggled', self._change_mode_bt)<br>
<br>
@@ -232,11 +232,16 @@ class MemorizeActivity(Activity):<br>
<br>
     def write_file(self, file_path):<br>
         logging.debug('WRITE_FILE is_demo %s', self.game.model.is_demo)<br>
+<br>
+        # We want to save the game-image for demo games too !!!<br>
+        """<br>
         if self.game.model.is_demo:<br>
             # if is a demo game only want keep the metadata<br>
             self._jobject.set_file_path(None)<br>
             raise NotImplementedError<br>
             return<br>
+        """<br>
+<br>
         if self.cardlist.pair_list_modified:<br>
             self.cardlist.update_model(self.game.model)<br>
<br>
@@ -285,6 +290,13 @@ class MemorizeActivity(Activity):<br>
         game_zip.close()<br>
         self.metadata['mime_type'] = 'application/x-memorize-project'<br>
<br>
+        # Store the game image as a string - that is simpler instead of<br>
+        # having to deal with the dbus-converted list.<br>
+        # When reading back, we use "eval" to convert the string into<br>
+        # the correct type ("list" in this case).<br>
+        self.metadata['saved_game_data_image'] = str(self.game.model.grid)<br>
+        self.metadata['size'] = int(self.game.model.data['size'])<br>
+<br>
     def _complete_close(self):<br>
         self._remove_temp_files()<br>
         Activity._complete_close(self)<br>
diff --git a/cardlist.py b/cardlist.py<br>
index 6bc781c..65e44f1 100644<br>
--- a/cardlist.py<br>
+++ b/cardlist.py<br>
@@ -40,8 +40,9 @@ class CardList(gtk.EventBox):<br>
         'update-create-toolbar': (SIGNAL_RUN_FIRST, None, 3 * [TYPE_PYOBJECT]),<br>
     }<br>
<br>
-    def __init__(self):<br>
+    def __init__(self, activity_instance):<br>
         gtk.EventBox.__init__(self)<br>
+        self._activity_instance = activity_instance<br>
         self.pairs = []<br>
         self.current_pair = None<br>
         self.current_game_key = None<br>
diff --git a/game.py b/game.py<br>
index c5e54d1..9d21b4b 100644<br>
--- a/game.py<br>
+++ b/game.py<br>
@@ -57,7 +57,7 @@ class MemorizeGame(GObject):<br>
         'change-turn': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),<br>
         }<br>
<br>
-    def __init__(self):<br>
+    def __init__(self, activity_instance):<br>
         gobject.GObject.__init__(self)<br>
         self.myself = None<br>
         self.players_score = {}<br>
@@ -70,7 +70,7 @@ class MemorizeGame(GObject):<br>
         self.messenger = None<br>
         self.sentitive = True<br>
<br>
-        self.model = Model()<br>
+        self.model = Model(activity_instance=activity_instance)<br>
         self.flip_block = False<br>
         self._flop_cards = None<br>
<br>
@@ -109,6 +109,16 @@ class MemorizeGame(GObject):<br>
         self.change_turn()<br>
         self.model.data['running'] = 'False'<br>
<br>
+        # Card 'state' is an aawesome field.<br>
+        # Its takes on the following values ::<br>
+        #<br>
+        # 0                              ==>    for flopped cards.<br>
+        #<br>
+        # 1                              ==>    for flipped unmatched cards<br>
+        #                                       (can be a maximum of 1 such card).<br>
+        #<br>
+        # <stroke_color>, <fill_color>   ==>    for flipped matched cards<br>
+<br>
         for card in self.model.grid:<br>
             if card['state'] == '1':<br>
                 self.emit('flip-card', self.model.grid.index(card), False)<br>
diff --git a/memorizetoolbar.py b/memorizetoolbar.py<br>
index 1a06da7..07ceecf 100644<br>
--- a/memorizetoolbar.py<br>
+++ b/memorizetoolbar.py<br>
@@ -135,5 +135,5 @@ class MemorizeToolbarBuilder(gobject.GObject):<br>
         self._demo_games.props.icon_name = 'memorize-collection'<br>
<br>
     def update_toolbar(self, widget, data, grid):<br>
-        size = data.get('size')<br>
+        size = str(data.get('size'))<br>
         self._size_combo.props.icon_name = size + ' X ' + size<br>
diff --git a/model.py b/model.py<br>
index 2567ed2..16239c4 100644<br>
--- a/model.py<br>
+++ b/model.py<br>
@@ -100,11 +100,13 @@ class Model(object):<br>
     information.<br>
     '''<br>
<br>
-    def __init__(self, game_path=None):<br>
+    def __init__(self, game_path=None, activity_instance=None):<br>
         tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance')<br>
         self.temp_folder = tempfile.mkdtemp(dir=tmp_root)<br>
         chmod(self.temp_folder, 0777)<br>
+        self._saved_game_loaded = False<br>
<br>
+        self._activity_instance = activity_instance<br>
         self.data = {}<br>
<br>
         if game_path is None:<br>
@@ -366,6 +368,14 @@ class Model(object):<br>
             temp1.extend(temp2)<br>
             random.shuffle(temp1)<br>
         self.grid = temp1<br>
+<br>
+        if not self._saved_game_loaded:<br>
+            if self._activity_instance is not None:<br>
+                if 'saved_game_data_image' in self._activity_instance.metadata.keys():<br>
+                    self.grid = eval(self._activity_instance.metadata['saved_game_data_image'])<br>
+                    self.data['size'] = int(self._activity_instance.metadata['size'])<br>
+                    self._saved_game_loaded = True<br>
+<br>
         _logger.debug('Defgrid: grid( size=%s ): %s'<br>
                       % (self.data['size'], self.grid))<br>
         _logger.debug('Defgrid: data: %s', self.data)<br>
--<br>
1.7.11.7<br>
<br>
_______________________________________________<br>
Sugar-devel mailing list<br>
<a href="mailto:Sugar-devel@lists.sugarlabs.org">Sugar-devel@lists.sugarlabs.org</a><br>
<a href="http://lists.sugarlabs.org/listinfo/sugar-devel" target="_blank">http://lists.sugarlabs.org/listinfo/sugar-devel</a><br>
</blockquote></div><br></div></div>