forked from jfd02/TFT-OCR-BOT
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharena.py
358 lines (328 loc) · 16.2 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
348
349
350
351
352
353
354
355
356
357
358
"""
Handles the board / bench state inside of the game and
other variables used by the bot to make decisions
"""
from time import sleep
import game_assets
import mk_functions
import screen_coords
from champion import Champion
import ocr
import arena_functions
from comps import CompsManager
import comps
class Arena:
"""Arena class that handles game logic such as board and bench state"""
# pylint: disable=too-many-instance-attributes,too-many-public-methods
def __init__(self, message_queue, comps_manager : CompsManager) -> None:
self.comps_manager = comps_manager
self.comps_manager.SelectNextComp()
self.message_queue = message_queue
self.board_size = 0
self.bench: list[None] = [None, None, None, None, None, None, None, None, None]
self.board: list = []
self.board_unknown: list = []
self.unknown_slots: list = comps_manager.get_unknown_slots()
self.champs_to_buy: list = comps_manager.champions_to_buy()
self.board_names: list = []
self.items: list = []
self.final_comp = False
self.level = 0
self.spam_roll = False
def fix_bench_state(self) -> None:
"""Iterates through bench and fixes invalid slots"""
bench_occupied: list = arena_functions.bench_occupied_check()
for index, slot in enumerate(self.bench):
if slot is None and bench_occupied[index]:
self.bench[index] = "?"
if isinstance(slot, str) and not bench_occupied[index]:
self.bench[index] = None
if isinstance(slot, Champion) and not bench_occupied[index]:
self.bench[index] = None
def bought_champion(self, name: str, slot: int) -> None:
"""Purchase champion and creates champion instance"""
self.bench[slot] = Champion(name=name,
coords=screen_coords.BENCH_LOC[slot].get_coords(
),
build=self.comps_manager.CURRENT_COMP()[1][name]["items"].copy(),
slot=slot,
size=self.comps_manager.champions[name]["Board Size"],
final_comp=self.comps_manager.CURRENT_COMP()[1][name]["final_comp"])
mk_functions.move_mouse(screen_coords.DEFAULT_LOC.get_coords())
sleep(0.5)
self.fix_bench_state()
def have_champion(self) -> Champion | None:
"""Checks the bench to see if champion exists"""
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: Champion) -> None:
"""Moves champion to the board"""
print(f" Moving {champion.name} to board")
destination: tuple = screen_coords.BOARD_LOC[self.comps_manager.CURRENT_COMP()[1][champion.name]["board_position"]].get_coords()
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 = self.comps_manager.CURRENT_COMP()[1][champion.name]["board_position"]
self.board_size += champion.size
def move_unknown(self) -> None:
"""Moves unknown champion to the board"""
for index, champion in enumerate(self.bench):
if isinstance(champion, str):
print(f" Moving {champion} to board")
mk_functions.left_click(
screen_coords.BENCH_LOC[index].get_coords())
mk_functions.left_click(
screen_coords.BOARD_LOC[self.unknown_slots[len(self.board_unknown)]].get_coords())
self.bench[index] = None
self.board_unknown.append(champion)
self.board_size += 1
return
def sell_bench(self) -> None:
"""Sells all of the champions on the bench"""
for index, _ in enumerate(self.bench):
mk_functions.press_e(screen_coords.BENCH_LOC[index].get_coords())
self.bench[index] = None
def unknown_in_bench(self) -> bool:
"""Sells all of the champions on the bench"""
for slot in self.bench:
if isinstance(slot, str):
return True
return False
def move_champions(self) -> None:
"""Moves champions to the board"""
self.level: int = arena_functions.get_level()
while self.level > self.board_size:
champion: Champion | None = self.have_champion()
if champion is not None:
self.move_known(champion)
elif self.unknown_in_bench():
self.move_unknown()
else:
bought_unknown = False
shop: list = arena_functions.get_shop(self.comps_manager)
for champion in shop:
gold: int = arena_functions.get_gold()
valid_champ: bool = (
champion[1] in self.comps_manager.champions and
self.comps_manager.champion_gold_cost(champion[1]) <= gold and
self.comps_manager.champion_board_size(champion[1]) == 1 and
champion[1] not in self.champs_to_buy and
champion[1] not in self.board_unknown
)
if valid_champ:
none_slot: int = arena_functions.empty_slot()
mk_functions.left_click(screen_coords.BUY_LOC[champion[0]].get_coords())
sleep(0.2)
self.bench[none_slot] = f"{champion[1]}"
self.move_unknown()
bought_unknown = True
break
if not bought_unknown:
print(" Need to sell entire bench to keep track of board")
self.sell_bench()
return
def replace_unknown(self) -> None:
"""Replaces unknown champion"""
champion: Champion | None = self.have_champion()
if len(self.board_unknown) > 0 and champion is not None:
mk_functions.press_e(screen_coords.BOARD_LOC[self.unknown_slots[len(
self.board_unknown) - 1]].get_coords())
self.board_unknown.pop()
self.board_size -= 1
self.move_known(champion)
def bench_cleanup(self) -> None:
"""Sells unknown champions"""
for index, champion in enumerate(self.bench):
if champion == "?" or isinstance(champion, str):
print(" Selling unknown champion")
mk_functions.press_e(
screen_coords.BENCH_LOC[index].get_coords())
self.bench[index] = None
elif isinstance(champion, Champion):
if champion.name not in self.champs_to_buy and champion.name in self.board_names:
print(" Selling unknown champion")
mk_functions.press_e(
screen_coords.BENCH_LOC[index].get_coords())
self.bench[index] = None
def place_items(self) -> None:
"""Iterates through items and tries to add them to champion"""
self.items = arena_functions.get_items()
print(f" Items: {list(filter((None).__ne__, self.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: int) -> None:
"""Iterates through champions in the board and checks if the champion needs items"""
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: int, champ: Champion) -> None:
"""Takes item index and champ and applies the item"""
item = self.items[item_index]
if item in game_assets.FULL_ITEMS:
if item in champ.build:
mk_functions.left_click(
screen_coords.ITEM_POS[item_index][0].get_coords())
mk_functions.left_click(champ.coords)
print(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 = None
for build_item in champ.build:
if build_item in game_assets.FULL_ITEMS:
build_item_components: list = list(
game_assets.FULL_ITEMS[build_item])
if item in build_item_components:
item_to_move: None = item
build_item_components.remove(item)
champ.current_building.append(
(build_item, build_item_components[0]))
champ.build.remove(build_item)
# break
if item_to_move is None:
if item in game_assets.ITEMS_WITHOUT_COMBINED:
if item in champ.build:
champ.completed_items.append(item)
champ.build.remove(item)
print(f" Placing non combined {item} on {champ.name}")
if item_to_move is not None:
mk_functions.left_click(
screen_coords.ITEM_POS[item_index][0].get_coords())
mk_functions.left_click(champ.coords)
print(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].get_coords())
mk_functions.left_click(champ.coords)
champ.completed_items.append(builditem[0])
champ.current_building.clear()
self.items[self.items.index(item)] = None
print(f" Placed {item} on {champ.name}")
print(f" Completed {builditem[0]}")
return
def fix_unknown(self) -> None:
"""Checks if the item passed in arg one is valid"""
sleep(0.25)
mk_functions.press_e(
screen_coords.BOARD_LOC[self.unknown_slots[0]].get_coords())
self.board_unknown.pop(0)
self.board_size -= 1
def remove_champion(self, champion: Champion) -> None:
"""Checks if the item passed in arg one is valid"""
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) -> None:
"""Checks the board and replaces champions not in final comp"""
for slot in self.bench:
if isinstance(slot, Champion):
if slot.final_comp and slot.name not in self.board_names:
for champion in self.board:
if not champion.final_comp and champion.size == slot.size:
print(
f" Replacing {champion.name} with {slot.name}")
self.remove_champion(champion)
self.move_known(slot)
break
def tacticians_crown_check(self) -> None:
"""Checks if the item from carousel is tacticians crown"""
mk_functions.move_mouse(screen_coords.ITEM_POS[0][0].get_coords())
sleep(2)
item: str = ocr.get_text(screenxy=screen_coords.ITEM_POS[0][1].get_coords(), scale=3, psm=13,
whitelist=ocr.ALPHABET_WHITELIST)
item: str = arena_functions.valid_item(item)
try:
if "TacticiansCrown" in item:
print(" Tacticians Crown on bench, adding extra slot to board")
self.board_size -= 1
else:
print(f"{item} is not TacticiansCrown")
except TypeError:
print(" Item could not be read for Tacticians Check")
def spend_gold(self) -> None:
"""Spends gold every round"""
first_run = True
min_gold = 24 if self.spam_roll else 50
while first_run or arena_functions.get_gold() >= min_gold:
if not first_run:
if arena_functions.get_level() != 9:
mk_functions.buy_xp()
print(" Purchasing XP")
mk_functions.reroll()
print(" Rerolling shop")
shop: list = arena_functions.get_shop(self.comps_manager)
print(f" Shop: {shop}")
for champion in shop:
if (champion[1] in self.champs_to_buy and
arena_functions.get_gold() - self.comps_manager.champions[champion[1]]["Gold"] >= 0
):
none_slot: int = arena_functions.empty_slot()
if none_slot != -1:
mk_functions.left_click(screen_coords.BUY_LOC[champion[0]].get_coords())
print(f" Purchased {champion[1]}")
self.bought_champion(champion[1], none_slot)
self.champs_to_buy.remove(champion[1])
first_run = False
def krug_round(self) -> None:
"""Checks if current round is krug round"""
if arena_functions.get_gold() >= 4:
mk_functions.buy_xp()
def pick_augment(self) -> None:
"""Picks an augment from user defined augment priority list or defaults to first augment"""
augments: list = []
for coords in screen_coords.AUGMENT_POS:
augment: str = ocr.get_text(
screenxy=coords.get_coords(), scale=3, psm=7)
augments.append(game_assets.AUGMENTS)
for augment in augments:
for potential in game_assets.AUGMENTS:
if potential in augment:
print(f" Choosing augment {augment}")
mk_functions.left_click(screen_coords.AUGMENT_LOC[augments.index(augment)].get_coords())
return
print(" [!] 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].get_coords())
def check_health(self) -> None:
"""Checks if current health is below 30 and conditionally activates spam roll"""
health: int = arena_functions.get_health()
if health > 0:
print(f" Health: {health}")
if not self.spam_roll:
if health < 30:
print(" Health under 30, spam roll activated")
self.spam_roll = True
else:
print(" Health check failed")
def get_label(self) -> None:
"""Gets labels used to display champion name UI on window"""
labels: list = []
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]].get_coords()))
self.message_queue.put(("LABEL", labels))