This commit is contained in:
NotSoToothless
2026-06-18 02:39:21 -07:00
committed by GitHub
parent 32e747212f
commit d490c1c8d5
4 changed files with 41 additions and 11 deletions
+27 -5
View File
@@ -27,11 +27,11 @@ CREATE TABLE IF NOT EXISTS match_logs (
session_id TEXT PRIMARY KEY,
chat_log_json TEXT,
battle_log_json TEXT,
event_log_json TEXT,
built_unix INTEGER
)
"""
def _fmt_time(ms: int) -> str:
total_s = int(ms) // 1000
return f"{total_s // 60:02d}:{total_s % 60:02d}"
@@ -49,6 +49,17 @@ def _decompress_events(raw_events: Any) -> dict:
return raw_events or {}
def build_event_log(game: dict[str, Any]) -> dict[str, Any]:
"""Return the raw event slices the website needs for per-unit state."""
events = _decompress_events(game.get("events", {}))
if not isinstance(events, dict):
return {"kills": [], "damage": []}
return {
"kills": list(events.get("kills") or []),
"damage": list(events.get("damage") or []),
}
def _strip_tag(tag: str) -> str:
s = (tag or "").strip()
if len(s) >= 3 and not s[0].isalnum() and not s[-1].isalnum():
@@ -166,19 +177,30 @@ def build_match_logs(game: dict[str, Any]) -> tuple[list[str], list[str]]:
async def upsert_match_logs(
db_path, session_id: str, chat_log: list[str], battle_log: list[str]
db_path,
session_id: str,
chat_log: list[str],
battle_log: list[str],
event_log: dict[str, Any] | None = None,
) -> None:
import time
async with aiosqlite.connect(db_path) as conn:
await conn.execute(MATCH_LOGS_SQL)
try:
await conn.execute("ALTER TABLE match_logs ADD COLUMN event_log_json TEXT")
except Exception:
pass
await conn.execute(
"""INSERT INTO match_logs (session_id, chat_log_json, battle_log_json, built_unix)
VALUES (?, ?, ?, ?)
"""INSERT INTO match_logs (session_id, chat_log_json, battle_log_json, event_log_json, built_unix)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(session_id) DO UPDATE SET
chat_log_json=excluded.chat_log_json,
battle_log_json=excluded.battle_log_json,
event_log_json=excluded.event_log_json,
built_unix=excluded.built_unix""",
(str(session_id), json.dumps(chat_log, ensure_ascii=False),
json.dumps(battle_log, ensure_ascii=False), int(time.time())),
json.dumps(battle_log, ensure_ascii=False),
json.dumps(event_log or {"kills": [], "damage": []}, ensure_ascii=False),
int(time.time())),
)
await conn.commit()