def handle(self, *args, **options): path = options["path"] db, _ = cardxml.load_dbf(path, locale=options["locale"]) self.stdout.write("%i cards available" % (len(db))) qs = Card.objects.all().values_list("dbf_id") known_ids = [item[0] for item in qs] missing = [id for id in db if id not in known_ids] self.stdout.write("%i known cards" % (len(known_ids))) new_cards = [Card.from_cardxml(db[id]) for id in missing] Card.objects.bulk_create(new_cards) self.stdout.write("%i new cards" % (len(new_cards))) if options["force"]: existing = Card.objects.filter(dbf_id__in=known_ids) for card in existing: if card.dbf_id not in db: self.stderr.write( f"WARNING: {repr(card)} ({card.dbf_id}) not in CardDefs.xml. " "Skipping.") continue c = db[card.dbf_id] if c: try: card.update_from_cardxml(c, save=True) except IntegrityError as e: if options["ignore_conflicts"]: self.stderr.write( f"WARNING: Ignoring {repr(card)} ({card.dbf_id}) conflict:" f"{e}") else: raise e self.stdout.write("%i updated cards" % (len(existing))) tags = [] for dbf_id, card in db.items(): for tag, value in card.tags.items(): tags.append(CardTag(card_id=dbf_id, game_tag=tag, value=value)) locstrings = [] for dbf_id, card in db.items(): for tag, locstring in card.strings.items(): if isinstance(locstring, str): # (non-localized string) continue for locale, value in locstring.items(): locale = Locale[locale] locstrings.append( CardString(card_id=dbf_id, locale=locale, game_tag=tag, value=value)) with transaction.atomic(): CardTag.objects.all().delete() CardString.objects.all().delete() CardTag.objects.bulk_create(tags) CardString.objects.bulk_create(locstrings)
def write_dbf_json(): dbf = cardxml.load_dbf()[0] new_json = { str(id): card.name for id, card in dbf.items() if card.collectible } f = open(path.join(path.dirname(__file__), 'src/main/resources/dbf.json'), 'w') dump(new_json, f, indent=2) f.close()
def test_cardxml_load(): cardid_db, _ = cardxml.load() dbf_db, _ = cardxml.load_dbf() assert cardid_db assert dbf_db for card_id, card in cardid_db.items(): assert dbf_db[card.dbf_id].id == card_id for dbf_id, card in dbf_db.items(): assert cardid_db[card.id].dbf_id == dbf_id
def test_cardxml_load(): cardid_db, _ = cardxml.load() dbf_db, _ = cardxml.load_dbf() assert cardid_db assert dbf_db for card_id, card in cardid_db.items(): assert dbf_db[card.dbf_id].id == card_id for dbf_id, card in dbf_db.items(): assert cardid_db[card.id].dbf_id == dbf_id assert cardid_db["EX1_001"].quest_reward == "" assert cardid_db["UNG_940"].quest_reward == "UNG_940t8"
def main(): LAMBDA = boto3.client("lambda") db, _ = cardxml.load_dbf() for card in db.values(): if card.type == CardType.ENCHANTMENT: continue print("Rendering %r" % (card.card_id)) for locale in Locale: if locale.unused: continue for resolution in (256, 512): LAMBDA.invoke(FunctionName=FUNCTION_NAME, InvocationType="Event", Payload=json.dumps({ "queryStringParameters": { "locale": locale.name, "template": card.card_id, "resolution": str(resolution), }, }))
def main(): LAMBDA = boto3.client("lambda") db, _ = cardxml.load_dbf() for card in db.values(): if card.type == CardType.ENCHANTMENT: continue print("Rendering %r" % (card.card_id)) for locale in Locale: if locale.unused: continue for resolution in (256, 512): LAMBDA.invoke( FunctionName=FUNCTION_NAME, InvocationType="Event", Payload=json.dumps({ "queryStringParameters": { "locale": locale.name, "template": card.card_id, "resolution": str(resolution), }, }) )
import sys from prettytable import PrettyTable from hearthstone.deckstrings import Deck from hearthstone.cardxml import load_dbf from hearthstone.enums import get_localized_name for i, hs_deck in enumerate(sys.argv): unknown = 0 if not i == 0: locale = 'enUS' deck = Deck.from_deckstring(hs_deck) db, _ = load_dbf(locale=locale) english_db = db card_includes = deck.cards cards = [] for dbf_id, count in card_includes: try: cards.append((db[dbf_id], count)) except KeyError: unknown += 1 cards.sort(key=lambda include: (include[0].cost, include[0].name)) try: hero = db[deck.heroes[0]] class_name = get_localized_name(hero.card_class, locale=locale) hero_name = f"{class_name} ({hero.name})" except KeyError: hero_name = "Unknown"
def card_db(): if "db" not in _CARD_DATA_CACHE: from hearthstone.cardxml import load_dbf db, _ = load_dbf() _CARD_DATA_CACHE["db"] = db return _CARD_DATA_CACHE["db"]
import pytest from django.core.management import call_command from hearthstone.cardxml import load_dbf from hearthstone.enums import Locale from django_hearthstone.cards.models import Card db, _ = load_dbf() @pytest.mark.django_db def test_load_cards(): call_command("load_cards") assert Card.objects.count() > 3500 wisp = Card.objects.get(dbf_id=179) assert wisp.dbf_id == 179 assert wisp.card_id == "CS2_231" assert wisp.name == "Wisp" assert wisp.localized_name(Locale.enUS) == "Wisp" assert wisp.localized_name(Locale.frFR) == "Feu follet" assert {o.game_tag: o.value for o in wisp.tags.all()} == db[179].tags assert wisp.strings.all().count() >= 28
def card_db(): if "db" not in _CARD_DATA_CACHE: db, _ = load_dbf() _CARD_DATA_CACHE["db"] = db return _CARD_DATA_CACHE["db"]
def make_reply(decks, locale="enUS"): db, _ = load_dbf(locale=locale) if locale != "enUS": english_db, _ = load_dbf(locale="enUS") # For Gamepedia links else: english_db = db reply = [] for deck in decks: reply_chunks = [] card_includes = deck.cards cards = [(db[dbf_id], count) for dbf_id, count in card_includes] cards.sort(key=lambda include: (include[0].cost, include[0].name)) try: hero = db[deck.heroes[0]] class_name = get_localized_name(hero.card_class, locale=locale) hero_name = f"{class_name} ({hero.name})" except KeyError: hero_name = "Unknown" format_name = get_localized_name(deck.format, locale=locale) standard_year_name = pretty_zodiac_year(ZodiacYear.as_of_date()) reply_chunks.append( f"**Format:** {format_name} ({standard_year_name})\n") reply_chunks.append(f"**Class:** {hero_name}\n") rows = [] for card, count in cards: english_card = english_db[card.dbf_id] image = get_card_image(card, locale) rows.append(( card.cost, markdown_link(card.name, image), count, get_card_links(card, english_card, locale), )) total_dust = sum(card.crafting_costs[0] * count for card, count in cards) table = tabulate(rows, headers=("Mana", "Card Name", "Qty", "Links")) reply_chunks.append(table) reply_chunks.append("\n") reply_chunks.append(f"**Total Dust:** {total_dust}") reply_chunks.append("\n") reply_chunks.append(f"**Deck Code:** {deck.as_deckstring}") reply_chunks.append("\n") reply_chunks.append("*****") reply_chunks.append("\n") if calculate_reply_length(reply + reply_chunks) > MAX_CHARACTERS: # Stop adding decks break reply += reply_chunks reply.append(I_AM_A_BOT) return "\n".join(reply)
from hearthstone.deckstrings import Deck from hearthstone import cardxml from hearthstone.cardxml import CardXML from hearthstone.enums import CardType, FormatType, GameTag, Race, Rarity db, _ = cardxml.load_dbf() def loc_name(self, locale): return self.strings[GameTag.CARDNAME][locale] def loc_text(self, locale): return self.strings[GameTag.CARDTEXT_INHAND][locale] def loc_flavor(self, locale): return self.strings[GameTag.FLAVORTEXT][locale] CardXML.loc_name = loc_name CardXML.loc_text = loc_text CardXML.loc_flavor = loc_flavor class DeckHandler(): def __init__(self): self.db = db def handle(self, input): return self.format_deckstring(input)
def get_string_id(cls, dbf_id): if not DBF_DB: db, _ = cardxml.load_dbf() DBF_DB.update(db) return DBF_DB[dbf_id].id
def load_cards(): db = load_dbf()[0] cards = {} for dbf_id in db: cards[dbf_id] = Card(db[dbf_id]) return cards