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
def test_count_card_colors(cards: dict[str, MtgCard], count_hybrids: bool): card_list = list(cards.values()) pack = MtgPack({"slot": {"cards": card_list}}) colors, counts = pack.count_cards_colors(card_list, count_hybrids=count_hybrids) assert counts == { "W": 2 if count_hybrids else 1, "U": 1, "B": 1, "R": 1, "G": 2 if count_hybrids else 1, "C": 2, }
def test_json(cards: dict[str, MtgCard]): card_list = [cards["Electrolyze"], cards["Mysterious Egg"]] pack = MtgPack({"slot": {"cards": card_list}}) assert pack.json() == [ { "name": "Mysterious Egg", "count": 1 }, { "name": "Electrolyze", "count": 1 }, ]
def get_random_jmp_decks(self, n: int = 1, replace: bool = True) -> Sequence[MtgPack]: assert self.has_jmp jmp_decks = self.data.sets["JMP"].decks decks = choice(jmp_decks, size=n, replace=replace) packs = [] for d in decks: logger.debug("Generating JMP pack...") cards = [MtgCard(c) for c in d["mainBoard"]] content = {"deck": {"cards": cards, "balance": False}} packs.append( MtgPack(content, set=self.data.sets["JMP"], name=d["name"])) logger.info(f"{d['name']} (JMP) pack generated") return packs
def unbalanced_pack(generator: MtgPackGenerator) -> MtgPack: m20 = generator.data.sets["M20"].cards_by_ascii_name content = { "basicOrCommonLand": { "cards": [MtgCard(m20["bloodfell caves"])], "balance": False, }, "nonlandCommon": { "cards": [ MtgCard(m20["thicket crasher"]), MtgCard(m20["destructive digger"]), MtgCard(m20["raise the alarm"]), MtgCard(m20["angelic gift"]), MtgCard(m20["ripscale predator"]), MtgCard(m20["inspiring captain"]), MtgCard(m20["bladebrand"]), MtgCard(m20["gorging vulture"]), MtgCard(m20["griffin protector"]), MtgCard(m20["act of treason"]), ], "backups": [MtgCard(m20["fortress crab"])], "balance": True, }, "rareMythic": { "cards": [MtgCard(m20["thunderkin awakener"])], "balance": False, }, "uncommon": { "cards": [ MtgCard(m20["blightbeetle"]), MtgCard(m20["overcome"]), MtgCard(m20["herald of the sun"]), ], "balance": False, }, } p = MtgPack(content) return p
def test_arena(cards: dict[str, MtgCard]): card_list = [cards["Electrolyze"], cards["Mysterious Egg"]] pack = MtgPack({"slot": {"cards": card_list}}) assert (pack.arena_format() == "1 Mysterious Egg (IKO) 3\n1 Electrolyze (STA) 123")
def test_duplicates_in_foil(unbalanced_pack: MtgPack): card = unbalanced_pack.content["nonlandCommon"]["cards"][0] card.foil = True unbalanced_pack.content["foil"] = {"cards": [card], "balance": False} assert not unbalanced_pack.has_duplicates()
def test_duplicates(unbalanced_pack: MtgPack): assert not unbalanced_pack.has_duplicates() card = unbalanced_pack.content["nonlandCommon"]["cards"][0] unbalanced_pack.content["nonlandCommon"]["cards"].append(card) assert unbalanced_pack.has_duplicates()
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)