-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
283 lines (253 loc) · 9.22 KB
/
main.py
File metadata and controls
283 lines (253 loc) · 9.22 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
import pyautogui, time
import scripts.utils as utils
import scripts.judge as judge
import scripts.config as config
import threading, keyboard
import pygetwindow as gw
def save_target_image(x, y):
"""
Pass in x and y and save the target needed to recognized picture
@params:
x: int, left position
y: int, top positionq
@return:
im_path: str, saved image path
"""
im = pyautogui.screenshot(region=(int(x - 95), int(y - 10), 145, 55))
im_path = utils.complete_path("images/cache1.png")
im.save(im_path)
time.sleep(0.1)
return im_path
def save_target_attr_image(x, y):
"""
Pass in shenghai.left and shenghai.top and save the target's attr needed to recognized picture
@params:
x: int, shenghai left position
y: int, shenghai top position
@return:
im_path: str, saved image path
"""
im = pyautogui.screenshot(region=(int(x + 1055), int(y + 315), 40, 38))
im_path = utils.complete_path("images/cache2.png")
im.save(im_path)
time.sleep(0.1)
return im_path
def save_target_COST_image(x, y):
"""
Pass in shenghai.left and shenghai.top and save the target's COST needed to recognized picture
@params
x: int, shenghai left position
y: int, shenghai top position
@return
im_path: str, saved image path
"""
im = pyautogui.screenshot(region=(int(x + 1374), int(y + 135), 110, 30))
im_path = utils.complete_path("images/cache3.png")
im.save(im_path)
time.sleep(0.1)
return im_path
def save_target_handle_image(x, y):
"""
Pass in shenghai.left and shenghai.top and save the target's handle message needed to recognized picture
@params
x: int, shenghai left position
y: int, shenghai top position
@return
im_path: str, saved image path
"""
im = pyautogui.screenshot(region=(int(x + 1330), int(y + 220), 160, 40))
im_path = utils.complete_path("images/cache4.png")
im.save(im_path)
time.sleep(0.1)
return im_path
def check_is_bottom(x, y):
"""
Check to see if you have scrolled to the end
@params:
x: int, shenghai left position
y: int, shenghai top position
@return:
is_bottom: bool, if is bottom
"""
im = pyautogui.screenshot(region=(int(x + 968), int(y + 735), 18, 20))
im_path = utils.complete_path('images/cache.png')
im.save(im_path)
mathced_degree = judge.get_matched_degree(im_path, utils.complete_path('images/is_bottom.png'))
if mathced_degree == 1.0:
return True
else:
return False
def check_is_locked(im_path):
"""
check target if is locked
@params
im_path: str, target image path
@return
is_locked: bool, if is locked
"""
matched_degree_locked, matched_degree_not_locked = judge.get_matched_degree(im_path, utils.complete_path('images/is_locked.png')), judge.get_matched_degree(im_path, utils.complete_path('images/is_not_locked.png'))
# print(f'DEBUG: locked---{matched_degree_locked} not-locked---{matched_degree_not_locked}')
return matched_degree_locked < matched_degree_not_locked
def check_is_discarded(im_path):
"""
check target if is discarded
@params
im_path: str, target image path
@return
is_discarded, bool, if is discarded
"""
matched_degree_discarded, matched_degree_not_discarded = judge.get_matched_degree(im_path, utils.complete_path('images/is_discarded.png')), judge.get_matched_degree(im_path, utils.complete_path('images/is_not_discarded.png'))
# print(f'DEBUG: discarded---{matched_degree_discarded} not-discarded---{matched_degree_not_discarded}')
return matched_degree_discarded < matched_degree_not_discarded
def start_selection(target_handle_path, suit_name, attr_name, COST):
"""
Filters the current target based on the contents of config
@params
target_handle_path: str, target handle message image path
suit_name: str, suit name
attr_name: str, attr name
COST: str, COST
@return
None
"""
if suit_name in config.config.keys():
if attr_name in config.config[suit_name][COST]:
if not config.ignore_handled:
pyautogui.press('c')
elif not check_is_locked(target_handle_path):
pyautogui.press('c')
else:
print('DEBUG: 已锁定')
else:
if not config.ignore_handled:
pyautogui.press('z')
elif not check_is_discarded(target_handle_path):
pyautogui.press('z')
else:
print('DEBUG: 已弃置')
time.sleep(0.5)
def get_suit_name(im_path):
"""
Get target suit name
@params
im_path: str, target image path
@return
suit_name: str, target image suit name
"""
mathced_degree_arr = [(judge.get_matched_degree(im_path, utils.complete_path(f'images/suit_{i}_{j}.png')), suit_name) for (i, j), suit_name in judge.suit.items()]
suit_name = min(mathced_degree_arr)[1]
return suit_name
def get_attr_name(im_path):
"""
Get target attr name
@params
im_path: str, target attr image path
@return
attr_name: str, target image attr name
"""
mathced_degree_arr = [((judge.get_matched_degree(im_path, utils.complete_path(f'images/attr_{i}.png')), attr_name)) for i, attr_name in judge.attr.items()]
attr_name = min(mathced_degree_arr)[1]
return attr_name
def get_COST(im_path):
"""
Get target COST
@params
im_path: str, target COST image path
@return
COST: str, target COST
"""
mathced_degree_arr = [((judge.get_matched_degree(im_path, utils.complete_path(f'images/COST_{i}.png'))), f'COST {i}') for i in (1, 3, 4)]
COST = min(mathced_degree_arr)[1]
return COST
def create_scroller():
"""
create a scroller to scroll the screen
"""
i = 0
def scroll():
nonlocal i
if i == 0:
for _ in range(6):
pyautogui.scroll(-157)
time.sleep(0.4)
else:
for _ in range(6):
pyautogui.scroll(-156)
time.sleep(0.4)
i = (i + 1) % 3
return scroll
def manager_shenghai():
try:
window = gw.getWindowsWithTitle("鸣潮")[0]
if window:
window.activate()
time.sleep(1)
else:
print('未找到鸣潮')
except IndexError:
print('未找到鸣潮')
# start project
try:
# Anchor the position through the icon
shenghai_image_path = utils.complete_path('images\\shenghai.png')
shenghai_position = pyautogui.locateOnScreen(shenghai_image_path, confidence=0.6)
if shenghai_position:
scroller = create_scroller()
while check_is_bottom(shenghai_position.left, shenghai_position.top):
# the first target position
x, y = shenghai_position.left + 220, shenghai_position.top + 180
# every row has six items
for _ in range(6):
pyautogui.click(x, y)
time.sleep(0.2)
# if ignore handled target
target_handle_path = save_target_handle_image(shenghai_position.left, shenghai_position.top)
if config.ignore_handled and check_is_locked(target_handle_path):
print('DEBUG: 已锁定')
# change left position
x += 138
continue
elif config.ignore_handled and check_is_discarded(target_handle_path):
print('DEBUG: 已弃置')
# change left position
x += 138
continue
target_path = save_target_image(x, y)
target_attr_path = save_target_attr_image(shenghai_position.left, shenghai_position.top)
target_COST_path = save_target_COST_image(shenghai_position.left, shenghai_position.top)
suit_name = get_suit_name(target_path)
attr_name = get_attr_name(target_attr_path)
COST = get_COST(target_COST_path)
print(f'DEBUG: {suit_name} {attr_name} {COST}')
# start selection
start_selection(target_handle_path, suit_name, attr_name, COST)
# change left position
x += 138
# refresh left position
x = shenghai_position.left + 220
# scroll the screen
scroller()
except pyautogui.ImageNotFoundException:
print('未打开背包声骸界面')
except KeyboardInterrupt:
print('手动终止脚本')
def check_for_exit():
"""
Check if need to exit script
"""
while True:
if keyboard.is_pressed('q'):
print("Exiting...")
break
time.sleep(0.05)
def start_project():
# 创建并启动manager_shenghai线程
manager_thread = threading.Thread(target=manager_shenghai)
manager_thread.daemon = True
manager_thread.start()
# 创建并启动检查退出线程
exit_thread = threading.Thread(target=check_for_exit)
exit_thread.start()
# 等待退出线程结束
exit_thread.join()
start_project()