spacereset.py
· 2.6 KiB · Python
Bruto
import asyncio
import time
import os, signal
import opengsq
from opengsq.responses.source import SourceInfo, GoldSourceInfo, Visibility
async def query(info):
host, port = str(info["host"]), int(str(info["port"]))
source = opengsq.Source(host, port, 15)
async def get_players():
try:
return await source.get_players()
except Exception:
return []
start = time.time()
info, players = await asyncio.gather(source.get_info(), get_players())
if isinstance(info, SourceInfo):
info: SourceInfo = info
connect = f"{host}:{info.port}"
game_id = info.game_id
keywords = info.keywords
elif isinstance(info, GoldSourceInfo):
info: GoldSourceInfo = info
connect = info.address
game_id = None
keywords = None
ping = int((time.time() - start) * 1000)
players.sort(key=lambda x: x.duration, reverse=True)
players, bots = players[info.bots :], players[: info.bots]
result = {
"name": info.name,
"map": info.map,
"password": info.visibility == Visibility.Private,
"numplayers": info.players,
"maxplayers": info.max_players,
"game_id": game_id,
"players": [
{
"name": player.name,
"raw": {"score": player.score, "time": player.duration},
}
for player in players
],
"connect": connect,
"ping": ping,
"raw": info.__dict__,
}
if keywords:
result["raw"]["tags"] = str(keywords).split(",")
return result
def close_pid(pid):
try:
os.kill(pid, signal.SIGKILL)
except Exception:
print(f"Failed to kill pid {pid}")
async def main():
while True:
try:
print("Checking server")
data = await query({
"host": "192.168.52.100", # Your server IP
"port": 27016,
})
name = data["name"]
map = data["map"]
numplayers = data["numplayers"]
maxplayers = data["maxplayers"]
print(f"Server OK: {name} {map} {numplayers}/{maxplayers}")
except Exception as e:
print(e)
print("Restaring server")
pids = os.popen("ps ax |grep Torch | awk '{ print $1 }' | xargs").read().split()
for pid in pids:
print(f"Killing pid {pid}")
close_pid(int(pid))
print("Waiting for 5 minutes before trying again")
await asyncio.sleep(300)
await asyncio.sleep(5)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
| 1 | import asyncio |
| 2 | import time |
| 3 | import os, signal |
| 4 | import opengsq |
| 5 | from opengsq.responses.source import SourceInfo, GoldSourceInfo, Visibility |
| 6 | |
| 7 | |
| 8 | async 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 | |
| 60 | def close_pid(pid): |
| 61 | try: |
| 62 | os.kill(pid, signal.SIGKILL) |
| 63 | except Exception: |
| 64 | print(f"Failed to kill pid {pid}") |
| 65 | |
| 66 | |
| 67 | async 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 | |
| 93 | loop = asyncio.get_event_loop() |
| 94 | loop.run_until_complete(main()) |