-
Notifications
You must be signed in to change notification settings - Fork 91
/
game.py
224 lines (197 loc) · 7.89 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
"""
Handles tasks that happen each game round
"""
from time import sleep, perf_counter
import random
import multiprocessing
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:
self.message_queue = message_queue
self.arena = Arena(self.message_queue)
self.round = "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() != "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
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: str = game_functions.get_round()
if (
settings.FORFEIT
and perf_counter() - self.start_time > self.forfeit_time
):
game_functions.forfeit()
return
if self.round != ran_round:
if self.round in game_assets.PVP_ROUND:
game_functions.default_pos()
self.pvp_round()
ran_round: str = self.round
elif self.round in game_assets.PVE_ROUND:
game_functions.default_pos()
self.pve_round()
ran_round: str = self.round
elif self.round in game_assets.CAROUSEL_ROUND:
self.carousel_round()
ran_round: str = self.round
elif self.round in game_assets.SECOND_ROUND:
self.second_round()
ran_round: str = self.round
sleep(0.5)
def second_round(self) -> None:
"""Move unknown champion to board after first carousel"""
print(f"\n[Second Round] {self.round}")
self.message_queue.put("CLEAR")
while True:
result = arena_functions.bench_occupied_check()
if any(result):
break
self.arena.bench[result.index(True)] = "?"
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}")
self.message_queue.put("CLEAR")
if self.round == "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)
def pve_round(self) -> None:
"""Handles tasks for PVE rounds"""
print(f"\n[PvE Round] {self.round}")
self.message_queue.put("CLEAR")
sleep(0.5)
if self.round 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 == "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}")
self.message_queue.put("CLEAR")
sleep(0.5)
if self.round in game_assets.AUGMENT_ROUNDS:
sleep(1)
self.arena.augment_roll = True
self.arena.pick_augment()
sleep(2.5)
if self.round in ("2-1", "2-5"):
self.arena.buy_xp_round()
if self.round 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 in game_assets.ANVIL_ROUNDS:
self.arena.clear_anvil()
if self.round in game_assets.PICKUP_ROUNDS:
self.arena.spend_gold(speedy=True)
else:
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()
if self.round 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()