tss db wipe and update (#1305)

This commit is contained in:
NotSoToothless
2026-06-07 17:28:16 -07:00
committed by GitHub
parent 632a441080
commit abab7ea9fa
7 changed files with 223 additions and 396 deletions
+17 -26
View File
@@ -15,7 +15,7 @@ from typing import Any, Optional
import discord
from . import preferences, storage
from . import preferences
log = logging.getLogger("tssbot.autolog")
@@ -33,18 +33,21 @@ def set_bot(bot: discord.Client) -> None:
_bot = bot
def _present_team_tags(game: dict[str, Any]) -> set[str]:
"""Return the set of team tags present in a game dict."""
players = game.get("players") or {}
tags: set[str] = set()
for p in players.values():
if not isinstance(p, dict):
def _present_teams(game: dict[str, Any]) -> tuple[set[str], set[str]]:
"""Return stable TSS team IDs and names embedded in a replay."""
tss = game.get("tss") or {}
team_ids: set[str] = set()
team_names: set[str] = set()
for slot in ("1", "2"):
team = tss.get(slot)
if not isinstance(team, dict):
continue
tag_raw = p.get("tag") or ""
tag = tag_raw[1:-1] if len(tag_raw) > 2 else tag_raw
if tag:
tags.add(tag)
return tags
if team.get("team_id") is not None:
team_ids.add(str(team["team_id"]))
name = str(team.get("team_name") or team.get("name") or "").strip()
if name:
team_names.add(name.lower())
return team_ids, team_names
async def process_game(game: dict[str, Any]) -> None:
@@ -59,19 +62,7 @@ async def process_game(game: dict[str, Any]) -> None:
if not session_id:
return
present_tags = _present_team_tags(game)
tags_lower = {t.lower() for t in present_tags}
# Resolve present tags → team_ids so team subscriptions (keyed by team_id) match.
present_team_ids: set[str] = set()
for tag in present_tags:
try:
tid = await storage.resolve_team_id_for_tag(tag)
except Exception as exc:
log.error("tag resolve failed for %s: %s", tag, exc)
tid = None
if tid is not None:
present_team_ids.add(str(tid))
present_team_ids, present_team_names = _present_teams(game)
sent = _sent_channels_by_session.setdefault(session_id, set())
@@ -84,7 +75,7 @@ async def process_game(game: dict[str, Any]) -> None:
continue
name = (entry.get("Name") or "").lower()
matched = str(entity_id) in present_team_ids or (name and name in tags_lower)
matched = str(entity_id) in present_team_ids or (name and name in present_team_names)
if not matched or channel_id in sent:
continue