forked from jfd02/TFT-OCR-BOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharena.py
347 lines (309 loc) · 15.8 KB
/
arena.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from time import sleep
import game_assets
import mk_functions
import screen_coords
from champion import Champion
from game_assets import champion_data, full_items
import comps
import ocr
import arena_functions
class Arena:
def __init__(self, message_queue):
self.message_queue = message_queue
self.board_size = 0
self.bench = [None, None, None, None, None, None, None, None, None]
self.board = []
self.board_unknown = []
self.unknown_slots = comps.get_unknown_slots()
self.champs_to_buy = comps.champions_to_buy()
self.board_names = []
self.items = []
self.final_comp = False
self.level = 0
self.spam_roll = False
def fix_board_state(self):
self.bench = arena_functions.get_bench()
board_champion = arena_functions.get_board()
self.board = board_champion["board"]
self.board_names.clear()
for champ in self.board:
self.board_names.append(champ.name)
self.board_unknown = board_champion["board_useless"]
self.board_size = board_champion["board_size"]
# bench_occupied = arena_functions.bench_occupied_check()
# for index, slot in enumerate(self.bench):
# if slot is None and bench_occupied[index] is True:
# self.bench[index] = "?"
# if isinstance(slot, str) and bench_occupied[index] is False:
# self.bench[index] = None
# if isinstance(slot, Champion) and bench_occupied[index] is False:
# self.bench[index] = None
def bought_champion(self):
# items = []
# for item in comps.comp[name]["items"]:
# items.append(item)
# self.bench[slot] = Champion(name, screen_coords.bench_loc[slot], items, slot,
# champion_data[name]["Board Size"], comps.comp[name]["final_comp"])
mk_functions.move_mouse(screen_coords.default_loc)
sleep(0.5)
self.fix_board_state()
def have_champion(self):
for champion in self.bench:
if isinstance(champion, Champion):
if champion.name not in self.board_names:
return champion
return None
def move_known(self, champion):
if champion.name in comps.comp:
self.message_queue.put(("CONSOLE", f"Moving {champion.name} to board"))
destination = screen_coords.board_loc[comps.comp[champion.name]["board_position"]]
mk_functions.left_click(champion.coords)
mk_functions.left_click(destination)
champion.coords = destination
self.board.append(champion)
self.board_names.append(champion.name)
self.bench[champion.index] = None
champion.index = comps.comp[champion.name]["board_position"]
self.board_size += champion.size
def move_unknown(self):
for index, champion in enumerate(self.bench):
if isinstance(champion, str):
self.message_queue.put(("CONSOLE", f"Moving {champion} to board"))
mk_functions.left_click(screen_coords.bench_loc[index])
mk_functions.left_click(screen_coords.board_loc[self.unknown_slots[len(self.board_unknown)]])
self.bench[index] = None
self.board_unknown.append(champion)
self.board_size += 1
return
def sell_bench(self):
for index, _ in enumerate(self.bench):
mk_functions.press_e(screen_coords.bench_loc[index])
self.bench[index] = None
def unknown_in_bench(self):
for slot in self.bench:
if isinstance(slot, str):
return True
return False
def move_champions(self):
self.bench_cleanup()
self.level = arena_functions.get_level()
while self.level > self.board_size:
champion = self.have_champion()
if champion is not None and self.level - self.board_size >= champion.size:
self.move_known(champion)
elif self.unknown_in_bench():
self.move_unknown()
else:
bought_unknown = False
shop = arena_functions.get_shop()
for index, champion in enumerate(shop):
try: # Can fail if the shop slot is ""
if champion_data[champion]["Gold"] <= arena_functions.get_gold() and champion_data[champion][
"Board Size"] == 1 and champion not in self.champs_to_buy and champion not in self.board_unknown:
none_slot = arena_functions.empty_slot()
mk_functions.left_click(screen_coords.buy_loc[index])
sleep(0.1)
self.bench[none_slot] = f"{champion}"
self.move_unknown()
bought_unknown = True
break
except KeyError:
pass
# if not bought_unknown:
# self.message_queue.put(("CONSOLE", "Need to sell entire bench to keep track of board"))
# self.sell_bench()
# return
def replace_unknown(self):
champion = self.have_champion()
if len(self.board_unknown) > 0 and champion is not None:
if self.level - self.board_size <= champion.size != 2:
mk_functions.press_e(screen_coords.board_loc[self.unknown_slots[len(self.board_unknown) - 1]])
self.board_unknown.pop()
self.board_size -= 1
self.move_known(champion)
elif len(self.board_unknown) > 1 and self.level - self.board_size + 1 <= champion.size:
mk_functions.press_e(screen_coords.board_loc[self.unknown_slots[len(self.board_unknown) - 1]])
self.board_unknown.pop()
mk_functions.press_e(screen_coords.board_loc[self.unknown_slots[len(self.board_unknown) - 1]])
self.board_unknown.pop()
self.board_size -= 2
self.move_known(champion)
elif self.level - self.board_size + 1 <= champion.size:
for slot in self.board:
if isinstance(slot, Champion):
if slot.final_comp is False:
mk_functions.press_e(slot.coords)
self.board_size -= 1
self.move_known(champion)
def bench_cleanup(self):
# print(self.bench)
for index, champion in enumerate(self.bench):
if champion == "?" or isinstance(champion, str):
pass
# self.message_queue.put(("CONSOLE", "Selling unknown champion"))
# mk_functions.press_e(screen_coords.bench_loc[index])
# self.bench[index] = None
elif isinstance(champion, Champion):
# if champion.name not in self.champs_to_buy and champion.name in self.board_names:
# self.message_queue.put(("CONSOLE", "Selling unknown champion"))
# mk_functions.press_e(screen_coords.bench_loc[index])
# self.bench[index] = None
if champion.name not in comps.comp:
self.message_queue.put(("CONSOLE", f"Selling useless champion: {champion.name}"))
mk_functions.press_e(screen_coords.bench_loc[index])
else:
for index1, champion1 in enumerate(self.board):
if isinstance(champion1, Champion) and champion1.name == champion.name and \
champion1.level >= comps.comp[champion1.name]["level"]:
self.message_queue.put(("CONSOLE", f"Selling useless champion(level-up): {champion.name}"))
mk_functions.press_e(screen_coords.bench_loc[index])
def place_items(self):
self.items = arena_functions.get_items()
log_items = list(filter((None).__ne__, self.items))
self.message_queue.put(("CONSOLE", f"Items: {log_items}"))
for index, _ in enumerate(self.items):
if self.items[index] is not None:
self.add_item_to_champs(index)
def add_item_to_champs(self, item_index):
for champ in self.board:
if champ.does_need_items() and self.items[item_index] is not None:
self.add_item_to_champ(item_index, champ)
def add_item_to_champ(self, item_index, champ):
item = self.items[item_index]
if item in full_items:
if item in champ.build:
mk_functions.left_click(screen_coords.item_pos[item_index][0])
mk_functions.left_click(champ.coords)
self.message_queue.put(("CONSOLE", f"Placed {item} on {champ.name}"))
champ.completed_items.append(item)
champ.build.remove(item)
self.items[self.items.index(item)] = None
else:
if len(champ.current_building) == 0:
item_to_move = None
for build_item in champ.build:
build_item_components = list(full_items[build_item])
if item in build_item_components:
item_to_move = item
build_item_components.remove(item)
champ.current_building.append((build_item, build_item_components[0]))
champ.build.remove(build_item)
if item_to_move is not None:
mk_functions.left_click(screen_coords.item_pos[item_index][0])
mk_functions.left_click(champ.coords)
self.message_queue.put(("CONSOLE", f"Placed {item} on {champ.name}"))
self.items[self.items.index(item)] = None
else:
for builditem in champ.current_building:
if item == builditem[1]:
mk_functions.left_click(screen_coords.item_pos[item_index][0])
mk_functions.left_click(champ.coords)
champ.completed_items.append(builditem[0])
champ.current_building.clear()
self.items[self.items.index(item)] = None
self.message_queue.put(("CONSOLE", f"Placed {item} on {champ.name}"))
self.message_queue.put(("CONSOLE", f"Completed {builditem[0]}"))
return
def remove_champion(self, champion):
for index, slot in enumerate(self.bench):
if isinstance(slot, Champion):
if slot.name == champion.name:
mk_functions.press_e(slot.coords)
self.bench[index] = None
self.champs_to_buy = list(filter(f"{champion.name}".__ne__,
self.champs_to_buy)) # Remove all instances of champion in champs_to_buy
mk_functions.press_e(champion.coords)
self.board_names.remove(champion.name)
self.board_size -= champion.size
self.board.remove(champion)
def final_comp_check(self):
for slot in self.bench:
if isinstance(slot, Champion):
if slot.final_comp is True and slot.name not in self.board_names:
for champion in self.board:
if champion.final_comp is False and champion.size == slot.size:
self.message_queue.put(("CONSOLE", f"Replacing {champion.name} with {slot.name}"))
self.remove_champion(champion)
self.move_known(slot)
break
def tacticians_check(self):
mk_functions.move_mouse(screen_coords.item_pos[0][0])
sleep(0.5)
item = ocr.get_text(screenxy=screen_coords.item_pos[0][1], scale=3, psm=13,
whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
item = arena_functions.valid_item(item)
try:
if "TacticiansCrown" in item:
self.message_queue.put(("CONSOLE", "Tacticians Crown on bench, adding extra slot to board"))
self.board_size -= 1
else:
self.message_queue.put(("CONSOLE", f"{item} is not TacticiansCrown"))
except TypeError:
self.message_queue.put(("CONSOLE", "Tacticians Crown check failed"))
def spend_gold(self): # Rework this function
first_run = True
min_gold = 24 if self.spam_roll is True else 56
while first_run or arena_functions.get_gold() >= min_gold:
if not first_run:
if arena_functions.get_level() != 9:
mk_functions.buy_xp()
self.message_queue.put(("CONSOLE", " Purchasing XP"))
mk_functions.reroll()
self.message_queue.put(("CONSOLE", " Rerolling shop"))
shop = arena_functions.get_shop()
self.message_queue.put(("CONSOLE", f"Shop: {shop}"))
for index, champion in enumerate(shop):
if champion in self.champs_to_buy:
if arena_functions.get_gold() - game_assets.champion_data[champion]["Gold"] >= 0:
# none_slot = arena_functions.empty_slot()
# if none_slot != -1:
mk_functions.left_click(screen_coords.buy_loc[index])
self.message_queue.put(("CONSOLE", f"Purchased {champion}"))
self.bought_champion()
self.champs_to_buy.remove(champion)
first_run = False
def krug_round(self):
if arena_functions.get_gold() >= 4:
mk_functions.buy_xp()
def pick_augment(self):
augments = []
for coords in screen_coords.augment_pos:
augment = ocr.get_text(screenxy=coords, scale=3, psm=7, whitelist="")
augments.append(augment)
for augment in augments:
for potential in comps.priority_augments:
if potential in augment:
self.message_queue.put(("CONSOLE", f"Choosing priority augment {augment}"))
mk_functions.left_click(screen_coords.augment_loc[augments.index(augment)])
return
for augment in augments:
for potential in comps.backup_augments:
if potential in augment:
self.message_queue.put(("CONSOLE", f"Choosing backup augment {augment}"))
mk_functions.left_click(screen_coords.augment_loc[augments.index(augment)])
return
self.message_queue.put(("CONSOLE",
"[!] No priority or backup augment found, undefined behavior may occur for the rest of the round"))
mk_functions.left_click(screen_coords.augment_loc[0])
def check_health(self):
health = arena_functions.get_health()
if health <= 100 and health >= 0:
self.message_queue.put(("CONSOLE", f"Health: {health}"))
if self.spam_roll is False:
if health <= 30:
self.message_queue.put(("CONSOLE", f"Health under 30, spam roll activated"))
self.spam_roll = True
else:
self.message_queue.put(("CONSOLE", "Health check failed"))
return
def get_label(self):
labels = []
for slot in self.bench:
if isinstance(slot, Champion):
labels.append((f"{slot.name}", slot.coords))
for slot in self.board:
if isinstance(slot, Champion):
labels.append((f"{slot.name}", slot.coords))
for index, slot in enumerate(self.board_unknown):
labels.append((slot, screen_coords.board_loc[self.unknown_slots[index]]))
self.message_queue.put(("LABEL", labels))