try out django
[mlpccg-meta.git] / mlpccg / DeckList.py
index 7494109..0e0cba1 100644 (file)
@@ -19,19 +19,50 @@ class DeckList:
         return self.name
 
     @property
-    def description(self):
+    def aspects(self):
         mane = None
-        colors = defaultdict(int)
-
+        aspects = defaultdict(int)
         for card in self.cards:
             if card['type'] == 'Mane':
-                mane = '%s %s' % (card['title'], mlpccg.CardDb.set_ids[card['set']].upper())
+                mane = (card['title'], mlpccg.CardDb.CardDb.set_ids[card['set']].upper())
+
+            elif card['type'] == 'Friend':
+                aspects[card['color']] += 1
+
+            elif card['type'] != 'Problem':
+                aspects[card['type']] += 1
+
+        aspects_sorted = sorted(aspects.iteritems(), reverse=True, key=operator.itemgetter(1))
+        aspects_sum = float(sum([aspect[1] for aspect in aspects_sorted]))
+        aspects_filtered = []
+        cutoff = 0.66
+        amount = 0.0
+        for aspect, value in aspects_sorted:
+            amount += value / aspects_sum
+            if amount < cutoff:
+                aspects_filtered += [aspect]
+            else:
+                break
+
+        return (mane, aspects_filtered)
+
+    @property
+    def description(self):
+        mane, aspects = self.aspects
+        if not mane or len(aspects) == 0:
+            return 'Unknown'
+
+        return '%s %s %s' % (mane[0], mane[1], '/'.join(aspects))
+
+    @property
+    def ponyhead_link(self):
+        cards = defaultdict(int)
+        for card in self.cards:
+            cards[card['id'].replace('f', 'F')] += 1
 
-            if card['type'] == 'Friend':
-                colors[card['color']] += 1
+        cards = ['%sx%d' % (id, amount) for id, amount in cards.iteritems()]
 
-        colors_sorted = filter(lambda c: c[1] >= 6, sorted(colors.iteritems(), reverse=True, key=operator.itemgetter(1)))
-        return '%s %s' % (mane, '/'.join(map(lambda c: c[0], colors_sorted)))
+        return 'http://ponyhead.com/deckbuilder?v1code=%s' % '-'.join(cards)
 
     def parse_ponyhead_xml(self, path):
         logging.debug('loading decklist from xml')