-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
438 lines (403 loc) · 17.1 KB
/
main.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import threading
from event_listeners import EVENT_LISTENERS_MAPPED
from macro import Macro_Type
import utils
import pyautogui
import keyboard
import pynput
import time
import os
import win32gui
import win32con
from typing import Callable
from pynput.keyboard import Key
import pydirectinput
import macro_edit
import menu
import cam_editor
import keyboard
import traceback
def close_roblox():
hwnd = win32gui.FindWindow(None, "Roblox")
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
def to_date(f: float):
s = int(f)
seconds = s % 60
minutes = (s % 3600) // 60
hours = (s % 86400) // 3600
return f"{hours}h {minutes}m {seconds}s"
def main(macro_id: str):
print("Executing macro in 3...", end="\r")
time.sleep(1)
print("Executing macro in 2...", end="\r")
time.sleep(1)
print("Executing macro in 1...", end="\r")
time.sleep(1)
should_stop = False
is_disconnected = False
time_runs = [] # A list of time of each run
logs = [] # Output during ingame macro execution
browser_window: int | None = None
losses = 0
wins = 0
disconnections = 0
config = utils.read_config()
macro = macro_edit.get_macro(macro_id)
def cancel_listener(key: pynput.keyboard.Key):
nonlocal should_stop
if key == pynput.keyboard.Key.backspace:
should_stop = True
def disconnection_listener(
should_stop_get: Callable[[], bool], get_is_disconnected: Callable[[], bool]
):
nonlocal is_disconnected
while not should_stop_get():
hwnd = win32gui.FindWindow(None, "Roblox")
pos, color = utils.get_config_prop(utils.DISCONNECTED_DIALOG_BOX)
x, y = pos
if (
pyautogui.pixelMatchesColor(x, y, color)
and hwnd == win32gui.GetForegroundWindow()
):
time.sleep(5)
if (
pyautogui.pixelMatchesColor(x, y, color)
and hwnd == win32gui.GetForegroundWindow()
):
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
is_disconnected = True
while get_is_disconnected():
pass
def info_listener(should_stop_get: Callable[[], bool]):
nonlocal time_runs
nonlocal wins
nonlocal losses
nonlocal disconnections
s = time.perf_counter()
info = "\n\t".join(
[
f"\tTime elapsed: {to_date(time.perf_counter() - s)}",
f"# of runs completed: {len(time_runs)}",
f"Current run #: {len(time_runs) + 1}",
]
)
while not should_stop_get():
info = "\n\t".join(
[
"",
"\t[INFO]",
f"Time elapsed: {to_date(time.perf_counter() - s)}",
f"# of waves per run: {macro['waves']}",
f" - # of wins: {wins}",
f" - # of losses: {losses}",
f" - win rate: {(f'{round((wins/len(time_runs)) * 10000)/100}%') if len(time_runs) != 0 else 'N/A'}",
f"Current run #: {len(time_runs) + 1}",
f"# of runs completed: {len(time_runs)}",
f" - Fastest run time: {to_date(min(time_runs)) if len(time_runs) != 0 else 'N/A'}",
f" - Slowest run time: {to_date(max(time_runs)) if len(time_runs) != 0 else 'N/A'}",
f" - Average run time: {to_date(sum(time_runs)/len(time_runs)) if len(time_runs) != 0 else 'N/A'}",
f"# of disconnections: {disconnections}",
"",
"[LOGS]",
"",
*logs,
]
)
os.system("cls")
print(info)
time.sleep(menu.Menu.TICK_RATE)
os.system("cls")
print(info)
listener = pynput.keyboard.Listener(on_press=cancel_listener)
keyboard_controller = pynput.keyboard.Controller()
d_listener = threading.Thread(
target=disconnection_listener,
args=(lambda: should_stop, lambda: is_disconnected),
)
i_listener = threading.Thread(target=info_listener, args=(lambda: should_stop,))
listener.start()
d_listener.start()
i_listener.start()
def execute_macros(
is_disconnected: Callable[[], bool],
should_stop: Callable[[], bool],
start: int | None = None,
end: int | None = None,
):
nonlocal browser_window
seq_macros: list[dict[str]] = macro_edit.get_macro(macro_id)["lobby_sequence"]
seq_macros = seq_macros[
start - 1 if start != None else 0 : end if end != None else len(seq_macros)
]
for i, macro_seq in enumerate(seq_macros):
if is_disconnected() or should_stop():
return
match macro_seq["type"]:
case Macro_Type.TINY_TASK:
logs.append(f"Executing tiny task {i + 1}.exe")
os.system(f'"%CD%/macros/{macro_id}/{i + 1}.exe"')
case Macro_Type.CLICK:
x, y = macro_seq["position"]
logs.append(f"Clicked ({x}, {y})")
try:
pydirectinput.click(x, y) # NOT CLICKING WHEN NO FOUCSED WINDOW
except Exception as e:
print(e)
should_stop = True
os._exit(1)
if macro_seq["should_fullscreen_roblox"]:
if browser_window == None:
browser_window = win32gui.GetForegroundWindow()
logs.append("Waiting for Roblox application...")
elapsed = time.perf_counter()
while not is_disconnected() and not should_stop():
hwnd = win32gui.FindWindow(None, "Roblox")
if time.perf_counter() - elapsed >= 10:
logs.append(
"Failed to detect Roblox within 10 seconds. Attempting to relaunch..."
)
elapsed = time.perf_counter()
try:
pydirectinput.click(x, y)
except Exception as e:
print(e)
should_stop = True
os._exit(1)
if hwnd != 0:
logs.append(
"Maximized Roblox application. Continuing..."
)
win32gui.ShowWindow(
win32gui.FindWindow(None, "Roblox"),
win32con.SW_RESTORE,
)
win32gui.SetWindowPos(
hwnd,
win32con.HWND_NOTOPMOST,
0,
0,
0,
0,
win32con.SWP_NOMOVE + win32con.SWP_NOSIZE,
)
win32gui.SetWindowPos(
hwnd,
win32con.HWND_TOPMOST,
0,
0,
0,
0,
win32con.SWP_NOMOVE + win32con.SWP_NOSIZE,
)
win32gui.SetWindowPos(
hwnd,
win32con.HWND_NOTOPMOST,
0,
0,
0,
0,
win32con.SWP_SHOWWINDOW
+ win32con.SWP_NOMOVE
+ win32con.SWP_NOSIZE,
)
break
case Macro_Type.WAIT:
time.sleep(macro_seq["ms"] / 1000)
case Macro_Type.WAIT_CONDITIONALLY:
x, y = macro_seq["position"]
color = macro_seq["color"]
logs.append(f"Waiting for condition...")
while (
not pyautogui.pixelMatchesColor(x, y, color)
and not should_stop()
and not is_disconnected()
):
pass
logs.append(f"Condition fulfilled. Continuing...")
case Macro_Type.KEY_DOWN:
keyboard_controller.press(eval(macro_seq["key"]))
case Macro_Type.KEY_UP:
keyboard_controller.release(eval(macro_seq["key"]))
case Macro_Type.KEY_PRESS:
keyboard_controller.tap(eval(macro_seq["key"]))
case Macro_Type.REPEAT_LINES:
if macro_seq["repeat_type"] == "timer":
s = time.perf_counter()
seconds = macro_seq["ms"] / 1000
start, end = macro_seq["lines"]
logs.append(
f"Repeating #{start} to #{end} for {seconds} seconds."
)
while time.perf_counter() - s <= seconds:
execute_macros(is_disconnected, should_stop, start, end)
logs.append(f"Finished repeating task. Continuing...")
case Macro_Type.GLOBAL_TINY_TASK:
logs.append(f"Executing bin tinytask {macro_seq['file_name']}")
os.system(f'"%CD%/bin/{macro_seq["file_name"]}"')
case Macro_Type.LISTENER:
try:
val = EVENT_LISTENERS_MAPPED[macro_seq["name"]]
logs.append(
f"Waiting for response from {macro_seq['name']} whose key value is {val}..."
)
pos, color = utils.get_config_prop(val)
x, y = pos
while (
not pyautogui.pixelMatchesColor(x, y, color)
and not should_stop()
and not is_disconnected()
):
pass
logs.append("\t- Response received!")
except Exception:
should_stop = True
time.sleep(5)
print(traceback.format_exc())
print(EVENT_LISTENERS_MAPPED)
print(macro_seq["name"])
os._exit(1)
def ingame_macros(
is_disconnected: Callable[[], bool],
should_stop: Callable[[], bool],
starting_run_time: float,
):
nonlocal logs
nonlocal time_runs
nonlocal wins
nonlocal losses
has_lost = False
run_ended = False
if is_disconnected() or should_stop():
return
def detect_loss(
should_stop: Callable[[], bool],
is_disconnected: Callable[[], bool],
run_ended: Callable[[], bool],
):
nonlocal has_lost
while not should_stop() and not is_disconnected() and not run_ended():
pos1, color1 = utils.get_config_prop(utils.DEFEAT_LABEL)
pos2, color2 = utils.get_config_prop(utils.HP_BAR_ZERO)
x1, y1 = pos1
x2, y2 = pos2
if pyautogui.pixelMatchesColor(
x1, y1, color1
) and pyautogui.pixelMatchesColor(x2, y2, color2):
has_lost = True
loss_detector = threading.Thread(
target=detect_loss,
args=(should_stop, is_disconnected, lambda: run_ended),
)
loss_detector.start()
dirs = [
path
for path in os.listdir(f"{os.getcwd()}\\macros\\{macro_id}")
if os.path.isdir(f"{os.getcwd()}\\macros\\{macro_id}\\{path}")
]
disabled_cam_angles = set(macro["disabled_cam_angles"])
while not is_disconnected() and not should_stop():
found_cam_angle_name = None
has_lost = False
s = time.perf_counter()
e = s
while (
not should_stop()
and found_cam_angle_name == None
and e - s <= 30
and len(dirs) > 0
and not is_disconnected()
):
e = time.perf_counter()
for cam_angle in dirs:
if (
cam_angle not in disabled_cam_angles
and cam_editor.compare_to(macro_id, cam_angle)
>= cam_editor.SIMILARITY
):
found_cam_angle_name = cam_angle
logs.append(f"Detected {found_cam_angle_name}")
break
current_wave = 1
while not should_stop() and not is_disconnected():
x, y = config[f"{utils.START_WAVE_BTN}_pos"]
color = config[f"{utils.START_WAVE_BTN}_color"]
if pyautogui.pixelMatchesColor(x, y, color):
os.system('"%CD%/bin/clickwavestart.exe"')
break
if len(dirs) == 0:
logs.append("No camera angles found for this macro")
if e - s >= 30:
logs.append("Failed to detect camera angles")
while not should_stop() and not is_disconnected() and not has_lost:
x, y = config[f"{utils.WAVE_COMPLETED_LABEL}_pos"]
color = config[f"{utils.WAVE_COMPLETED_LABEL}_color"]
if pyautogui.pixelMatchesColor(x, y, color):
current_wave += 1
if current_wave > macro["waves"]:
pyautogui.press("alt")
win32gui.SetForegroundWindow(browser_window)
close_roblox()
wins += 1
time_runs.append(time.perf_counter() - starting_run_time)
return
path = f"{os.getcwd()}\\macros\\{macro_id}\\{found_cam_angle_name}\\{current_wave}"
if os.path.exists(path):
macros_in_wave = os.listdir(path)
for executable in macros_in_wave:
os.system(
f'"%CD%/macros/{macro_id}/{found_cam_angle_name}/{current_wave}/{executable}"'
)
else:
logs.append(f"No macros found for wave {current_wave}")
time.sleep(5)
if has_lost:
logs.append("Loss detected. Retrying...")
losses += 1
now = time.perf_counter()
time_runs.append(now - starting_run_time)
os.system('"%CD%/bin/retryfromloss.exe"')
time.sleep(3)
else:
run_ended = True
break
while not should_stop:
starting_run_time = time.perf_counter()
logs.append("Executing pre-game macro sequence...")
execute_macros(lambda: is_disconnected, lambda: should_stop)
logs.append("")
ingame_macros(lambda: is_disconnected, lambda: should_stop, starting_run_time)
logs.clear()
if is_disconnected:
logs.append("Detected disconnection. Attempting to reconnect...")
close_roblox()
pyautogui.press("alt")
win32gui.SetForegroundWindow(browser_window)
while not utils.has_internet():
logs.append("There is no internet connection! Waiting...")
is_disconnected = False
listener.stop()
should_exit = False
def on_escape(key: pynput.keyboard.Key):
nonlocal should_exit
if (
key == pynput.keyboard.Key.esc
and win32gui.GetForegroundWindow() == menu.HWND
):
should_exit = True
exit_listener = pynput.keyboard.Listener(on_press=on_escape)
exit_listener.start()
print("\nPress ESCAPE to return to macro profile selector")
time.sleep(0.1)
while not should_exit:
pass
exit_listener.stop()
def list_macros() -> list[menu.MenuItem]:
macros = utils.read_macros()
return [
menu.MenuItem(x["name"], (lambda y: lambda: main(y["id"]))(x)) for x in macros
]
if __name__ == "__main__":
m = menu.Menu("Roblox Macro Executor")
m.header("Select a macro profile to execute")
m.item(list_macros)
m.show()