#!/usr/bin/env python # -*- coding: utf-8 -*- # Modify key directories here # Recommended parameters: -i -w (will add ! to all unsupported decks) OR -i -f (will only convert supported decks) CARDSFOLDER = "../../res/cardsfolder" DECKFOLDER = "." OUT_DECKFOLDER = "./ForgeDecks" import argparse, os, re print("Agetian's MtgDecks.net DEC to MTG Forge Deck Converter v3.5\n") parser = argparse.ArgumentParser(description="Convert MtgDecks.net DEC to Forge DCK.") parser.add_argument("-f", action="store_true", help="only convert decks that have 100% card support in Forge") parser.add_argument("-i", action="store_true", help="add MtgDecksNet deck ID to the output deck name for uniqueness") parser.add_argument("-t", help="only convert decks that belong to the specified format") parser.add_argument("-w", action="store_true", help="add [!] to the name of decks that have unsupported cards") parser.add_argument("-U", action="store_true", help="preserve UTF-8 characters in file names") parser.add_argument("-D", action="store_true", help="sort converted decks into folders according to format") parser.add_argument("-P", action="store_true", help="convert period (.) to underscore (_) in deck names") args = parser.parse_args() # simple structural self-test (can this tool work?) if not (os.access(os.path.join(CARDSFOLDER,"a","abu_jafar.txt"),os.F_OK) or os.access(os.path.join("decks"),os.F_OK)): print("Fatal error:\n This utility requires the 'cardsfolder' folder with unpacked card files at " + CARDSFOLDER + " and the 'decks' folder with .dck files at " + DECKFOLDER + " in order to operate. Exiting.") exit(1) # basic variables cardlist = {} total_cards = 0 ai_playable_cards = 0 total_decks = 0 playable_decks = 0 nonplayable_in_deck = 0 # main algorithm print("Loading cards...") for root, dirs, files in os.walk(CARDSFOLDER): for name in files: if name.find(".txt") != -1: total_cards += 1 fullpath = os.path.join(root, name) cardtext = open(fullpath).read() cardtext_lower = cardtext.lower() cardname_literal = cardtext.replace('\r','').split('\n')[0].split(':') cardname = ":".join(cardname_literal[1:]).strip() if (cardtext_lower.find("alternatemode:split") != -1) or (cardtext_lower.find("alternatemode: split") != -1): # split card, special handling needed cardsplittext = cardtext.replace('\r','').split('\n') cardnames = [] for line in cardsplittext: if line.lower().find("name:") != -1: cardnames.extend([line.split('\n')[0].split(':')[1]]) cardname = " // ".join(cardnames) if cardtext.lower().find("remaideck") != -1: cardlist[cardname] = 0 else: cardlist[cardname] = 1 ai_playable_cards += 1 perc_playable = (float(ai_playable_cards) / total_cards) * 100 perc_unplayable = ((float(total_cards) - ai_playable_cards) / total_cards) * 100 print("Loaded %d cards, among them %d playable by the AI (%d%%), %d unplayable by the AI (%d%%).\n" % (total_cards, ai_playable_cards, perc_playable, total_cards - ai_playable_cards, perc_unplayable)) re_Metadata = '^//(.*) a (.*) deck by (.*)$' re_Metadata2 = '^//(.*) a ([A-Za-z]+) MTG deck played by (.*) in (.*) - MTGDECKS.NET.*$' re_DeckID = '^([0-9]+)\.dec$' re_Maindeck = '^([0-9]+) (.*)$' re_Sideboard = '^SB:[ \t]+([0-9]+) (.*)$' re_Timeinfo = '