1+
2+ import time
3+ import win32gui
4+ import mouse , math
5+ from utils import captureWindow
6+
7+ class GameInfo :
8+ def __init__ (self , hwnd , force_rect = None ):
9+ self .hwnd = hwnd
10+ self .w , self .h = win32gui .GetClientRect (self .hwnd )[2 :]
11+ self .left , self .top = win32gui .ClientToScreen (self .hwnd , (0 ,0 ))
12+
13+ def calculateCoordinates (self ):
14+ self .scale_ratio = min (self .w / 2560 , self .h / 1440 )
15+
16+ self .art_width = 164.39 * self .scale_ratio
17+ self .art_height = 203.21 * self .scale_ratio
18+ self .art_expand = 6 * self .scale_ratio
19+
20+ self .art_gap_x = 30.89 * self .scale_ratio
21+ self .art_gap_y = 30.35 * self .scale_ratio
22+
23+ self .art_info_width = 656 * self .scale_ratio
24+ self .art_info_height = 1119 * self .scale_ratio
25+
26+ self .left_margin = (199.33 if self .w < 2 * self .h else 295.33 )* self .scale_ratio
27+ self .right_margin = (871.33 if self .w < 2 * self .h else 967.33 )* self .scale_ratio
28+ self .info_margin = 0 if self .w < 2 * self .h else 96 * self .scale_ratio
29+
30+ self .art_cols = int (math .floor ((self .w - self .left_margin - self .right_margin + self .art_gap_x )/ (self .art_width + self .art_gap_x )))
31+
32+ self .art_shift = ((self .w - self .left_margin - self .right_margin + self .art_gap_x ) - self .art_cols * (self .art_width + self .art_gap_x ))/ 2
33+
34+ self .first_art_x = self .left_margin + self .art_shift
35+ self .first_art_y = 161 * self .scale_ratio
36+
37+ self .art_info_top = 160 * self .scale_ratio
38+ self .art_info_left = self .w - 837 * self .scale_ratio - self .info_margin
39+ # scroll_keypt_x = left_margin + art_shift + 158*scale_ratio
40+ # scroll_keypt_y = 1270*scale_ratio+h-1440*scale_ratio
41+ self .scroll_fin_keypt_x = self .left_margin + self .art_shift + 10 * self .scale_ratio
42+ self .scroll_fin_keypt_y = 335 * self .scale_ratio
43+
44+ self .art_rows = int (round ((1270 * self .scale_ratio + self .h - 1440 * self .scale_ratio - self .first_art_y + self .art_gap_y )/ (self .art_height + self .art_gap_y )))
45+ self .incomplete_lastrow = (1270 * self .scale_ratio + self .h - 1440 * self .scale_ratio - self .first_art_y + self .art_gap_y )/ (self .art_height + self .art_gap_y )- self .art_rows < 0.7
46+
47+
48+
49+ class ArtScannerLogic :
50+ def __init__ (self , game_info ):
51+ self .game_info = game_info
52+ self .stopped = False
53+
54+ def interrupt (self ):
55+ self .stopped = True
56+
57+
58+ def waitSwitched (self , art_center_x , art_center_y , min_wait = 0.1 , max_wait = 3 ):
59+ total_wait = 0
60+ while True :
61+ pix = captureWindow (self .game_info .hwnd , (
62+ art_center_x - self .game_info .art_width / 2 - self .game_info .art_expand ,
63+ art_center_y ,
64+ art_center_x - self .game_info .art_width / 2 - self .game_info .art_expand + 1.5 ,
65+ art_center_y + 1.5 ))
66+ if sum (pix .getpixel ((0 ,0 )))/ 3 > 200 :
67+ return True
68+ else :
69+ time .sleep (min_wait )
70+ total_wait += min_wait
71+ if total_wait > max_wait :
72+ return False
73+
74+ def getArtCenter (self , row , col ):
75+ art_center_x = self .game_info .first_art_x + (self .game_info .art_width + self .game_info .art_gap_x )* col + self .game_info .art_width / 2
76+ art_center_y = self .game_info .first_art_y + (self .game_info .art_height + self .game_info .art_gap_y )* row + self .game_info .art_height / 5
77+ return art_center_x , art_center_y
78+
79+ def scanRows (self , rows , callback ):
80+ '''
81+ callback: function to take in artifact image and do what ever you want
82+ '''
83+ rows = list (rows )
84+ if len (rows )< 1 :
85+ return True
86+ art_center_x , art_center_y = self .getArtCenter (rows [0 ], 0 )
87+ mouse .move (self .game_info .left + art_center_x , self .game_info .top + art_center_y )
88+ mouse .click ()
89+ for art_row in rows :
90+ for art_col in range (self .game_info .art_cols ):
91+ if self .stopped :
92+ return False
93+ if self .waitSwitched (art_center_x , art_center_y , min_wait = 0.1 , max_wait = 3 ):
94+ art_img = captureWindow (self .game_info .hwnd , (
95+ self .game_info .art_info_left ,
96+ self .game_info .art_info_top ,
97+ self .game_info .art_info_left + self .game_info .art_info_width ,
98+ self .game_info .art_info_top + self .game_info .art_info_height ))
99+ if art_col == self .game_info .art_cols - 1 :
100+ art_row += 1
101+ art_col = 0
102+ else :
103+ art_col += 1
104+ if art_row in rows :
105+ art_center_x , art_center_y = self .getArtCenter (art_row , art_col )
106+ mouse .move (self .game_info .left + art_center_x , self .game_info .top + art_center_y )
107+ mouse .click ()
108+ callback (art_img )
109+ else :
110+ return False
111+ return True
112+
113+ def alignFirstRow (self ):
114+ mouse .move (self .game_info .left + self .game_info .first_art_x , self .game_info .top + self .game_info .first_art_y )
115+ pix = captureWindow (self .game_info .hwnd , (
116+ self .game_info .scroll_fin_keypt_x ,
117+ self .game_info .scroll_fin_keypt_y ,
118+ self .game_info .scroll_fin_keypt_x + 1.5 ,
119+ self .game_info .scroll_fin_keypt_y + 1.5 ))
120+ if pix .getpixel ((0 ,0 ))[0 ]!= 233 or pix .getpixel ((0 ,0 ))[1 ]!= 229 or pix .getpixel ((0 ,0 ))[2 ]!= 220 :
121+ for _ in range (3 ):
122+ mouse .wheel (1 )
123+ time .sleep (0.1 )
124+ self .scrollToRow (0 )
125+
126+
127+ def scrollToRow (self , target_row , max_scrolls = 20 , extra_scroll = 0 ):
128+ in_between_row = False
129+ rows_scrolled = 0
130+ lines_scrolled = 0
131+ while True :
132+ pix = captureWindow (self .game_info .hwnd , (
133+ self .game_info .scroll_fin_keypt_x ,
134+ self .game_info .scroll_fin_keypt_y ,
135+ self .game_info .scroll_fin_keypt_x + 1.5 ,
136+ self .game_info .scroll_fin_keypt_y + 1.5 ))
137+ if pix .getpixel ((0 ,0 ))[0 ]!= 233 or pix .getpixel ((0 ,0 ))[1 ]!= 229 or pix .getpixel ((0 ,0 ))[2 ]!= 220 :
138+ # if in_between_row==False:
139+ # print('到行之间了')
140+ in_between_row = True
141+ elif in_between_row :
142+ in_between_row = False
143+ rows_scrolled += 1
144+ lines_scrolled = 0
145+ # print(f'已翻{rows_scrolled}行')
146+ if rows_scrolled >= target_row :
147+ for _ in range (extra_scroll ):
148+ mouse .wheel (- 1 )
149+ return rows_scrolled
150+ if lines_scrolled > max_scrolls :
151+ return rows_scrolled
152+ for _ in range (6 if lines_scrolled == 0 and target_row > 0 else 1 ):
153+ mouse .wheel (- 1 )
154+ lines_scrolled += 1
155+ # print('翻一下')
156+ time .sleep (0.05 )
0 commit comments