From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 9 Nov 2020 23:27:34 +0000 (+0100)
Subject: Register game commands and tasks outside of game module.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/decks/%27%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28escapeHTML%28span%5B2%5D%29%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28%27?a=commitdiff_plain;p=plomrogue2-experiments

Register game commands and tasks outside of game module.
---

diff --git a/new2/plomrogue/game.py b/new2/plomrogue/game.py
index bbc2393..68bcb65 100755
--- a/new2/plomrogue/game.py
+++ b/new2/plomrogue/game.py
@@ -1,9 +1,6 @@
 from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE,
                              Task_FLATTEN_SURROUNDINGS)
 from plomrogue.errors import GameError, PlayError
-from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING, cmd_MAP,
-                                cmd_TURN, cmd_MAP_LINE, cmd_GET_ANNOTATION,
-                                cmd_ANNOTATE, cmd_PORTAL, cmd_GET_GAMESTATE)
 from plomrogue.io import GameIO
 from plomrogue.misc import quote
 from plomrogue.things import Thing, ThingPlayer
@@ -17,6 +14,7 @@ class GameBase:
         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
@@ -30,30 +28,23 @@ 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, save_file, *args, **kwargs):
-        import os
         super().__init__(*args, **kwargs)
         self.changed = True
         self.io = GameIO(self, save_file)
-        self.tasks = {'WAIT': Task_WAIT,
-                      'MOVE': Task_MOVE,
-                      'WRITE': Task_WRITE,
-                      'FLATTEN_SURROUNDINGS': Task_FLATTEN_SURROUNDINGS}
-        self.commands = {'QUERY': cmd_QUERY,
-                         'ALL': cmd_ALL,
-                         'LOGIN': cmd_LOGIN,
-                         'TURN': cmd_TURN,
-                         'MAP_LINE': cmd_MAP_LINE,
-                         'GET_ANNOTATION': cmd_GET_ANNOTATION,
-                         'PORTAL': cmd_PORTAL,
-                         'GET_GAMESTATE': cmd_GET_GAMESTATE,
-                         'ANNOTATE': cmd_ANNOTATE,
-                         'MAP': cmd_MAP,
-                         'PING': cmd_PING}
+        self.tasks = {}
         self.thing_type = Thing
         self.thing_types = {'player': ThingPlayer}
         self.sessions = {}
@@ -63,13 +54,22 @@ class Game(GameBase):
         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, god_mode=True)
+
+    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
@@ -186,6 +186,7 @@ class Game(GameBase):
           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,))
diff --git a/new2/rogue_chat.py b/new2/rogue_chat.py
index d2f0a24..026fcdf 100755
--- a/new2/rogue_chat.py
+++ b/new2/rogue_chat.py
@@ -1,7 +1,12 @@
 #!/usr/bin/env python3
-from plomrogue.game import Game 
+from plomrogue.game import Game
 from plomrogue.io_websocket import PlomWebSocketServer
 from plomrogue.io_tcp import PlomTCPServer
+from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING, cmd_MAP,
+                                cmd_TURN, cmd_MAP_LINE, cmd_GET_ANNOTATION,
+                                cmd_ANNOTATE, cmd_PORTAL, cmd_GET_GAMESTATE)
+from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE,
+                             Task_FLATTEN_SURROUNDINGS)
 import sys
 
 if len(sys.argv) != 2:
@@ -9,6 +14,21 @@ if len(sys.argv) != 2:
     exit(1)
 savefile = sys.argv[1]
 game = Game(savefile)
+game.register_command(cmd_PING)
+game.register_command(cmd_LOGIN)
+game.register_command(cmd_QUERY)
+game.register_command(cmd_TURN)
+game.register_command(cmd_MAP)
+game.register_command(cmd_MAP_LINE)
+game.register_command(cmd_GET_ANNOTATION)
+game.register_command(cmd_ANNOTATE)
+game.register_command(cmd_PORTAL)
+game.register_command(cmd_GET_GAMESTATE)
+game.register_task(Task_WAIT)
+game.register_task(Task_MOVE)
+game.register_task(Task_WRITE)
+game.register_task(Task_FLATTEN_SURROUNDINGS)
+game.read_savefile()
 game.io.start_loop()
 game.io.start_server(8000, PlomWebSocketServer)
 game.io.start_server(5000, PlomTCPServer)
diff --git a/new2/rogue_chat_curses.py b/new2/rogue_chat_curses.py
index c8a17c8..a6d1ca7 100755
--- a/new2/rogue_chat_curses.py
+++ b/new2/rogue_chat_curses.py
@@ -148,25 +148,25 @@ def cmd_PONG(game):
 cmd_PONG.argtypes = ''
 
 class Game(GameBase):
-    commands = {'LOGIN_OK': cmd_LOGIN_OK,
-                'PONG': cmd_PONG,
-                'CHAT': cmd_CHAT,
-                'PLAYER_ID': cmd_PLAYER_ID,
-                'TURN': cmd_TURN,
-                'THING_POS': cmd_THING_POS,
-                'THING_NAME': cmd_THING_NAME,
-                'MAP': cmd_MAP,
-                'PORTAL': cmd_PORTAL,
-                'ANNOTATION': cmd_ANNOTATION,
-                'GAME_STATE_COMPLETE': cmd_GAME_STATE_COMPLETE,
-                'ARGUMENT_ERROR': cmd_ARGUMENT_ERROR,
-                'GAME_ERROR': cmd_GAME_ERROR,
-                'PLAY_ERROR': cmd_PLAY_ERROR}
     thing_type = ThingBase
     turn_complete = False
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
+        self.register_command(cmd_LOGIN_OK)
+        self.register_command(cmd_PONG)
+        self.register_command(cmd_CHAT)
+        self.register_command(cmd_PLAYER_ID)
+        self.register_command(cmd_TURN)
+        self.register_command(cmd_THING_POS)
+        self.register_command(cmd_THING_NAME)
+        self.register_command(cmd_MAP)
+        self.register_command(cmd_PORTAL)
+        self.register_command(cmd_ANNOTATION)
+        self.register_command(cmd_GAME_STATE_COMPLETE)
+        self.register_command(cmd_ARGUMENT_ERROR)
+        self.register_command(cmd_GAME_ERROR)
+        self.register_command(cmd_PLAY_ERROR)
         self.map_content = ''
         self.player_id = -1
         self.info_db = {}