home · contact · privacy
Derive legal geometries directly from Map class names.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 18 Jan 2019 02:30:48 +0000 (03:30 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 18 Jan 2019 02:30:48 +0000 (03:30 +0100)
client.py
game_common.py
server_/game.py
server_/map_.py

index 9d0da0d534399be8f546258d8976036ac53c9eb6..11d33ccaba367f06d0a70cba78fd14dd662823a9 100755 (executable)
--- a/client.py
+++ b/client.py
@@ -7,10 +7,6 @@ from parser import ArgError, Parser
 import game_common
 
 
-def get_map_class(geometry):
-    return globals()['Map' + geometry]
-
-
 class MapSquare(game_common.Map):
 
     def list_terrain_to_lines(self, terrain_as_list):
@@ -42,22 +38,28 @@ class MapHex(game_common.Map):
         return ''.join(new_terrain_list)
 
 
+map_manager = game_common.MapManager(globals())
+
+
 class World(game_common.World):
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, game, *args, **kwargs):
         """Extend original with local classes and empty default map.
 
         We need the empty default map because we draw the map widget
         on any update, even before we actually receive map data.
         """
         super().__init__(*args, **kwargs)
-        self.get_map_class = get_map_class
-        self.map_ = self.get_map_class('Hex')()
+        self.game = game
+        self.map_ = self.game.map_manager.get_map_class('Hex')()
 
 
 class Game(game_common.CommonCommandsMixin):
-    world = World()
-    log_text = ''
+
+    def __init__(self):
+        self.map_manager = map_manager
+        self.world = World(self)
+        self.log_text = ''
 
     def log(self, msg):
         """Prefix msg plus newline to self.log_text."""
index 2277f13c36ab99469dfdaf62cde8d25ac0046fcd..599e54bcda4c51b936b9dac9ac1d90b03debd8af 100644 (file)
@@ -1,6 +1,27 @@
 from parser import ArgError
 
 
+class MapManager:
+
+    def __init__(self, globs):
+        """With globs a globals() call, collect caller's Map classes."""
+        self.map_classes = []
+        for name in globs:
+            if name[:3] == 'Map':
+                self.map_classes += [globs[name]]
+
+    def get_map_geometries(self):
+        geometries = []
+        for map_class in self.map_classes:
+            geometries += [map_class.__name__[3:]]
+        return geometries
+
+    def get_map_class(self, geometry):
+        for map_class in self.map_classes:
+            if map_class.__name__[3:] == geometry:
+                return map_class
+
+
 class Map:
 
     def __init__(self, size=(0, 0)):
@@ -42,7 +63,7 @@ class World:
         return t
 
     def new_map(self, geometry, yx):
-        map_type = self.get_map_class(geometry)
+        map_type = self.game.map_manager.get_map_class(geometry)
         self.map_ = map_type(yx)
 
 
@@ -59,7 +80,7 @@ class CommonCommandsMixin:
 
     def cmd_MAP(self, geometry, yx):
         """Create new map of grid geometry, size yx and only '?' cells."""
-        legal_grids = {'Hex', 'Square'}
+        legal_grids = self.map_manager.get_map_geometries()
         if geometry not in legal_grids:
             raise ArgError('First map argument must be one of: ' +
                            ', '.join(legal_grids))
index 4cd28fba99b2ba13426da0563482ca5d21723209..3b3eb0844cc7d527c95eed54282db4799c0ecff7 100644 (file)
@@ -10,12 +10,12 @@ class GameError(Exception):
 
 class World(game_common.World):
 
-    def __init__(self):
+    def __init__(self, game):
         super().__init__()
+        self.game = game
         self.player_id = 0
         # use extended local classes
         self.Thing = Thing
-        self.get_map_class = server_.map_.get_map_class
 
     def proceed_to_next_player_turn(self):
         """Run game world turns until player can decide their next step.
@@ -163,7 +163,9 @@ class Game(game_common.CommonCommandsMixin):
 
     def __init__(self, game_file_name):
         import server_.io
-        self.world = World()
+        #self.get_map_class = server_.map_.get_map_class
+        self.map_manager = server_.map_.map_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
         # command and the demo of a parallelized game loop in cmd_inc_p.
index 9dc96d0a3c304c0fe19f1e2df13f741ac84a4b3f..768160005ea3bf985d313939ad4aa7fc1dbd5da5 100644 (file)
@@ -111,3 +111,6 @@ class MapSquare(Map):
 
 def get_map_class(geometry):
     return globals()['Map' + geometry]
+
+
+map_manager = game_common.MapManager(globals())