fix: resolve current match fallback snapshots
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
import re
|
||||
|
||||
from .config import (
|
||||
get_historical_data_source_kind,
|
||||
@@ -302,14 +303,7 @@ def build_current_match_payload(*, server_slug: str) -> dict[str, object]:
|
||||
# drops richer RCON session fields such as game mode and current scores.
|
||||
server_payload = build_servers_payload()
|
||||
server_data = server_payload["data"]
|
||||
item = next(
|
||||
(
|
||||
candidate
|
||||
for candidate in server_data.get("items", [])
|
||||
if candidate.get("external_server_id") == origin.slug
|
||||
),
|
||||
None,
|
||||
)
|
||||
item = _find_current_match_snapshot_item(server_data.get("items", []), origin)
|
||||
return {
|
||||
"status": "ok",
|
||||
"data": {
|
||||
@@ -350,6 +344,59 @@ def build_current_match_payload(*, server_slug: str) -> dict[str, object]:
|
||||
}
|
||||
|
||||
|
||||
def _find_current_match_snapshot_item(
|
||||
items: list[dict[str, object]],
|
||||
origin: object,
|
||||
) -> dict[str, object] | None:
|
||||
"""Resolve one trusted live snapshot for the current-match fallback."""
|
||||
origin_slug = str(getattr(origin, "slug", "") or "").strip()
|
||||
source_markers = {
|
||||
"comunidad-hispana-01": ("152.114.195.174", ":7779"),
|
||||
"comunidad-hispana-02": ("152.114.195.150", ":7879"),
|
||||
}.get(origin_slug)
|
||||
server_number = getattr(origin, "server_number", None)
|
||||
|
||||
for item in items:
|
||||
if any(
|
||||
str(item.get(field) or "").strip() == origin_slug
|
||||
for field in (
|
||||
"external_server_id",
|
||||
"server_slug",
|
||||
"target_key",
|
||||
"slug",
|
||||
"community_slug",
|
||||
)
|
||||
):
|
||||
return item
|
||||
|
||||
server_label = str(item.get("server_name") or item.get("name") or "")
|
||||
if _current_match_server_name_matches(server_label, server_number):
|
||||
return item
|
||||
|
||||
source_identity = " ".join(
|
||||
str(item.get(field) or "") for field in ("external_server_id", "source_ref")
|
||||
)
|
||||
if source_markers and any(marker in source_identity for marker in source_markers):
|
||||
return item
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _current_match_server_name_matches(server_label: str, server_number: object) -> bool:
|
||||
if not isinstance(server_number, int):
|
||||
return False
|
||||
|
||||
normalized_label = server_label.strip().casefold()
|
||||
if not normalized_label:
|
||||
return False
|
||||
|
||||
numbered_marker = re.compile(rf"(?<!\d)#0*{server_number}(?!\d)")
|
||||
if numbered_marker.search(normalized_label):
|
||||
return True
|
||||
|
||||
return f"comunidad hispana #{server_number:02d}" in normalized_label
|
||||
|
||||
|
||||
def build_current_match_kill_feed_payload(
|
||||
*,
|
||||
server_slug: str,
|
||||
|
||||
@@ -104,6 +104,90 @@ def test_current_match_payload_keeps_explicit_zero_score():
|
||||
assert data["score_source"] == "rcon-session"
|
||||
|
||||
|
||||
def test_current_match_payload_fallback_resolves_legacy_rcon_external_id_for_01():
|
||||
data = _build_with_snapshot_fallback(
|
||||
"comunidad-hispana-01",
|
||||
{
|
||||
"external_server_id": "rcon:152.114.195.174:7779",
|
||||
"server_name": "#01 [ESP] Comunidad Hispana",
|
||||
"status": "online",
|
||||
"current_map": "St. Marie Du Mont",
|
||||
"players": 0,
|
||||
"max_players": 100,
|
||||
"captured_at": "2026-03-24T14:08:41.008487Z",
|
||||
},
|
||||
)
|
||||
|
||||
assert data["found"] is True
|
||||
assert data["map"] == "St. Marie Du Mont"
|
||||
assert data["map_pretty_name"] == "St. Marie Du Mont"
|
||||
assert data["status"] == "online"
|
||||
assert data["players"] == 0
|
||||
assert data["max_players"] == 100
|
||||
assert data["captured_at"] == "2026-03-24T14:08:41.008487Z"
|
||||
assert data["updated_at"] == "2026-03-24T14:08:41.008487Z"
|
||||
assert data["public_scoreboard_url"] == "https://scoreboard.comunidadhll.es"
|
||||
|
||||
|
||||
def test_current_match_payload_fallback_resolves_legacy_rcon_source_ref_for_02():
|
||||
data = _build_with_snapshot_fallback(
|
||||
"comunidad-hispana-02",
|
||||
{
|
||||
"external_server_id": "snapshot-server-02",
|
||||
"source_ref": "rcon://152.114.195.150:7879",
|
||||
"status": "online",
|
||||
"current_map": "Elsenborn Ridge",
|
||||
"captured_at": "2026-03-24T14:08:41.008487Z",
|
||||
},
|
||||
)
|
||||
|
||||
assert data["found"] is True
|
||||
assert data["server_slug"] == "comunidad-hispana-02"
|
||||
assert data["map"] == "Elsenborn Ridge"
|
||||
assert data["map_pretty_name"] == "Elsenborn Ridge"
|
||||
assert data["public_scoreboard_url"] == "https://scoreboard.comunidadhll.es:5443"
|
||||
|
||||
|
||||
def test_current_match_payload_fallback_resolves_community_server_names():
|
||||
number_first = _build_with_snapshot_fallback(
|
||||
"comunidad-hispana-01",
|
||||
{
|
||||
"external_server_id": "snapshot-server-01",
|
||||
"server_name": "#01 [ESP] Comunidad Hispana - Spa Onl",
|
||||
"current_map": "Mortain",
|
||||
},
|
||||
)
|
||||
community_first = _build_with_snapshot_fallback(
|
||||
"comunidad-hispana-02",
|
||||
{
|
||||
"external_server_id": "snapshot-server-02",
|
||||
"name": "Comunidad Hispana #02",
|
||||
"current_map": "Carentan",
|
||||
},
|
||||
)
|
||||
|
||||
assert number_first["found"] is True
|
||||
assert number_first["map"] == "Mortain"
|
||||
assert community_first["found"] is True
|
||||
assert community_first["map"] == "Carentan"
|
||||
|
||||
|
||||
def test_current_match_payload_fallback_does_not_match_unknown_snapshot():
|
||||
data = _build_with_snapshot_fallback(
|
||||
"comunidad-hispana-01",
|
||||
{
|
||||
"external_server_id": "rcon:203.0.113.10:9000",
|
||||
"source_ref": "rcon://203.0.113.10:9000",
|
||||
"server_name": "#03 Comunidad Hispana",
|
||||
"current_map": "Unknown Match",
|
||||
},
|
||||
)
|
||||
|
||||
assert data["found"] is False
|
||||
assert data["map"] is None
|
||||
assert data["status"] == "unavailable"
|
||||
|
||||
|
||||
def test_current_match_route_rejects_unsupported_server():
|
||||
status, payload = resolve_get_payload("/api/current-match?server=not-trusted")
|
||||
|
||||
@@ -216,3 +300,24 @@ def _build_with_rcon_sample(sample: dict[str, object]) -> dict[str, object]:
|
||||
):
|
||||
payload = build_current_match_payload(server_slug="comunidad-hispana-01")
|
||||
return payload["data"]
|
||||
|
||||
|
||||
def _build_with_snapshot_fallback(
|
||||
server_slug: str,
|
||||
item: dict[str, object],
|
||||
) -> dict[str, object]:
|
||||
with (
|
||||
patch("app.payloads._query_current_match_rcon_sample", return_value=None),
|
||||
patch(
|
||||
"app.payloads.build_servers_payload",
|
||||
return_value={
|
||||
"status": "ok",
|
||||
"data": {
|
||||
"last_snapshot_at": "2026-03-24T14:08:41.008487Z",
|
||||
"items": [item],
|
||||
},
|
||||
},
|
||||
),
|
||||
):
|
||||
payload = build_current_match_payload(server_slug=server_slug)
|
||||
return payload["data"]
|
||||
|
||||
Reference in New Issue
Block a user