[gnome-games/sudoku-tube] Save game
- From: Zhang Sen <zhangsen src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnome-games/sudoku-tube] Save game
- Date: Sat, 25 Jul 2009 03:36:24 +0000 (UTC)
commit f28b27dfb94e8c4caa3c218aa6257b2795c8530b
Author: Zhang Sen <zh jesse gmail com>
Date: Sat Jul 25 12:22:46 2009 +0800
Save game
gnome-sudoku/src/lib/main.py | 47 ++++++++++++++++++++++++++++------------
gnome-sudoku/src/lib/model.py | 7 ++++++
gnome-sudoku/src/lib/saver.py | 25 ++-------------------
gnome-sudoku/src/lib/timer.py | 14 ++++++++++++
gnome-sudoku/src/lib/undo.py | 9 +++++--
gnome-sudoku/src/lib/view.py | 32 +++++++++++++++++++++------
6 files changed, 88 insertions(+), 46 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index 9952061..8e930c6 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -361,7 +361,7 @@ class UI (gconf_wrapper.GConfWrapper):
# increase difficulty for next time.
self.gconf['difficulty'] = self.gconf['difficulty'] + 0.1
self.timer.finish_timing()
-# self.sudoku_tracker.finish_game(self)
+ self.sudoku_tracker.finish_game(self._jar_game())
sublabel = _("You completed the puzzle in %(totalTime)s (%(activeTime)s active)") % {'totalTime': self.timer.total_time_string(),
'activeTime': self.timer.active_time_string()
}
@@ -398,6 +398,9 @@ class UI (gconf_wrapper.GConfWrapper):
def new_cb (self, *args):
if self._close_current_game():
+ self._auto_open_game()
+ return
+
choice = self._select_game()
if choice:
self._open_game(choice[0], choice[1])
@@ -423,15 +426,15 @@ class UI (gconf_wrapper.GConfWrapper):
"""Close current running game
Return True if game is closed, or else return False"""
- if (self.gsd.grid and self.gsd.grid.is_changed() and not self.won):
+ if (self._main_model.grid.is_changed() and not self.won):
try:
if dialog_extras.getBoolean(
label = _("Save current game?"),
custom_yes = _("_Save"),
custom_no = _("_Abandon")):
- self.save_game()
+ self._save_game()
else:
- self.sudoku_tracker.abandon_game(self)
+ self.sudoku_tracker.abandon_game(self._jar_game())
except dialog_extras.UserCancelledError:
# User cancelled; current game is not closed
return False
@@ -440,9 +443,10 @@ class UI (gconf_wrapper.GConfWrapper):
def do_stop (self):
self.stop_dancer()
- self.gsd.grid = None
- self.tracker_ui.reset()
- self.history.clear()
+ self._main_model = None
+ self._main_grid_vew.reset()
+ self._tracker_ui.reset()
+ self._history_manager.clear()
self.won = False
def resize_cb (self, widget, event):
@@ -451,10 +455,9 @@ class UI (gconf_wrapper.GConfWrapper):
def quit_cb (self, *args):
self.w.hide()
- if (self._main_model.grid
- and self._main_model.grid.is_changed()
- and (not self.won)):
- self.save_game(self)
+ if (self._main_model and self._main_model.grid.is_changed()
+ and (not self.won)):
+ self._save_game()
# make sure we really go away before doing our saving --
# otherwise we appear sluggish.
while gtk.events_pending():
@@ -471,8 +474,24 @@ class UI (gconf_wrapper.GConfWrapper):
except RuntimeError:
pass
- def save_game (self, *args):
- self.sudoku_tracker.save_game(self)
+ def _save_game(self):
+ self.sudoku_tracker.save_game(self._jar_game())
+
+ def _jar_game(self):
+ jar = {} # what we will pickle
+ self.timer.finish_timing()
+ jar["game"] = self._main_model.grid.to_string()
+ jar["trackers"] = self._main_grid_vew.get_trackers()
+ jar["tracking"] = self._main_grid_vew.get_trackers_tracking()
+ jar["notes"] = self._notes_model.get_all_notes()
+ jar["gsd.hints"] = self._notes_model.get_hints_count()
+ jar["gsd.auto_fills"] = self._main_model.get_autofill_count()
+ jar["timer.__absolute_start_time__"] = \
+ self.timer.get_absolute_start_time()
+ jar["timer.active_time"] = self.timer.get_active_time()
+ jar["timer.total_time"] = self.timer.get_total_time()
+
+ return jar
def full_screen_cb (self, *args):
if self.is_fullscreen:
@@ -582,7 +601,7 @@ class UI (gconf_wrapper.GConfWrapper):
# this is called on a regular loop and will autosave if we
# have reason to...
if self.gsd.grid and self.gsd.grid.is_changed() and not self.won:
- self.sudoku_tracker.save_game(self)
+ self._save_game()
return True
def show_about (self, *args):
diff --git a/gnome-sudoku/src/lib/model.py b/gnome-sudoku/src/lib/model.py
index 78f70f4..ddb5039 100644
--- a/gnome-sudoku/src/lib/model.py
+++ b/gnome-sudoku/src/lib/model.py
@@ -106,6 +106,13 @@ class NotesModel:
change = (x, y, top_note, bottom_note)
self._signal_observers([change])
+ def get_all_notes(self):
+ result = []
+ for (x, y) in self._notes:
+ top, bottom = self._notes[(x, y)]
+ result.append((x, y, top, bottom))
+ return result
+
def update(self, changes):
if not self._always_show_hints:
return
diff --git a/gnome-sudoku/src/lib/saver.py b/gnome-sudoku/src/lib/saver.py
index 0477c92..a5d3438 100644
--- a/gnome-sudoku/src/lib/saver.py
+++ b/gnome-sudoku/src/lib/saver.py
@@ -45,21 +45,6 @@ def super_setattr (obj, attr, val):
obj = getattr(obj, a)
setattr(obj, attr, val)
-def jar_game (ui):
- jar = {} # what we will pickle
- ui.timer.finish_timing()
- jar['game'] = ui.main_model.grid.to_string()
- jar['trackers'] = ui.gsd.trackers
- jar['tracking'] = ui.gsd.__trackers_tracking__
- jar['notes'] = []
- for e in ui.gsd.__entries__.values():
- top, bot = e.get_note_text()
- if top or bot:
- jar['notes'].append((e.x, e.y, top, bot))
- for attr in SAVE_ATTRIBUTES:
- jar[attr] = super_getattr(ui, attr)
- return jar
-
def set_value_from_jar (dest, jar):
for attr in SAVE_ATTRIBUTES:
super_setattr(dest, attr, jar[attr])
@@ -104,9 +89,7 @@ class SudokuTracker:
def get_filename (self, gamestring):
return gamestring.split('\n')[0].replace(' ', '')
- def save_game (self, ui):
- return
- jar = jar_game(ui)
+ def save_game (self, jar):
filename = os.path.join(self.save_path, self.get_filename(jar['game']))
try:
outfi = file(filename, 'w')
@@ -125,8 +108,7 @@ class SudokuTracker:
})
)
- def finish_game (self, ui):
- jar = jar_game(ui)
+ def finish_game(self, jar):
self.finish_jar(jar)
def finish_jar (self, jar):
@@ -174,8 +156,7 @@ class SudokuTracker:
if os.path.exists(previously_saved_game):
os.remove(os.path.join(previously_saved_game))
- def abandon_game (self, ui):
- jar = jar_game(ui)
+ def abandon_game(self, jar):
self.remove_from_saved_games(jar)
def list_saved_games (self):
diff --git a/gnome-sudoku/src/lib/timer.py b/gnome-sudoku/src/lib/timer.py
index dfaa4fa..7a72cd8 100644
--- a/gnome-sudoku/src/lib/timer.py
+++ b/gnome-sudoku/src/lib/timer.py
@@ -140,6 +140,20 @@ class ActiveTimer (gobject.GObject):
if self.active_time > self.total_time:
self.total_time = self.active_time
+ #
+ # the following should only be called _after_ finish_timing; and yes some
+ # methods should be useless
+ #
+
+ def get_active_time(self):
+ return self.active_time
+
+ def get_absolute_start_time(self):
+ return self.__absolute_start_time__
+
+ def get_total_time(self):
+ return self.total_time
+
def active_time_string (self):
return format_time(self.active_time)
diff --git a/gnome-sudoku/src/lib/undo.py b/gnome-sudoku/src/lib/undo.py
index fa9ec0c..7516297 100644
--- a/gnome-sudoku/src/lib/undo.py
+++ b/gnome-sudoku/src/lib/undo.py
@@ -2,11 +2,9 @@
class HistoryManager:
def __init__(self, undo_action, redo_action):
- self._history = []
- self._next_undo = -1
self._undo_action = undo_action
self._redo_action = redo_action
- self._update_gui()
+ self.clear()
def execute_command(self, cmd):
# first clear any undone items from the history list
@@ -37,6 +35,11 @@ class HistoryManager:
self._next_undo += 1
self._update_gui()
+ def clear(self):
+ self._history = []
+ self._next_undo = -1
+ self._update_gui()
+
def _trim_history_list(self):
"""Purge all undone commands from the history list
"""
diff --git a/gnome-sudoku/src/lib/view.py b/gnome-sudoku/src/lib/view.py
index c581555..33d9a19 100644
--- a/gnome-sudoku/src/lib/view.py
+++ b/gnome-sudoku/src/lib/view.py
@@ -39,6 +39,7 @@ def _generate_new_color():
class _Tracker:
def __init__(self, sudoku_view):
self._trackers = {} # id => [coords]
+ # FIXME what a bad name
self.__trackers_tracking__ = {} # id => True/False
self._tracker_by_coords = {} # coords => id
self._sudoku_view = sudoku_view
@@ -95,6 +96,11 @@ class _Tracker:
TRACKER_COLORS.append(new_color)
return self.get_tracker_color(identifier)
+ def get_trackers(self):
+ return self._trackers
+
+ def get_trackers_tracking(self):
+ return self.__trackers_tracking__
def gtkcolor_to_rgb (color):
return (color.red / float(2**16),
@@ -175,13 +181,6 @@ class SudokuNumberGrid (gtk.AspectFrame):
def get_value(self, x, y):
return self.__entries__[(x, y)].get_value()
- def set_value(self, x, y, value):
- if not value:
- tid = self._tracker.get_tracker_by_coord(x, y)
- if tid is not None:
- self._tracker.remove_from_tracker(tid, x, y)
- self.__entries__[(x, y)].set_value(value)
-
def set_readonly_appearance(self, x, y, flag):
self.__entries__[(x, y)].set_read_only(flag)
@@ -226,6 +225,12 @@ class SudokuView(SudokuNumberGrid, gobject.GObject):
self._tracker = _Tracker(self)
+ def reset(self):
+ for x in range(self.group_size):
+ for y in range(self.group_size):
+ self.set_value(x, y, 0)
+ self.set_readonly_appearance(x, y, False)
+
def connect_to_model(self, model):
if not model:
return
@@ -255,6 +260,13 @@ class SudokuView(SudokuNumberGrid, gobject.GObject):
self.set_color(x, y,
self._refresh_color(x, y, conflict, is_new_value))
+ def set_value(self, x, y, value):
+ if not value:
+ tid = self._tracker.get_tracker_by_coord(x, y)
+ if tid is not None:
+ self._tracker.remove_from_tracker(tid, x, y)
+ self.__entries__[(x, y)].set_value(value)
+
def _refresh_color(self, x, y, conflict, is_new_value):
"""Set the color according to several rules, conflict/tracker, etc
"""
@@ -383,3 +395,9 @@ class SudokuView(SudokuNumberGrid, gobject.GObject):
def get_tracker_controller(self):
return self._tracker
+
+ def get_trackers(self):
+ return self._tracker.get_trackers()
+
+ def get_trackers_tracking(self):
+ return self._tracker.get_trackers_tracking()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]