Naposledy aktivní 1 month ago

SpaceEngineers Torch DS restarter for cases where the MSCV debug window prevents it from auto restart. Should be run in the same machine (linux) as the DS

Revize a702d2b82cfad2dab4ded38e8b90df2d4af773b9

spacereset.py Raw
1import asyncio
2import time
3import os, signal
4import opengsq
5from opengsq.responses.source import SourceInfo, GoldSourceInfo, Visibility
6
7
8async def query(info):
9 host, port = str(info["host"]), int(str(info["port"]))
10 source = opengsq.Source(host, port, 15)
11
12 async def get_players():
13 try:
14 return await source.get_players()
15 except Exception:
16 return []
17
18 start = time.time()
19 info, players = await asyncio.gather(source.get_info(), get_players())
20
21 if isinstance(info, SourceInfo):
22 info: SourceInfo = info
23 connect = f"{host}:{info.port}"
24 game_id = info.game_id
25 keywords = info.keywords
26 elif isinstance(info, GoldSourceInfo):
27 info: GoldSourceInfo = info
28 connect = info.address
29 game_id = None
30 keywords = None
31
32 ping = int((time.time() - start) * 1000)
33 players.sort(key=lambda x: x.duration, reverse=True)
34 players, bots = players[info.bots :], players[: info.bots]
35
36 result = {
37 "name": info.name,
38 "map": info.map,
39 "password": info.visibility == Visibility.Private,
40 "numplayers": info.players,
41 "maxplayers": info.max_players,
42 "game_id": game_id,
43 "players": [
44 {
45 "name": player.name,
46 "raw": {"score": player.score, "time": player.duration},
47 }
48 for player in players
49 ],
50 "connect": connect,
51 "ping": ping,
52 "raw": info.__dict__,
53 }
54
55 if keywords:
56 result["raw"]["tags"] = str(keywords).split(",")
57
58 return result
59
60def close_pid(pid):
61 try:
62 os.kill(pid, signal.SIGKILL)
63 except Exception:
64 print(f"Failed to kill pid {pid}")
65
66
67async def main():
68 while True:
69 try:
70 print("Checking server")
71 data = await query({
72 "host": "192.168.52.100", # Your server IP
73 "port": 27016,
74 })
75 name = data["name"]
76 map = data["map"]
77 numplayers = data["numplayers"]
78 maxplayers = data["maxplayers"]
79
80 print(f"Server OK: {name} {map} {numplayers}/{maxplayers}")
81 except Exception as e:
82 print(e)
83 print("Restaring server")
84 pids = os.popen("ps ax |grep Torch | awk '{ print $1 }' | xargs").read().split()
85 for pid in pids:
86 print(f"Killing pid {pid}")
87 close_pid(int(pid))
88 print("Waiting for 5 minutes before trying again")
89 await asyncio.sleep(300)
90
91 await asyncio.sleep(5)
92
93loop = asyncio.get_event_loop()
94loop.run_until_complete(main())