· 6 years ago · Mar 22, 2020, 09:30 PM
1import configparser
2import operator
3import os
4import webbrowser
5from datetime import datetime, timedelta
6from time import time
7
8import pushbullet
9import pyperclip
10import requests
11import win32api
12import win32con
13import win32gui
14from PIL import ImageGrab
15from playsound import playsound
16
17
18def Avg(lst):
19 return sum(lst) / len(lst)
20
21
22def enum_cb(hwnd, results):
23 winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
24
25
26def write(message, add_time=True, push=0, push_now=False):
27 if message:
28 if add_time:
29 m = datetime.now().strftime("%H:%M:%S") + ": " + str(message)
30 else:
31 m = message
32 print(m)
33
34 if push >= 2:
35 global note
36 if message:
37 note = note + m + "\n"
38 if push_now:
39 device.push_note("CSGO AUTO ACCEPT", note)
40 note = ""
41
42
43def click(x, y):
44 win32api.SetCursorPos((x, y))
45 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
46 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
47
48
49def relate_list(l_org, l1, l2=[], relate=operator.le):
50 if not l_org:
51 return False
52 truth_list, l3 = [], []
53 for i, val in enumerate(l1, start=0):
54 l3.append(relate(l_org[i], val))
55 truth_list.append(all(l3))
56 l3 = []
57 if l2:
58 for i, val in enumerate(l2, start=3):
59 l3.append(relate(l_org[i], val))
60 truth_list.append(all(l3))
61 return any(truth_list)
62
63
64def color_average(image, compare_list):
65 average = []
66 r, g, b = [], [], []
67 data = image.getdata()
68 for i in data:
69 r.append(i[0])
70 g.append(i[1])
71 b.append(i[2])
72
73 rgb = [Avg(r), Avg(g), Avg(b)] * int(len(compare_list) / 3)
74
75 for i, val in enumerate(compare_list, start=0):
76 average.append(val - rgb[i])
77 average = list(map(abs, average))
78
79 return average
80
81
82def getScreenShot(window_id, area=(0, 0, 0, 0)):
83 area = list(area)
84 scaled_area = [screen_width / 2560, screen_height / 1440]
85 scaled_area = 2 * scaled_area
86 for i, _ in enumerate(area[-2:], start=len(area) - 2):
87 area[i] += 1
88 for i, val in enumerate(area, start=0):
89 scaled_area[i] = scaled_area[i] * val
90 scaled_area = list(map(int, scaled_area))
91 win32gui.ShowWindow(window_id, win32con.SW_MAXIMIZE)
92 image = ImageGrab.grab(scaled_area)
93 return image
94
95
96def getOldSharecodes(num=-1):
97 try:
98 last_game = open("last_game.txt", "r")
99 games = last_game.readlines()
100 last_game.close()
101 except FileNotFoundError:
102 last_game = open("last_game.txt", "w")
103 last_game.write(config.get("csgostats.gg", "Match Token") + "\n")
104 games = [config.get("csgostats.gg", "Match Token")]
105 last_game.close()
106 last_game = open("last_game.txt", "w")
107 games = games[-200:]
108 for i, val in enumerate(games):
109 games[i] = "CSGO" + val.strip("\n").split("CSGO")[1]
110 last_game.write(games[i] + "\n")
111 last_game.close()
112 return games[num:]
113
114
115def getNewCSGOMatches(game_id):
116 sharecodes = []
117 next_code = game_id
118 last_game = open("last_game.txt", "a")
119 while next_code != "n/a":
120 steam_url = "https://api.steampowered.com/ICSGOPlayers_730/GetNextMatchSharingCode/v1?key=" + steam_api_key + "&steamid=" + steam_id + "&steamidkey=" + game_code + "&knowncode=" + game_id
121 try:
122 next_code = (requests.get(steam_url).json()["result"]["nextcode"])
123 except KeyError:
124 write("WRONG GAME_CODE, GAME_ID or STEAM_ID ")
125 return 0
126
127 if next_code:
128 if next_code != "n/a":
129 sharecodes.append(next_code)
130 game_id = next_code
131 last_game.write(next_code + "\n")
132 else:
133 write("Found %s Game[s]" % len(sharecodes))
134 return sharecodes
135
136
137def UpdateCSGOstats(sharecodes):
138 if any(sharecodes):
139 for i in sharecodes:
140 write('Added Sharecode: %s' % i, push=push_urgency)
141 response = requests.post("https://csgostats.gg/match/upload/ajax", data={'sharecode': i, 'index': '1'})
142 info = response.json()["data"]["msg"].split("<")[0].replace('-', '').rstrip(" ")
143 game_url = response.json()["data"]["url"]
144 write("URL: %s" % game_url, add_time=False, push=push_urgency)
145 write("Game %s." % info, add_time=False, push=push_urgency)
146 # print(response.json())
147 write(None, add_time=False, push=push_urgency, push_now=True)
148
149
150def getHotKeys():
151 getKeys = []
152 getKeys.append(int(config.get("HotKeys", "Activate Script"), 16))
153 getKeys.append(int(config.get("HotKeys", "Activate Push Notification"), 16))
154 getKeys.append(int(config.get("HotKeys", "Upload newest Match"), 16))
155 getKeys.append(int(config.get("HotKeys", "Get Info on newest Match"), 16))
156 getKeys.append(int(config.get("HotKeys", "Get Info on multiple Matches"), 16))
157 getKeys.append(int(config.get("HotKeys", "Live Tab Key"), 16))
158 getKeys.append(int(config.get("HotKeys", "End Script"), 16))
159 return getKeys
160
161
162config = configparser.ConfigParser()
163config.read("config.ini")
164
165steam_api_key = config.get("csgostats.gg", "API Key")
166steam_id = config.get("csgostats.gg", "Steam ID")
167game_code = config.get("csgostats.gg", "Game Code")
168
169keys = getHotKeys()
170
171device = 0
172
173screen_width, screen_height = win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)
174toplist, winlist = [], []
175hwnd = 0
176
177test_for_live_game, test_for_success, push_urgency, testing = False, False, False, False
178accept_avg = []
179
180note = ""
181
182start_time = time()
183write("Ready")
184
185while True:
186 if win32api.GetAsyncKeyState(keys[0]) & 1: # F9 (ACTIVATE / DEACTIVATE SCRIPT)
187 test_for_live_game = not test_for_live_game
188 write("TESTING: %s" % test_for_live_game)
189 if test_for_live_game:
190 playsound('sounds/activated.mp3')
191 time_searching = time()
192 else:
193 playsound('sounds/deactivated.mp3')
194
195 if win32api.GetAsyncKeyState(keys[1]) & 1: # F8 (ACTIVATE / DEACTIVATE PUSH NOTIFICATION)
196 if not device:
197 PushBulletDeviceName = config.get('Pushbullet', 'DeviceName')
198 PushBulletAPIKey = config.get('Pushbullet', 'API Key')
199 try:
200 device = pushbullet.PushBullet(PushBulletAPIKey).get_device(PushBulletDeviceName)
201 except pushbullet.errors.PushbulletError or pushbullet.errors.InvalidKeyError:
202 write("Pushbullet is wrongly configured.\nWrong API Key or DeviceName in config.ini")
203 if device:
204 push_urgency += 1
205 if push_urgency > 2:
206 push_urgency = 0
207 push_info = ["not active", "little information", "all information"]
208 write("Pushing: %s" % push_info[push_urgency])
209
210 if win32api.GetAsyncKeyState(keys[2]) & 1: # F7 Key (UPLOAD NEWEST MATCH)
211 newest_match = getNewCSGOMatches(getOldSharecodes()[0])
212 if newest_match:
213 pyperclip.copy(newest_match[0])
214 UpdateCSGOstats(newest_match)
215
216 if win32api.GetAsyncKeyState(keys[3]) & 1: # F6 Key (GET INFO ON NEWEST MATCH)
217 UpdateCSGOstats(getOldSharecodes())
218
219 if win32api.GetAsyncKeyState(keys[4]) & 1: # F5 Key (GET INFO ON LAST X MATCHES)
220 last_x_matches = config.getint("csgostats.gg", "Get Info from")
221 UpdateCSGOstats(getOldSharecodes(num=last_x_matches * -1))
222
223 if win32api.GetAsyncKeyState(keys[5]) & 1: # F13 Key (OPEN WEB BROWSER ON LIVE GAME TAB)
224 webbrowser.open_new_tab("https://csgostats.gg/player/" + steam_id + "#/live")
225
226 if win32api.GetAsyncKeyState(keys[6]) & 1: # POS1/HOME Key
227 write("Exiting Script")
228 break
229
230 winlist = []
231 win32gui.EnumWindows(enum_cb, toplist)
232 csgo = [(hwnd, title) for hwnd, title in winlist if 'counter-strike: global offensive' in title.lower()]
233 if not csgo:
234 continue
235 hwnd = csgo[0][0]
236
237 # TESTING HERE
238 if win32api.GetAsyncKeyState(0x73) & 1: # F5, TEST CODE
239 write("Executing TestCode")
240 testing = not testing
241
242 if testing:
243 # start_time = time()
244 img = getScreenShot(hwnd, (2435, 65, 2555, 100))
245 not_searching_avg = color_average(img, [6, 10, 10])
246 searching_avg = color_average(img, [6, 163, 97, 4, 63, 35])
247 not_searching = relate_list(not_searching_avg, [2, 5, 5])
248 searching = relate_list(searching_avg, [2.7, 55, 35], l2=[1, 50, 35])
249 img = getScreenShot(hwnd, (467, 1409, 1300, 1417))
250 success_avg = color_average(img, [21, 123, 169])
251 success = relate_list(success_avg, [1, 8, 7])
252 # print("Took: %s " % str(timedelta(milliseconds=int(time()*1000 - start_time*1000))))
253 # TESTING ENDS HERE
254
255 if test_for_live_game:
256 if time() - start_time < 4:
257 continue
258 start_time = time()
259 img = getScreenShot(hwnd, (1265, 760, 1295, 785))
260 if not img:
261 continue
262 accept_avg = color_average(img, [76, 176, 80, 90, 203, 95])
263
264 if relate_list(accept_avg, [1, 2, 1], l2=[1, 1, 2]):
265 write("Trying to Accept", push=push_urgency)
266
267 test_for_success = True
268 test_for_live_game = False
269 accept_avg = []
270
271 for _ in range(5):
272 click(int(screen_width / 2), int(screen_height / 1.78))
273 # sleep(0.5)
274 # pass
275
276 write("Trying to catch a loading map")
277 playsound('sounds/accept_found.mp3')
278 start_time = time()
279
280 if test_for_success:
281 if time() - start_time < 40:
282 img = getScreenShot(hwnd, (2435, 65, 2555, 100))
283 not_searching_avg = color_average(img, [6, 10, 10])
284 searching_avg = color_average(img, [6, 163, 97, 4, 63, 35])
285
286 not_searching = relate_list(not_searching_avg, [2, 5, 5])
287 searching = relate_list(searching_avg, [2.7, 55, 35], l2=[1, 50, 35])
288
289 img = getScreenShot(hwnd, (467, 1409, 1300, 1417))
290 success_avg = color_average(img, [21, 123, 169])
291 success = relate_list(success_avg, [1, 8, 7])
292
293 if success:
294 write("Took %s since pressing accept." % str(timedelta(seconds=int(time() - start_time))), add_time=False, push=push_urgency)
295 write("Took %s since trying to find a game." % str(timedelta(seconds=int(time() - time_searching))), add_time=False, push=push_urgency)
296 write("Game should have started", push=push_urgency + 1, push_now=True)
297 test_for_success = False
298 playsound('sounds/done_testing.mp3')
299
300 if any([searching, not_searching]):
301 write("Took: %s " % str(timedelta(seconds=int(time() - start_time))), add_time=False, push=push_urgency)
302 write("Game doesnt seem to have started. Continuing to search for accept Button!", push=push_urgency, push_now=True)
303 playsound('sounds/back_to_testing.mp3')
304 test_for_success = False
305 test_for_live_game = True
306
307 else:
308 write("Unknown Error")
309 test_for_success = False
310 print(success_avg)
311 print(searching_avg)
312 print(not_searching_avg)
313 playsound('sounds/fail.mp3')
314 img.save(os.path.expanduser("~") + '\\Unknown Error.png')