forked from SachaDef/Dofus-clicker-bot
/
new_backend.py
395 lines (343 loc) · 13.5 KB
/
new_backend.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
import new_globals as globals
import win32gui
import os
import time
import pyautogui as pag
import threading
import win32api
import win32con
import pynput.mouse as m
import pickle
# ==== WINDOW HANDLING ==== #
# Get active windows
def winEnumHandler(window_handle: int, _) -> None: # Copied from https://stackoverflow.com/questions/61865399/win32gui-shows-some-windows-that-are-not-open
if win32gui.IsWindowVisible(window_handle) and win32gui.GetWindowText(window_handle) != '':
globals.DwmGetWindowAttribute(globals.HWND(window_handle),
globals.DWORD(globals.DWMWA_CLOAKED),
globals.byref(globals.isCloacked),
globals.sizeof(globals.isCloacked))
title: str = win32gui.GetWindowText(window_handle)
if (globals.isCloacked.value == 0):
globals.active_windows.append((window_handle, title))
# Filter out non-dofus windows
def window_filtering() -> None:
globals.active_windows = []
win32gui.EnumWindows(winEnumHandler, None)
globals.active_windows = [window for window in globals.active_windows if "Dofus 2" in window[1]]
globals.active_character_names = [window[1].split(" - ")[0] for window in globals.active_windows]
# Get window handle from given character name
Window = tuple[()] | tuple[int, str]
def get_character_window(character_name: str) -> Window:
for window in globals.active_windows:
if window[1].startswith(character_name):
return window
return ()
# ==== MAP & CLICKS ==== #
MapCoords = tuple[int, int]
ScreenCoords = tuple[int, int]
Clicks = list[ScreenCoords]
# Get click coordinates associated to map coordinates from file
def load_click_coordinates(x: int, y: int) -> str:
with open("data/maps.bin", "rb") as file:
try:
click_coordinates: dict[MapCoords, Clicks] = pickle.load(file) #TODO cache ?
except EOFError:
return "No click coordinates for this map yet !"
click_coordinates = click_coordinates.get((x, y))
if click_coordinates is None:
return "No click coordinates for this map yet !"
click_coordinates_as_text = ["("+str(a)+", "+str(b)+")" for a, b in click_coordinates]
return '\n'.join(click_coordinates_as_text)
def click_coordinates_exist(x: int, y: int) -> bool:
with open("data/maps.bin", "rb") as file:
try:
click_coordinates: dict[MapCoords, Clicks] = pickle.load(file) #TODO cache ?
except EOFError:
return False
if (x, y) in click_coordinates.keys():
return True
return False
def save_click_coordinates(x: int, y: int, coordinates: str) -> None:
with open("data/maps.bin", "rb") as file:
try:
click_coordinates: dict[MapCoords, Clicks] = pickle.load(file)
except EOFError:
click_coordinates: dict[MapCoords, Clicks] = {}
coordinates = coordinates.split(";")[:-1]
coordinates_list = []
for coordinate in coordinates:
x_click, y_click = coordinate.replace("(", "").replace(")", "").split(",")
coordinates_list.append((int(x_click), int(y_click)))
click_coordinates[(x, y)] = coordinates_list
with open("data/maps.bin", "wb") as file:
pickle.dump(click_coordinates, file)
# ==== PATHS & MAPS ==== #
# Get all path files
def get_all_paths(string_filter: str) -> list[str]:
orig_dir = os.getcwd()
os.chdir("data/paths")
root_dir = os.getcwd()
path_files = os.listdir()
os.chdir(orig_dir)
return [root_dir+"\\"+path_file.replace(".bin", "") for path_file in path_files if (".bin" in path_file and
(path_file.startswith(string_filter) or string_filter == "Farming path"))]
# ==== SAFE EXIT ==== #
# Save backup data before app closing
def save_backup(type: str) -> None:
program_root = os.getcwd()
for root, _, files in os.walk("data"):
if root == "data":
for filename in files:
data_filename = os.path.join(program_root, "data", filename)
backup_filename = os.path.join(program_root, f"backup_{type}", filename)
with open(data_filename, "rb") as data_file:
try:
data = pickle.load(data_file)
except EOFError:
continue
with open(backup_filename, "wb") as backup_file:
pickle.dump(data, backup_file)
elif root == "data\\paths":
for filename in files:
data_filename = os.path.join(program_root, "data\\paths", filename)
backup_filename = os.path.join(program_root, f"backup_{type}\\paths", filename)
with open(data_filename, "rb") as data_file:
try:
data = pickle.load(data_file)
except EOFError:
continue
with open(backup_filename, "wb") as backup_file:
pickle.dump(data, backup_file)
# Automatisation bot
class CharacterBot():
def __init__(self, char_name, x_pos, y_pos, window_handle):
self.traveling = False
self.creating = False
self.mapfarming = False
self.pathfarming = False
self.character_name = char_name
self.x_pos = x_pos
self.y_pos = y_pos
self.x_dest = 0
self.y_dest = 0
self.previous_map = None
self.travel_thread = None
self.mapfarm_thread = None
self.pathfarm_thread = None
self.cancel_thread = None
self.cancel_flag = False
self.window_hwnd = window_handle
self.click_coords = ""
self.maps_file = r"data\maps.bin"
# ==== START/STOP METHODS ==== #
def travel_start(self):
self.traveling = True
self.travel_thread = threading.Thread(target=self.travel_complete)
self.travel_thread.start()
def travel_stop(self):
self.traveling = False
self.travel_thread = None
def creation_start(self):
self.creating = True
self.click_listener()
def creation_stop(self):
self.creating = False
def mapfarming_start(self):
self.mapfarming = True
self.mapfarm_thread = threading.Thread(target=self.farm_map)
self.mapfarm_thread.start()
def mapfarming_stop(self):
self.mapfarming = False
self.mapfarm_thread = None
def pathfarming_start(self, arg):
self.pathfarming = True
self.pathfarm_thread = threading.Thread(target=self.farm_path, args=(arg,))
self.pathfarm_thread.start()
def pathfarming_stop(self):
self.pathfarming = False
self.pathfarm_thread = None
def exit(self):
self.travel_stop()
self.creation_stop()
# ==== POSITION/DESTINATION MODIFICATION METHODS ==== #
def set_pos(self, coords):
self.x_pos = coords[0]
self.y_pos = coords[1]
def set_dest(self, coords):
self.x_dest = coords[0]
self.y_dest = coords[1]
# ==== CLICKING METHOD ==== #
def click(self, x, y):
lParam = win32api.MAKELONG(x, y-28)
win32gui.PostMessage(self.window_hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
time.sleep(0.05)
win32gui.PostMessage(self.window_hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)
# ==== MOVEMENT BY CLICK METHODS ==== #
def wrong_movement_modif(self):
if self.previous_map == 'u':
self.y_pos += 1
if self.previous_map == 'd':
self.y_pos -= 1
if self.previous_map == 'l':
self.x_pos += 1
if self.previous_map == 'r':
self.x_pos -= 1
self.previous_map = None
def wait_3_sec(self):
self.cancel_flag = True
time.sleep(3)
self.cancel_flag = False
def move_right(self):
if self.cancel_flag and self.previous_map != 'r':
self.wrong_movement_modif()
self.x_pos += 1
self.cancel_thread = threading.Thread(target=self.wait_3_sec)
self.cancel_thread.start()
self.click(1650, 410)
self.previous_map = 'r'
def move_left(self):
if self.cancel_flag and self.previous_map != 'l':
self.wrong_movement_modif()
self.x_pos -= 1
self.cancel_thread = threading.Thread(target=self.wait_3_sec)
self.cancel_thread.start()
self.click(300, 530)
self.previous_map = 'l'
def move_up(self):
if self.cancel_flag and self.previous_map != 'u':
self.wrong_movement_modif()
self.y_pos -= 1
self.cancel_thread = threading.Thread(target=self.wait_3_sec)
self.cancel_thread.start()
self.click(1020, 40)
self.previous_map = 'u'
def move_down(self):
if self.cancel_flag and self.previous_map != 'd':
self.wrong_movement_modif()
self.y_pos += 1
self.cancel_thread = threading.Thread(target=self.wait_3_sec)
self.cancel_thread.start()
self.click(900, 900)
self.previous_map = 'd'
def reset(self):
if self.cancel_flag:
self.wrong_movement_modif()
self.click(960, 470)
self.travel_stop()
# ==== MOVEMENT AUTOMATION METHODS ==== #
def travel_vertical(self, value):
if value == 0:
return
if value < 0:
for i in range(abs(value)):
if not self.traveling:
return
self.move_up()
time.sleep(3)
if self.x_pos == self.x_dest and self.y_pos == self.y_dest:
pass
# gl.popQ.MyPut("endTr")
# gl.popQ.open = False
if self.traveling:
time.sleep(5)
self.cancel_thread = None
else:
for i in range(value):
if not self.traveling:
return
self.move_down()
time.sleep(3)
if self.x_pos == self.x_dest and self.y_pos == self.y_dest:
pass
# gl.popQ.MyPut("endTr")
# gl.popQ.open = False
if self.traveling:
time.sleep(5)
self.cancel_thread = None
def travel_horizontal(self, value):
if value == 0:
return
if value < 0:
for i in range(abs(value)):
if not self.traveling:
return
self.move_left()
time.sleep(3)
if self.x_pos == self.x_dest and self.y_pos == self.y_dest:
pass
# gl.popQ.MyPut("endTr")
# gl.popQ.open = False
if self.traveling:
time.sleep(5)
self.cancel_thread = None
else:
for i in range(value):
if not self.traveling:
return
self.move_right()
time.sleep(3)
if self.x_pos == self.x_dest and self.y_pos == self.y_dest:
pass
# gl.popQ.MyPut("endTr")
# gl.popQ.open = False
if self.traveling:
time.sleep(5)
self.cancel_thread = None
def travel_complete(self):
if self.traveling:
self.travel_vertical(self.y_dest - self.y_pos)
self.travel_horizontal(self.x_dest - self.x_pos)
self.travel_stop()
return
# ==== MAP CREATION METHODS ==== #
def click_listener(self):
def on_click(x, y, _, pressed):
if self.creating:
if pressed:
x, y = int(x), int(y)
self.click_coords += f"({x},{y});"
else:
return False
def on_move(x, y):
if not self.creating:
return False
listener = m.Listener(on_click=on_click, on_move=on_move)
listener.start()
# ==== AUTOMATE FARMING METHODS ==== #
def farm_map(self):
flag = False
with open(self.maps_file, "r") as f:
for line in f.readlines():
map, coords = line.split(":")
if map == str((self.x_pos, self.y_pos)):
flag = True
coords = coords.split(";")[:-1]
pag.keyDown("shift")
for coord in coords:
x, y = int(coord.split(",")[0][1:]), int(coord.split(",")[1][:-1])
self.click(x, y)
time.sleep(0.1)
pag.keyUp("shift")
break
else:
continue
if not flag:
pass
# gl.popQ.MyPut("noMap")
return len(coords)
def farm_path(self, path_file):
path_file = "data\\paths\\"+path_file+".bin"
with open(path_file, "r") as f:
for line in f.readlines():
line = line.strip()
mapx, mapy = line.split(',')
mapx, mapy = int(mapx), int(mapy)
print(mapx, mapy)
if (mapx, mapy) != (self.x_pos, self.y_pos):
print("traveling")
self.set_dest((mapx, mapy))
self.travel_start()
while self.traveling:
time.sleep(1)
count = self.farm_map()
time.sleep(7 + count*3)