From: Christian Heller Date: Fri, 18 Jan 2019 02:30:48 +0000 (+0100) Subject: Derive legal geometries directly from Map class names. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/todos?a=commitdiff_plain;h=2b91efc835f8e68c97830d13d1e3070fdbcf1cc5;p=plomrogue2-experiments Derive legal geometries directly from Map class names. --- diff --git a/client.py b/client.py index 9d0da0d..11d33cc 100755 --- 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.""" diff --git a/game_common.py b/game_common.py index 2277f13..599e54b 100644 --- a/game_common.py +++ b/game_common.py @@ -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)) diff --git a/server_/game.py b/server_/game.py index 4cd28fb..3b3eb08 100644 --- a/server_/game.py +++ b/server_/game.py @@ -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. diff --git a/server_/map_.py b/server_/map_.py index 9dc96d0..7681600 100644 --- a/server_/map_.py +++ b/server_/map_.py @@ -111,3 +111,6 @@ class MapSquare(Map): def get_map_class(geometry): return globals()['Map' + geometry] + + +map_manager = game_common.MapManager(globals())