home · contact · privacy
Add basic save file mechanism.
[plomrogue2-experiments] / new2 / plomrogue / game.py
index 25f4d809b69b04083abb4bcea83996608920ee42..6325fd0d0849dc9ef37df9612046fcf92f54ba1d 100755 (executable)
@@ -1,6 +1,7 @@
 from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_WRITE
 from plomrogue.errors import GameError
-from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING
+from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING,
+                                cmd_SAVE, cmd_TURN, cmd_MAP_LINE)
 from plomrogue.io import GameIO
 from plomrogue.misc import quote
 from plomrogue.things import Thing, ThingPlayer 
@@ -31,10 +32,11 @@ class GameBase:
 
 class Game(GameBase):
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, save_file, *args, **kwargs):
+        import os
         super().__init__(*args, **kwargs)
         self.changed = True
-        self.io = GameIO(self)
+        self.io = GameIO(self, save_file)
         self.tasks = {'WAIT': Task_WAIT,
                       'MOVE': Task_MOVE,
                       'WRITE': Task_WRITE}
@@ -42,11 +44,24 @@ class Game(GameBase):
         self.commands = {'QUERY': cmd_QUERY,
                          'ALL': cmd_ALL,
                          'LOGIN': cmd_LOGIN,
+                         'SAVE': cmd_SAVE,
+                         'TURN': cmd_TURN,
+                         'MAP_LINE': cmd_MAP_LINE,
                          'PING': cmd_PING}
         self.thing_type = Thing
         self.thing_types = {'player': ThingPlayer}
         self.sessions = {}
         self.map = Map(self.map_geometry.size)
+        if os.path.exists(self.io.save_file):
+            if not os.path.isfile(self.io.save_file):
+                raise GameError('save file path refers to non-file')
+            else:
+                with open(self.io.save_file, 'r') as f:
+                    lines = f.readlines()
+                for i in range(len(lines)):
+                    line = lines[i]
+                    print("FILE INPUT LINE %5s: %s" % (i, line), end='')
+                    self.io.handle_input(line)
 
     def get_string_options(self, string_option_type):
         import string
@@ -87,8 +102,8 @@ class Game(GameBase):
                     for connection_id in [c_id for c_id in self.sessions
                                           if self.sessions[c_id] == t.id_]:
                         self.io.send('GAME_ERROR ' + quote(str(e)), connection_id)
-        self.turn += 1
         if self.changed:
+            self.turn += 1
             self.send_gamestate()
             self.changed = False