-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.py
146 lines (118 loc) · 4.37 KB
/
deck.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from enum import Enum
from typing import List
class CardType(Enum):
MONSTER = "m"
SPELL = "s"
TRAP = "t"
class DeckCardType(Enum):
BRICK = "b"
ENGINE = "e"
NONENGINE = "ne"
class _ReadMode(Enum):
NONE = "NONE"
CARDS = "CARDS"
COMBOS = "COMBOS"
class Card:
def __init__(
self, name: str, count: int, type: CardType, deck_type: DeckCardType
) -> None:
self.name = name
self.count = count
self.type = type
self.deck_type = deck_type
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return self.__str__()
def __eq__(self, other):
return other.name == self.name and other.count == self.count
def __hash__(self) -> int:
return hash(f"{self.name}{self.count}{self.type.value}{self.deck_type.value}")
class Combo:
def __init__(self, cards: List[Card], weight: int) -> None:
self.cards = cards
self.weight = weight
def __str__(self) -> str:
card_names = [i.name for i in self.cards]
return " + ".join(card_names)
def __repr__(self) -> str:
return self.__str__()
def __eq__(self, other):
eq = True
for card in other.cards:
eq = eq and card in self.cards
eq = eq and self.weight == other.weight
return eq
def __hash__(self) -> int:
return hash("".join([str(x) for x in self.cards] + [str(self.weight)]))
class Deck:
def __init__(self, initial_cards: List[Card], initial_combos: List[Combo]) -> None:
self.cards = initial_cards
self.combos = initial_combos
self._calculate_size()
def _calculate_size(self):
self.size = sum([i.count for i in self.cards])
def add_card(self, card: Card):
self.cards.append(card)
self._calculate_size()
def add_combo(self, combo: Combo):
self.combos.append(combo)
def card_by_name(self, name: str):
card = list(filter(lambda x: x.name == name, self.cards))
if len(card) > 1:
raise ValueError(f'Too many cards match "{name}"')
elif len(card) == 0:
raise ValueError(f'No card matched "{name}"')
else:
return card[0]
def cards_by_deck_type(self, type: DeckCardType):
cards = list(filter(lambda x: x.deck_type == type, self.cards))
return cards
def get_simulated_cards(self):
out = []
for card in self.cards:
out.extend([card for _ in range(0, card.count)])
return out
def __str__(self) -> str:
return "\n".join([f"{i.name}:{i.count}" for i in self.cards])
@classmethod
def from_file(_, filepath: str):
print(filepath)
deck = Deck([], [])
with open(filepath, "r") as f:
mode = _ReadMode.NONE
for line in f.readlines():
sline = line.strip("\n")
match sline:
case "CARDS":
mode = _ReadMode(sline)
continue
case "COMBOS":
mode = _ReadMode(sline)
continue
case _:
match mode:
case _ReadMode.CARDS:
card = sline.split(" ")
deck.add_card(
Card(
card[0],
int(card[1]),
CardType(card[2]),
DeckCardType(card[3]),
)
)
case _ReadMode.COMBOS:
combo = sline.split(" ")
weight = int(combo[0])
cards_str = combo[1:]
try:
cards = [deck.card_by_name(i) for i in cards_str]
except Exception as e:
print(f"Not adding combo {sline}")
print(e)
continue
deck.add_combo(Combo(cards, weight))
case _:
pass
return deck