home · contact · privacy
Register game commands and tasks outside of game module.
[plomrogue2-experiments] / new2 / plomrogue / game.py
index e8c85da35adef872a0667bf97a75c09189049995..68bcb65ccd98ff456512a97071b541addc637b6f 100755 (executable)
@@ -1,9 +1,9 @@
-from plomrogue.tasks import Task_WAIT, Task_MOVE
-from plomrogue.errors import GameError
-from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY
+from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE,
+                             Task_FLATTEN_SURROUNDINGS)
+from plomrogue.errors import GameError, PlayError
 from plomrogue.io import GameIO
 from plomrogue.misc import quote
-from plomrogue.things import Thing, ThingPlayer 
+from plomrogue.things import Thing, ThingPlayer
 from plomrogue.mapping import YX, MapGeometrySquare, Map
 
 
@@ -11,9 +11,10 @@ from plomrogue.mapping import YX, MapGeometrySquare, Map
 class GameBase:
 
     def __init__(self):
-        pass
         self.turn = 0
         self.things = []
+        self.map_geometry = MapGeometrySquare(YX(24, 40))
+        self.commands = {}
 
     def get_thing(self, id_, create_unfound):
         # No default for create_unfound because every call to get_thing
@@ -27,52 +28,97 @@ class GameBase:
             return t
         return None
 
+    def register_command(self, command):
+        prefix = 'cmd_'
+        if not command.__name__.startswith(prefix):
+           raise GameError('illegal command object name: %s' % command.__name__)
+        command_name = command.__name__[len(prefix):]
+        self.commands[command_name] = command
+
 
 
+import os
 class Game(GameBase):
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, save_file, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.changed = True
-        self.io = GameIO(self)
-        self.tasks = {'WAIT': Task_WAIT,
-                      'MOVE': Task_MOVE}
-        self.map_geometry = MapGeometrySquare(YX(24, 40))
-        self.commands = {'QUERY': cmd_QUERY,
-                         'ALL': cmd_ALL,
-                         'LOGIN': cmd_LOGIN}
+        self.io = GameIO(self, save_file)
+        self.tasks = {}
         self.thing_type = Thing
         self.thing_types = {'player': ThingPlayer}
         self.sessions = {}
         self.map = Map(self.map_geometry.size)
+        self.annotations = {}
+        self.portals = {}
+        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')
+
+    def register_task(self, task):
+        prefix = 'Task_'
+        if not task.__name__.startswith(prefix):
+           raise GameError('illegal task object name: %s' % task.__name__)
+        task_name = task.__name__[len(prefix):]
+        self.tasks[task_name] = task
+
+    def read_savefile(self):
+        if os.path.exists(self.io.save_file):
+            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, god_mode=True)
 
     def get_string_options(self, string_option_type):
+        import string
         if string_option_type == 'direction':
             return self.map_geometry.get_directions()
+        elif string_option_type == 'char':
+            return [c for c in
+                    string.digits + string.ascii_letters + string.punctuation + ' ']
+        elif string_option_type == 'map_geometry':
+            return ['Hex', 'Square']
         return None
 
+    def get_map_geometry_shape(self):
+        return self.map_geometry.__class__.__name__[len('MapGeometry'):]
+
     def send_gamestate(self, connection_id=None):
         """Send out game state data relevant to clients."""
 
         def send_thing(thing):
             self.io.send('THING_POS %s %s' % (thing.id_, t.position))
+            if hasattr(thing, 'nickname'):
+                self.io.send('THING_NAME %s %s' % (thing.id_, quote(t.nickname)))
 
         self.io.send('TURN ' + str(self.turn))
         for t in self.things:
             send_thing(t)
-        self.io.send('MAP %s %s' % (self.map_geometry.size, quote(self.map.terrain)))
+        self.io.send('MAP %s %s %s' % (self.get_map_geometry_shape(),
+                                       self.map_geometry.size, quote(self.map.terrain)))
+        for yx in self.portals:
+            self.io.send('PORTAL %s %s' % (yx, quote(self.portals[yx])))
         self.io.send('GAME_STATE_COMPLETE')
 
     def run_tick(self):
         to_delete = []
         for connection_id in self.sessions:
-            if not connection_id in self.io.server.clients:
+            connection_id_found = False
+            for server in self.io.servers:
+                if connection_id in server.clients:
+                    connection_id_found = True
+                    break
+            if not connection_id_found:
                 t = self.get_thing(self.sessions[connection_id], create_unfound=False)
+                if hasattr(t, 'nickname'):
+                    self.io.send('CHAT ' + quote(t.nickname + ' left the map.'))
                 self.things.remove(t)
                 to_delete += [connection_id]
         for connection_id in to_delete:
             del self.sessions[connection_id]
-            self.changed = True 
+            self.changed = True
         for t in [t for t in self.things]:
             if t in self.things:
                 try:
@@ -81,10 +127,15 @@ 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
+                except PlayError as e:
+                    for connection_id in [c_id for c_id in self.sessions
+                                          if self.sessions[c_id] == t.id_]:
+                        self.io.send('PLAY_ERROR ' + quote(str(e)), connection_id)
         if self.changed:
+            self.turn += 1
             self.send_gamestate()
             self.changed = False
+            self.save()
 
     def get_command(self, command_name):
 
@@ -128,3 +179,25 @@ class Game(GameBase):
         # the more obvious indicators that it does – that's why there's
         # no safeguard here against this.
         return self.things[-1].id_ + 1
+
+    def save(self):
+
+      def write(f, msg):
+          f.write(msg + '\n')
+
+      with open(self.io.save_file, 'w') as f:
+          # TODO: save tasks
+          write(f, 'TURN %s' % self.turn)
+          map_geometry_shape = self.get_map_geometry_shape()
+          write(f, 'MAP %s %s' % (map_geometry_shape, self.map_geometry.size,))
+          for y, line in self.map.lines():
+              write(f, 'MAP_LINE %5s %s' % (y, quote(line)))
+          for yx in self.annotations:
+              write(f, 'ANNOTATE %s %s' % (yx, quote(self.annotations[yx])))
+          for yx in self.portals:
+              write(f, 'PORTAL %s %s' % (yx, quote(self.portals[yx])))
+
+    def new_world(self, map_geometry):
+        self.map_geometry = map_geometry
+        self.map = Map(self.map_geometry.size)
+        self.annotations = {}