This repository was archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
390 lines (333 loc) · 11.7 KB
/
main.py
File metadata and controls
390 lines (333 loc) · 11.7 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
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
# -*- coding: utf-8 -*-
from pathlib import Path
import time
import cv2
import numpy as np
from utils.get_screenshot import get_screenshot
from utils.mouse_move import mouse_move
class PatternPosition:
def __init__(self, screenshot, pattern, threshold=0.7):
self.threshold = threshold
self.screenshot = screenshot
self.pattern = cv2.cvtColor(pattern, cv2.COLOR_BGR2GRAY)
self.pts = self.get_pts()
def is_valid(self):
if self.pts:
return True
return False
def get_pts(self) -> list:
# 獲得對應物件的位置
w, h = self.pattern.shape[::-1]
res = cv2.matchTemplate(
self.screenshot,
self.pattern,
cv2.TM_CCOEFF_NORMED
)
loc = np.where( res >= self.threshold)
pts = []
for pt in zip(*loc[::-1]):
x1, y1 = pt # 左上點
x2, y2 = w+x1, h+y1 # 右下點
pts.append((x1, y1, x2, y2))
return pts
def get_screenshot(self):
return self.screenshot
def get_debug_screenshot(self):
x1, y1, x2, y2 = self.get_pt()
screenshot = self.get_screenshot()
cv2.rectangle(
screenshot,
(x1, y1),
(x2, y2),
(0, 0, 255),
2
)
screenshot = cv2.resize(screenshot, fx=0.3, fy=0.3, dsize=None)
return screenshot
def show_debug_screenshot(self):
cv2.imshow("debug-screenshot", self.get_debug_screenshot())
cv2.waitKey()
return
def get_pt(self):
x1, y1, x2, y2 = self.pts[0]
return x1, y1, x2, y2
def get_center_pt(self):
x1, y1, x2, y2 = self.pts[0]
return ((x1+x2)//2, (y1+y2)//2)
class Button:
def __init__(self, pattern_path):
self.pattern_path = pattern_path
self.pattern = self.load_pattern()
def load_pattern(self):
return cv2.imread(self.pattern_path)
def detect(self):
screenshot = get_screenshot()
pts = PatternPosition(screenshot, self.pattern)
if pts.is_valid():
print(f'---- Find [{self.pattern_path}]')
return pts
def wait_until(self, timeout):
print(f"---- Wait [{self.pattern_path}]")
start_time = time.time()
pts = self.detect()
count = 1
while not pts.is_valid() and (time.time() - start_time) <= timeout:
print(f"Waiting count: {count}")
count += 1
pts = self.detect()
if not pts:
print(f'---- Timeout [{self.pattern_path}]')
return pts
class Switcher:
def __init__(self, before:str, after:str, name=''):
self.name = name
self.before = Button(before)
self.after = Button(after)
def switch(self):
before_pts = self.before.wait_until(3)
if not before_pts.is_valid():
print(f'---- 此畫面找不到 Before {self.name} 按鈕')
print(f'---- 嘗試搜尋 After {self.name} 按鈕')
after_pts = self.after.wait_until(2)
if not after_pts.is_valid():
print(f'---- 找不到任何 Before & After {self.name} 按鈕')
return False
else:
print(f'---- 已經在 {self.name} 頁面')
return True
else:
print(f'---- 此畫面找到 Before {self.name} 按鈕')
x, y = before_pts.get_center_pt()
mouse_move(x, y)
after_pts = self.after.wait_until(2)
if not after_pts.is_valid():
print(f'---- 此畫面找不到 After {self.name} 按鈕')
return False
else:
print(f'---- 已經在 {self.name} 頁面')
return True
def home() -> bool:
print("---- 執行前往主頁")
switch = Switcher(
name='主頁按鈕',
before='./img/home-before.png',
after='./img/home-after.png',
)
return switch.switch()
def gachapon() -> bool:
print("---- 執行前往轉蛋頁面")
switch = Switcher(
name='轉蛋',
before='./img/gachapon-before.png',
after='./img/gachapon-after.png',
)
return switch.switch()
def gachapon_common() -> bool:
print("---- 執行點選轉蛋頁面的普通轉蛋按鈕")
switch = Switcher(
name='普通轉蛋按鈕',
before='./img/gachapon-common-before.png',
after='./img/gachapon-common-after.png',
)
return switch.switch()
def adventure() -> bool:
print("---- 執行前往冒險")
switch = Switcher(
name='冒險按鈕',
before='./img/adventure-before.png',
after='./img/adventure-after.png',
)
return switch.switch()
def ok(timeout=3):
ok_buttion = Button('./img/OK.png')
pts = ok_buttion.wait_until(timeout)
if not pts.is_valid():
print("---- 找不到OK按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊OK按鈕")
return True
def action_click_skip_button() -> bool:
print("---- 執行點擊跳過按鈕")
skip_pattern = Button('./img/skip-play.png')
pts = skip_pattern.wait_until(3)
if pts.is_valid():
x, y = pts.get_center_pt()
mouse_move(x, y)
return True
return False
def action_skip_scene() -> bool:
print("---- 執行跳過劇情")
menu = Button('./img/menu.png')
pts = menu.wait_until(3)
if pts.is_valid():
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊清單")
skip_scene = Button('./img/skip-scene.png')
pts = skip_scene.wait_until(3)
if pts.is_valid():
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊跳過")
dialog_skip = Button('./img/dialog-skip.png')
pts = dialog_skip.wait_until(3)
if pts.is_valid():
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊對話跳過")
return True
def action_free_gachapon(after_wait_sec=3) -> bool:
print("---- 執行免費轉蛋")
if not gachapon():
return False
if not gachapon_common():
return False
gacha_10_free_buttion = Button('./img/gachapon-10-free.png')
pts = gacha_10_free_buttion.wait_until(5)
if not pts.is_valid():
print("---- 找不到或是已使用過免費轉蛋")
return False
x, y = pts.get_center_pt()
mouse_move(x, y, click_before_sec=2)
print("---- 已點擊免費轉蛋按鈕")
if not ok(timeout=5):
return False
time.sleep(after_wait_sec)
return True
def action_explorer(after_wait_sec=3) -> bool:
print('---- 執行探索')
print('---- 先回到主頁')
if not home():
return False
if not adventure():
return False
adventure_explorer_button = Button('./img/adventure-explorer.png')
pts = adventure_explorer_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到探索按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊探索按鈕")
adventure_explorer_exp_button = Button('./img/adventure-explorer-exp.png')
pts = adventure_explorer_exp_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到經驗值冒險按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊經驗值冒險按鈕")
adventure_explorer_exp_lv_button = Button('./img/adventure-explorer-exp-lv.png')
pts = adventure_explorer_exp_lv_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到經驗值冒險任務按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊經驗值冒險任務按鈕")
adventure_explorer_exp_lv_button = Button('./img/adventure-explorer-exp-lv.png')
pts = adventure_explorer_exp_lv_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到經驗值冒險任務按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊經驗值冒險任務按鈕")
adventure_explorer_lv_use_2_button = Button('./img/adventure-explorer-lv-use-2.png')
pts = adventure_explorer_lv_use_2_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到使用2張券按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊使用2張券按鈕")
if not ok(timeout=5):
return False
print('---- 等待動畫結束')
time.sleep(5)
adventure_explorer_exp_go_mana_button = Button('./img/adventure-explorer-exp-go-mana.png')
pts = adventure_explorer_exp_go_mana_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到前往瑪娜冒險按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊前往瑪娜冒險按鈕")
adventure_explorer_mana_lv_button = Button('./img/adventure-explorer-mana-lv.png')
pts = adventure_explorer_mana_lv_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到瑪娜冒險任務按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊瑪娜冒險任務按鈕")
adventure_explorer_lv_use_2_button = Button('./img/adventure-explorer-lv-use-2.png')
pts = adventure_explorer_lv_use_2_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到使用2張券按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊使用2張券按鈕")
back_explorer_button = Button('./img/back-explorer.png')
pts = back_explorer_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到返回探索TOP")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊返回探索TOP按鈕")
time.sleep(after_wait_sec)
return True
def action_dungeon(after_wait_sec=3) -> bool:
print('---- 執行地下城')
print('---- 先回到主頁')
if not home():
return False
print('---- 前往冒險')
if not adventure():
return False
adventure_dungeon_button = Button('./img/adventure-dungeon.png')
pts = adventure_dungeon_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到地下城按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y, click_after_sec=2)
print("---- 已點擊地下城按鈕")
adventure_dungeon_extreme_button = Button('./img/adventure-dungeon-extreme-v.png') # 這邊先只抓Extreme V的地城任務
pts = adventure_dungeon_extreme_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到地下城任務按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y, click_after_sec=2)
print("---- 已點擊地下城任務按鈕")
adventure_dungeon_skip_button = Button('./img/adventure-dungeon-skip.png')
pts = adventure_dungeon_skip_button.wait_until(3)
if not pts.is_valid():
print("---- 找不到跳過地下城任務按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊跳過地下城任務按鈕")
adventure_dungeon_ok_button = Button('./img/adventure-dungeon-ok.png')
pts = adventure_dungeon_ok_button.wait_until(20)
if not pts.is_valid():
print("---- 找不到地下城任務OK按鈕")
return False
x, y = pts.get_center_pt()
mouse_move(x, y)
print("---- 已點擊地下城任務OK按鈕")
time.sleep(after_wait_sec)
return True
if __name__ == '__main__':
#debug()
#home()
#action_skip_scene()
# daily routine
action_free_gachapon()
action_explorer()
action_dungeon()