示例#1
0
    def from_log_document(cls,
                          log_document,
                          player_intelligence=Intelligence.Human,
                          opponent_intelligence=Intelligence.Human,
                          player_profile=None):

        initial_gamestate_document = log_document["initial_gamestate"]
        gamestate = Gamestate.from_document(initial_gamestate_document)
        if "player1" in log_document:
            player1 = Player.from_document(log_document["player1"])
            player2 = Player.from_document(log_document["player2"])
        else:
            player1 = Player("Green", player_intelligence)
            player2 = Player("Red", opponent_intelligence)

        if player_profile:
            if player1.profile == player_profile:
                player2.intelligence = player2.ai = Intelligence.Network
            elif player2.profile == player_profile:
                player1.intelligence = player1.ai = Intelligence.Network
            else:
                print(player_profile, "is not playing this game.")
                print("The players are:", player1.profile, "and",
                      player2.profile)
                return

        if "created_at" in log_document:
            created_at = log_document["created_at"]
        else:
            created_at = None

        game = cls([player1, player2], gamestate, created_at)
        game.gamestate = gamestate

        action_count = int(log_document["action_count"])

        game.actions = {}
        game.outcomes = {}

        for action_number in range(1, action_count + 1):
            if game.is_turn_done():
                game.shift_turn()

            game.gamestate.set_available_actions()

            action_document = log_document[str(action_number)]

            action = Action.from_document(gamestate.all_units(),
                                          action_document)

            game.actions[str(action_number)] = action

            options = None
            if str(action_number) + "_options" in log_document:
                options = log_document[str(action_number) + "_options"]

            game.options[str(action_number)] = options

            outcome = None
            outcome_document = None
            if action.has_outcome:
                outcome_document = log_document[str(action_number) +
                                                "_outcome"]
                outcome = Outcome.from_document(outcome_document)
                if options and "move_with_attack" in options:
                    action.move_with_attack = bool(options["move_with_attack"])

            game.outcomes[str(action_number)] = outcome_document

            game.do_action(action, outcome)

            if options and "upgrade" in options:
                upgrade = get_enum_attributes(options["upgrade"])

                position = action.end_at
                if not position in game.gamestate.player_units:
                    position = action.target_at

                upgraded_unit = action.unit.get_upgraded_unit_from_upgrade(
                    upgrade)
                game.gamestate.player_units[position] = upgraded_unit

        if game.is_turn_done():
            game.shift_turn()

        return game