forked from aparoski/WhiteoutSurvivalBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper_Funcs.py
More file actions
287 lines (216 loc) · 8.87 KB
/
Helper_Funcs.py
File metadata and controls
287 lines (216 loc) · 8.87 KB
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
import pyautogui as p
p.useImageNotFoundException()
import time
import relative_locations as rl
import pandas as pd
import dir_config
import datetime
import os
#General Funcs -------------------------------------------------------
def time_w_clock_loc(x1, y1, W, L, x_offset, y_offset):
"""returns the top left corner coordinates of the rectangle containing
a march time with a clock icon"""
dir = "A:\\Data_Science\\Projects\\Whiteout_Survival\\WoS Bot\\images\\"
clock_loc = check_image(x1, y1, W, L, dir + "Main_UI\\wait_clock.JPG",
message = " clock ")
clock_loc = [clock_loc[0] + x_offset, clock_loc[1] + y_offset]
new_TL = relativexy(x1, y1, W, L, clock_loc)
return(new_TL)
def Window_lw(x1, y1, x2, y2):
"""finds the length and width of a rectangular
box between two coordinates if the coordinates are
located at the top left and bottom right corners of the rectangle"""
l = y2 - y1
w = x2 - x1
return(w, l)
def relativexy (x1, y1, W, L, position):
pos_x, pos_y = position
x_distance = pos_x - x1
y_distance = pos_y - y1
rel_x = x_distance / W
rel_y = y_distance / L
return(rel_x, rel_y)
def screenshotter(x1, y1, W, L,
locx1,
locy1,
locx2,
locy2,
save_name) -> None:
"""Takes screenshot and saves named item to screenshot
directory"""
x1_temp = round(x1 + W * locx1)
y1_temp = round(y1 + L * locy1)
x2_temp = round(x1 + W * locx2)
y2_temp = round(y1 + L * locy2)
W_temp = x2_temp - x1_temp
L_temp = y2_temp - y1_temp
print(W_temp, L_temp)
p.screenshot("Screenshots\\" + save_name + "_temp.JPG",
region = (x1_temp, y1_temp,
W_temp, L_temp))
def check_image(x1, y1, W, L, path, itterator = 10,
confidence = 0.7, message = "",
raise_error = True):
i = 0
while True and i <= itterator:
i += 1
try:
image_loc = p.locateCenterOnScreen(path,
region = (x1, y1, W, L),
confidence= confidence)
break
except:
print(message + "Check " + str(i))
time.sleep(1)
pass
if raise_error:
if i >= itterator:
raise("Function to check " + str(message) + " timed out")
else:
return(image_loc)
else:
if i >= itterator:
return(False)
else:
return(image_loc)
def swipe(x1, y1, W, L, dir = "up", magnitude = 1, release_delay = 0.1,
manual_duration = False,
starting_x = 0.5, starting_y = 0.5) -> None:
"""direction refers to where the screen moves"""
print(starting_x, starting_y, magnitude)
#function does not work properly for map below the 0.5 threshold.
if (#starting_y + starting_y * magnitude > 1 or
starting_x + starting_x * magnitude > 1 or
starting_x > 1 or starting_y > 1 or magnitude > 1):
raise("starting value or magnitude inapprorpiate")
if manual_duration:
duration = manual_duration
else:
duration = magnitude * 0.4
p.moveTo(x1 + W * starting_x,
y1 + L * starting_y)
p.mouseDown(button = "left")
if dir == "up":
#place cursor in center of screen and swipe up
p.moveTo(x1 + W * starting_x,
y1 + L * (starting_y + starting_y * magnitude),
duration = duration)
elif dir == "down":
p.moveTo(x1 + W * starting_x,
y1 + L * (starting_y - starting_y * magnitude),
duration = duration)
elif dir == "right":
p.moveTo(x1 + W * (starting_x - starting_x * magnitude),
y1 + L * starting_y,
duration = duration)
elif dir =="left":
p.moveTo(x1 + W * (starting_x + starting_x * magnitude),
y1 + L * starting_y,
duration = duration)
elif dir == "bottomleft":
p.moveTo(x1 + W * (starting_x + starting_x * magnitude),
y1 + L * (starting_y - starting_y * magnitude),
duration = duration * 2)
elif dir == "bottomright":
p.moveTo(x1 + W * (starting_x - starting_x * magnitude),
y1 + L * (starting_y - starting_y * magnitude),
duration = duration * 2)
elif dir == "topleft":
p.moveTo(x1 + W * (starting_x + starting_x * magnitude),
y1 + L * (starting_y + starting_y * magnitude),
duration = duration * 2)
elif dir == "topright":
p.moveTo(x1 + W * (starting_x - starting_x * magnitude),
y1 + L * (starting_y + starting_y * magnitude),
duration = duration * 2)
time.sleep(release_delay)
p.mouseUp(button = "left")
#General Funcs -------------------------------------------------------
#Error Management ---------------------------------------------------
def start_video_recording(x1, y1, W, L):
p.moveTo(x1 + W * rl.video_record_step1[0],
y1 + L * rl.video_record_step1[1])
p.click()
time.sleep(2)
p.moveTo(x1 + W * rl.video_record_step2[0],
y1 + L * rl.video_record_step2[1])
p.click()
print("screen recording started")
def stop_video_recording(x1, y1, W, L):
dir = "A:\\Data_Science\\Projects\\Whiteout_Survival\\WoS Bot\\"
stop_path = "images\\error_handling\\"
stop = "video_recording_stop.JPG"
path = dir + stop_path + stop
TL = [round(i) for i in
[x1 + W * rl.video_record_step3_TL[0], y1 + L * rl.video_record_step3_TL[1]]]
BR = [round(i) for i in
[x1 + W * rl.video_record_step3_BR[0], y1 + L * rl.video_record_step3_BR[1]]]
BWL = Window_lw(TL[0], TL[1],
BR[0], BR[1])
p.screenshot(dir + stop_path + "tester.JPG",
TL + [BWL[0]] + [BWL[1]])
video_stop = p.locateCenterOnScreen(path, confidence = 0.6,
region = TL +
[BWL[0]] + [BWL[1]])
p.moveTo(video_stop)
time.sleep(1)
p.click()
print("screen recording stopped")
time.sleep(2)
def no_error_video_delete(window_name) -> None:
"""delete the most recently recorded video from the directory
associated with the blue stacks player selected"""
window_serial = window_name.replace("BlueStacks App Player", "")
window_serial = window_serial.strip()
dir = dir_config.BlueStacks_Vids
if window_serial == "":
folder_name = "BlueStacks-Pie64"
else:
folder_name = "BlueStacks-Pie64_" + window_serial
files = [f for f in os.scandir(dir + folder_name) if f.is_file()]
file_frame = pd.DataFrame({"file" : files})
file_frame["file_name"] = file_frame["file"].apply(lambda x: x.name)
file_frame["datetime"] = file_frame["file_name"].apply(lambda x: x.replace("Recording-", "").replace(os.path.splitext(x)[1], ""), "%Y-%m-%d-%H%M%S")
final_file_frame = file_frame[file_frame["datetime"] == max(file_frame["datetime"])]
for file in final_file_frame["file"]:
print(file.name)
if os.path.exists(file):
os.remove(file)
#Error Management --------------------------------------------------
#Helpful Whiteout Funcs----------------------------------------------
def check_location(x1, y1, W, L):
"""checks the UI to determine whether the game is in the world map,
the City map, or neither.
there has to be a better way than try/except..."""
i = 0
while True and i <= 10:
i += 1
if i % 2 == 0:
try:
img = p.locateOnScreen(r"images/Main_UI/City_door.JPG",
region = (x1, y1, W, L),
confidence= 0.7)
return("World Map")
except:
pass
else:
try:
img = p.locateOnScreen(r"images/Main_UI/world_map.JPG",
region = (x1, y1, W, L),
confidence= 0.7)
return("City")
except:
pass
if i >= 10:
return("Neither")
def check_victory(x1, y1, W, L):
print("checking for victory text")
check_victory = check_image(x1, y1, W, L, r"images/images_Events/misc/Victory_Screen.JPG",
20, confidence = 0.7,
message = "Victory_Screen")
if check_victory:
return(1)
#Helpful Whiteout Funcs----------------------------------------------
if __name__ == "__main__":
window_name = "BlueStacks App Player"
no_error_video_delete(window_name)