Ejemplo n.º 1
0
    def create(self, properties):
        """
        Create new scenario (from the create new scenario dialog).

        :param properties:
        """
        logger.debug('create properties:%s', properties)

        self.server_scenario = ServerScenario(ServerScenarioBase())
        self.server_scenario[constants.ScenarioProperty.TITLE] = properties[
            constants.ScenarioProperty.TITLE]
        self.server_scenario.create_empty_map(
            properties[constants.ScenarioProperty.MAP_COLUMNS],
            properties[constants.ScenarioProperty.MAP_ROWS])

        # standard rules
        self.server_scenario[
            constants.ScenarioProperty.RULES] = 'standard.rules'
        # self.scenario.load_rules()
        # TODO rules as extra?
        rule_file = constants.extend(
            constants.SCENARIO_RULESET_FOLDER,
            self.server_scenario[constants.ScenarioProperty.RULES])
        self.server_scenario.get_scenario_base()._rules = utils.read_from_file(
            rule_file)

        self._init()
Ejemplo n.º 2
0
class EditorScenario(GenericScenario):
    """
    Wrap around the Scenario file to get notified of recreations
    """
    def __init__(self):
        super().__init__()

        self.server_scenario = None

    def load(self, file_name):
        """
        :param file_name:
        """
        logger.debug('load file_name:%s', file_name)

        if os.path.isfile(file_name):
            self.server_scenario = ServerScenario.from_file(file_name)

            self._init()

    def save(self, file_name):
        """
        :param file_name:
        """
        logger.debug('save file_name:%s', file_name)

        self.server_scenario.save(file_name)

    def create(self, properties):
        """
        Create new scenario (from the create new scenario dialog).

        :param properties:
        """
        logger.debug('create properties:%s', properties)

        self.server_scenario = ServerScenario(ServerScenarioBase())
        self.server_scenario[constants.ScenarioProperty.TITLE] = properties[
            constants.ScenarioProperty.TITLE]
        self.server_scenario.create_empty_map(
            properties[constants.ScenarioProperty.MAP_COLUMNS],
            properties[constants.ScenarioProperty.MAP_ROWS])

        # standard rules
        self.server_scenario[
            constants.ScenarioProperty.RULES] = 'standard.rules'
        # self.scenario.load_rules()
        # TODO rules as extra?
        rule_file = constants.extend(
            constants.SCENARIO_RULESET_FOLDER,
            self.server_scenario[constants.ScenarioProperty.RULES])
        self.server_scenario.get_scenario_base()._rules = utils.read_from_file(
            rule_file)

        self._init()
Ejemplo n.º 3
0
    def _system_messages(self, client: ServerNetworkClient,
                         channel: constants.C, action: constants.M, content):
        """
        Handles system messages of a local client to its local server. Not intended for outside use.

        :param client:
        :param channel:
        :param action:
        :param content:
        """
        logger.debug('_system_messages action: %s', action)

        if action == constants.M.SYSTEM_SHUTDOWN:
            # shuts down

            logger.info('server manager shuts down')
            # TODO disconnect all server clients, clean up, ...
            self.server.stop()
            self.shutdown.emit()

        elif action == constants.M.SYSTEM_MONITOR_UPDATE:

            # assemble monitor update
            update = {'number_connected_clients': len(self.server_clients)}
            client.send(constants.C.SYSTEM, constants.M.SYSTEM_MONITOR_UPDATE,
                        update)

        elif action == constants.M.GAME_SAVE_REQUEST:
            filename = content
            self._server_turn_processor.get_scenario().save(filename)
            client.send(channel, constants.M.GAME_SAVE_RESPONSE)

        elif action == constants.M.GAME_LOAD_REQUEST:
            filename = content['filename']
            selected_nation = content['nation']

            new_server_scenario = ServerScenario.from_file(filename)

            self._server_turn_processor.set_scenario(new_server_scenario)

            if selected_nation == -1:
                selected_nation = new_server_scenario.get_player_nation()
                logger.info(
                    "This is loading saved file. Use nation from it: %s",
                    selected_nation)
            else:
                new_server_scenario.set_player_nation(selected_nation)
                logger.info(
                    "This is loading saved file. Set current player nation to: %s",
                    selected_nation)

            new_server_scenario_base_for_nation = new_server_scenario.get_scenario_base_for_nation(
                selected_nation)

            client.send(
                channel, constants.M.GAME_LOAD_RESPONSE, {
                    'server_scenario_base':
                    new_server_scenario_base_for_nation,
                    'nation': selected_nation
                })
Ejemplo n.º 4
0
    def scenario_preview(self, 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.perf_counter()

        # TODO existing? can be loaded?
        server_scenario = ServerScenario.from_file(scenario_file_name)
        logger.info('reading of the file took {}s'.format(time.perf_counter() -
                                                          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] = server_scenario[key]

        # some nations properties should be copied
        nations = {}
        nation_copy_keys = [
            constants.NationProperty.COLOR, constants.NationProperty.NAME,
            constants.NationProperty.DESCRIPTION
        ]
        for nation in server_scenario.nations():
            nations[nation] = {}
            for key in nation_copy_keys:
                nations[nation][key] = server_scenario.nation_property(
                    nation, key)
        preview[constants.SCENARIO_FILE_NATIONS] = nations

        # assemble a nations map (-1 means no nation)
        columns = server_scenario[constants.ScenarioProperty.MAP_COLUMNS]
        rows = server_scenario[constants.ScenarioProperty.MAP_ROWS]
        nations_map = [-1] * (columns * rows)
        for nation_id in server_scenario.nations():
            provinces = server_scenario.provinces_of_nation(nation_id)
            for province in provinces:
                tiles = server_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.perf_counter() -
                                                         t0))

        self._server_turn_processor.set_scenario(server_scenario)

        return preview
Ejemplo n.º 5
0
    def load(self, file_name):
        """
        :param file_name:
        """
        logger.debug('load file_name:%s', file_name)

        if os.path.isfile(file_name):
            self.server_scenario = ServerScenario.from_file(file_name)

            self._init()
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.server_scenario import ServerScenario

    # load scenario
    scenario = ServerScenario.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)
Ejemplo n.º 7
0
    def init(self, server_base_scenario):
        logger.debug("init")

        self.server_scenario = ServerScenario(server_base_scenario)

        self._init()