-
Notifications
You must be signed in to change notification settings - Fork 16
/
deck_value.py
54 lines (45 loc) · 1.82 KB
/
deck_value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import argparse
import matplotlib.pyplot as plt
import scryfall
from mtgproxies.cli import parse_decklist_spec
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Show deck value decomposition.")
parser.add_argument(
"decklist",
metavar="decklist_spec",
help="path to a decklist in text/arena format, or manastack:{manastack_id}, or archidekt:{archidekt_id}",
)
parser.add_argument(
"--lump-threshold",
help="lump together cards with lesser proportional value",
type=float,
default=0.03,
metavar="FLOAT",
)
args = parser.parse_args()
# Parse decklist
decklist = parse_decklist_spec(args.decklist, warn_levels=["ERROR", "WARNING"])
# Fetch prices
card_prices = []
for card in decklist.cards:
price = scryfall.get_price(card["oracle_id"])
if price is not None:
card_prices.append((card["name"], card.count * price))
else:
print(f"WARNING: Unable to find price for {card['name']}")
card_prices.sort(key=lambda x: x[1], reverse=True)
price_total = sum(p for _, p in card_prices)
# Partition cards in named and bulk
cards_named = [(card, price) for card, price in card_prices if price >= args.lump_threshold * price_total]
cards_lump = [(card, price) for card, price in card_prices if price < args.lump_threshold * price_total]
# Plot
plt.pie(
[price for _, price in cards_named] + [sum(p for _, p in cards_lump)],
labels=[name for name, _ in cards_named] + ["other"],
autopct=lambda pct: f"{pct / 100. * price_total:0.2f}€",
shadow=True,
startangle=90,
)
plt.title(f"{decklist.name} ({price_total:0.2f}€)")
plt.tight_layout()
plt.show()