|
6 | 6 | from http.server import BaseHTTPRequestHandler, HTTPServer |
7 | 7 | import json |
8 | 8 |
|
| 9 | +want_exit = False |
| 10 | +def exit(): |
| 11 | + print("Exiting...") |
| 12 | + sys.exit() |
| 13 | +def state_exit(*args, **kwargs): |
| 14 | + global want_exit |
| 15 | + want_exit = True |
| 16 | + |
9 | 17 |
|
10 | 18 |
|
11 | 19 | phases = { |
12 | 20 | "freezetime": "freeze time", |
13 | 21 | "live": "playing", |
14 | | - "over": "round over" |
| 22 | + "over": "round over", |
| 23 | + "warmup": "warmup" |
15 | 24 | } |
16 | 25 | modes = { |
17 | 26 | "casual": "Casual", |
@@ -39,6 +48,9 @@ def __init__(self, *args, **kwargs): |
39 | 48 | HTTPServer.__init__(self, *args, **kwargs) |
40 | 49 |
|
41 | 50 | def service_actions(self): |
| 51 | + if want_exit: |
| 52 | + self.server_close() |
| 53 | + exit() |
42 | 54 | running = False |
43 | 55 | for pid in psutil.pids(): |
44 | 56 | try: |
@@ -90,14 +102,14 @@ def handle_json(self, data): |
90 | 102 | team_text = "Terrorist" |
91 | 103 | else: |
92 | 104 | team_text = "Spectator" # never seen unless I add an icon |
93 | | - team_text += " - " + str(state["health"]) + " HP, " + str(state["armor"]) + " Armor" |
| 105 | + # team_text += " - " + str(state["health"]) + " HP, " + str(state["armor"]) + " Armor" |
| 106 | + team_text += " - $" + str(state["money"]) |
94 | 107 |
|
95 | 108 | # player stats info |
96 | | - stats_text = str(match_stats["kills"]) + "|" |
97 | | - stats_text += str(match_stats["assists"]) + "|" |
98 | | - stats_text += str(match_stats["deaths"]) + " - " |
| 109 | + stats_text = str(match_stats["kills"]) + "K / " |
| 110 | + stats_text += str(match_stats["assists"]) + "A /" |
| 111 | + stats_text += str(match_stats["deaths"]) + "D / " |
99 | 112 | stats_text += str(match_stats["mvps"]) + " MVPs" |
100 | | - stats_text += " - $" + str(state["money"]) |
101 | 113 |
|
102 | 114 | # compile activity dict |
103 | 115 | activity = { |
@@ -163,13 +175,7 @@ def do_POST(self): |
163 | 175 | def notify(msg): |
164 | 176 | print(msg) |
165 | 177 | if toaster: # silent mode only |
166 | | - toaster.show_toast(icon_path="icon.ico", title=our_name, msg=msg, duration=10, threaded=True) |
167 | | - |
168 | | -def exit(): |
169 | | - notify("Exiting...") |
170 | | - if systray: # silent mode only |
171 | | - systray.shutdown() |
172 | | - sys.exit() |
| 178 | + toaster.show_toast(icon_path="icon.ico", title=our_name, msg=msg, duration=5, threaded=True) |
173 | 179 |
|
174 | 180 | parser = argparse.ArgumentParser(prog="csgo_richpresence") |
175 | 181 | parser.add_argument("-S", "--silent", action="store_true", default=False, help="hide the console window entirely, leaving the program to run in the background (Windows only)") |
@@ -230,44 +236,46 @@ def hide_window(pid): |
230 | 236 | hide_window(our_process.parent().parent().pid) # fuck it, why not |
231 | 237 |
|
232 | 238 | toaster = ToastNotifier() |
233 | | - systray = SysTrayIcon("icon.ico", our_name, on_quit=exit) |
| 239 | + systray = SysTrayIcon("icon.ico", our_name, on_quit=state_exit) |
234 | 240 | systray.start() |
235 | 241 |
|
236 | 242 | server_address = ("127.0.0.1", port) |
237 | 243 | httpd = None |
| 244 | +next_csgo_check = 0 |
238 | 245 | try: |
239 | 246 | notify("Discord Rich Presence for CS:GO is now running in the background.") |
240 | 247 | while True: |
241 | | - print("Looking for csgo.exe...") |
242 | | - found_csgo = False |
243 | | - for pid in psutil.pids(): |
244 | | - try: # same as for enum_window_callback |
245 | | - p = psutil.Process(pid) |
246 | | - if p.name() == "csgo.exe": |
247 | | - found_csgo = True |
248 | | - break |
249 | | - except: |
250 | | - # print("Something could have gone wrong here while finding csgo.exe, ignore this") |
251 | | - pass |
252 | | - if found_csgo: |
253 | | - notify("Found csgo.exe, running server.") |
254 | | - if systray: |
255 | | - systray.update(hover_text=our_name + ": running") |
256 | | - |
257 | | - httpd = CSGOGameStateServer(server_address, CSGOGameStateRequestHandler) |
258 | | - print("Starting httpd at {}:{}".format(server_address[0], port)) |
259 | | - httpd.serve_forever() |
260 | | - time.sleep(30) |
| 248 | + if want_exit: |
| 249 | + exit() |
| 250 | + if next_csgo_check < time.time(): |
| 251 | + print("Looking for csgo.exe...") |
| 252 | + found_csgo = False |
| 253 | + for pid in psutil.pids(): |
| 254 | + try: # same as for enum_window_callback |
| 255 | + p = psutil.Process(pid) |
| 256 | + if p.name() == "csgo.exe": |
| 257 | + found_csgo = True |
| 258 | + break |
| 259 | + except: |
| 260 | + # print("Something could have gone wrong here while finding csgo.exe, ignore this") |
| 261 | + pass |
| 262 | + if found_csgo: |
| 263 | + notify("Found csgo.exe, running server.") |
| 264 | + if systray: |
| 265 | + systray.update(hover_text=our_name + ": running") |
| 266 | + |
| 267 | + httpd = CSGOGameStateServer(server_address, CSGOGameStateRequestHandler) |
| 268 | + print("Starting httpd at {}:{}".format(server_address[0], port)) |
| 269 | + httpd.serve_forever() |
| 270 | + next_csgo_check = time.time() + 30 |
261 | 271 | except KeyboardInterrupt: |
262 | 272 | if httpd: |
263 | 273 | print('Stopping httpd...') |
264 | 274 | httpd.server_close() |
265 | | - |
266 | 275 | exit() |
267 | 276 | except Exception as e: |
268 | 277 | notify("Error: " + str(e)) |
269 | 278 | input("Press Enter to quit.") |
270 | | - |
271 | 279 | exit() |
272 | 280 |
|
273 | 281 | # so many try statements in this script jesus fuck i don't know how to code |
|
0 commit comments