[Sugar-devel] [PATCH] SDXO#2620 Add ability to save unfinished games in Memorize.

Ariel Calzada ariel.calzada at gmail.com
Thu Jan 10 09:44:18 EST 2013


From: Ajay Garg <ajay at activitycentral.com>

The approach followed has been simply to save the current-game image
when closing the game. The game resumes from that image next time
onwards.

Tests conducted ::

a)

   (i)
   User exits, without finishing the current game.

   (ii)
   When she next starts, she will proceed from the same game, from the same stage.

b)

   (i)
   User exits, without finishing the current game.

   (ii)
   She then transfers the journal-entry to another XO.

   (iii)
   Tries to resume the game, from the (transferred) journal entry.

   (iv)
   Game resumes, from the same stage.


Signed-off-by: Ajay Garg <ajay at activitycentral.com>
---
 activity.py        | 16 ++++++++++++++--
 cardlist.py        |  3 ++-
 game.py            | 14 ++++++++++++--
 memorizetoolbar.py |  2 +-
 model.py           | 12 +++++++++++-
 5 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/activity.py b/activity.py
index 8bfd758..8bc679d 100644
--- a/activity.py
+++ b/activity.py
@@ -107,7 +107,7 @@ class MemorizeActivity(Activity):
         # Play game mode
         self.table = cardtable.CardTable()
         self.scoreboard = scoreboard.Scoreboard()
-        self.cardlist = cardlist.CardList()
+        self.cardlist = cardlist.CardList(self)
         self.createcardpanel = createcardpanel.CreateCardPanel()
         self.cardlist.connect('pair-selected',
                 self.createcardpanel.pair_selected)
@@ -127,7 +127,7 @@ class MemorizeActivity(Activity):
                 self._memorizeToolbarBuilder.reset)
         self._createToolbarBuilder.connect('create_equal_pairs',
                 self.change_equal_pairs)
-        self.game = game.MemorizeGame()
+        self.game = game.MemorizeGame(self)

         self._edit_button.connect('toggled', self._change_mode_bt)

@@ -232,11 +232,16 @@ class MemorizeActivity(Activity):

     def write_file(self, file_path):
         logging.debug('WRITE_FILE is_demo %s', self.game.model.is_demo)
+
+        # We want to save the game-image for demo games too !!!
+        """
         if self.game.model.is_demo:
             # if is a demo game only want keep the metadata
             self._jobject.set_file_path(None)
             raise NotImplementedError
             return
+        """
+
         if self.cardlist.pair_list_modified:
             self.cardlist.update_model(self.game.model)

@@ -285,6 +290,13 @@ class MemorizeActivity(Activity):
         game_zip.close()
         self.metadata['mime_type'] = 'application/x-memorize-project'

+        # Store the game image as a string - that is simpler instead of
+        # having to deal with the dbus-converted list.
+        # When reading back, we use "eval" to convert the string into
+        # the correct type ("list" in this case).
+        self.metadata['saved_game_data_image'] = str(self.game.model.grid)
+        self.metadata['size'] = int(self.game.model.data['size'])
+
     def _complete_close(self):
         self._remove_temp_files()
         Activity._complete_close(self)
diff --git a/cardlist.py b/cardlist.py
index 6bc781c..65e44f1 100644
--- a/cardlist.py
+++ b/cardlist.py
@@ -40,8 +40,9 @@ class CardList(gtk.EventBox):
         'update-create-toolbar': (SIGNAL_RUN_FIRST, None, 3 * [TYPE_PYOBJECT]),
     }

-    def __init__(self):
+    def __init__(self, activity_instance):
         gtk.EventBox.__init__(self)
+        self._activity_instance = activity_instance
         self.pairs = []
         self.current_pair = None
         self.current_game_key = None
diff --git a/game.py b/game.py
index c5e54d1..9d21b4b 100644
--- a/game.py
+++ b/game.py
@@ -57,7 +57,7 @@ class MemorizeGame(GObject):
         'change-turn': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),
         }

-    def __init__(self):
+    def __init__(self, activity_instance):
         gobject.GObject.__init__(self)
         self.myself = None
         self.players_score = {}
@@ -70,7 +70,7 @@ class MemorizeGame(GObject):
         self.messenger = None
         self.sentitive = True

-        self.model = Model()
+        self.model = Model(activity_instance=activity_instance)
         self.flip_block = False
         self._flop_cards = None

@@ -109,6 +109,16 @@ class MemorizeGame(GObject):
         self.change_turn()
         self.model.data['running'] = 'False'

+        # Card 'state' is an aawesome field.
+        # Its takes on the following values ::
+        #
+        # 0                              ==>    for flopped cards.
+        #
+        # 1                              ==>    for flipped unmatched cards
+        #                                       (can be a maximum of 1 such card).
+        #
+        # <stroke_color>, <fill_color>   ==>    for flipped matched cards
+
         for card in self.model.grid:
             if card['state'] == '1':
                 self.emit('flip-card', self.model.grid.index(card), False)
diff --git a/memorizetoolbar.py b/memorizetoolbar.py
index 1a06da7..07ceecf 100644
--- a/memorizetoolbar.py
+++ b/memorizetoolbar.py
@@ -135,5 +135,5 @@ class MemorizeToolbarBuilder(gobject.GObject):
         self._demo_games.props.icon_name = 'memorize-collection'

     def update_toolbar(self, widget, data, grid):
-        size = data.get('size')
+        size = str(data.get('size'))
         self._size_combo.props.icon_name = size + ' X ' + size
diff --git a/model.py b/model.py
index 2567ed2..16239c4 100644
--- a/model.py
+++ b/model.py
@@ -100,11 +100,13 @@ class Model(object):
     information.
     '''

-    def __init__(self, game_path=None):
+    def __init__(self, game_path=None, activity_instance=None):
         tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance')
         self.temp_folder = tempfile.mkdtemp(dir=tmp_root)
         chmod(self.temp_folder, 0777)
+        self._saved_game_loaded = False

+        self._activity_instance = activity_instance
         self.data = {}

         if game_path is None:
@@ -366,6 +368,14 @@ class Model(object):
             temp1.extend(temp2)
             random.shuffle(temp1)
         self.grid = temp1
+
+        if not self._saved_game_loaded:
+            if self._activity_instance is not None:
+                if 'saved_game_data_image' in self._activity_instance.metadata.keys():
+                    self.grid = eval(self._activity_instance.metadata['saved_game_data_image'])
+                    self.data['size'] = int(self._activity_instance.metadata['size'])
+                    self._saved_game_loaded = True
+
         _logger.debug('Defgrid: grid( size=%s ): %s'
                       % (self.data['size'], self.grid))
         _logger.debug('Defgrid: data: %s', self.data)
--
1.7.11.7



More information about the Sugar-devel mailing list