def load(self, file_name): """ :param file_name: """ # TODO what if file name does not exist or is not a valid scenario file self.scenario = Scenario.from_file(file_name) self.changed.emit()
def scenario_preview(scenario_file_name): """ A client got a message on the constants.C.SCENARIO_PREVIEW channel. In the message should be a scenario file name (key = 'scenario'). Assemble a preview and send it back. """ t0 = time.clock() # TODO existing? can be loaded? scenario = Scenario.from_file(scenario_file_name) print('reading the file took {}s'.format(time.clock() - t0)) preview = {'scenario': scenario_file_name} # some scenario properties should be copied scenario_copy_keys = [ constants.ScenarioProperty.MAP_COLUMNS, constants.ScenarioProperty.MAP_ROWS, constants.ScenarioProperty.TITLE, constants.ScenarioProperty.DESCRIPTION ] for key in scenario_copy_keys: preview[key] = scenario[key] # some nations properties should be copied nations = {} nation_copy_keys = [ constants.NationProperty.COLOR, constants.NationProperty.NAME, constants.NationProperty.DESCRIPTION ] for nation in scenario.nations(): nations[nation] = {} for key in nation_copy_keys: nations[nation][key] = scenario.nation_property(nation, key) preview['nations'] = nations # assemble a nations map (-1 means no nation) columns = scenario[constants.ScenarioProperty.MAP_COLUMNS] rows = scenario[constants.ScenarioProperty.MAP_ROWS] nations_map = [-1] * (columns * rows) for nation_id in scenario.nations(): provinces = scenario.provinces_of_nation(nation_id) for province in provinces: tiles = scenario.province_property( province, constants.ProvinceProperty.TILES) for column, row in tiles: nations_map[row * columns + column] = nation_id preview['map'] = nations_map print('generating preview took {}s'.format(time.clock() - t0)) return preview
def scenario_preview(scenario_file_name): """ A client got a message on the constants.C.SCENARIO_PREVIEW channel. In the message should be a scenario file name (key = 'scenario'). Assemble a preview and send it back. """ t0 = time.clock() # TODO existing? can be loaded? scenario = Scenario.from_file(scenario_file_name) logger.info('reading of the file took {}s'.format(time.clock() - t0)) preview = {'scenario': scenario_file_name} # some scenario properties should be copied scenario_copy_keys = [constants.ScenarioProperty.MAP_COLUMNS, constants.ScenarioProperty.MAP_ROWS, constants.ScenarioProperty.TITLE, constants.ScenarioProperty.DESCRIPTION] for key in scenario_copy_keys: preview[key] = scenario[key] # some nations properties should be copied nations = {} nation_copy_keys = [constants.NationProperty.COLOR, constants.NationProperty.NAME, constants.NationProperty.DESCRIPTION] for nation in scenario.nations(): nations[nation] = {} for key in nation_copy_keys: nations[nation][key] = scenario.nation_property(nation, key) preview['nations'] = nations # assemble a nations map (-1 means no nation) columns = scenario[constants.ScenarioProperty.MAP_COLUMNS] rows = scenario[constants.ScenarioProperty.MAP_ROWS] nations_map = [-1] * (columns * rows) for nation_id in scenario.nations(): provinces = scenario.provinces_of_nation(nation_id) for province in provinces: tiles = scenario.province_property(province, constants.ProvinceProperty.TILES) for column, row in tiles: nations_map[row * columns + column] = nation_id preview['map'] = nations_map logger.info('generating preview took {}s'.format(time.clock() - t0)) return preview
if __name__ == '__main__': # add source directory to path if needed source_directory = os.path.realpath( os.path.join(os.path.abspath(os.path.dirname(__file__)), os.path.pardir, os.path.pardir, 'source')) if source_directory not in sys.path: sys.path.insert(0, source_directory) from imperialism_remake.base import constants from imperialism_remake.server.scenario import Scenario # load scenario scenario = Scenario.from_file( constants.extend(constants.CORE_SCENARIO_FOLDER, 'Europe1814.scenario')) # nation map columns = scenario[constants.ScenarioProperty.MAP_COLUMNS] rows = scenario[constants.ScenarioProperty.MAP_ROWS] map = [0] * (columns * rows) for nation in scenario.nations(): provinces = scenario.provinces_of_nation(nation) for province in provinces: tiles = scenario.province_property(province, 'tiles') for column, row in tiles: map[row * columns + column] = nation # get outlines for nation in scenario.nations():
import os, sys from PyQt5 import QtWidgets if __name__ == '__main__': # add source directory to path if needed source_directory = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.path.pardir, os.path.pardir, 'source')) if source_directory not in sys.path: sys.path.insert(0, source_directory) from imperialism_remake.base import constants from imperialism_remake.server.scenario import Scenario # load scenario scenario = Scenario.from_file(constants.extend(constants.CORE_SCENARIO_FOLDER, 'Europe1814.scenario')) # nation map columns = scenario[constants.ScenarioProperty.MAP_COLUMNS] rows = scenario[constants.ScenarioProperty.MAP_ROWS] map = [0] * (columns * rows) for nation in scenario.nations(): provinces = scenario.provinces_of_nation(nation) for province in provinces: tiles = scenario.province_property(province, 'tiles') for column, row in tiles: map[row * columns + column] = nation # get outlines for nation in scenario.nations(): visited = [False] * (columns * rows)