[gnome-games/applygsoc2009: 26/76] Clean up the process of opening game



commit 63b81e3a44e43237f69bfb49207c6df15e0dd9e0
Author: Pablo Castellano <pablog src gnome org>
Date:   Thu Aug 26 12:31:40 2010 +0200

    Clean up the process of opening game

 gnome-sudoku/src/lib/main.py |   41 ++++++++++++++++++++++++-----------------
 1 files changed, 24 insertions(+), 17 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index 04f2955..00a53e8 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -80,20 +80,21 @@ class UI (gconf_wrapper.GConfWrapper):
         self.worker_connections = []
         self.is_fullscreen = False
 
-		self._game = None
-		self._auto_load_saved()
-		if not self._game: # no game loaded
-		    self._auto_load_new()
-		if self._game:
-            self._post_open_setup()
-        else:
-			# TODO
-            print 'No game opened, should die'
+        self._auto_open_game()
 
         self.show()
 		# generate puzzles while our use is working...
 		self._run_background_generator()
 
+    def _auto_open_game(self):
+        game = self._auto_load_saved() or self._auto_load_new()
+        if game:
+            self._open_game(*game)
+            self._post_open_setup()
+        else:
+            logger.error("can't open game")
+            self.quit_cb()
+
 	def _auto_load_saved(self):
 		"""Automatically open a saved game
 	
@@ -101,20 +102,26 @@ class UI (gconf_wrapper.GConfWrapper):
         save *one* game in the future. And auto load this one game on launch"""
         saved_games = self.sudoku_tracker.list_saved_games()
         if saved_games:
-            self._game = saved_games[0]
-			self._open_game(game_selector.NewOrSavedGameSelector.SAVED_GAME,
-                self._game)
+		    return (game_selector.NewOrSavedGameSelector.SAVED_GAME,
+                    saved_games[0])
+        else:
+            logger.info("no saved game found")
+            return None
 
     def _auto_load_new(self):
         """Automatically open a new game"""
         # TODO should use preconfigured level
-		new_puzzle = self.sudoku_maker.get_puzzles(1, [self.gconf['game-level']])
+        level = self.gconf['game-level']
+        new_puzzle = self.sudoku_maker.get_puzzles(1, [level])
 		if new_puzzle:
 	        # get_puzzles returns a list of (<puzzle>, <difficulty>)
-    	    self._game = self.sudoku_maker.get_puzzles(1, ['easy'])[0][0]
-			self._open_game(game_selector.NewOrSavedGameSelector.NEW_GAME,
-                self._game)
-	
+		    logger.info("%s game generated" % level)
+            return (game_selector.NewOrSavedGameSelector.NEW_GAME,
+                    new_puzzle[0][0])
+        else:
+            logger.warning("failed to genereate new puzzle")
+            return None
+
     def _open_game(self, type, puzzle):
         """Finally enter the puzzle"""
         if type == game_selector.NewOrSavedGameSelector.NEW_GAME:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]