예제 #1
0
def test_balancing(unbalanced_pack: MtgPack):
    assert not unbalanced_pack.is_balanced()
    unbalanced_pack.is_balanced(rebalance=True)
    assert unbalanced_pack.is_balanced()

    cards = [c.card.name for c in unbalanced_pack.cards]
    assert "Griffin Protector" not in cards
    assert "Fortress Crab" in cards
예제 #2
0
    def _get_pack_internal(self, set: str, iterations: int) -> MtgPack:
        assert set.upper() in self.data.sets

        booster = self.data.sets[set.upper()].booster
        if "default" in booster:
            booster_meta = booster["default"]
        elif "arena" in booster:
            booster_meta = booster["arena"]
        else:
            booster_type = next(iter(booster))
            booster_meta = booster[booster_type]

        boosters_p = [
            x["weight"] / booster_meta["boostersTotalWeight"]
            for x in booster_meta["boosters"]
        ]

        booster = booster_meta["boosters"][choice(len(
            booster_meta["boosters"]),
                                                  p=boosters_p)]["contents"]

        pack_content = {}

        balance = False
        for sheet_name, k in booster.items():
            sheet_meta = booster_meta["sheets"][sheet_name]
            if "balanceColors" in sheet_meta.keys():
                balance = balance or sheet_meta["balanceColors"]
                num_of_backups = 20
            else:
                num_of_backups = 0

            cards = list(sheet_meta["cards"].keys())
            cards_p = [
                x / sheet_meta["totalWeight"]
                for x in sheet_meta["cards"].values()
            ]

            picks = choice(cards,
                           size=k + num_of_backups,
                           replace=False,
                           p=cards_p)

            pick_i = 0
            slot_content = []
            slot_backup = []
            for card_id in picks:
                if pick_i < k:
                    slot_content.append(
                        MtgCard(self.data.cards_by_id[card_id],
                                sheet_meta["foil"]))
                else:
                    slot_backup.append(
                        MtgCard(self.data.cards_by_id[card_id],
                                sheet_meta["foil"]))
                pick_i += 1

            slot = {"cards": slot_content}
            slot["balance"] = (
                "balanceColors" in sheet_meta.keys()  # type: ignore
            )
            if num_of_backups:
                slot["backups"] = slot_backup

            pack_content[sheet_name] = slot

        pack_name = booster_meta.get("name", None)

        pack = MtgPack(pack_content, name=pack_name)

        if not balance:
            logger.debug("Pack should not be balanced, skipping.")
            iterations = 1

        if iterations <= 1 or pack.is_balanced(rebalance=True):
            logger.info(
                f"{set.upper()} pack generated, iterations needed: "
                f"{str(self.max_balancing_iterations - iterations + 1)}")
            return pack
        else:
            return self._get_pack_internal(set, iterations - 1)