예제 #1
0
def to_saved_game_format(game):
    """ Converts a game to a standardized JSON format
        :param game: game to convert.
        :return: A game in the standard JSON format used to saved game (returned object is a dictionary)
        :type game: Game
    """

    # Get phase history.
    phases = Game.get_phase_history(game)
    # Add current game phase.
    phases.append(Game.get_phase_data(game))
    # Filter rules.
    rules = [rule for rule in game.rules if rule not in RULES_TO_SKIP]
    # Extend states fields.
    phases_to_dict = [phase.to_dict() for phase in phases]
    for phase_dct in phases_to_dict:
        phase_dct['state']['game_id'] = game.game_id
        phase_dct['state']['map'] = game.map_name
        phase_dct['state']['rules'] = rules

    # Building saved game
    return {
        'id': game.game_id,
        'map': game.map_name,
        'rules': rules,
        'phases': phases_to_dict
    }
예제 #2
0
def to_saved_game_format(game, output_path=None, output_mode='a'):
    """ Converts a game to a standardized JSON format

        :param game: game to convert.
        :param output_path: Optional path to file. If set, the json.dumps() of the saved_game is written to that file.
        :param output_mode: Optional. The mode to use to write to the output_path (if provided). Defaults to 'a'
        :return: A game in the standard format used to saved game, that can be converted to JSON for serialization
        :type game: diplomacy.engine.game.Game
        :type output_path: str | None, optional
        :type output_mode: str, optional
        :rtype: Dict
    """
    phases = Game.get_phase_history(game)                                       # Get phase history.
    phases.append(Game.get_phase_data(game))                                    # Add current game phase.
    rules = [rule for rule in game.rules if rule not in RULES_TO_SKIP]          # Filter rules.

    # Extend states fields.
    phases_to_dict = [phase.to_dict() for phase in phases]
    for phase_dct in phases_to_dict:
        phase_dct['state']['game_id'] = game.game_id
        phase_dct['state']['map'] = game.map_name
        phase_dct['state']['rules'] = rules

    # Building saved game
    saved_game = {'id': game.game_id,
                  'map': game.map_name,
                  'rules': rules,
                  'phases': phases_to_dict}

    # Writing to disk
    if output_path:
        with open(output_path, output_mode) as output_file:
            output_file.write(json.dumps(saved_game) + '\n')

    # Returning
    return saved_game