home · contact · privacy
Refactor.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 15 Feb 2019 01:42:19 +0000 (02:42 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 15 Feb 2019 01:42:19 +0000 (02:42 +0100)
server_/game.py
server_/game_error.py [new file with mode: 0644]
server_/io.py
server_/map_.py
server_/tasks.py [new file with mode: 0644]

index 08ffd118c913699ceff8c59382bf31e3ff3668a8..554bce6fb13f462e1e25878bbea0efaf4a02b66c 100644 (file)
@@ -3,13 +3,11 @@ sys.path.append('../')
 import game_common
 import server_.map_
 import server_.io
+import server_.tasks
+from server_.game_error import GameError
 from parser import ArgError
 
 
-class GameError(Exception):
-    pass
-
-
 class World(game_common.World):
 
     def __init__(self, game):
@@ -67,55 +65,6 @@ class World(game_common.World):
         self.things = [player, npc]
 
 
-class Task:
-    argtypes = ''
-
-    def __init__(self, thing, args=()):
-        self.thing = thing
-        self.args = args
-        self.todo = 3
-
-    @property
-    def name(self):
-        prefix = 'Task_'
-        class_name = self.__class__.__name__
-        return class_name[len(prefix):]
-
-    def check(self):
-        pass
-
-    def get_args_string(self):
-        stringed_args = []
-        for arg in self.args:
-            if type(arg) == str:
-                stringed_args += [server_.io.quote(arg)]
-            else:
-                raise GameError('stringifying arg type not implemented')
-        return ' '.join(stringed_args)
-
-
-
-class Task_WAIT(Task):
-
-    def do(self):
-        return 'success'
-
-
-
-class Task_MOVE(Task):
-    argtypes = 'string:direction'
-
-    def check(self):
-        test_pos = self.thing.world.map_.move(self.thing.position, self.args[0])
-        if self.thing.world.map_[test_pos] != '.':
-            raise GameError('%s would move into illegal terrain' % self.thing.id_)
-        for t in self.thing.world.things:
-            if t.position == test_pos:
-                raise GameError('%s would move into other thing' % self.thing.id_)
-
-    def do(self):
-        self.thing.position = self.thing.world.map_.move(self.thing.position,
-                                                         self.args[0])
         return 'success'
 
 
@@ -124,7 +73,7 @@ class Thing(game_common.Thing):
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
-        self.task = Task_WAIT(self)
+        self.task = self.world.game.task_manager.get_task_class('WAIT')(self)
         self._last_task_result = None
         self._stencil = None
 
@@ -200,7 +149,7 @@ class Thing(game_common.Thing):
 
 
     def set_task(self, task_name, args=()):
-        task_class = globals()['Task_' + task_name]
+        task_class = self.world.game.task_manager.get_task_class(task_name)
         self.task = task_class(self, args)
         self.task.check()  # will throw GameError if necessary
 
@@ -273,6 +222,7 @@ class Game(game_common.CommonCommandsMixin):
 
     def __init__(self, game_file_name):
         self.map_manager = server_.map_.map_manager
+        self.task_manager = server_.tasks.task_manager
         self.world = World(self)
         self.io = server_.io.GameIO(game_file_name, self)
         # self.pool and self.pool_result are currently only needed by the FIB
@@ -392,7 +342,7 @@ class Game(game_common.CommonCommandsMixin):
             t = self.world.get_thing(thing_id, False)
             if t is None:
                 raiseArgError('No such Thing.')
-            task_class = globals()['Task_' + task_name]
+            task_class = self.task_manager.get_task_class(task_name)
             t.task = task_class(t, args)
             t.task.todo = todo
 
@@ -402,10 +352,10 @@ class Game(game_common.CommonCommandsMixin):
             argtypes = ''
             if command_name[:len(task_prefix)] == task_prefix:
                 task_name = command_name[len(task_prefix):]
-                task_class_candidate = 'Task_' + task_name
-                if task_class_candidate in globals():
+                task_manager_reply = self.task_manager.get_task_class(task_name)
+                if task_manager_reply is not None:
                     func = partial(task_command, task_name)
-                    task_class = globals()[task_class_candidate]
+                    task_class = task_manager_reply
                     argtypes = task_class.argtypes
             if func is not None:
                 return func, argtypes_prefix + argtypes
diff --git a/server_/game_error.py b/server_/game_error.py
new file mode 100644 (file)
index 0000000..afdd6ec
--- /dev/null
@@ -0,0 +1,2 @@
+class GameError(Exception):
+    pass
index 220003768c598c71ed48d26fc090c68f0a0d2b5c..501399f7dda4ea7920c3302ae26d254829f6f47a 100644 (file)
@@ -4,6 +4,7 @@ import queue
 import sys
 sys.path.append('../')
 import parser
+from server_.game_error import GameError
 
 
 # Avoid "Address already in use" errors.
@@ -172,7 +173,7 @@ class GameIO():
                             f.write(input_ + '\n')
         except parser.ArgError as e:
             answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e)))
-        except server_.game.GameError as e:
+        except GameError as e:
             answer(connection_id, 'GAME_ERROR ' + quote(str(e)))
 
     def send(self, msg, connection_id=None):
index dd47be5dcce61f976160e92480ad0d02efbde70d..4f686edb32831fc50f79a02ec0855b0ac7502d1f 100644 (file)
@@ -3,6 +3,7 @@ sys.path.append('../')
 import game_common
 import server_.game
 import math
+from server_.game_error import GameError
 
 
 class Map(game_common.Map):
@@ -60,7 +61,7 @@ class Map(game_common.Map):
             neighbors[direction] = None
             try:
                 neighbors[direction] = self.move(pos, direction)
-            except server_.game.GameError:
+            except GameError:
                 pass
         self.neighbors_to[pos] = neighbors
         return neighbors
@@ -77,7 +78,7 @@ class Map(game_common.Map):
         new_pos = mover(start_pos)
         if new_pos[0] < 0 or new_pos[1] < 0 or \
                 new_pos[0] >= self.size[0] or new_pos[1] >= self.size[1]:
-            raise server_.game.GameError('would move outside map bounds')
+            raise GameError('would move outside map bounds')
         return new_pos
 
     def move_LEFT(self, start_pos):
diff --git a/server_/tasks.py b/server_/tasks.py
new file mode 100644 (file)
index 0000000..f07828d
--- /dev/null
@@ -0,0 +1,69 @@
+from server_.game_error import GameError
+
+
+
+class TaskManager:
+
+    def __init__(self, task_classes):
+        self.task_classes = task_classes
+
+    def get_task_class(self, task_name):
+        for task_class in self.task_classes:
+            if task_class.__name__ == 'Task_' + task_name:
+                return task_class
+        return None
+
+
+class Task:
+    argtypes = ''
+
+    def __init__(self, thing, args=()):
+        self.thing = thing
+        self.args = args
+        self.todo = 3
+
+    @property
+    def name(self):
+        prefix = 'Task_'
+        class_name = self.__class__.__name__
+        return class_name[len(prefix):]
+
+    def check(self):
+        pass
+
+    def get_args_string(self):
+        stringed_args = []
+        for arg in self.args:
+            if type(arg) == str:
+                stringed_args += [server_.io.quote(arg)]
+            else:
+                raise GameError('stringifying arg type not implemented')
+        return ' '.join(stringed_args)
+
+
+
+class Task_WAIT(Task):
+
+    def do(self):
+        return 'success'
+
+
+
+class Task_MOVE(Task):
+    argtypes = 'string:direction'
+
+    def check(self):
+        test_pos = self.thing.world.map_.move(self.thing.position, self.args[0])
+        if self.thing.world.map_[test_pos] != '.':
+            raise GameError('%s would move into illegal terrain' % self.thing.id_)
+        for t in self.thing.world.things:
+            if t.position == test_pos:
+                raise GameError('%s would move into other thing' % self.thing.id_)
+
+    def do(self):
+        self.thing.position = self.thing.world.map_.move(self.thing.position,
+                                                         self.args[0])
+
+
+
+task_manager = TaskManager((Task_WAIT, Task_MOVE))