Ticket #4373: save_game_state.patch

File save_game_state.patch, 11.2 KB (added by godiard, 10 years ago)

Proposed patch

  • activity.py

                                                                                                                                                                                                                                                                   
    Delivered-To: godiard@gmail.com
    Received: by 10.204.155.92 with SMTP id r28csp91523bkw;
            Thu, 10 Jan 2013 06:44:44 -0800 (PST)
    X-Received: by 10.224.186.82 with SMTP id cr18mr58044098qab.64.1357829083499;
            Thu, 10 Jan 2013 06:44:43 -0800 (PST)
    Return-Path: <sugar-devel-bounces@lists.sugarlabs.org>
    Received: from sunjammer.sugarlabs.org ([2001:4830:134:7::11])
            by mx.google.com with ESMTP id o18si750450qct.44.2013.01.10.06.44.42;
            Thu, 10 Jan 2013 06:44:43 -0800 (PST)
    Received-SPF: neutral (google.com: 2001:4830:134:7::11 is neither permitted nor denied by best guess record for domain of sugar-devel-bounces@lists.sugarlabs.org) client-ip=2001:4830:134:7::11;
    Authentication-Results: mx.google.com;
           spf=neutral (google.com: 2001:4830:134:7::11 is neither permitted nor denied by best guess record for domain of sugar-devel-bounces@lists.sugarlabs.org) smtp.mail=sugar-devel-bounces@lists.sugarlabs.org;
           dkim=neutral (body hash did not verify) header.i=@gmail.com
    Received: from sunjammer.sugarlabs.org (localhost [127.0.0.1])
    	by sunjammer.sugarlabs.org (Postfix) with ESMTP id BB874120546;
    	Thu, 10 Jan 2013 09:44:40 -0500 (EST)
    Received: from mail-qc0-f181.google.com (mail-qc0-f181.google.com
    	[209.85.216.181])
    	by sunjammer.sugarlabs.org (Postfix) with ESMTPS id 0BB7A120734
    	for <sugar-devel@lists.sugarlabs.org>;
    	Thu, 10 Jan 2013 09:44:34 -0500 (EST)
    Received: by mail-qc0-f181.google.com with SMTP id x40so390723qcp.26
    	for <sugar-devel@lists.sugarlabs.org>;
    	Thu, 10 Jan 2013 06:44:34 -0800 (PST)
    DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
    	h=x-received:from:to:cc:subject:date:message-id:x-mailer:organization; 
    	bh=5BqCKrLa8MnvaDsD4xTjj8J3WZDUfLJZ3bUXuXpD9OY=;
    	b=IFgwkSpCVn4J7zfEZKRfZBvgGPgKCe7Q5j5SquZ3hxtYl1H9+mfr3MztquBccOkzzI
    	NrQCfekpzNyvJBKgETPEkX8lVePoAr8G20mKGgVlLwl+9opUBFNZJPgsPaZL9JeI9q/e
    	D7vSm+9ikjXOeuBQn10jKa2PNOu4aRcRKHZS15/LCqgn3etUkaEWGLFV8/qu7a4Vllws
    	foJJB/ONcun/Ap3Awak8jDWj2koAbDuiBkq8QlugYyrVZLH2sS3BCZ2so5Ceu1KiBPZ7
    	w5CXOxGhRbFpIyoe0/CF3lwh7+I1JJdKcVI7+v4JF3JtD9IXXbaqTqMv+yLCsVwt1lXd
    	KpJA==
    X-Received: by 10.224.222.15 with SMTP id ie15mr22464683qab.75.1357829074058; 
    	Thu, 10 Jan 2013 06:44:34 -0800 (PST)
    Received: from localhost.localdomain ([190.99.138.244])
    	by mx.google.com with ESMTPS id cy2sm1223590qeb.9.2013.01.10.06.44.31
    	(version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
    	Thu, 10 Jan 2013 06:44:33 -0800 (PST)
    From: Ariel Calzada <ariel.calzada@gmail.com>
    To: ariel@activitycentral.com
    Date: Thu, 10 Jan 2013 09:44:18 -0500
    Message-Id: <1357829058-13107-1-git-send-email-ariel@acivitycentral.com>
    X-Mailer: git-send-email 1.7.10.4
    Organization: Sugar Labs Foundation
    X-Spam-Status: No, score=-0.7 required=3.5 tests=FREEMAIL_FROM,
    	RCVD_IN_DNSWL_LOW, SPF_PASS,
    	T_DKIM_INVALID autolearn=unavailable version=3.3.2
    X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on
    	sunjammer.sugarlabs.org
    X-Topics: Patches and patch reviews
    Cc: Ajay Garg <ajay@activitycentral.com>, team@lists.activitycentral.com,
    	sugar-devel@lists.sugarlabs.org, ariel.calzada@gmail.com
    Subject: [Sugar-devel] [PATCH] SDXO#2620 Add ability to save unfinished
    	games in Memorize.
    X-BeenThere: sugar-devel@lists.sugarlabs.org
    X-Mailman-Version: 2.1.13
    Precedence: list
    List-Id: "Discussion of Sugar development and other technical matters."
    	<sugar-devel.lists.sugarlabs.org>
    List-Unsubscribe: <http://lists.sugarlabs.org/options/sugar-devel>,
    	<mailto:sugar-devel-request@lists.sugarlabs.org?subject=unsubscribe>
    List-Archive: <http://lists.sugarlabs.org/archive/sugar-devel>
    List-Post: <mailto:sugar-devel@lists.sugarlabs.org>
    List-Help: <mailto:sugar-devel-request@lists.sugarlabs.org?subject=help>
    List-Subscribe: <http://lists.sugarlabs.org/listinfo/sugar-devel>,
    	<mailto:sugar-devel-request@lists.sugarlabs.org?subject=subscribe>
    MIME-Version: 1.0
    Content-Type: text/plain; charset="us-ascii"
    Content-Transfer-Encoding: 7bit
    Sender: sugar-devel-bounces@lists.sugarlabs.org
    Errors-To: sugar-devel-bounces@lists.sugarlabs.org
    
    From: Ajay Garg <ajay@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@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 b class MemorizeActivity(Activity): 
    107107        # Play game mode
    108108        self.table = cardtable.CardTable()
    109109        self.scoreboard = scoreboard.Scoreboard()
    110         self.cardlist = cardlist.CardList()
     110        self.cardlist = cardlist.CardList(self)
    111111        self.createcardpanel = createcardpanel.CreateCardPanel()
    112112        self.cardlist.connect('pair-selected',
    113113                self.createcardpanel.pair_selected)
    class MemorizeActivity(Activity): 
    127127                self._memorizeToolbarBuilder.reset)
    128128        self._createToolbarBuilder.connect('create_equal_pairs',
    129129                self.change_equal_pairs)
    130         self.game = game.MemorizeGame()
     130        self.game = game.MemorizeGame(self)
    131131
    132132        self._edit_button.connect('toggled', self._change_mode_bt)
    133133
    class MemorizeActivity(Activity): 
    232232
    233233    def write_file(self, file_path):
    234234        logging.debug('WRITE_FILE is_demo %s', self.game.model.is_demo)
     235
     236        # We want to save the game-image for demo games too !!!
     237        """
    235238        if self.game.model.is_demo:
    236239            # if is a demo game only want keep the metadata
    237240            self._jobject.set_file_path(None)
    238241            raise NotImplementedError
    239242            return
     243        """
     244
    240245        if self.cardlist.pair_list_modified:
    241246            self.cardlist.update_model(self.game.model)
    242247
    class MemorizeActivity(Activity): 
    285290        game_zip.close()
    286291        self.metadata['mime_type'] = 'application/x-memorize-project'
    287292
     293        # Store the game image as a string - that is simpler instead of
     294        # having to deal with the dbus-converted list.
     295        # When reading back, we use "eval" to convert the string into
     296        # the correct type ("list" in this case).
     297        self.metadata['saved_game_data_image'] = str(self.game.model.grid)
     298        self.metadata['size'] = int(self.game.model.data['size'])
     299
    288300    def _complete_close(self):
    289301        self._remove_temp_files()
    290302        Activity._complete_close(self)
  • cardlist.py

    diff --git a/cardlist.py b/cardlist.py
    index 6bc781c..65e44f1 100644
    a b class CardList(gtk.EventBox): 
    4040        'update-create-toolbar': (SIGNAL_RUN_FIRST, None, 3 * [TYPE_PYOBJECT]),
    4141    }
    4242
    43     def __init__(self):
     43    def __init__(self, activity_instance):
    4444        gtk.EventBox.__init__(self)
     45        self._activity_instance = activity_instance
    4546        self.pairs = []
    4647        self.current_pair = None
    4748        self.current_game_key = None
  • game.py

    diff --git a/game.py b/game.py
    index c5e54d1..9d21b4b 100644
    a b class MemorizeGame(GObject): 
    5757        'change-turn': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),
    5858        }
    5959
    60     def __init__(self):
     60    def __init__(self, activity_instance):
    6161        gobject.GObject.__init__(self)
    6262        self.myself = None
    6363        self.players_score = {}
    class MemorizeGame(GObject): 
    7070        self.messenger = None
    7171        self.sentitive = True
    7272
    73         self.model = Model()
     73        self.model = Model(activity_instance=activity_instance)
    7474        self.flip_block = False
    7575        self._flop_cards = None
    7676
    class MemorizeGame(GObject): 
    109109        self.change_turn()
    110110        self.model.data['running'] = 'False'
    111111
     112        # Card 'state' is an aawesome field.
     113        # Its takes on the following values ::
     114        #
     115        # 0                              ==>    for flopped cards.
     116        #
     117        # 1                              ==>    for flipped unmatched cards
     118        #                                       (can be a maximum of 1 such card).
     119        #
     120        # <stroke_color>, <fill_color>   ==>    for flipped matched cards
     121
    112122        for card in self.model.grid:
    113123            if card['state'] == '1':
    114124                self.emit('flip-card', self.model.grid.index(card), False)
  • memorizetoolbar.py

    diff --git a/memorizetoolbar.py b/memorizetoolbar.py
    index 1a06da7..07ceecf 100644
    a b class MemorizeToolbarBuilder(gobject.GObject): 
    135135        self._demo_games.props.icon_name = 'memorize-collection'
    136136
    137137    def update_toolbar(self, widget, data, grid):
    138         size = data.get('size')
     138        size = str(data.get('size'))
    139139        self._size_combo.props.icon_name = size + ' X ' + size
  • model.py

    diff --git a/model.py b/model.py
    index 2567ed2..16239c4 100644
    a b class Model(object): 
    100100    information.
    101101    '''
    102102
    103     def __init__(self, game_path=None):
     103    def __init__(self, game_path=None, activity_instance=None):
    104104        tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance')
    105105        self.temp_folder = tempfile.mkdtemp(dir=tmp_root)
    106106        chmod(self.temp_folder, 0777)
     107        self._saved_game_loaded = False
    107108
     109        self._activity_instance = activity_instance
    108110        self.data = {}
    109111
    110112        if game_path is None:
    class Model(object): 
    366368            temp1.extend(temp2)
    367369            random.shuffle(temp1)
    368370        self.grid = temp1
     371
     372        if not self._saved_game_loaded:
     373            if self._activity_instance is not None:
     374                if 'saved_game_data_image' in self._activity_instance.metadata.keys():
     375                    self.grid = eval(self._activity_instance.metadata['saved_game_data_image'])
     376                    self.data['size'] = int(self._activity_instance.metadata['size'])
     377                    self._saved_game_loaded = True
     378
    369379        _logger.debug('Defgrid: grid( size=%s ): %s'
    370380                      % (self.data['size'], self.grid))
    371381        _logger.debug('Defgrid: data: %s', self.data)