Skip to content

Commit b47ffae

Browse files
Merge pull request #204 from altNightHawk/feature/asa-battlemetrics-fallback
Feature/asa battlemetrics fallback
2 parents 9ffcf42 + b64fbb8 commit b47ffae

1 file changed

Lines changed: 70 additions & 22 deletions

File tree

discordgsm/protocols/asa.py

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
from typing import TYPE_CHECKING
33

4+
import aiohttp
45
import opengsq
56

67
from discordgsm.protocols.protocol import Protocol
@@ -36,29 +37,76 @@ async def query(self):
3637
await self.pre_query()
3738

3839
host, port = str(self.kv["host"]), int(str(self.kv["port"]))
39-
eos = opengsq.EOS(
40-
host, port, self._deployment_id, ASA._access_token, self.timeout
41-
)
4240
start = time.time()
43-
info = await eos.get_info()
44-
ping = int((time.time() - start) * 1000)
41+
42+
# Try EOS first
43+
try:
44+
eos = opengsq.EOS(
45+
host, port, self._deployment_id, ASA._access_token, self.timeout
46+
)
47+
info = await eos.get_info()
48+
ping = int((time.time() - start) * 1000)
49+
50+
# Credits: @dkoz https://github.com/DiscordGSM/GameServerMonitor/pull/54/files
51+
attributes = dict(info.get("attributes", {}))
52+
settings = dict(info.get("settings", {}))
4553

46-
# Credits: @dkoz https://github.com/DiscordGSM/GameServerMonitor/pull/54/files
47-
attributes = dict(info.get("attributes", {}))
48-
settings = dict(info.get("settings", {}))
54+
result: GamedigResult = {
55+
"name": attributes.get("CUSTOMSERVERNAME_s", ""),
56+
"map": attributes.get("MAPNAME_s", ""),
57+
"password": attributes.get("SERVERPASSWORD_b", False),
58+
"numplayers": info.get("totalPlayers", 0),
59+
"numbots": 0,
60+
"maxplayers": settings.get("maxPublicPlayers", 0),
61+
"players": None,
62+
"bots": None,
63+
"connect": f"{host}:{port}",
64+
"ping": ping,
65+
"raw": info,
66+
}
4967

50-
result: GamedigResult = {
51-
"name": attributes.get("CUSTOMSERVERNAME_s", ""),
52-
"map": attributes.get("MAPNAME_s", ""),
53-
"password": attributes.get("SERVERPASSWORD_b", False),
54-
"numplayers": info.get("totalPlayers", 0),
55-
"numbots": 0,
56-
"maxplayers": settings.get("maxPublicPlayers", 0),
57-
"players": None,
58-
"bots": None,
59-
"connect": f"{host}:{port}",
60-
"ping": ping,
61-
"raw": info,
62-
}
68+
return result
69+
except Exception:
70+
# EOS failed, fallback to BattleMetrics
71+
start = time.time() # Restart timer for BattleMetrics query
72+
73+
# Fallback: Query BattleMetrics API by IP:port
74+
async with aiohttp.ClientSession() as session:
75+
url = f"https://api.battlemetrics.com/servers?filter[game]=arksa&filter[search]={host}:{port}"
76+
async with session.get(url, timeout=aiohttp.ClientTimeout(total=self.timeout)) as response:
77+
if response.status != 200:
78+
raise Exception(f"BattleMetrics API returned {response.status}")
79+
80+
data = await response.json()
81+
servers = data.get("data", [])
82+
83+
# Find the online server matching our IP:port
84+
server_info = None
85+
for server in servers:
86+
attrs = server.get("attributes", {})
87+
if attrs.get("ip") == host and attrs.get("port") == port and attrs.get("status") == "online":
88+
server_info = server
89+
break
90+
91+
if not server_info:
92+
raise Exception(f"No online server found on BattleMetrics for {host}:{port}")
93+
94+
ping = int((time.time() - start) * 1000)
95+
attrs = server_info.get("attributes", {})
96+
details = attrs.get("details", {})
97+
98+
result: GamedigResult = {
99+
"name": attrs.get("name", ""),
100+
"map": details.get("map", ""),
101+
"password": details.get("password", False),
102+
"numplayers": attrs.get("players", 0),
103+
"numbots": 0,
104+
"maxplayers": attrs.get("maxPlayers", 0),
105+
"players": None,
106+
"bots": None,
107+
"connect": f"{host}:{port}",
108+
"ping": ping,
109+
"raw": attrs,
110+
}
63111

64-
return result
112+
return result

0 commit comments

Comments
 (0)