Beispiel #1
0
    def test_create_game(self):
        card_set1 = []
        card_set2 = []
        test_env = self

        for cardIndex in range(0, 30):
            card_set1.append(card_lookup("Stonetusk Boar"))
            card_set2.append(card_lookup("Novice Engineer"))

        deck1 = Deck(card_set1, CHARACTER_CLASS.DRUID)
        deck2 = Deck(card_set2, CHARACTER_CLASS.MAGE)
        checked_cards = []

        class MockAgent1:
            def do_card_check(self, cards):
                test_env.assertEqual(len(cards), 3)
                checked_cards.append(list(cards))
                return [False, True, True]

            def set_game(self, game):
                pass

        class MockAgent2:
            def do_card_check(self, cards):
                test_env.assertEqual(len(cards), 4)
                checked_cards.append(list(cards))
                return [False, True, True, False]

            def set_game(self, game):
                pass

        agent1 = mock.Mock(spec=MockAgent1(), wraps=MockAgent1())
        agent2 = mock.Mock(spec=MockAgent2(), wraps=MockAgent2())
        game = Game([deck1, deck2], [agent1, agent2])
        game.pre_game()

        self.assertEqual(agent1.method_calls[0][0], "do_card_check",
                         "Agent not asked to select cards")
        self.assertEqual(agent2.method_calls[0][0], "do_card_check",
                         "Agent not asked to select cards")

        self.assertTrue(game.players[0].deck == deck1,
                        "Deck not assigned to player")
        self.assertTrue(game.players[1].deck == deck2,
                        "Deck not assigned to player")

        self.assertTrue(game.players[0].agent == agent1,
                        "Agent not stored in the hearthbreaker")
        self.assertTrue(game.players[1].agent == agent2,
                        "Agent not stored in the hearthbreaker")

        self.assertListEqual(checked_cards[0][1:], game.players[0].hand[:-1],
                             "Cards not retained after request")
        self.assertListEqual(checked_cards[1][1:2], game.players[1].hand[:-4],
                             "Cards not retained after request")
Beispiel #2
0
    def test_first_turn(self):
        card_set1 = []
        card_set2 = []

        for cardIndex in range(0, 30):
            card_set1.append(card_lookup("Stonetusk Boar"))
            card_set2.append(card_lookup("Novice Engineer"))

        deck1 = Deck(card_set1, CHARACTER_CLASS.DRUID)
        deck2 = Deck(card_set2, CHARACTER_CLASS.MAGE)

        agent1 = mock.Mock(spec=DoNothingBot(), wraps=DoNothingBot())
        agent2 = mock.Mock(spec=DoNothingBot(), wraps=DoNothingBot())
        game = Game([deck1, deck2], [agent1, agent2])

        game.start()
    def test_first_turn(self):
        card_set1 = []
        card_set2 = []

        for cardIndex in range(0, 30):
            card_set1.append(card_lookup("Stonetusk Boar"))
            card_set2.append(card_lookup("Novice Engineer"))

        deck1 = Deck(card_set1, CHARACTER_CLASS.DRUID)
        deck2 = Deck(card_set2, CHARACTER_CLASS.MAGE)

        agent1 = mock.Mock(spec=DoNothingBot(), wraps=DoNothingBot())
        agent2 = mock.Mock(spec=DoNothingBot(), wraps=DoNothingBot())
        game = Game([deck1, deck2], [agent1, agent2])

        game.start()
    def test_all_cards(self):
        fake_game = generate_game_for(StonetuskBoar, StonetuskBoar, DoNothingBot, DoNothingBot)
        overload_re = re.compile("Overload: \((\\d)\)")
        spell_damage_re = re.compile("Spell Damage \\+(\\d)")
        battlecry_re = re.compile("Battlecry: .*")
        deathrattle_re = re.compile("Deathrattle: .*")
        split_re = re.compile("\\s*\\.\\s*")
        file = open("cards.csv", "r")
        reader = csv.DictReader(file)
        for row in reader:
            if row['Implemented?'] == "yes":
                card = card_lookup(row["Name"])
                self.assertEqual(int(row["Cost"]), card.mana, row["Name"])
                self.assertEqual(CHARACTER_CLASS.from_str(row["Class"]), card.character_class, row["Name"])
                self.assertEqual(CARD_RARITY.from_str(row["Rarity"]), card.rarity,
                                 "Expected card '{0}' to have rarity {1}".format(row["Name"], row["Rarity"]))
                if row["Type"] == "Minion":
                    minion = card.create_minion(fake_game.current_player)
                    minion.player = fake_game.current_player
                    minion.game = fake_game
                    for effect in split_re.split(row["Text"]):
                        if effect == "Taunt":
                            self.assertTrue(minion.taunt, "Expected {:s} to have taunt".format(row["Name"]))
                        elif effect == "Divine Shield":
                            self.assertTrue(minion.divine_shield,
                                            "Expected {:s} to have divine shield".format(row["Name"]))
                        elif effect == "Stealth":
                            self.assertTrue(minion.stealth, "Expected {:s} to have stealth".format(row["Name"]))
                        elif effect == "Windfury":
                            self.assertTrue(minion.windfury, "Expected {:s} to have windfury".format(row["Name"]))
                        elif effect == "Charge":
                            self.assertTrue(minion.charge, "Expected {:s} to have charge".format(row["Name"]))
                        elif battlecry_re.match(effect):
                            self.assertTrue(minion.battlecry is not None, "Expected {:s} to have a battlecry".format
                                            (row["Name"]))
                        elif deathrattle_re.match(effect):
                            self.assertTrue(minion.deathrattle is not None, "Expected {:s} to have a deathrattle".format
                                            (row["Name"]))
                        elif overload_re.match(effect) is not None:
                            self.assertEqual(int(overload_re.match(effect).group(1)), card.overload,
                                             ("Expected {:s} to have overload of" +
                                             " {:s}, but had {:d}").format(row["Name"],
                                                                           overload_re.match(effect).group(1),
                                             card.overload))
                        elif spell_damage_re.match(effect) is not None:
                            self.assertEqual(int(spell_damage_re.match(effect).group(1)), minion.spell_damage,
                                             ("Expected {:s} to have spell damage of" +
                                             " {:s}, but had {:d}").format(row["Name"],
                                                                           spell_damage_re.match(effect).group(1),
                                             minion.spell_damage))
                    minion.silence()
                    self.assertEqual(int(row["Attack"]), minion.calculate_attack(), row["Name"])
                    self.assertEqual(int(row["Health"]), minion.health, row["Name"])
                    self.assertEqual(MINION_TYPE.from_str(row["Race"]), card.minion_type, row["Name"])
                elif row["Type"] == "Weapon":
                    weapon = card.create_weapon(fake_game.current_player)
                    self.assertEqual(int(row["Attack"]), weapon.base_attack, row["Name"])
                    self.assertEqual(int(row["Health"]), weapon.durability, row["Name"])

        file.close()
    def test_create_game(self):
        card_set1 = []
        card_set2 = []
        test_env = self

        for cardIndex in range(0, 30):
            card_set1.append(card_lookup("Stonetusk Boar"))
            card_set2.append(card_lookup("Novice Engineer"))

        deck1 = Deck(card_set1, CHARACTER_CLASS.DRUID)
        deck2 = Deck(card_set2, CHARACTER_CLASS.MAGE)
        checked_cards = []

        class MockAgent1:
            def do_card_check(self, cards):
                test_env.assertEqual(len(cards), 3)
                checked_cards.append(list(cards))
                return [False, True, True]

            def set_game(self, game):
                pass

        class MockAgent2:
            def do_card_check(self, cards):
                test_env.assertEqual(len(cards), 4)
                checked_cards.append(list(cards))
                return [False, True, True, False]

            def set_game(self, game):
                pass

        agent1 = mock.Mock(spec=MockAgent1(), wraps=MockAgent1())
        agent2 = mock.Mock(spec=MockAgent2(), wraps=MockAgent2())
        game = Game([deck1, deck2], [agent1, agent2])
        game.pre_game()

        self.assertEqual(agent1.method_calls[0][0], "do_card_check", "Agent not asked to select cards")
        self.assertEqual(agent2.method_calls[0][0], "do_card_check", "Agent not asked to select cards")

        self.assertTrue(game.players[0].deck == deck1, "Deck not assigned to player")
        self.assertTrue(game.players[1].deck == deck2, "Deck not assigned to player")

        self.assertTrue(game.players[0].agent == agent1, "Agent not stored in the hearthbreaker")
        self.assertTrue(game.players[1].agent == agent2, "Agent not stored in the hearthbreaker")

        self.assertListEqual(checked_cards[0][1:], game.players[0].hand[:-1], "Cards not retained after request")
        self.assertListEqual(checked_cards[1][1:2], game.players[1].hand[:-4], "Cards not retained after request")
Beispiel #6
0
 def from_json(card, actions, selector, condition=None):
     from hearthbreaker.game_objects import card_lookup
     actions = [Action.from_json(**action) for action in actions]
     selector = Selector.from_json(**selector)
     if condition:
         condition = Condition.from_json(**condition)
     card = card_lookup(card)
     return Choice(card, actions, selector, condition)
Beispiel #7
0
 def from_json(card, actions, selector, condition=None):
     from hearthbreaker.game_objects import card_lookup
     actions = [Action.from_json(**action) for action in actions]
     selector = Selector.from_json(**selector)
     if condition:
         condition = Condition.from_json(**condition)
     card = card_lookup(card)
     return Choice(card, actions, selector, condition)
Beispiel #8
0
    def test_play_with_one_card(self):
            file = open("cards.csv", "r")
            reader = csv.DictReader(file)
            for row in reader:
                if row["Implemented?"] == "yes":
                    card = card_lookup(row["Name"])
                    game = generate_game_for(type(card), StonetuskBoar, PredictableAgentWithoutHeroPower, DoNothingBot)

                    while not game.game_ended:
                        game.play_single_turn()
Beispiel #9
0
    def get_card(self, player):
        from hearthbreaker.game_objects import card_lookup, get_cards
        if self.name:
            return card_lookup(self.name)

        if self.source == CARD_SOURCE.COLLECTION:
            card_list = get_cards()
        elif self.source == CARD_SOURCE.MY_DECK:
            card_list = filter(lambda c: not c.drawn, player.deck.cards)
        elif self.source == CARD_SOURCE.MY_HAND:
            card_list = player.hand
        elif self.source == CARD_SOURCE.OPPONENT_DECK:
            card_list = filter(lambda c: not c.drawn,
                               player.opponent.deck.cards)
        elif self.source == CARD_SOURCE.OPPONENT_HAND:
            card_list = player.opponent.hand
        elif self.source == CARD_SOURCE.LIST:
            card_list = self.source_list
        elif self.source == CARD_SOURCE.LAST_SPELL:
            return player.game.last_spell
        else:
            card_list = []
        # TODO Throw an exception in any other case?

        def check_condition(condition):
            return lambda c: condition.evaluate(player, c)

        for condition in self.conditions:
            card_list = filter(check_condition(condition), card_list)

        card_list = [card for card in card_list]
        card_len = len(card_list)
        if card_len == 1:
            chosen_card = card_list[0]
        elif card_len == 0:
            return None
        else:
            chosen_card = player.game.random_choice(card_list)

        if self.source == CARD_SOURCE.COLLECTION or self.source == CARD_SOURCE.LIST or self.make_copy:
            return chosen_card
        elif self.source == CARD_SOURCE.MY_DECK:
            chosen_card.drawn = True
            player.deck.left -= 1
            return chosen_card
        elif self.source == CARD_SOURCE.OPPONENT_DECK:
            chosen_card.drawn = True
            player.opponent.deck.left -= 1
            return chosen_card
        elif self.source == CARD_SOURCE.MY_HAND:
            player.hand.remove(chosen_card)
            return chosen_card
        elif self.source == CARD_SOURCE.OPPONENT_HAND:
            player.opponent.hand.remove(chosen_card)
            return chosen_card
Beispiel #10
0
    def get_card(self, player):
        from hearthbreaker.game_objects import card_lookup, get_cards
        if self.name:
            return card_lookup(self.name)

        if self.source == CARD_SOURCE.COLLECTION:
            card_list = get_cards()
        elif self.source == CARD_SOURCE.MY_DECK:
            card_list = filter(lambda c: not c.drawn, player.deck.cards)
        elif self.source == CARD_SOURCE.MY_HAND:
            card_list = player.hand
        elif self.source == CARD_SOURCE.OPPONENT_DECK:
            card_list = filter(lambda c: not c.drawn, player.opponent.deck.cards)
        elif self.source == CARD_SOURCE.OPPONENT_HAND:
            card_list = player.opponent.hand
        elif self.source == CARD_SOURCE.LIST:
            card_list = self.source_list
        elif self.source == CARD_SOURCE.LAST_SPELL:
            return type(player.game.last_spell)()
        else:
            card_list = []
        # TODO Throw an exception in any other case?

        def check_condition(condition):
            return lambda c: condition.evaluate(player, c)

        for condition in self.conditions:
            card_list = filter(check_condition(condition), card_list)

        card_list = [card for card in card_list]
        card_len = len(card_list)
        if card_len == 1:
            chosen_card = card_list[0]
        elif card_len == 0:
            return None
        else:
            chosen_card = player.game.random_choice(card_list)

        if self.source == CARD_SOURCE.COLLECTION or self.source == CARD_SOURCE.LIST or self.make_copy:
            return chosen_card
        elif self.source == CARD_SOURCE.MY_DECK:
            chosen_card.drawn = True
            player.deck.left -= 1
            return chosen_card
        elif self.source == CARD_SOURCE.OPPONENT_DECK:
            chosen_card.drawn = True
            player.opponent.deck.left -= 1
            return chosen_card
        elif self.source == CARD_SOURCE.MY_HAND:
            player.hand.remove(chosen_card)
            return chosen_card
        elif self.source == CARD_SOURCE.OPPONENT_HAND:
            player.opponent.hand.remove(chosen_card)
            return chosen_card
Beispiel #11
0
    def test_play_with_one_card(self):
        file = open("cards.csv", "r")
        reader = csv.DictReader(file)
        for row in reader:
            card = card_lookup(row["Name"])
            game = generate_game_for(type(card), StonetuskBoar,
                                     PredictableAgentWithoutHeroPower,
                                     DoNothingBot)

            while not game.game_ended:
                game.play_single_turn()
Beispiel #12
0
    def test_play_with_one_card(self):
            file = open("cards.csv", "r")
            reader = csv.DictReader(file)
            for row in reader:
                if row["Implemented?"] == "yes":
                    card = card_lookup(row["Name"])
                    game = generate_game_for(type(card), StonetuskBoar, PlayAndAttackAgent, DoNothingAgent)

                    while not game.game_ended:
                        game.play_single_turn()
            file.close()
    def test_play_with_one_card(self):
        file = open("AllSets.enUS.json", "r", encoding="UTF-8")
        card_dict = json.load(file)
        for card_set in ['Expert', "Basic", "Curse of Naxxramas", "Goblins vs Gnomes"]:

            for card_info in card_dict[card_set]:
                if 'collectible' in card_info and card_info['collectible'] and card_info["type"] != "Hero":
                    try:
                        card = card_lookup(card_info["name"])
                    except KeyError:
                        continue
                    game = generate_game_for(type(card), StonetuskBoar, PlayAndAttackAgent, DoNothingAgent)

                    while not game.game_ended:
                        game.play_single_turn()
            file.close()
Beispiel #14
0
 def from_json(name=None, conditions=[], source="collection", source_list=None, make_copy=False):
     from hearthbreaker.game_objects import card_lookup
     query = CardQuery.__new__(CardQuery)
     query.name = name
     query.conditions = []
     for condition in conditions:
         query.conditions.append(Condition.from_json(**condition))
     else:
         query.condition = None
     query.source = CARD_SOURCE.from_str(source)
     if source_list:
         query.source_list = [card_lookup(item) for item in source_list]
     else:
         query.source_list = None
     query.make_copy = make_copy
     return query
Beispiel #15
0
def load_deck(filename):
    deck_file = open(filename, "r")
    contents = deck_file.read()
    items = re.split('\n', contents)
    cards = []
    character_class = CHARACTER_CLASS.MAGE
    for line in items[0:]:
        line = line.strip(" \n\t\r")
        parts = line.split(" ", 1)
        count = int(parts[0])
        for i in range(0, count):
            card = card_lookup(parts[1])
            if card.character_class != CHARACTER_CLASS.ALL:
                character_class = card.character_class
            cards.append(card)

    deck_file.close()

    return Deck(cards, character_class)
Beispiel #16
0
def load_deck(filename):
    deck_file = open(filename, "r")
    contents = deck_file.read()
    items = re.split('\n', contents)
    cards = []
    character_class = CHARACTER_CLASS.MAGE
    for line in items[0:]:
        line = line.strip(" \n\t\r")
        parts = line.split(" ", 1)
        count = int(parts[0])
        for i in range(0, count):
            card = card_lookup(parts[1])
            if card.character_class != CHARACTER_CLASS.ALL:
                character_class = card.character_class
            cards.append(card)

    deck_file.close()

    return Deck(cards, character_class)
Beispiel #17
0
def load_deck(filename):
    cards = []
    character_class = CHARACTER_CLASS.MAGE

    with open(filename, "r") as deck_file:
        contents = deck_file.read()
        items = contents.splitlines()
        for line in items[0:]:
            parts = line.split(" ", 1)
            count = int(parts[0])
            for i in range(0, count):
                card = card_lookup(parts[1])
                if card.character_class != CHARACTER_CLASS.ALL:
                    character_class = card.character_class
                cards.append(card)

    if len(cards) > 30:
        pass

    return Deck(cards, character_class)
def load_deck(filename):
    cards = []
    character_class = CHARACTER_CLASS.MAGE

    with open(filename, "r") as deck_file:
        contents = deck_file.read()
        items = contents.splitlines()
        for line in items[0:]:
            parts = line.split(" ", 1)
            count = int(parts[0])
            for i in range(0, count):
                card = card_lookup(parts[1])
                if card.character_class != CHARACTER_CLASS.ALL:
                    character_class = card.character_class
                cards.append(card)

    if len(cards) > 30:
        pass

    return Deck(cards, character_class)
Beispiel #19
0
 def from_json(name=None,
               conditions=[],
               source="collection",
               source_list=None,
               make_copy=False):
     from hearthbreaker.game_objects import card_lookup
     query = CardQuery.__new__(CardQuery)
     query.name = name
     query.conditions = []
     for condition in conditions:
         query.conditions.append(Condition.from_json(**condition))
     else:
         query.condition = None
     query.source = CARD_SOURCE.from_str(source)
     if source_list:
         query.source_list = [card_lookup(item) for item in source_list]
     else:
         query.source_list = None
     query.make_copy = make_copy
     return query
Beispiel #20
0
    def test_play_with_one_card(self):
        file = open("AllSets.enUS.json", "r", encoding="UTF-8")
        card_dict = json.load(file)
        for card_set in [
                'Expert', "Basic", "Curse of Naxxramas", "Goblins vs Gnomes"
        ]:

            for card_info in card_dict[card_set]:
                if 'collectible' in card_info and card_info[
                        'collectible'] and card_info["type"] != "Hero":
                    try:
                        card = card_lookup(card_info["name"])
                    except KeyError:
                        continue
                    game = generate_game_for(type(card), StonetuskBoar,
                                             PlayAndAttackAgent,
                                             DoNothingAgent)

                    while not game.game_ended:
                        game.play_single_turn()
            file.close()
Beispiel #21
0
    def test_all_cards(self):
        fake_game = generate_game_for(StonetuskBoar, StonetuskBoar,
                                      DoNothingAgent, DoNothingAgent)
        overload_re = re.compile("Overload: \\((\\d)\\)")
        spell_damage_re = re.compile("Spell Damage \\+(\\d)")
        battlecry_re = re.compile("Battlecry: .*")
        deathrattle_re = re.compile("Deathrattle: .*")
        split_re = re.compile("\\s*\\.\\s*")
        bold_tag_re = re.compile("</?b>")
        file = open("AllSets.enUS.json", "r", encoding="UTF-8")
        card_dict = json.load(file)
        not_implemented = []
        for card_set in [
                'Expert', "Basic", "Curse of Naxxramas", "Goblins vs Gnomes"
        ]:
            for card_info in card_dict[card_set]:
                if 'collectible' in card_info and card_info[
                        'collectible'] and card_info["type"] != "Hero":
                    try:
                        card = card_lookup(card_info["name"])
                    except KeyError:
                        not_implemented.append(card_info["name"])
                        continue
                    self.assertEqual(
                        int(card_info["cost"]), card.mana,
                        "Expected {} to have cost {}.  Got {}".format(
                            card_info["name"], card_info["cost"], card.mana))
                    if "playerClass" in card_info:
                        self.assertEqual(
                            CHARACTER_CLASS.from_str(card_info["playerClass"]),
                            card.character_class,
                            "Expected {} to have class {}.  Got {}".format(
                                card_info["name"], card_info["playerClass"],
                                CHARACTER_CLASS.to_str(card.character_class)))
                    else:
                        self.assertEqual(
                            CHARACTER_CLASS.ALL, card.character_class,
                            "Expected {} to have no class.  Got {}".format(
                                card_info["name"],
                                CHARACTER_CLASS.to_str(card.character_class)))
                    self.assertEqual(
                        CARD_RARITY.from_str(card_info["rarity"]), card.rarity,
                        "Expected card {} to have rarity {}.  Got {}".format(
                            card_info["name"], card_info["rarity"],
                            CARD_RARITY.to_str(card.rarity)))
                    if card_info["type"] == "Minion":
                        minion = card.create_minion(fake_game.current_player)
                        minion.player = fake_game.current_player
                        minion.game = fake_game
                        minion.card = card
                        minion.add_to_board(0)
                        if "text" in card_info:
                            for effect in split_re.split(
                                    re.sub(bold_tag_re, "",
                                           card_info["text"])):
                                if effect == "Taunt":
                                    self.assertTrue(
                                        minion.taunt,
                                        "Expected {:s} to have taunt".format(
                                            card_info["name"]))
                                elif effect == "Divine Shield":
                                    self.assertTrue(
                                        minion.divine_shield,
                                        "Expected {:s} to have divine shield".
                                        format(card_info["name"]))
                                elif effect == "Stealth":
                                    self.assertTrue(
                                        minion.stealth,
                                        "Expected {:s} to have stealth".format(
                                            card_info["name"]))
                                elif effect == "Windfury":
                                    self.assertTrue(
                                        minion.windfury,
                                        "Expected {:s} to have windfury".
                                        format(card_info["name"]))
                                elif effect == "Charge":
                                    self.assertTrue(
                                        minion.charge,
                                        "Expected {:s} to have charge".format(
                                            card_info["name"]))
                                elif battlecry_re.match(effect):
                                    self.assertTrue(
                                        minion.battlecry is not None
                                        or card.battlecry is not None,
                                        "Expected {:s} to have a battlecry".
                                        format(card_info["name"]))
                                elif deathrattle_re.match(effect):
                                    self.assertTrue(
                                        minion.deathrattle is not None,
                                        "Expected {:s} to have a deathrattle".
                                        format(card_info["name"]))
                                elif overload_re.match(effect) is not None:
                                    self.assertEqual(
                                        int(
                                            overload_re.match(effect).group(
                                                1)), card.overload,
                                        ("Expected {:s} to have overload of" +
                                         " {:s}, but had {:d}").format(
                                             card_info["name"],
                                             overload_re.match(effect).group(
                                                 1), card.overload))
                                elif spell_damage_re.match(effect) is not None:
                                    self.assertEqual(
                                        int(
                                            spell_damage_re.match(
                                                effect).group(1)),
                                        minion.player.spell_damage,
                                        ("Expected {:s} to have spell damage of"
                                         + " {}, but had {}").format(
                                             card_info["name"],
                                             spell_damage_re.match(
                                                 effect).group(1),
                                             minion.player.spell_damage))
                        minion.silence()
                        self.assertEqual(
                            int(card_info["attack"]),
                            minion.calculate_attack(),
                            "Expected {} to have attack of {}.  Got {}".format(
                                card_info["name"], card_info["attack"],
                                minion.calculate_attack()))
                        self.assertEqual(
                            int(card_info["health"]), minion.health,
                            "Expected {} to have health of {}.  Got {}".format(
                                card_info["name"], card_info["health"],
                                minion.health))
                        if "race" in card_info:
                            self.assertEqual(
                                MINION_TYPE.from_str(card_info["race"]),
                                card.minion_type,
                                "Expected {} to have race {}.  Got {}".format(
                                    card_info["name"], card_info["race"],
                                    MINION_TYPE.to_str(card.minion_type)))
                        else:
                            self.assertEqual(
                                MINION_TYPE.NONE, card.minion_type,
                                "Expected {} to have no race.  Got {}".format(
                                    card_info["name"],
                                    MINION_TYPE.to_str(card.minion_type)))
                    elif card_info["type"] == "Weapon":
                        weapon = card.create_weapon(fake_game.current_player)
                        self.assertEqual(
                            int(card_info["attack"]), weapon.base_attack,
                            "Expected {} to have attack of {}.  Got {}".format(
                                card_info["name"], card_info["attack"],
                                weapon.base_attack))
                        self.assertEqual(
                            int(card_info["durability"]), weapon.durability,
                            "Expected {} to have durability of {}.  Got {}".
                            format(card_info["name"], card_info["durability"],
                                   weapon.durability))

        file.close()
        if len(not_implemented) > 0:
            print("Cards not implemented: {}".format(str(not_implemented)))
    def test_all_cards(self):
        fake_game = generate_game_for(StonetuskBoar, StonetuskBoar, DoNothingAgent, DoNothingAgent)
        overload_re = re.compile("Overload: \\((\\d)\\)")
        spell_damage_re = re.compile("Spell Damage \\+(\\d)")
        battlecry_re = re.compile("Battlecry: .*")
        deathrattle_re = re.compile("Deathrattle: .*")
        split_re = re.compile("\\s*\\.\\s*")
        bold_tag_re = re.compile("</?b>")
        file = open("AllSets.enUS.json", "r", encoding="UTF-8")
        card_dict = json.load(file)
        not_implemented = []
        total_cards = 0
        for card_set in ['Expert', "Basic", "Curse of Naxxramas", "Goblins vs Gnomes"]:
            for card_info in card_dict[card_set]:
                if 'collectible' in card_info and card_info['collectible'] and card_info["type"] != "Hero":
                    total_cards += 1
                    try:
                        card = card_lookup(card_info["name"])
                    except KeyError:
                        not_implemented.append(card_info["name"])
                        continue
                    self.assertEqual(int(card_info["cost"]), card.mana,
                                     "Expected {} to have cost {}.  Got {}".format(
                                     card_info["name"], card_info["cost"], card.mana))
                    if "playerClass" in card_info:
                        self.assertEqual(CHARACTER_CLASS.from_str(card_info["playerClass"]), card.character_class,
                                         "Expected {} to have class {}.  Got {}".format(
                                             card_info["name"], card_info["playerClass"],
                                             CHARACTER_CLASS.to_str(card.character_class)))
                    else:
                        self.assertEqual(CHARACTER_CLASS.ALL, card.character_class,
                                         "Expected {} to have no class.  Got {}".format(
                                             card_info["name"], CHARACTER_CLASS.to_str(card.character_class)))
                    self.assertEqual(CARD_RARITY.from_str(card_info["rarity"]), card.rarity,
                                     "Expected card {} to have rarity {}.  Got {}".format(
                                         card_info["name"], card_info["rarity"], CARD_RARITY.to_str(card.rarity)))
                    if card_info["type"] == "Minion":
                        minion = card.create_minion(fake_game.current_player)
                        minion.player = fake_game.current_player
                        minion.game = fake_game
                        minion.card = card
                        minion.add_to_board(0)
                        if "text" in card_info:
                            for effect in split_re.split(re.sub(bold_tag_re, "", card_info["text"])):
                                if effect == "Taunt":
                                    self.assertTrue(minion.taunt,
                                                    "Expected {:s} to have taunt".format(card_info["name"]))
                                elif effect == "Divine Shield":
                                    self.assertTrue(minion.divine_shield,
                                                    "Expected {:s} to have divine shield".format(card_info["name"]))
                                elif effect == "Stealth":
                                    self.assertTrue(minion.stealth,
                                                    "Expected {:s} to have stealth".format(card_info["name"]))
                                elif effect == "Windfury":
                                    self.assertTrue(minion.windfury,
                                                    "Expected {:s} to have windfury".format(card_info["name"]))
                                elif effect == "Charge":
                                    self.assertTrue(minion.charge,
                                                    "Expected {:s} to have charge".format(card_info["name"]))
                                elif battlecry_re.match(effect):
                                    self.assertTrue(minion.battlecry is not None or card.battlecry is not None,
                                                    "Expected {:s} to have a battlecry".format
                                                    (card_info["name"]))
                                elif deathrattle_re.match(effect):
                                    self.assertTrue(minion.deathrattle is not None,
                                                    "Expected {:s} to have a deathrattle".format
                                                    (card_info["name"]))
                                elif overload_re.match(effect) is not None:
                                    self.assertEqual(int(overload_re.match(effect).group(1)), card.overload,
                                                     ("Expected {:s} to have overload of" +
                                                     " {:s}, but had {:d}").format(card_info["name"],
                                                                                   overload_re.match(effect).group(1),
                                                     card.overload))
                                elif spell_damage_re.match(effect) is not None:
                                    self.assertEqual(int(spell_damage_re.match(effect).group(1)),
                                                     minion.player.spell_damage,
                                                     ("Expected {:s} to have spell damage of" +
                                                     " {}, but had {}").format(card_info["name"],
                                                                               spell_damage_re.match(effect).group(1),
                                                     minion.player.spell_damage))
                        minion.silence()
                        self.assertEqual(int(card_info["attack"]), minion.calculate_attack(),
                                         "Expected {} to have attack of {}.  Got {}".format(
                                         card_info["name"], card_info["attack"], minion.calculate_attack()))
                        self.assertEqual(int(card_info["health"]), minion.health,
                                         "Expected {} to have health of {}.  Got {}".format(
                                         card_info["name"], card_info["health"], minion.health))
                        if "race" in card_info:
                            self.assertEqual(MINION_TYPE.from_str(card_info["race"]), card.minion_type,
                                             "Expected {} to have race {}.  Got {}".format(
                                             card_info["name"], card_info["race"],
                                             MINION_TYPE.to_str(card.minion_type)))
                        else:
                            self.assertEqual(MINION_TYPE.NONE, card.minion_type,
                                             "Expected {} to have no race.  Got {}".format(
                                                 card_info["name"], MINION_TYPE.to_str(card.minion_type)))
                    elif card_info["type"] == "Weapon":
                        weapon = card.create_weapon(fake_game.current_player)
                        self.assertEqual(int(card_info["attack"]), weapon.base_attack,
                                         "Expected {} to have attack of {}.  Got {}".format(
                                         card_info["name"], card_info["attack"], weapon.base_attack))
                        self.assertEqual(int(card_info["durability"]), weapon.durability,
                                         "Expected {} to have durability of {}.  Got {}".format(
                                         card_info["name"], card_info["durability"], weapon.durability))

        file.close()
        if len(not_implemented) > 0:
            print("{} of {} cards implemented".format(total_cards - len(not_implemented), total_cards))
            print("Cards not implemented:")
            for card in not_implemented:
                print("  - {}".format(card))
Beispiel #23
0
    def test_all_cards(self):
        fake_game = generate_game_for(StonetuskBoar, StonetuskBoar,
                                      DoNothingBot, DoNothingBot)
        overload_re = re.compile("Overload: \((\\d)\)")
        spell_damage_re = re.compile("Spell Damage \\+(\\d)")
        battlecry_re = re.compile("Battlecry: .*")
        deathrattle_re = re.compile("Deathrattle: .*")
        split_re = re.compile("\\s*\\.\\s*")
        file = open("cards.csv", "r")
        reader = csv.DictReader(file)
        implemented_count = 0
        total_count = 0
        for row in reader:
            total_count += 1
            if row['Implemented?'] == "yes":
                implemented_count += 1
                card = card_lookup(row["Name"])
                self.assertEqual(int(row["Cost"]), card.mana, row["Name"])
                self.assertEqual(CHARACTER_CLASS.from_str(row["Class"]),
                                 card.character_class, row["Name"])
                self.assertEqual(
                    CARD_RARITY.from_str(row["Rarity"]), card.rarity,
                    "Expected card '{0}' to have rarity {1}".format(
                        row["Name"], row["Rarity"]))
                if row["Type"] == "Minion":
                    minion = card.create_minion(fake_game.current_player)
                    minion.player = fake_game.current_player
                    for effect in split_re.split(row["Text"]):
                        if effect == "Taunt":
                            self.assertTrue(
                                minion.taunt,
                                "Expected {:s} to have taunt".format(
                                    row["Name"]))
                        elif effect == "Divine Shield":
                            self.assertTrue(
                                minion.divine_shield,
                                "Expected {:s} to have divine shield".format(
                                    row["Name"]))
                        elif effect == "Stealth":
                            self.assertTrue(
                                minion.stealth,
                                "Expected {:s} to have stealth".format(
                                    row["Name"]))
                        elif effect == "Windfury":
                            self.assertTrue(
                                minion.windfury,
                                "Expected {:s} to have windfury".format(
                                    row["Name"]))
                        elif effect == "Charge":
                            self.assertTrue(
                                minion.charge,
                                "Expected {:s} to have charge".format(
                                    row["Name"]))
                        elif battlecry_re.match(effect):
                            self.assertTrue(
                                minion.battlecry is not None,
                                "Expected {:s} to have a battlecry".format(
                                    row["Name"]))
                        elif deathrattle_re.match(effect):
                            self.assertTrue(
                                minion.deathrattle is not None,
                                "Expected {:s} to have a deathrattle".format(
                                    row["Name"]))
                        elif overload_re.match(effect) is not None:
                            self.assertEqual(
                                int(overload_re.match(effect).group(1)),
                                card.overload,
                                ("Expected {:s} to have overload of" +
                                 " {:s}, but had {:d}").format(
                                     row["Name"],
                                     overload_re.match(effect).group(1),
                                     card.overload))
                        elif spell_damage_re.match(effect) is not None:
                            self.assertEqual(
                                int(spell_damage_re.match(effect).group(1)),
                                minion.spell_damage,
                                ("Expected {:s} to have spell damage of" +
                                 " {:s}, but had {:d}").format(
                                     row["Name"],
                                     spell_damage_re.match(effect).group(1),
                                     minion.spell_damage))
                    minion.silence()
                    self.assertEqual(int(row["Attack"]),
                                     minion.calculate_attack(), row["Name"])
                    self.assertEqual(int(row["Health"]), minion.health,
                                     row["Name"])
                    self.assertEqual(MINION_TYPE.from_str(row["Race"]),
                                     card.minion_type, row["Name"])
                elif row["Type"] == "Weapon":
                    weapon = card.create_weapon(fake_game.current_player)
                    self.assertEqual(int(row["Attack"]), weapon.base_attack,
                                     row["Name"])
                    self.assertEqual(int(row["Health"]), weapon.durability,
                                     row["Name"])

        file.close()
        print("Implemented {0} cards, with {1} cards left".format(
            implemented_count, total_count - implemented_count))