From 8bb27f919c360a260a8181232cd933846e2fe35e Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sat, 17 May 2014 12:46:40 +0200 Subject: [PATCH] Factor out game saving and loading for re-use with --load. --- naja/scenes/load_save.py | 64 +++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/naja/scenes/load_save.py b/naja/scenes/load_save.py index 4bdd71b..c021ea7 100644 --- a/naja/scenes/load_save.py +++ b/naja/scenes/load_save.py @@ -28,6 +28,39 @@ def ensure_save_path_exists(): os.makedirs(location) +class SaveGame(object): + def __init__(self, slot_num): + self.slot_num = slot_num + + def save_path(self): + return save_path('slot_%s.json' % (self.slot_num,)) + + def get_game_data(self): + try: + with open(self.save_path(), 'r') as save_file: + return json.load(save_file) + except IOError: + return None + except Exception as e: + print "Error reading savegame in slot %s: %s" % (self.slot_num, e) + return None + + def save(self, state): + ensure_save_path_exists() + save_data = { + 'timestamp': datetime.now().ctime(), + 'data': state.gameboard.export(), + } + with open(self.save_path(), 'w') as save_file: + json.dump(save_data, save_file) + + def load(self): + game_data = self.get_game_data() + if game_data is None: + return + return GameState.load(game_data['data']) + + class LoadSaveGameBase(Scene): def __init__(self, state): super(LoadSaveGameBase, self).__init__(state) @@ -40,21 +73,8 @@ class LoadSaveGameBase(Scene): self.slots[slot_num] = slot selector.add(slot) - def save_path(self, slot_num): - return save_path('slot_%s.json' % (slot_num,)) - - def get_game_data(self, slot_num): - try: - with open(self.save_path(slot_num), 'r') as save_file: - return json.load(save_file) - except IOError: - return None - except Exception as e: - print "Error reading savegame in slot %s: %s" % (slot_num, e) - return None - def make_slot_widget(self, slot_num): - game_data = self.get_game_data(slot_num) + game_data = SaveGame(slot_num).get_game_data() y_offset = 74 * slot_num slot = SaveSlotWidget((100, y_offset), slot_num, game_data) slot.add_callback('click', lambda event: self.perform_action(slot_num)) @@ -72,9 +92,6 @@ class LoadSaveGameBase(Scene): def succeed(self): raise NotImplementedError("Success not implemented.") - from naja.scenes.menu import MenuScene - sound.play_sound('chirp.ogg', volume=0.5) - SceneChangeEvent.post(MenuScene) def perform_action(self, slot_num): raise NotImplementedError("Nothing to see here.") @@ -82,14 +99,8 @@ class LoadSaveGameBase(Scene): class SaveGameScene(LoadSaveGameBase): def perform_action(self, slot_num): - save_data = { - 'timestamp': datetime.now().ctime(), - 'data': self.state.gameboard.export(), - } try: - ensure_save_path_exists() - with open(self.save_path(slot_num), 'w') as save_file: - json.dump(save_data, save_file) + SaveGame(slot_num).save(self.state) except Exception as e: print "Error saving game in slot %s: %s" % (slot_num, e) self.fail() @@ -104,9 +115,8 @@ class SaveGameScene(LoadSaveGameBase): class LoadGameScene(LoadSaveGameBase): def perform_action(self, slot_num): - game_data = self.get_game_data(slot_num) - if game_data is not None: - state = GameState.load(game_data['data']) + state = SaveGame(slot_num).load() + if state is not None: LoadGameEvent.post(state) self.succeed() else: -- 2.34.1