Last active
March 14, 2020 04:28
-
-
Save lelandbatey/3f601f576696cabb0c76b7bd6f0006f2 to your computer and use it in GitHub Desktop.
Show what cards you could be holding up in MTGA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import json | |
''' | |
Show the cards you could be holding up in MTGA at instant speed. | |
TODOs: | |
1. Make it color sensitive | |
2. Account for cards with flash | |
3. Make a UI (like at ALL) | |
''' | |
def logical(cards): | |
for card in cards: | |
if 'faceConvertedManaCost' in card: | |
card['convertedManaCost'] = card['faceConvertedManaCost'] | |
return cards | |
def has_cost(cards): | |
return [x for x in cards if x['costs']] | |
def cmc(cards, mana_up): | |
mana_amount = len(mana_up) | |
valid_cards = list() | |
for card in cards: | |
if card['convertedManaCost'] <= mana_amount: | |
valid_cards.append(card) | |
return valid_cards | |
def permute_mana(cost, depth=0): | |
potential_costs = list() | |
if not cost: | |
return list() | |
symbol = cost[0] | |
cost = [x.split('/') for x in cost] | |
return list(itertools.product(*cost)) | |
def normalize_cost(card): | |
costs = [] | |
has_hybrid = False | |
cost = [] | |
if not 'manaCost' in card: | |
card['costs'] = [] | |
return card | |
for x in card['manaCost'].split('}'): | |
if x: | |
bare_cost = x.replace('{', '') | |
cost.append(bare_cost) | |
if '/' in bare_cost: | |
has_hybrid = True | |
#print(cost) | |
costs = permute_mana(cost) | |
card['costs'] = costs | |
return card | |
def colors(cards, mana_available): | |
valid_cards = list() | |
color_available = count_colors(mana_available) | |
# print(color_available) | |
for card in cards: | |
costs = card['costs'] | |
color_costs = list() | |
for cost in costs: | |
color_cost = count_colors(cost) | |
color_costs.append(color_cost) | |
card['color_costs'] = color_costs | |
for cost in color_costs: | |
if compare_colors(cost, color_available): | |
valid_cards.append(card) | |
break | |
return valid_cards | |
def compare_colors(cost, color_available): | |
for i in range(len(cost)): | |
if cost[i] > color_available[i]: | |
return False | |
return True | |
def count_colors(cost): | |
color_avail = [0, 0, 0, 0, 0] | |
for pip in cost: | |
if pip == 'W': | |
color_avail[0] += 1 | |
elif pip == 'U': | |
color_avail[1] += 1 | |
elif pip == 'B': | |
color_avail[2] += 1 | |
elif pip == 'R': | |
color_avail[3] += 1 | |
elif pip == 'G': | |
color_avail[4] += 1 | |
return color_avail | |
def main(): | |
cards = list() | |
with open('./AllPrintings.json') as cardfile: | |
allcards = json.load(cardfile) | |
for setname, mset in allcards.items(): | |
if not 'cards' in mset: | |
print(mset) | |
cards.extend(mset['cards']) | |
cards = [x for x in cards if x.get('legalities', dict()).get('standard') == 'Legal'] | |
cnames = set() | |
possible = cards | |
cards = list() | |
for c in possible: | |
if not c['name'] in cnames: | |
cards.append(c) | |
cnames.add(c['name']) | |
cards = sorted(cards, key=lambda x: x['name']) | |
mana_available = ['W', 'U', 'B', 'R', 'G'] | |
cards = logical(cards) | |
cards = [x for x in cards if x['type'] == 'Instant'] | |
cards = [normalize_cost(x) for x in cards] | |
cards = cmc(cards, mana_available) | |
cards = has_cost(cards) | |
cards = colors(cards, mana_available) | |
for c in cards: | |
print('{} - {} - {}'.format(c['name'], c['costs'], c['color_costs'])) | |
if __name__ == '__main__': main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment