Package horizons :: Module spsession
[hide private]
[frames] | no frames]

Source Code for Module horizons.spsession

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  import random 
23   
24  from horizons.constants import SINGLEPLAYER 
25  from horizons.i18n import gettext as T 
26  from horizons.manager import SPManager 
27  from horizons.savegamemanager import SavegameManager 
28  from horizons.session import Session 
29  from horizons.timer import Timer 
30   
31   
32 -class SPSession(Session):
33 """Session tailored for singleplayer games.""" 34
35 - def create_manager(self):
36 return SPManager(self)
37
38 - def create_rng(self, seed=None):
39 return random.Random(seed if seed is not None else SINGLEPLAYER.SEED)
40
41 - def create_timer(self):
42 return Timer(freeze_protection=SINGLEPLAYER.FREEZE_PROTECTION)
43
44 - def load(self, *args, **kwargs):
45 super().load(*args, **kwargs) 46 # single player games start right away 47 self.start()
48
49 - def autosave(self):
50 """Called automatically in an interval""" 51 self.log.debug("Session: autosaving") 52 success = self._do_save(SavegameManager.create_autosave_filename()) 53 if success: 54 SavegameManager.delete_dispensable_savegames(autosaves=True) 55 self.ingame_gui.message_widget.add('AUTOSAVE')
56
57 - def quicksave(self):
58 """Called when user presses the quicksave hotkey""" 59 self.log.debug("Session: quicksaving") 60 # call saving through horizons.main and not directly through session, so that save errors are handled 61 success = self._do_save(SavegameManager.create_quicksave_filename()) 62 if success: 63 SavegameManager.delete_dispensable_savegames(quicksaves=True) 64 self.ingame_gui.message_widget.add('QUICKSAVE') 65 else: 66 headline = T("Failed to quicksave.") 67 descr = T("An error happened during quicksave.") + "\n" + T("Your game has not been saved.") 68 advice = T("If this error happens again, please contact the development team: " 69 "{website}").format(website="http://unknown-horizons.org/support/") 70 self.ingame_gui.open_error_popup(headline, descr, advice)
71
72 - def save(self, savegamename=None):
73 """Saves a game 74 @param savegamename: string with the full path of the savegame file or None to let user pick one 75 @return: bool, whether no error happened (user aborting dialog means success) 76 """ 77 if savegamename is None: 78 savegamename = self.ingame_gui.show_select_savegame(mode='save') 79 if savegamename is None: 80 return True # user aborted dialog 81 savegamename = SavegameManager.create_filename(savegamename) 82 83 success = self._do_save(savegamename) 84 if success: 85 self.ingame_gui.message_widget.add('SAVED_GAME') 86 return success
87