-
Notifications
You must be signed in to change notification settings - Fork 94
/
game.py
293 lines (266 loc) · 11.1 KB
/
game.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
"""
Handles tasks that happen each game round
"""
from time import sleep, perf_counter
import random
import multiprocessing
import importlib
from win32con import BM_CLICK
import win32gui
import settings
import arena_functions
import game_assets
import game_functions
from arena import Arena
from vec4 import Vec4
from vec2 import Vec2
class Game:
"""Game class that handles game logic such as round tasks"""
def __init__(self, message_queue: multiprocessing.Queue) -> None:
importlib.reload(game_assets)
self.message_queue = message_queue
self.arena = Arena(self.message_queue)
self.round: list[str, int] = ["0-0", 0]
self.time: None = None
self.forfeit_time: int = settings.FORFEIT_TIME + random.randint(50, 150)
self.found_window = False
print("\n[!] Searching for game window")
while not self.found_window:
print(" Did not find window, trying again...")
win32gui.EnumWindows(self.callback, None)
sleep(1)
self.loading_screen()
def callback(self, hwnd, extra) -> None: # pylint: disable=unused-argument
"""Function used to find the game window and get its size"""
if "League of Legends (TM) Client" not in win32gui.GetWindowText(hwnd):
return
rect = win32gui.GetWindowRect(hwnd)
x_pos = rect[0]
y_pos = rect[1]
width = rect[2] - x_pos
height = rect[3] - y_pos
if width < 200 or height < 200:
return
print(f" Window {win32gui.GetWindowText(hwnd)} found")
print(f" Location: ({x_pos}, {y_pos})")
print(f" Size: ({width}, {height})")
Vec4.setup_screen(x_pos, y_pos, width, height)
Vec2.setup_screen(x_pos, y_pos, width, height)
self.found_window = True
def loading_screen(self) -> None:
"""Loop that runs while the game is in the loading screen"""
game_functions.default_pos()
while game_functions.get_round()[0] != "1-1":
if self.check_failed_to_connect_window():
return
sleep(1)
self.start_time: float = perf_counter()
self.game_loop()
def check_failed_to_connect_window(self) -> bool:
"""Check "Failed to Connect" windows and try to reconnect"""
hwnd = win32gui.FindWindow(None, "Failed to Connect")
if hwnd:
print(' Found "Failed to Connect" window, trying to exit and reconnect')
if reconnect_button := win32gui.FindWindowEx(hwnd, 0, "Button", None):
if cancel_button := win32gui.FindWindowEx(
hwnd, reconnect_button, "Button", None
):
print(" Exiting the game.")
win32gui.SendMessage(cancel_button, BM_CLICK, 0, 0)
return True
print(" Cancel button not found.")
else:
print(" Reconnect button not found.")
return False
# pylint: disable=too-many-branches
def game_loop(self) -> None:
"""Loop that runs while the game is active, handles calling the correct tasks for round and exiting game"""
ran_round: str = None
last_game_health: int = 100
while True:
game_health: int = arena_functions.get_health()
if game_health == 0 and last_game_health > 0:
count: int = 15
while count > 0:
if not game_functions.check_alive():
self.message_queue.put("CLEAR")
game_functions.exit_game()
break
sleep(1)
count -= 1
break
if game_health == -1 and last_game_health > 0:
self.message_queue.put("CLEAR")
break
last_game_health = game_health
self.round = game_functions.get_round()
if (
settings.FORFEIT
and perf_counter() - self.start_time > self.forfeit_time
):
game_functions.forfeit()
continue
if self.round[0] != ran_round:
if self.round[0] in game_assets.PVP_ROUND:
game_functions.default_pos()
self.pvp_round()
ran_round: str = self.round[0]
elif self.round[0] in game_assets.PVE_ROUND:
game_functions.default_pos()
self.pve_round()
ran_round: str = self.round[0]
elif self.round[0] in game_assets.CAROUSEL_ROUND:
self.carousel_round()
ran_round: str = self.round[0]
elif self.round[0] in game_assets.SECOND_ROUND:
self.second_round()
ran_round: str = self.round[0]
elif self.round[0] in game_assets.ENCOUNTER_ROUNDS:
print(f"\n[Encounter Round] {self.round[0]}")
print(" Do nothing")
self.message_queue.put("CLEAR")
self.arena.check_health()
ran_round: str = self.round[0]
if self.round[1] == 1 and self.round[0].split("-")[1] == "1":
print("\n[Encounter round setup]")
self.encounter_round_setup()
sleep(0.5)
def encounter_round_setup(self) -> None:
"""Remove rounds from game_assets and add it back by checking round message"""
game_assets.CAROUSEL_ROUND = {
carousel_round
for carousel_round in game_assets.CAROUSEL_ROUND
if not carousel_round.startswith(self.round[0].split("-")[0])
}
game_assets.PVE_ROUND = {
pve_round
for pve_round in game_assets.PVE_ROUND
if not pve_round.startswith(self.round[0].split("-")[0])
}
game_assets.PVP_ROUND = {
pvp_round
for pvp_round in game_assets.PVP_ROUND
if not pvp_round.startswith(self.round[0].split("-")[0])
}
game_assets.ANVIL_ROUNDS = {
anvil_round
for anvil_round in game_assets.ANVIL_ROUNDS
if not anvil_round.startswith(self.round[0].split("-")[0])
}
game_assets.ITEM_PLACEMENT_ROUNDS = {
item_placement_round
for item_placement_round in game_assets.ITEM_PLACEMENT_ROUNDS
if not item_placement_round.startswith(self.round[0].split("-")[0])
}
for index, round_msg in enumerate(game_functions.check_encounter_round()):
print(f" Round {self.round[0].split('-')[0]}-{str(index + 1)}: {round_msg.upper()} ROUND")
if index == 0:
continue
if round_msg == "carousel":
game_assets.CAROUSEL_ROUND.add(
self.round[0].split("-")[0] + "-" + str(index + 1)
)
game_assets.ANVIL_ROUNDS.add(
self.round[0].split("-")[0] + "-" + str(index + 2)
)
game_assets.ITEM_PLACEMENT_ROUNDS.add(
self.round[0].split("-")[0] + "-" + str(index + 2)
)
elif round_msg == "pve":
game_assets.PVE_ROUND.add(
self.round[0].split("-")[0] + "-" + str(index + 1)
)
elif round_msg == "pvp":
game_assets.PVP_ROUND.add(
self.round[0].split("-")[0] + "-" + str(index + 1)
)
elif round_msg == "encounter":
game_assets.ENCOUNTER_ROUNDS.add(
self.round[0].split("-")[0] + "-" + str(index + 1)
)
if index+1 == 2 and 3 <= int(self.round[0].split("-")[0]) <= 4:
game_assets.AUGMENT_ROUNDS.add(
self.round[0].split("-")[0] + "-" + str(index + 2)
)
def second_round(self) -> None:
"""Move unknown champion to board after first carousel"""
print(f"\n[Second Round] {self.round[0]}")
self.message_queue.put("CLEAR")
while True:
result = arena_functions.bench_occupied_check()
if any(result):
break
self.arena.bench[result.index(True)] = "?"
for _ in range(arena_functions.get_level()):
self.arena.move_unknown()
self.end_round_tasks()
def carousel_round(self) -> None:
"""Handles tasks for carousel rounds"""
print(f"\n[Carousel Round] {self.round[0]}")
self.message_queue.put("CLEAR")
if self.round[0] == "3-4":
self.arena.final_comp = True
self.arena.check_health()
print(" Getting a champ from the carousel")
game_functions.get_champ_carousel(self.round[0])
def pve_round(self) -> None:
"""Handles tasks for PVE rounds"""
print(f"\n[PvE Round] {self.round[0]}")
self.message_queue.put("CLEAR")
sleep(0.5)
if self.round[0] in game_assets.AUGMENT_ROUNDS:
sleep(1)
self.arena.augment_roll = True
self.arena.pick_augment()
# Can't purchase champions for a short period after choosing augment
sleep(2.5)
if self.round[0] == "1-3":
sleep(1.5)
self.arena.fix_unknown()
self.arena.anvil_free[1:] = [True] * 8
self.arena.clear_anvil()
self.arena.anvil_free[:2] = [True, False]
self.arena.clear_anvil()
self.arena.tacticians_crown_check()
self.arena.fix_bench_state()
self.arena.spend_gold()
self.arena.move_champions()
self.arena.replace_unknown()
if self.arena.final_comp:
self.arena.final_comp_check()
self.arena.bench_cleanup()
self.end_round_tasks()
def pvp_round(self) -> None:
"""Handles tasks for PVP rounds"""
print(f"\n[PvP Round] {self.round[0]}")
self.message_queue.put("CLEAR")
sleep(0.5)
if self.round[0] in game_assets.AUGMENT_ROUNDS:
sleep(1)
self.arena.augment_roll = True
self.arena.pick_augment()
sleep(2.5)
if self.round[0] in ("2-1", "2-5"):
self.arena.buy_xp_round()
if self.round[0] in game_assets.PICKUP_ROUNDS:
print(" Picking up items")
game_functions.pickup_items()
self.arena.fix_bench_state()
self.arena.bench_cleanup()
if self.round[0] in game_assets.ANVIL_ROUNDS:
self.arena.clear_anvil()
self.arena.spend_gold(speedy=self.round[0] in game_assets.PICKUP_ROUNDS)
self.arena.move_champions()
self.arena.replace_unknown()
if self.arena.final_comp:
self.arena.final_comp_check()
self.arena.bench_cleanup()
if self.round[0] in game_assets.ITEM_PLACEMENT_ROUNDS:
sleep(1)
self.arena.place_items()
self.end_round_tasks()
def end_round_tasks(self) -> None:
"""Common tasks across rounds that happen at the end"""
self.arena.check_health()
self.arena.get_label()
game_functions.default_pos()