diff options
author | Walter Bender <walter.bender@gmail.com> | 2011-04-01 22:58:06 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2011-04-01 22:58:06 (GMT) |
commit | ab38ffec490a6f65af6ae8e0ae17e7bc37cdfd85 (patch) | |
tree | f274df728d8fdb586c4eea500775d33aac2e464b | |
parent | 311f1cae52ac3abfada53eb8221c35b4bc219c3b (diff) |
save/restore fixes -- needs to be tested with sharing
-rw-r--r-- | PathsActivity.py | 50 | ||||
-rw-r--r-- | deck.py | 6 | ||||
-rw-r--r-- | game.py | 32 | ||||
-rw-r--r-- | gettextutil.py | 24 | ||||
-rw-r--r-- | grid.py | 2 | ||||
-rw-r--r-- | hand.py | 2 | ||||
-rwxr-xr-x | path.py | 2 |
7 files changed, 81 insertions, 37 deletions
diff --git a/PathsActivity.py b/PathsActivity.py index a8db230..e8bd810 100644 --- a/PathsActivity.py +++ b/PathsActivity.py @@ -39,7 +39,7 @@ from dbus.gobject_service import ExportedGObject from sugar.presence import presenceservice from sugar.presence.tubeconn import TubeConnection -from gettext import gettext as _ +import gettextutil import locale import os.path @@ -137,21 +137,15 @@ class PathsActivity(activity.Activity): self._setup_presence_service() # Restore game state from Journal or start new game - # TODO: Sort out restore issues for sharer; - # Sort out init issues for joiner. if 'deck' in self.metadata: self._restore() - elif not hasattr(self, 'initiating'): - self._game.new_game() - elif not self.initiating: - self._game.new_game() - elif len(self._game.buddies) == 1: + else: self._game.new_game() def _setup_toolbars(self, have_toolbox): """ Setup the toolbars. """ - self.max_participants = 4 + self.max_participants = MAX_HANDS if have_toolbox: toolbox = ToolbarBox() @@ -241,6 +235,11 @@ class PathsActivity(activity.Activity): """ Write the grid status to the Journal """ if not hasattr(self, '_game'): return + for i in range(MAX_HANDS): + if 'hand-' + str(i) in self.metadata: + del self.metadata['hand-' + str(i)] + if 'robot' in self.metadata: + del self.metadata['robot'] self.metadata['deck'] = self._game.deck.serialize() self.metadata['grid'] = self._game.grid.serialize() if self._game.we_are_sharing(): @@ -251,12 +250,9 @@ class PathsActivity(activity.Activity): if self._game.playing_with_robot: self.metadata['hand-1'] = self._game.hands[1].serialize() self.metadata['robot'] = 'True' - else: - if 'hand-1' in self.metadata: - del self.metadata['hand-1'] - if 'robot' in self.metadata: - del self.metadata['robot'] + self.metadata['score'] = str(self._game.score) + self.metadata['index'] = str(self._game.deck.index) if self._game.last_spr_moved is not None and \ self._game.grid.spr_to_grid(self._game.last_spr_moved) is not None: self.metadata['last'] = str(self._game.grid.grid[ @@ -274,16 +270,25 @@ class PathsActivity(activity.Activity): for i in range(MAX_HANDS): if 'hand-' + str(i) in self.metadata: - if len(self._game.hands) < i + 1: # Add robot or shared hand? + # hand-0 is already appended + if i > 0: # Add robot or shared hand? self._game.hands.append( Hand(self._game.tile_width, self._game.tile_height, remote=True)) self._game.hands[i].restore(self.metadata['hand-' + str(i)], self._game.deck) - self._game.deck.index = ROW * COL - self._game.grid.tiles_in_grid() - for h in self._game.hands: - self._game.deck.index += (COL - h.tiles_in_hand()) + if 'index' in self.metadata: + print 'deck index', self.metadata['index'] + self._game.deck.index = int(self.metadata['index']) + else: + self._game.deck.index = ROW * COL - self._game.grid.tiles_in_grid() + for hand in self._game.hands: + self._game.deck.index += (COL - hand.tiles_in_hand()) + + if 'score' in self.metadata: + self._game.score = int(self.metadata['score']) + self.score.set_label(_('Score: ') + str(self._game.score)) self._game.last_spr_moved = None if 'last' in self.metadata: @@ -291,7 +296,7 @@ class PathsActivity(activity.Activity): for k in range(ROW * COL): if self._game.deck.tiles[k].number == j: self._game.last_spr_moved = self._game.deck.tiles[k].spr - return + break # Collaboration-related methods @@ -398,7 +403,8 @@ state=%d' % (id, initiator, type, service, params, state)) 'd': [self._sending_deck, 'sending deck'], 'h': [self._sending_hand, 'sending hand'], 'p': [self._play_a_piece, 'play a piece'], - 't': [self._take_a_turn, 'take a turn'] + 't': [self._take_a_turn, 'take a turn'], + 'g': [self._game_over, 'game over'] } def event_received_cb(self, event_message): @@ -440,6 +446,10 @@ state=%d' % (id, initiator, type, service, params, state)) if not self.initiating: self._game.new_game() + def _game_over(self, payload): + ''' Someone cannot plce a tile. ''' + self._game.game_over() + def _sending_deck(self, payload): ''' Sharer sends the deck. ''' self._game.deck.restore(payload) @@ -132,8 +132,12 @@ class Deck: deck = [] order = json_load(deck_as_text) for i in order: - deck.append(self.tiles[order[i]]) + # deck.append(self.tiles[order[i]]) + deck.append(self.tiles[i]) self.tiles = deck[:] + print 'restoring deck' + for i in range(COL * ROW): + print self.tiles[i].number def clear(self): ''' Remove any highlight from the tiles. ''' @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +#-*- coding: utf-8 -*- #Copyright (c) 2011 Walter Bender # This program is free software; you can redistribute it and/or modify @@ -119,7 +119,7 @@ class Game(): self.placed_a_tile = False self._there_are_errors = False - self._score = 0 + self.score = 0 self._score_card.set_layer(HIDE) self._score_card.move(((int(self._width / 2) - self.tile_width), int(self._height / 2) - self.tile_height)) @@ -181,10 +181,10 @@ class Game(): ''' Set the label in the toolbar or the window frame. ''' if self._running_sugar: self._activity.status.set_label(string) - self._activity.score.set_label(_('Score: ') + str(self._score)) + self._activity.score.set_label(_('Score: ') + str(self.score)) elif hasattr(self, 'win'): self.win.set_title('%s: %s [%d]' % (_('Paths'), string, - self._score)) + self.score)) def its_my_turn(self): # I need to play a piece... @@ -212,10 +212,10 @@ class Game(): self._activity.dialog_button.set_icon( 'media-playback-stop-insensitive') self._activity.dialog_button.set_tooltip(_('Game over')) - self._game_over() + self.game_over() elif self._initiating(): if self.deck.empty(): - self._game_over() + self.game_over() return if self.deck.tiles_remaining() < COL * len(self.buddies): number_of_tiles_to_deal = \ @@ -265,7 +265,7 @@ class Game(): elif not self.we_are_sharing(): if self.deck.empty() and \ self.hands[self._my_hand].tiles_in_hand() == 0: - self._game_over() + self.game_over() else: self.its_my_turn() elif self._initiating(): @@ -482,19 +482,19 @@ class Game(): self.hands[hand].hand[empty] = tile self.hands[hand].hand[i] = None - def _game_over(self, msg=_('Game over')): + def game_over(self, msg=_('Game over')): ''' Nothing left to do except show the results. ''' self._set_label(msg) if self.hands[self._my_hand].tiles_in_hand() == 0: - self._score += 50 # Bonus points + self.score += 50 # Bonus points else: for tile in self.hands[self._my_hand].hand: if tile is not None: - self._score -= 2 * tile.get_value() # Penalty + self.score -= 2 * tile.get_value() # Penalty self._shuffle_up(self._my_hand) if self._running_sugar: - self._activity.score.set_label(_('Score: ') + str(self._score)) - self._score_card.set_label(str(self._score)) + self._activity.score.set_label(_('Score: ') + str(self.score)) + self._score_card.set_label(str(self.score)) self._score_card.set_layer(OVER_THE_TOP) self._score_card.move((int(self.tile_width / 2), int(self._height / 2) + 2 * self.tile_height)) @@ -507,6 +507,8 @@ class Game(): (self.grid.left_hand + self.grid.xinc, y)) if self._running_sugar: self._activity.set_robot_status(False, 'robot-off') + elif self.we_are_sharing(): + self._activity.send_event('g| ') def show_connected_tiles(self): ''' Highlight the squares that surround the tiles already on the grid. @@ -552,7 +554,7 @@ class Game(): pos=self.grid.grid_to_xy(order[i])) return # Nowhere to play. - self._game_over(_('Nowhere to play.')) + self.game_over(_('Nowhere to play.')) def _robot_play(self): ''' The robot tries random tiles in random locations. ''' @@ -571,7 +573,7 @@ class Game(): return # If we didn't return above, we were unable to play a tile. - self._game_over(_('Robot unable to play')) + self.game_over(_('Robot unable to play')) def _try_placement(self, tile, i): ''' Try to place a tile at grid posiion i. Rotate it, if necessary. ''' @@ -613,7 +615,7 @@ class Game(): if not break_in_path[p] and len(self._paths[p]) > 0: for i in self._paths[p]: self.grid.grid[i[0]].set_shape(i[1]) - self._score += self.grid.grid[i[0]].get_value() + self.score += self.grid.grid[i[0]].get_value() def _tile_to_test(self, test_path): ''' Find a tile that needs testing. ''' diff --git a/gettextutil.py b/gettextutil.py new file mode 100644 index 0000000..ebeebb3 --- /dev/null +++ b/gettextutil.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2010-11 Walter Bender, Martin Langhoff +# License: GPLv2 + +# Defines the magic global _() with the right params so all modules +# can use it. +# +# Plugins that want to override MUST use a different technique. See +# the developer notes in the TA wikipage. +# +import gettext +import os + +# In a git checkout, locale is in the root of the project +# so one dir "up" from tagettext.py +localedir = os.path.join(os.path.dirname(os.path.dirname(__file__)), + 'locale' ) + +if os.path.exists(localedir): + # works from a git checkout + gettext.install('org.laptop.TurtleArtActivity', localedir) +else: + # fallback for packaged TA (rpm, xo) + gettext.install('org.laptop.TurtleArtActivity') @@ -64,12 +64,14 @@ class Grid: ''' Restore tiles to grid upon resume or share. ''' self.hide() grid = json_load(grid_as_text) + print 'restoring grid' for i in range(ROW * COL): if grid[i][0] is None: self.grid[i] = None else: for k in range(ROW * COL): if deck.tiles[k].number == grid[i][0]: + print k, '-->', i self.add_tile_to_grid(k, grid[i][1], i, deck) break self.show() @@ -79,6 +79,7 @@ class Hand: def restore(self, hand_as_text, deck, buddy=False): ''' Restore tiles to hand upon resume or share. ''' hand = json_load(hand_as_text) + print 'restoring hand' if buddy: offset = 1 # skip the buddy else: @@ -90,6 +91,7 @@ class Hand: else: for k in range(ROW * COL): if deck.tiles[k].number == hand[i]: + print k, '-->', tile self.hand[tile] = deck.tiles[k] self.hand[tile].spr.move(self.hand_to_xy(tile)) self.hand[tile].spr.set_layer(TILES) @@ -14,7 +14,7 @@ import gtk -from gettext import gettext as _ +import gettextutil import os from game import Game |