-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto-maple-auction_v0.01.py
350 lines (285 loc) · 11.7 KB
/
auto-maple-auction_v0.01.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
import pyautogui
import time
from cnocr import CnOcr
from PIL import Image, ImageGrab
import threading
import openpyxl
import datetime
import os
start_time = time.time()
game_x, game_y, game_w, game_h = pyautogui.locateOnScreen('maple-icon.png')
pyautogui.moveTo(game_x + game_w//2, game_y + game_h//2, duration=0.2)
pyautogui.click()
time.sleep(1)
filename = 'maple-auto-auction.xlsx'
try:
consume_pos = pyautogui.locateOnScreen('consume.png')
goto_pos = pyautogui.center(consume_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
except:
print("No consume button")
try:
consume_item_all_pos = pyautogui.locateOnScreen('consume-item-all.png')
goto_pos = pyautogui.center(consume_item_all_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
monster_pos = pyautogui.locateOnScreen('monster.png')
goto_pos = pyautogui.center(monster_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
except:
print("No consume item all button")
try:
search_all_pos = pyautogui.locateOnScreen('search-all.png')
goto_pos = pyautogui.center(search_all_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
monster_pos = pyautogui.locateOnScreen('search-all-monster.png')
goto_pos = pyautogui.center(monster_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
except:
print("No search all button")
start_search_pos = pyautogui.locateOnScreen('start-search.png')
goto_pos = pyautogui.center(start_search_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
confirm_pos = pyautogui.locateOnScreen('confirm.png')
goto_pos = pyautogui.center(confirm_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
confirm_pos = pyautogui.locateOnScreen('confirm-2.png')
goto_pos = pyautogui.center(confirm_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
while(True):
try:
warn_pos = pyautogui.locateOnScreen('warning.png')
goto_pos = pyautogui.center(warn_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
except:
pass
next_pos = pyautogui.locateOnScreen('next.png')
goto_pos = pyautogui.center(next_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
try:
confirm_pos = pyautogui.locateOnScreen('confirm-3-1.png')
goto_pos = pyautogui.center(confirm_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
except:
pass
try:
confirm_pos = pyautogui.locateOnScreen('confirm-3-2.png')
goto_pos = pyautogui.center(confirm_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
except:
pass
# if find the end-next button, then break
try:
next_pos = pyautogui.locateOnScreen('end-next.png')
if next_pos:
break
except:
pass
time.sleep(5)
price_sort_pos = pyautogui.locateOnScreen('price-sort.png')
goto_pos = pyautogui.center(price_sort_pos)
pyautogui.moveTo(goto_pos, duration=0.2)
pyautogui.click()
time.sleep(0.5)
pyautogui.click()
ocr_naive_det = CnOcr(det_model_name='naive_det')
ocr_en_pp = CnOcr(det_model_name='en_PP-OCRv3_det', rec_model_name='en_PP-OCRv3')
card_types_1 = {
'legend.png': '傳說',
'legend-5.png': '傳說-5',
'legend-2.png': '傳說-2',
'legend-4.png': '傳說-4',
'legend-3.png': '傳說-3'
}
card_types_2 = {
'special.png': '特殊',
'rare.png': '稀有',
'epic.png': '罕見'
}
card_images_1 = set(card_types_1.keys())
card_images_2 = set(card_types_2.keys())
def get_card_type_1(card_img, card_types, flag):
for image_name in card_images_1:
if pyautogui.locateOnScreen(image_name) is not None:
card_type = card_types.get(image_name)
# print(f"Card type: {card_type}")
card_types_found.append(card_type)
flag = True
return
# card_types_found.append('None')
def get_card_type_2(card_img, card_types, flag):
for image_name in card_images_2:
if pyautogui.locateOnScreen(image_name) is not None:
card_type = card_types.get(image_name)
# print(f"Card type: {card_type}")
card_types_found.append(card_type)
flag = True
return
'''
workbook = openpyxl.Workbook()
worksheet = workbook.active
'''
# page = 1
end_of_flag = False
if __name__ == "__main__":
try:
# Open xlsx file, the filename is maple-auto-auction.xlsx
workbook = openpyxl.load_workbook('maple-auto-auction.xlsx')
except:
workbook = openpyxl.Workbook()
worksheet = workbook.active
search_result_pos = pyautogui.locateOnScreen('search-result.png')
goto_pos = pyautogui.center(search_result_pos)
game_x, game_y = goto_pos
screenshot = ImageGrab.grab(bbox=(game_x+336, game_y-12, game_x+336+28, game_y-12+25))
img = Image.frombytes('RGB', screenshot.size, screenshot.tobytes())
page_result = ocr_en_pp.ocr(img)
total_page = page_result[0]['text']
search_result_pos = pyautogui.locateOnScreen('search-result.png')
goto_pos = pyautogui.center(search_result_pos)
game_x, game_y = goto_pos
screenshot = ImageGrab.grab(bbox=(game_x+280, game_y-12, game_x+300+28, game_y-12+25))
img = Image.frombytes('RGB', screenshot.size, screenshot.tobytes())
page_result = ocr_en_pp.ocr(img)
img.save('test.png')
try:
page = int(page_result[0]['text'])
except:
page = 1
while(True):
# for i in range(0, 3):
count = 0
search_result_pos = pyautogui.locateOnScreen('search-result.png')
goto_pos = pyautogui.center(search_result_pos)
# goto_pos x座標 - 10
goto_pos = (goto_pos[0], goto_pos[1])
pyautogui.moveTo(goto_pos, duration=0.2)
# Card List
game_x, game_y = goto_pos
screenshot = ImageGrab.grab(bbox=(game_x+35, game_y+45, game_x+35+240, game_y+45+493))
img = Image.frombytes('RGB', screenshot.size, screenshot.tobytes())
card_result = ocr_naive_det.ocr(img)
# Write the result to the excel
for i in range(0, len(card_result)):
worksheet.cell(row=i+1+(page-1)*9, column=1).value = card_result[i]['text']
# worksheet.cell(row=i+1+(page-1)*9, column=4).value = card_result[i]['score']
# Price List
game_x, game_y = goto_pos
screenshot = ImageGrab.grab(bbox=(game_x+255, game_y+45, game_x+255+170, game_y+45+493))
img = Image.frombytes('RGB', screenshot.size, screenshot.tobytes())
price_result = ocr_en_pp.ocr(img)
for i in range(0, 18, 2):
# 取得文字
# try break the loop if IndexError: list index out of range
try:
price_string = price_result[i]['text']
price_string = price_string.replace('Q', '0') # replace 'Q' with '0'
price_string = price_string.replace(',', '') # remove commas
price_string = price_string.replace('.', '') # remove dots
price_string = price_string.replace(' ', '') # remove spaces
value = int(price_string)
worksheet.cell(row=i//2+1+(page-1)*9, column=2).value = value
except:
pass
pyautogui.moveRel(0, 68, duration=0.001)
card_result = [{'text': 'legend.png'}]
card_types_found = []
flag = False
t1 = threading.Thread(target=get_card_type_1, args=(card_result[0]['text'], card_types_1, flag))
t2 = threading.Thread(target=get_card_type_2, args=(card_result[0]['text'], card_types_2, flag))
t1.start()
t2.start()
t1.join()
if flag == False:
t2.join()
try:
worksheet.cell(row=1+(page-1)*9, column=3).value = card_types_found[-1]
except:
worksheet.cell(row=1+(page-1)*9, column=3).value = 'None'
locate_ability_pos = pyautogui.locateOnScreen('locate-ability.png')
goto_pos = pyautogui.center(locate_ability_pos)
game_x, game_y = goto_pos
screenshot = ImageGrab.grab(bbox=(game_x, game_y+48, game_x+230, game_y+48+96))
img = Image.frombytes('RGB', screenshot.size, screenshot.tobytes())
card_ability = ocr_naive_det.ocr(img)
for i in range(1, len(card_ability)):
worksheet.cell(row=1+(page-1)*9, column=i+5).value = card_ability[i]['text']
'''
for i in range(1, len(card_ability)):
worksheet.cell(row=1+(page-1)*9, column=i+11).value = card_ability[i]['score']
'''
# print(len(price_result))
for i in range(0, (len(price_result)//2)-1):
flag = False
pyautogui.moveRel(0, 55, duration=0.001)
end_block_pos = pyautogui.locateOnScreen('end-block.png')
if end_block_pos is None:
count += 1
card_type = None
t1 = threading.Thread(target=get_card_type_1, args=(card_result[0]['text'], card_types_1, flag))
t2 = threading.Thread(target=get_card_type_2, args=(card_result[0]['text'], card_types_2, flag))
t1.start()
t2.start()
t1.join()
if flag == False:
t2.join()
worksheet.cell(row=i+2+(page-1)*9, column=3).value = card_types_found[-1]
locate_ability_pos = pyautogui.locateOnScreen('locate-ability.png')
goto_pos = pyautogui.center(locate_ability_pos)
game_x, game_y = goto_pos
screenshot = ImageGrab.grab(bbox=(game_x, game_y+48, game_x+230, game_y+48+96))
img = Image.frombytes('RGB', screenshot.size, screenshot.tobytes())
card_ability = ocr_naive_det.ocr(img)
for j in range(0, len(card_ability)):
worksheet.cell(row=2+i+(page-1)*9, column=j+5).value = card_ability[j]['text']
'''
for j in range(0, len(card_ability)):
worksheet.cell(row=2+i+(page-1)*9, column=j+11).value = card_ability[j]['score']
'''
next_page_pos = pyautogui.locateOnScreen('next-page.png')
goto_pos = pyautogui.center(next_page_pos)
print(f"Page {page} done")
pyautogui.moveTo(goto_pos, duration=0.001)
pyautogui.click()
if int(page) == int(total_page):
break
page += 1
card_types_found = []
if end_of_flag == True:
break
def seconds_to_hms(seconds):
"""將秒數轉換為時:分:秒的格式"""
hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
workbook.save(filename)
end_time = time.time()
elapsed_time = end_time - start_time
hms_time = seconds_to_hms(elapsed_time)
print(f"Elapsed time: {hms_time}")
'''
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
filename = f"{timestamp}-auction.xlsx"
workbook.save(filename)
def seconds_to_hms(seconds):
"""將秒數轉換為時:分:秒的格式"""
hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
end_time = time.time()
elapsed_time = end_time - start_time
hms_time = seconds_to_hms(elapsed_time)
print(f"Elapsed time: {hms_time}")
print("Done")
'''