change to hex and update DBs (#1284)

This commit is contained in:
NotSoToothless
2026-05-29 07:05:20 -07:00
committed by GitHub
parent 66e010c890
commit 6fb303e26f
5 changed files with 543 additions and 44 deletions
+17 -8
View File
@@ -29,6 +29,8 @@ import zstandard as zstd
from dotenv import load_dotenv
from websockets.asyncio.client import connect as wsconnect
from BOT.storage import insert_match, insert_player_games
_HERE = Path(__file__).resolve().parent
load_dotenv(dotenv_path=_HERE / ".env")
@@ -56,20 +58,21 @@ def _auth_header() -> str:
def _session_id(game: Dict[str, Any]) -> str:
"""Extract a filesystem-safe session ID from a game dict."""
"""Return a hex session ID, converting decimal string/int IDs from Spectra."""
raw = game.get("_id") or game.get("id")
if raw is None:
return f"unknown_{int(time.time())}"
# Numeric ID → hex string (matches SREBOT convention)
if isinstance(raw, int):
return hex(raw)[2:].lower()
s = str(raw).strip().lower()
return s[2:] if s.startswith("0x") else s
try:
return hex(int(raw))[2:].lower()
except (ValueError, TypeError):
s = str(raw).strip().lower()
return s[2:] if s.startswith("0x") else s
def _write_game(game: Dict[str, Any]) -> Path:
"""Write game dict to REPLAYS/TSS/<session_id>/replay_data.json.gz."""
"""Normalize _id to hex, then write to REPLAYS/TSS/<session_id>/replay_data.json.gz."""
sid = _session_id(game)
game["_id"] = sid # hex from this point forward
session_dir = REPLAYS_DIR / sid
session_dir.mkdir(parents=True, exist_ok=True)
out = session_dir / "replay_data.json.gz"
@@ -161,8 +164,14 @@ async def listen(
async def _handle_game(game: Dict[str, Any]) -> None:
out = _write_game(game)
sid = _session_id(game)
sid = game["_id"] # hex, normalized by _write_game
log.info("Saved game %s%s", sid, out)
try:
await insert_match(game)
await insert_player_games(game)
log.info("Stored game %s in DB", sid)
except Exception as exc:
log.error("DB insert failed for %s: %s", sid, exc)
print(json.dumps(game, indent=2, ensure_ascii=False))
print("-" * 80)