diff --git a/BOT/match_logs_store.py b/BOT/match_logs_store.py new file mode 100644 index 0000000..494d974 --- /dev/null +++ b/BOT/match_logs_store.py @@ -0,0 +1,36 @@ +"""match_logs table for sq_battles.db (SREBOT parity with TSSBOT). + +Stores pre-formatted chat/battle log string arrays keyed by hex session id, +matching how the rest of sq_battles.db keys match data. +""" +import json +import sqlite3 +import time + +MATCH_LOGS_SQL = """ +CREATE TABLE IF NOT EXISTS match_logs ( + session_id TEXT PRIMARY KEY, + chat_log_json TEXT, + battle_log_json TEXT, + built_unix INTEGER +) +""" + + +def upsert_match_logs_sync(db_path, session_id, chat_log, battle_log) -> None: + conn = sqlite3.connect(db_path) + try: + conn.execute(MATCH_LOGS_SQL) + conn.execute( + """INSERT INTO match_logs (session_id, chat_log_json, battle_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, + 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())), + ) + conn.commit() + finally: + conn.close() diff --git a/BOT/utils.py b/BOT/utils.py index 23cb5ad..a4ae738 100644 --- a/BOT/utils.py +++ b/BOT/utils.py @@ -1497,6 +1497,16 @@ def transform_to_local_format(api_data: Dict[str, Any]) -> Optional[Dict[str, An if loser_team_slot: team_slot_names[loser_team_slot] = loser_squadron + try: + from BOT.match_logs_store import upsert_match_logs_sync + from pathlib import Path as _Path + import os as _os + _sq = _Path(_os.environ["STORAGE_VOL_PATH"]) / "sq_battles.db" + if session_id_hex: + upsert_match_logs_sync(_sq, session_id_hex, chat_log, battle_log) + except Exception as _e: + logging.error(f"match_logs upsert failed for {session_id_hex}: {_e}") + return { "winning_team_squadron": winner_squadron, "losing_team_squadron": loser_squadron,