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

Walter Bender walter.bender at gmail.com
Thu Jan 10 11:26:45 EST 2013


On Thu, Jan 10, 2013 at 11:02 AM, Ajay Garg <ajay at activitycentral.com> wrote:
>
>
> On Thu, Jan 10, 2013 at 8:49 PM, Gonzalo Odiard <gonzalo at laptop.org> wrote:
>>
>> Thanks Ajay and Ariel.
>> I filled sl #4373 to follow this issue.
>>
>> Comments:
>> * 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.
>
>
> Or may be ask the user  (while exiting), that whether the user wishes to
> save the game?

Not a very Sugary approach.

>
>
>> * I think is better use json to persist a structure like a list instead of
>> str/eval.
>
>
> Hmm.. the  current approach kind of copies the game-image, thus making it
> immune to any change in the game-state-image.
> Also, having it persisted in any other form will just make the code too
> bloatful and error-prone.

Not sure how json makes things either more bloated or more error-prone.

regards.

-walter

>
>
>>
>>
>> I haven't tested this yet.
>>
>> Gonzalo
>>
>>
>> On Thu, Jan 10, 2013 at 11:44 AM, Ariel Calzada <ariel.calzada at gmail.com>
>> wrote:
>>>
>>> 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
>>>
>>> _______________________________________________
>>> Sugar-devel mailing list
>>> Sugar-devel at lists.sugarlabs.org
>>> http://lists.sugarlabs.org/listinfo/sugar-devel
>>
>>
>
>
>
> --
> Regards,
>
> Ajay Garg
> Dextrose Developer
> Activity Central: http://activitycentral.com
> _______________________________________________
> Sugar-devel mailing list
> Sugar-devel at lists.sugarlabs.org
> http://lists.sugarlabs.org/listinfo/sugar-devel
>



--
Walter Bender
Sugar Labs
http://www.sugarlabs.org


More information about the Sugar-devel mailing list