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):
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."""
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)):
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)
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))
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.
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.
def get_map_class(geometry):
return globals()['Map' + geometry]
+
+
+map_manager = game_common.MapManager(globals())