<br><br><div class="gmail_quote">On Thu, Jan 10, 2013 at 9:56 PM, Walter Bender <span dir="ltr"><<a href="mailto:walter.bender@gmail.com" target="_blank">walter.bender@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Thu, Jan 10, 2013 at 11:02 AM, Ajay Garg <<a href="mailto:ajay@activitycentral.com">ajay@activitycentral.com</a>> wrote:<br>
><br>
><br>
> On Thu, Jan 10, 2013 at 8:49 PM, Gonzalo Odiard <<a href="mailto:gonzalo@laptop.org">gonzalo@laptop.org</a>> wrote:<br>
>><br>
>> Thanks Ajay and Ariel.<br>
>> I filled sl #4373 to follow this issue.<br>
>><br>
>> Comments:<br>
>> * Now you save the game data and the game session data every time. There<br>
>> are small games, like aritmetic or letters, but can be big if have images or<br>
>> sounds attached. Should be good save the data only if is a custom game.<br>
><br>
><br>
> Or may be ask the user  (while exiting), that whether the user wishes to<br>
> save the game?<br>
<br>
</div>Not a very Sugary approach.<br></blockquote><div><br><br>:D<br><br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class="im"><br>
><br>
><br>
>> * I think is better use json to persist a structure like a list instead of<br>
>> str/eval.<br>
><br>
><br>
> Hmm.. the  current approach kind of copies the game-image, thus making it<br>
> immune to any change in the game-state-image.<br>
> Also, having it persisted in any other form will just make the code too<br>
> bloatful and error-prone.<br>
<br>
</div>Not sure how json makes things either more bloated or more error-prone.<br></blockquote><div><br>Hmmm.. I was just a bit sceptical,  since we seem to be doing the "byte_arrays=True"  and "byte_arrays=False"  at different places.<br>
Having it as a string, will eliminate all such issues  :)<br><br><br><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
regards.<br>
<br>
-walter<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
><br>
>><br>
>><br>
>> I haven't tested this yet.<br>
>><br>
>> Gonzalo<br>
>><br>
>><br>
>> On Thu, Jan 10, 2013 at 11:44 AM, Ariel Calzada <<a href="mailto:ariel.calzada@gmail.com">ariel.calzada@gmail.com</a>><br>
>> wrote:<br>
>>><br>
>>> 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<br>
>>> 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'] =<br>
>>> 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 *<br>
>>> [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<br>
>>> cards<br>
>>> +        #                                       (can be a maximum of 1<br>
>>> such card).<br>
>>> +        #<br>
>>> +        # <stroke_color>, <fill_color>   ==>    for flipped matched<br>
>>> cards<br>
>>> +<br>
>>>          for card in self.model.grid:<br>
>>>              if card['state'] == '1':<br>
>>>                  self.emit('flip-card', self.model.grid.index(card),<br>
>>> 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<br>
>>> self._activity_instance.metadata.keys():<br>
>>> +                    self.grid =<br>
>>> eval(self._activity_instance.metadata['saved_game_data_image'])<br>
>>> +                    self.data['size'] =<br>
>>> 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>
>><br>
>><br>
><br>
><br>
><br>
> --<br>
> Regards,<br>
><br>
> Ajay Garg<br>
> Dextrose Developer<br>
> Activity Central: <a href="http://activitycentral.com" target="_blank">http://activitycentral.com</a><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>
><br>
<br>
<br>
<br>
--<br>
</div></div><span class="HOEnZb"><font color="#888888">Walter Bender<br>
Sugar Labs<br>
<a href="http://www.sugarlabs.org" target="_blank">http://www.sugarlabs.org</a><br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<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>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><font face="arial, sans-serif">Regards,<br><br>Ajay Garg</font><br style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><font face="arial, sans-serif">Dextrose Developer</font><br style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Activity Central: </span><a href="http://activitycentral.com/" style="font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)" target="_blank">http://activitycentral.com</a>