Example #1
0
def scenario_core_titles():
    """
    A server client received a message on the constants.C.SCENARIO_CORE_TITLES channel. Return all available core
    scenario titles and file names.
    """
    # get all core scenario files
    scenario_files = [
        x for x in os.listdir(constants.CORE_SCENARIO_FOLDER)
        if x.endswith('.scenario')
    ]

    # join the path
    scenario_files = [
        os.path.join(constants.CORE_SCENARIO_FOLDER, x) for x in scenario_files
    ]

    # read scenario titles
    scenario_titles = []
    for scenario_file in scenario_files:
        reader = utils.ZipArchiveReader(scenario_file)
        properties = reader.read_as_yaml(constants.SCENARIO_FILE_PROPERTIES)
        scenario_titles.append(properties[constants.ScenarioProperty.TITLE])

    # zip files and titles together
    scenarios = zip(scenario_titles, scenario_files)

    # sort them
    scenarios = sorted(
        scenarios)  # default sort order is by first element anyway

    return scenarios
Example #2
0
    def from_file(file_path):
        """
        Load/deserialize all internal variables from a zipped archive via YAML.
        """
        # TODO what if not a valid scenario file, we should raise an error then

        scenario = Scenario()

        reader = utils.ZipArchiveReader(file_path)

        scenario._properties = reader.read_as_yaml(
            constants.SCENARIO_FILE_PROPERTIES)
        scenario._maps = reader.read_as_yaml(constants.SCENARIO_FILE_MAPS)
        scenario._provinces = reader.read_as_yaml(
            constants.SCENARIO_FILE_PROVINCES)
        # TODO check all ids are smaller then len()

        scenario._nations = reader.read_as_yaml(
            constants.SCENARIO_FILE_NATIONS)
        # TODO check all ids are smaller then len()

        # read rule file
        # TODO how to specify which rules file apply
        rule_file = constants.extend(
            constants.SCENARIO_RULESET_FOLDER,
            scenario[constants.ScenarioProperty.RULES])
        scenario._rules = utils.read_as_yaml(rule_file)

        return scenario
Example #3
0
    def from_file(file_path):
        """
        Load/deserialize all internal variables from a zipped archive
        """
        # TODO what if not a valid scenario file, we should raise an error then

        logger.debug('from_file file: %s', file_path)

        server_scenario = ServerScenario(ServerScenarioBase())

        reader = utils.ZipArchiveReader(file_path)

        server_scenario.get_scenario_base().properties = reader.read_from_file(
            constants.SCENARIO_FILE_PROPERTIES)
        server_scenario.get_scenario_base().maps = reader.read_from_file(
            constants.SCENARIO_FILE_MAPS)
        server_scenario.get_scenario_base().provinces = reader.read_from_file(
            constants.SCENARIO_FILE_PROVINCES)
        # TODO check all ids are smaller then len()

        server_scenario.get_scenario_base().nations = reader.read_from_file(
            constants.SCENARIO_FILE_NATIONS)
        # TODO check all ids are smaller then len()

        # read rule file
        # TODO how to specify which rules file apply
        rule_file = constants.extend(
            constants.SCENARIO_RULESET_FOLDER,
            server_scenario[constants.ScenarioProperty.RULES])
        server_scenario.get_scenario_base().rules = utils.read_from_file(
            rule_file)

        if ServerScenarioBase.ROAD not in server_scenario._scenario_base.maps:
            server_scenario._scenario_base.maps[ServerScenarioBase.ROAD] = []

        if ServerScenarioBase.STRUCTURE not in server_scenario._scenario_base.maps:
            server_scenario._scenario_base.maps[
                ServerScenarioBase.STRUCTURE] = {}

        return server_scenario