-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_deck.py
157 lines (125 loc) · 3.56 KB
/
generate_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
147
148
149
150
151
152
153
154
155
156
157
import os
import sqlite3
import random, os, sys
from typing import List
card_pool = []
class Deck:
main = []
extra = []
side = []
def generate_deck(deck_name):
input_path = os.getcwd() + '/deck_gen' + deck_name
output_path = os.getcwd() + '/decks' + deck_name
core, variants = _get_data(input_path)
final_deck:Deck = _create_deck(core, variants)
f = open(output_path, "w")
f.write("#created by deck_maker_ai\n")
f.write("#main\n")
for i in final_deck.main:
f.write(i +'\n')
f.write("#extra\n")
for i in final_deck.extra:
f.write(i +'\n')
f.write("!side\n")
for i in final_deck.side:
f.write(i +'\n')
f.close()
def _get_data(file_path):
core = Deck()
variants = []
to_add = Deck()
f = open(file_path,"r")
part = 0
mode = 0
for line in f.readlines():
if "#extra" in line:
part = 1
continue
elif "!side" in line:
part = 2
continue
elif "#main" in line:
part = 0
if (mode == 1):
variants.append(to_add)
to_add = Deck()
continue
elif "#variant" in line:
mode = 1
continue
elif "#" in line:
continue
if (mode == 0):
if part == 0:
core.main.append(line.strip())
elif part == 1:
core.extra.append(line.strip())
else:
core.side.append(line.strip())
else:
if part == 0:
to_add.main.append(line.strip())
elif part == 1:
to_add.extra.append(line.strip())
else:
to_add.side.append(line.strip())
if (mode == 1):
variants.append(to_add)
f.close()
return core,variants
def _create_deck(core:Deck, variant:List[Deck], main_limit = 40) -> Deck:
random.shuffle(variant)
while(len(variant) > 0):
cur:Deck = variant.pop(0)
core_all:List[int] = core.main.copy()
core_all.extend(core.extra)
core_all.extend(core.side)
cur_all:List[int] = cur.main.copy()
cur_all.extend(cur.extra)
cur_all.extend(cur.side)
if intersect(core_all, cur_all):
continue
if (len(core.extra) + len(cur.extra) > 15):
continue
if (len(core.side) + len(cur.side) > 15):
continue
core.main.extend(cur.main)
core.extra.extend(cur.extra)
core.side.extend(cur.side)
if (len(core.main) > main_limit):
break
return core
def GetCardPool():
dbfile = './cardData.cdb'
con = sqlite3.connect(dbfile)
cur = con.cursor()
cur.execute('SELECT distinct CardId from GameStats')
result = cur.fetchone()
ForbiddenLimitedUpdate()
# Updates the Card Pool based on Limited list
# Assumes that the card pool contains 3 of each card
def ForbiddenLimitedUpdate():
file_path = os.getcwd() + "/edopro_bin/repositories/lflists/0TCG.lflist.conf"
limited = -1
f = open(file_path,"r")
for line in f.readlines():
if "#Forbidden" in line:
limited = 0
continue
elif "#Limited" in line:
limited = 1
continue
elif "#Semi-limited" in line:
limited = 2
continue
elif "#" == line[0] or "!" == line[0]:
continue
card = line.split(' ')[0]
if card in card_pool:
for _ in range(3 - limited):
card_pool.remove(card)
f.close()
def intersect(lst1, lst2):
return len(list(set(lst1) & set(lst2))) > 0
if __name__ == "__main__":
generate_deck(sys.argv[1])