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
 def test_write_read(self):
     value = {"One": [2, 3, 'Four', None], 2: ("Cat", "Dog")}
     temp_file = 'temporary_file'
     if os.path.exists(temp_file):
         raise RuntimeError('temporary file already existing')
     utils.write_as_yaml(temp_file, value)
     copy = utils.read_as_yaml(temp_file)
     os.remove(temp_file)
     self.assertEqual(value, copy)
Exemple #3
0
    def __init__(self, client):
        super().__init__()

        self.setAttribute(QtCore.Qt.WA_StyledBackground)
        self.setProperty('background', 'black')

        layout = qt.RelativeLayout(self)

        start_image = QtGui.QPixmap(constants.extend(constants.GRAPHICS_UI_FOLDER, 'start.background.jpg'))
        start_image_item = QtWidgets.QGraphicsPixmapItem(start_image)
        start_image_item.setZValue(1)

        scene = QtWidgets.QGraphicsScene(self)
        scene.addItem(start_image_item)

        view = QtWidgets.QGraphicsView(scene)
        view.resize(start_image.size())
        view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        view.setSceneRect(0, 0, start_image.width(), start_image.height())
        view.layout_constraint = qt.RelativeLayoutConstraint().center_horizontal().center_vertical()
        layout.addWidget(view)

        subtitle = QtWidgets.QLabel('')
        subtitle.layout_constraint = qt.RelativeLayoutConstraint((0.5, -0.5, 0),
            (0.5, -0.5, start_image.height() / 2 + 20))
        layout.addWidget(subtitle)

        actions = {'exit': client.quit, 'help': client.show_help_browser, 'lobby': client.show_game_lobby_dialog,
                   'editor': client.switch_to_editor_screen, 'options': client.show_preferences_dialog}

        image_map_file = constants.extend(constants.GRAPHICS_UI_FOLDER, 'start.overlay.info')
        image_map = utils.read_as_yaml(image_map_file)

        # security check, they have to be the same
        if actions.keys() != image_map.keys():
            raise RuntimeError('Start screen hot map info file ({}) corrupt.'.format(image_map_file))

        for k, v in image_map.items():
            # add action from our predefined action dictionary
            pixmap = QtGui.QPixmap(constants.extend(constants.GRAPHICS_UI_FOLDER, v['overlay']))
            map_item = MapItem(view, pixmap, label=subtitle, description=v['label'])
            map_item.item.setZValue(3)
            offset = v['offset']
            map_item.item.setOffset(QtCore.QPointF(offset[0], offset[1]))
            map_item.item.signaller.clicked.connect(actions[k])

            frame_path = QtGui.QPainterPath()
            frame_path.addRect(map_item.item.boundingRect())
            frame_item = scene.addPath(frame_path, StartScreen.frame_pen)
            frame_item.setZValue(4)
            scene.addItem(map_item.item)

        version_label = QtWidgets.QLabel('<font color=#ffffff>{}</font>'.format(version.__version_full__))
        version_label.layout_constraint = qt.RelativeLayoutConstraint().east(20).south(20)
        layout.addWidget(version_label)
 def test_write_read(self):
     value = {
         "One": [2, 3, 'Four', None],
         2: ("Cat", "Dog")
     }
     temp_file = 'temporary_file'
     if os.path.exists(temp_file):
         raise RuntimeError('temporary file already existing')
     utils.write_as_yaml(temp_file, value)
     copy = utils.read_as_yaml(temp_file)
     os.remove(temp_file)
     self.assertEqual(value, copy)
Exemple #5
0
    def create(self, properties):
        """
        Create new scenario (from the create new scenario dialog).

        :param properties:
        """
        self.scenario = Scenario()
        self.scenario[constants.ScenarioProperty.TITLE] = properties[constants.ScenarioProperty.TITLE]
        self.scenario.create_empty_map(properties[constants.ScenarioProperty.MAP_COLUMNS],
            properties[constants.ScenarioProperty.MAP_ROWS])

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

        # emit that everything has changed
        self.changed.emit()
def load_soundtrack_playlist():
    """
        Loads the play list of the soundtracks and replaces the file name with the full path.

        A playlist is a list where each entry is a list of two strings: file path, title
    """
    global soundtrack_playlist

    # create playlist
    soundtrack_playlist = QtMultimedia.QMediaPlaylist()
    soundtrack_playlist.setPlaybackMode(QtMultimedia.QMediaPlaylist.Loop)

    # read information file
    data = utils.read_as_yaml(constants.SOUNDTRACK_INFO_FILE)

    # add the soundtrack folder to each file name
    for entry in data:
        file = constants.extend(constants.SOUNDTRACK_FOLDER, entry[0])
        url = qt.local_url(file)
        media = QtMultimedia.QMediaContent(url)
        soundtrack_playlist.addMedia(media)
Exemple #7
0
def load_options(file_name):
    """
    Load options from a JSON file and apply some conversions like changing the main window bounding rectangle
    from list to QtCore.QRect.

    :param file_name:
    """
    global options
    options = utils.read_as_yaml(file_name)

    # if for some reason no dict, make it a dict
    if type(options) is not dict:
        options = {}

    # delete entries that are not in Constants.Options
    remove = [o for o in list(options.keys()) if o not in constants.Options]
    for o in remove:
        del options[o]

    # copy values that are in Constants.Options but not here
    for option in constants.Options:
        if option not in options and hasattr(option, 'default'):
            options[option] = option.default
Exemple #8
0
def load_options(file_name):
    """
    Load options from a JSON file and apply some conversions like changing the main window bounding rectangle
    from list to QtCore.QRect.

    :param file_name:
    """
    global options
    options = utils.read_as_yaml(file_name)

    # if for some reason no dict, make it a dict
    if type(options) is not dict:
        options = {}

    # delete entries that are not in Constants.Options
    remove = [o for o in options.keys() if o not in constants.Options]
    for o in remove:
        del options[o]

    # copy values that are in Constants.Options but not here
    for option in constants.Options:
        if option not in options and hasattr(option, 'default'):
                options[option] = option.default
Exemple #9
0
    def from_file(file_name):
        """
        Load/deserialize all internal variables from a zipped archive via YAML.
        """

        scenario = Scenario()

        reader = utils.ZipArchiveReader(file_name)

        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
    def __init__(self, client):
        super().__init__()

        self.setAttribute(QtCore.Qt.WA_StyledBackground)
        self.setProperty('background', 'black')

        layout = qt.RelativeLayout(self)

        start_image = QtGui.QPixmap(
            constants.extend(constants.GRAPHICS_UI_FOLDER,
                             'start.background.jpg'))
        start_image_item = QtWidgets.QGraphicsPixmapItem(start_image)
        start_image_item.setZValue(1)

        scene = QtWidgets.QGraphicsScene(self)
        scene.addItem(start_image_item)

        view = QtWidgets.QGraphicsView(scene)
        view.resize(start_image.size())
        view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        view.setSceneRect(0, 0, start_image.width(), start_image.height())
        view.layout_constraint = qt.RelativeLayoutConstraint(
        ).center_horizontal().center_vertical()
        layout.addWidget(view)

        subtitle = QtWidgets.QLabel('')
        subtitle.layout_constraint = qt.RelativeLayoutConstraint(
            (0.5, -0.5, 0), (0.5, -0.5, start_image.height() / 2 + 20))
        layout.addWidget(subtitle)

        actions = {
            'exit': client.quit,
            'help': client.show_help_browser,
            'lobby': client.show_game_lobby_dialog,
            'editor': client.switch_to_editor_screen,
            'options': client.show_preferences_dialog
        }

        image_map_file = constants.extend(constants.GRAPHICS_UI_FOLDER,
                                          'start.overlay.info')
        image_map = utils.read_as_yaml(image_map_file)

        # security check, they have to be the same
        if list(actions.keys()) != list(image_map.keys()):
            raise RuntimeError(
                'Start screen hot map info file ({}) corrupt.'.format(
                    image_map_file))

        for k, v in list(image_map.items()):
            # add action from our predefined action dictionary
            pixmap = QtGui.QPixmap(
                constants.extend(constants.GRAPHICS_UI_FOLDER, v['overlay']))
            map_item = MapItem(view,
                               pixmap,
                               label=subtitle,
                               description=v['label'])
            map_item.item.setZValue(3)
            offset = v['offset']
            map_item.item.setOffset(QtCore.QPointF(offset[0], offset[1]))
            map_item.item.signaller.clicked.connect(actions[k])

            frame_path = QtGui.QPainterPath()
            frame_path.addRect(map_item.item.boundingRect())
            frame_item = scene.addPath(frame_path, StartScreen.frame_pen)
            frame_item.setZValue(4)
            scene.addItem(map_item.item)

        version_label = QtWidgets.QLabel(
            '<font color=#ffffff>{}</font>'.format(version.__version_full__))
        version_label.layout_constraint = qt.RelativeLayoutConstraint().east(
            20).south(20)
        layout.addWidget(version_label)
Exemple #11
0
def create_start_screen_widget(actions):
    """
    Creates the start screen.

    :param client:
    :return:
    """

    screen = QtWidgets.QWidget()
    screen.setAttribute(QtCore.Qt.WA_StyledBackground, True)
    screen.setProperty('background', 'black')

    layout = qt.RelativeLayout(screen)

    path = constants.extend(constants.GRAPHICS_UI_FOLDER,
                            'start.background.jpg')
    start_image = QtGui.QPixmap(path)
    start_image_item = QtWidgets.QGraphicsPixmapItem(start_image)
    start_image_item.setZValue(1)

    scene = QtWidgets.QGraphicsScene(screen)
    scene.addItem(start_image_item)

    view = QtWidgets.QGraphicsView(scene)
    view.resize(start_image.size())
    view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    view.setSceneRect(0, 0, start_image.width(), start_image.height())
    view.layout_constraint = qt.RelativeLayoutConstraint().center_horizontal()\
        .center_vertical()
    layout.addWidget(view)

    subtitle = QtWidgets.QLabel('')
    subtitle.resize(0, 0)
    # TODO this is below the main image but collides with screens only 768 px high
    subtitle.layout_constraint = qt.RelativeLayoutConstraint(
        (0.5, -0.5, 0), (0.5, -0.5, start_image.height() / 2 + 20))
    layout.addWidget(subtitle)

    image_map_file = constants.extend(constants.GRAPHICS_UI_FOLDER,
                                      'start.overlay.info')
    image_map = utils.read_as_yaml(image_map_file)

    # security check, they have to be the same
    if actions.keys() != image_map.keys():
        raise RuntimeError(
            'Start screen hot map info file ({}) corrupt.'.format(
                image_map_file))

    frame_pen = QtGui.QPen(QtGui.QBrush(QtGui.QColor(255, 255, 255, 64)), 6)
    for k, v in image_map.items():
        # add action from our predefined action dictionary
        pixmap = QtGui.QPixmap(
            constants.extend(constants.GRAPHICS_UI_FOLDER, v['overlay']))
        map_item = MapItem(scene,
                           pixmap,
                           label=subtitle,
                           description=v['label'])
        map_item.item.setZValue(3)
        offset = v['offset']
        map_item.item.setOffset(QtCore.QPointF(offset[0], offset[1]))
        map_item.item.signaller.clicked.connect(actions[k])

        frame_path = QtGui.QPainterPath()
        frame_path.addRect(map_item.item.boundingRect())
        frame_item = scene.addPath(frame_path, frame_pen)
        frame_item.setZValue(4)
        scene.addItem(map_item.item)

    version_label = QtWidgets.QLabel('<font color=#ffffff>{}</font>'.format(
        version.__version_full__))
    version_label.resize(version_label.sizeHint())
    version_label.layout_constraint = qt.RelativeLayoutConstraint().east(
        20).south(20)
    layout.addWidget(version_label)

    return screen
def create_start_screen_widget(actions):
    """
    Creates the start screen.

    :param client:
    :return:
    """

    screen = QtWidgets.QWidget()
    screen.setAttribute(QtCore.Qt.WA_StyledBackground, True)
    screen.setProperty('background', 'black')

    layout = qt.RelativeLayout(screen)

    path = constants.extend(constants.GRAPHICS_UI_FOLDER, 'start.background.jpg')
    start_image = QtGui.QPixmap(path)
    start_image_item = QtWidgets.QGraphicsPixmapItem(start_image)
    start_image_item.setZValue(1)

    scene = QtWidgets.QGraphicsScene(screen)
    scene.addItem(start_image_item)

    view = QtWidgets.QGraphicsView(scene)
    view.resize(start_image.size())
    view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    view.setSceneRect(0, 0, start_image.width(), start_image.height())
    view.layout_constraint = qt.RelativeLayoutConstraint().center_horizontal()\
        .center_vertical()
    layout.addWidget(view)

    subtitle = QtWidgets.QLabel('')
    subtitle.resize(0, 0)
    # TODO this is below the main image but collides with screens only 768 px high
    subtitle.layout_constraint = qt.RelativeLayoutConstraint(
        (0.5, -0.5, 0), (0.5, -0.5, start_image.height() / 2 + 20))
    layout.addWidget(subtitle)

    image_map_file = constants.extend(constants.GRAPHICS_UI_FOLDER, 'start.overlay.info')
    image_map = utils.read_as_yaml(image_map_file)

    # security check, they have to be the same
    if actions.keys() != image_map.keys():
        raise RuntimeError('Start screen hot map info file ({}) corrupt.'
                           .format(image_map_file))

    frame_pen = QtGui.QPen(QtGui.QBrush(QtGui.QColor(255, 255, 255, 64)), 6)
    for k, v in image_map.items():
        # add action from our predefined action dictionary
        pixmap = QtGui.QPixmap(constants.extend(constants.GRAPHICS_UI_FOLDER, v['overlay']))
        map_item = MapItem(scene, pixmap, label=subtitle, description=v['label'])
        map_item.item.setZValue(3)
        offset = v['offset']
        map_item.item.setOffset(QtCore.QPointF(offset[0], offset[1]))
        map_item.item.signaller.clicked.connect(actions[k])

        frame_path = QtGui.QPainterPath()
        frame_path.addRect(map_item.item.boundingRect())
        frame_item = scene.addPath(frame_path, frame_pen)
        frame_item.setZValue(4)
        scene.addItem(map_item.item)

    version_label = QtWidgets.QLabel('<font color=#ffffff>{}</font>'
                                     .format(version.__version_full__))
    version_label.resize(version_label.sizeHint())
    version_label.layout_constraint = qt.RelativeLayoutConstraint().east(20).south(20)
    layout.addWidget(version_label)

    return screen