· 6 years ago · Mar 20, 2020, 01:43 PM
1from time import time
2from datetime import datetime
3from playsound import playsound
4from PIL import Image, ImageChops, ImageGrab
5from pushbullet import PushBullet
6import configparser
7import win32api
8import win32con
9import win32gui
10import operator
11
12
13def Avg(lst):
14 return sum(lst) / len(lst)
15
16
17def enum_cb(hwnd, results):
18 winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
19
20
21def compare_list(l1, l2, relate=operator.le):
22 if not l1:
23 return False
24 l3 = []
25 for i, val in enumerate(l1, start=0):
26 l3.append(relate(val, l2[i]))
27 return all(l3)
28
29
30def color_average(image, x1, y1, x2, y2, compare_images=True, org_image=0):
31 average = []
32 r, g, b = [], [], []
33 if compare_images:
34 data = ImageChops.difference(org_image, image).getdata()
35 else:
36 data = image.getdata()
37 for y in range(y1, y2 + 1):
38 for x in range(x1, x2 + 1):
39 r.append(data[y * 2560 + x][0])
40 g.append(data[y * 2560 + x][1])
41 b.append(data[y * 2560 + x][2])
42 average.append(Avg(r))
43 average.append(Avg(g))
44 average.append(Avg(b))
45 return average
46
47
48def getScreenShot(window_id):
49 win32gui.ShowWindow(window_id, win32con.SW_MAXIMIZE)
50 try:
51 bbox = win32gui.GetWindowRect(window_id)
52 except pywintypes.error:
53 return 0
54 image = ImageGrab.grab(bbox)
55 if not image:
56 return 0
57 i_width, i_height = image.size
58 if i_width != 2650 and i_height != 1440:
59 image = image.resize([2560, 1440])
60 return image
61
62
63def click(x, y):
64 win32api.SetCursorPos((x, y))
65 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
66 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
67
68
69def push_note(message):
70 push = str(datetime.now().strftime("%H:%M:%S")) + " " + str(message)
71 device.push_note("CSGO AUTO ACCEPT", push)
72
73
74def write(message, pushing=False):
75 print(datetime.now().strftime("%H:%M:%S") + " " + message)
76 if pushing:
77 device.push_note("CSGO AUTO ACCEPT", str(datetime.now().strftime("%H:%M:%S")) + " " + str(message))
78
79
80screen_width, screen_height = win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)
81toplist, winlist = [], []
82hwnd = 0
83
84test_for_live_game, test_for_success, debugging, push_active = False, False, False, False
85accept_avg = []
86
87
88config = configparser.ConfigParser()
89config.read("config.ini")
90
91PushBulletDeviceName = config.get('Pushbullet', 'DeviceName')
92PushBulletAPIKey = config.get('Pushbullet', 'API Key')
93device = PushBullet(PushBulletAPIKey).get_device(PushBulletDeviceName)
94
95img_accept = Image.open("images/accept.jpg")
96img_not_searching = Image.open("images/not_searching.jpg")
97start_time = time()
98write("Ready")
99
100while True:
101 if win32api.GetAsyncKeyState(0x78) & 1: # F9 (ACTIVATE / DEACTIVATE SCRIPT)
102 test_for_live_game = not test_for_live_game
103 write("TESTING: %s" % test_for_live_game)
104 accept_avg = []
105 if test_for_live_game:
106 playsound('sounds/activated.mp3')
107 else:
108 playsound('sounds/deactivated.mp3')
109
110 if win32api.GetAsyncKeyState(0x77) & 1: # F8 (ACTIVATE / DEACTIVATE PUSH NOTIFICATION)
111 push_active = not push_active
112 write("Pushing: %s" % push_active)
113
114 if win32api.GetAsyncKeyState(0x76) & 1: # F7 (DEBUGGING)
115 debugging = not debugging
116 print("debugging: ", debugging)
117
118 if win32api.GetAsyncKeyState(0x24) & 1: # POS1/HOME Key
119 write("Exiting Script")
120 break
121
122 winlist = []
123 win32gui.EnumWindows(enum_cb, toplist)
124 csgo = [(hwnd, title) for hwnd, title in winlist if 'counter-strike: global offensive' in title.lower()]
125 if not csgo:
126 continue
127 hwnd = csgo[0][0]
128
129 # TESTING HERE
130 if win32api.GetAsyncKeyState(0x75) & 1: # F6, TEST CODE
131 write("Executing TestCode")
132 test_for_success = True
133 start_time = time()
134
135 # TESTING ENDS HERE
136
137 if test_for_live_game:
138 if time() - start_time < 4:
139 continue
140 start_time = time()
141 img = getScreenShot(hwnd)
142 if not img:
143 continue
144 accept_avg = color_average(img, 1265, 760, 1295, 785, org_image=img_accept)
145
146 if debugging:
147 print("Avg: ", accept_avg)
148 print(time() - start_time)
149
150 if compare_list(accept_avg, [15, 30, 15]):
151 write("Trying to Accept", push_active)
152
153 test_for_success = True
154 test_for_live_game = False
155 accept_avg = []
156
157 for _ in range(5):
158 click(int(screen_width / 2), int(screen_height / 1.78))
159 # sleep(0.5)
160
161 write("Trying to catch a loading map")
162 playsound('sounds/accept_found.mp3')
163 start_time = time()
164
165 if test_for_success:
166 if time() - start_time < 40:
167 img = getScreenShot(hwnd)
168 success_avg = color_average(img, 467, 1409, 1300, 1417, compare_images=False)
169
170 searching_avg = color_average(img, 2435, 55, 2550, 100, org_image=img_accept)
171 not_searching_avg = color_average(img, 2435, 55, 2550, 100, org_image=img_not_searching)
172 solo_not_searching_avg = color_average(img, 2435, 115, 2555, 135, org_image=img_not_searching)
173 no_success_truth = [compare_list(searching_avg, [0.7, 12, 10]), compare_list(not_searching_avg, [3, 10, 10]), compare_list(solo_not_searching_avg, [25, 25, 25])]
174
175 if compare_list(success_avg, [17, 100, 150], relate=operator.ge):
176 write("Game should have started", push_active)
177 print("Took: ", time() - start_time)
178 test_for_success = False
179 playsound('sounds/done.mp3')
180
181 if any(no_success_truth):
182 write("Game doesnt seem to have started. Continuing to search for accept Button", push_active)
183 # write("Searching again: "+str(no_success_truth[0]))
184 print("Took: ", time() - start_time)
185 playsound('sounds/back_to_testing.mp3')
186 test_for_success = False
187 test_for_live_game = True
188 else:
189 write("Unknown Error")
190 test_for_success = False
191 print(success_avg)
192 print(searching_avg)
193 print(not_searching_avg)
194 print(solo_not_searching_avg)
195 img.save("Unknown Error.png")
196 playsound('sounds/fail.mp3')