-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
executable file
·103 lines (80 loc) · 2.75 KB
/
classes.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from itertools import combinations
import re
cardParse = re.compile("(?P<quantity>\d*) (?P<name>.+)")
all_cards = {}
class Card():
def __init__(self, name, position, sideboard=False):
self.name = name
self.position = position
self.uprank = 0.
self.sideboard = sideboard
def updateRank(self, num):
self.uprank += num
def resetRank(self):
self.uprank = 0
def __gt__(self, other):
if self.name > other.name:
return True
if self.name == other.name and self.position < other.position:
return True
return False
def __eq__(self, other):
if self.name == other.name and self.position == other.position:
return True
return False
def __ge__(self, other):
return self > other or self == other
def __ne__(self, other):
return not self == other
def __lt__(self, other):
return not self > other and not self == other
def __le__(self, other):
return self < other or self == other
def __hash__(self):
return hash(tuple([self.name, self.position]))
def __repr__(self):
return "({}, {}) : {}".format(self.name, self.position, self.uprank)
class Ranking():
def __init__(self):
self.rankings = {}
def update(self, cards):
cards = tuple(sorted(cards))
if cards in self.rankings:
self.rankings[cards] += 1
else:
self.rankings[cards] = 1
def getNext(self):
for cur in self.rankings.keys():
yield cur, self.rankings[cur]
def addDeck(self, decklist, n): # decklist is a list of Cards
for i in range(1, n + 1):
for cur in combinations(decklist, i):
self.update(sorted(cur))
def getCollective(self):
return set([x for y in self.rankings.keys() for x in y])
def remove(self, item):
self.rankings = {i:v for i, v in self.rankings.items() if item not in i}
def parseDecklist(file, sideboard=False):
with open(file, "r") as fp:
lines = fp.readlines()
decklist = []
check = False
for line in lines:
line = line.strip()
if sideboard and not check:
if line != "" and line != "\n" and line.lower() != "sideboard":
continue
else:
check = True
continue
match = cardParse.search(line)
if match:
match = match.groupdict()
else:
break
for i in range(1, int(match['quantity']) + 1):
insert = Card(match['name'], i, sideboard)
if insert not in all_cards:
all_cards[insert] = insert
decklist.append(all_cards[insert])
return decklist