-am (#1329)
This commit is contained in:
@@ -0,0 +1,381 @@
|
||||
"""TSS scoreboard renderer.
|
||||
|
||||
Generates the match-result image posted by autologging. Mirrors SREBOT's visual
|
||||
language (blurred map background, vignette, two team columns, stat icons) but is
|
||||
TSS-native: team names instead of squadron tags, the full per-player vehicle lineup
|
||||
(unused dimmed) instead of a single vehicle, and per-player pvp_ratio instead of
|
||||
squadron points.
|
||||
|
||||
Input is the flat model from ``transform.build_scoreboard_model`` — this module never
|
||||
touches the raw feed.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
from PIL.Image import Resampling
|
||||
|
||||
from . import SHARED_DIR
|
||||
from .wl import get_tss_standings
|
||||
|
||||
log = logging.getLogger("tssbot.scoreboard")
|
||||
|
||||
MAPS_DIR = SHARED_DIR / "MAPS"
|
||||
ICON_BASE_DIR = SHARED_DIR / "ICONS"
|
||||
VEHICLE_ICON_DIR = ICON_BASE_DIR / "VEHICLES"
|
||||
TEXT_FONT_PATH = SHARED_DIR / "FONTS" / "arial_unicode_ms.otf"
|
||||
|
||||
# Colors
|
||||
WIN_FILL = (60, 255, 60, 255)
|
||||
LOSS_FILL = (255, 60, 60, 255)
|
||||
DRAW_FILL = (255, 255, 0, 255)
|
||||
NEUTRAL_FILL = (255, 255, 255, 255)
|
||||
GREY_FILL = (200, 200, 200, 255)
|
||||
NAME_FILL = (250, 227, 200, 255)
|
||||
DIM_FILL = (150, 150, 150, 255)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Asset caching (mirrors SREBOT)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_FONTS: dict[str, ImageFont.FreeTypeFont] = {}
|
||||
|
||||
|
||||
def load_fonts(base_width: int) -> dict[str, ImageFont.FreeTypeFont]:
|
||||
global _FONTS
|
||||
if _FONTS:
|
||||
return _FONTS
|
||||
_FONTS = {
|
||||
"title": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.04)),
|
||||
"team": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.03)),
|
||||
"sub": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.019)),
|
||||
"body": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.020)),
|
||||
"stat": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.022)),
|
||||
"info": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.016)),
|
||||
"small": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.013)),
|
||||
"veh": ImageFont.truetype(str(TEXT_FONT_PATH), int(base_width * 0.016)),
|
||||
}
|
||||
return _FONTS
|
||||
|
||||
|
||||
@lru_cache(maxsize=256)
|
||||
def _load_icon_cached(path_str: str, size: tuple, alpha: int):
|
||||
img = Image.open(path_str).convert("RGBA")
|
||||
if size:
|
||||
img = img.resize(size, Resampling.LANCZOS)
|
||||
if alpha < 255:
|
||||
a = img.getchannel("A").point(lambda v: int(v * alpha / 255))
|
||||
img.putalpha(a)
|
||||
return img
|
||||
|
||||
|
||||
def load_icon(path: Path, size: Optional[tuple] = None, alpha: int = 255) -> Image.Image:
|
||||
return _load_icon_cached(str(path), tuple(size) if size else None, alpha)
|
||||
|
||||
|
||||
@lru_cache(maxsize=24)
|
||||
def _load_map_cached(path_str: str, blur_radius: int):
|
||||
from PIL import ImageFilter
|
||||
img = Image.open(path_str).convert("RGBA")
|
||||
return img.filter(ImageFilter.GaussianBlur(blur_radius))
|
||||
|
||||
|
||||
def _make_vignette(width: int, height: int, base_alpha=140, max_alpha=175, power=4) -> Image.Image:
|
||||
ys = np.linspace(-1, 1, height)[:, None]
|
||||
xs = np.linspace(-1, 1, width)[None, :]
|
||||
dist = np.sqrt(xs ** 2 + ys ** 2) / np.sqrt(2)
|
||||
band = base_alpha + (max_alpha - base_alpha) * (dist ** power)
|
||||
return Image.fromarray(np.clip(band, 0, 255).astype(np.uint8), mode="L")
|
||||
|
||||
|
||||
def _resolve_map_image(map_name: str) -> Optional[str]:
|
||||
"""Match a TSS mission_name against SHARED/MAPS (spaces→_, .jpg), like SRE."""
|
||||
clean = re.sub(r"^\s*\[[^]]+\]\s*", "", map_name).replace(" ", "_")
|
||||
target = f"{clean}.jpg"
|
||||
try:
|
||||
candidates = {f.name for f in MAPS_DIR.iterdir() if f.is_file()}
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
match = next((fn for fn in candidates if fn.lower() == target.lower()), None)
|
||||
return str(MAPS_DIR / match) if match else None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Render
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _bar_fill(bar_color: str) -> tuple:
|
||||
return {"win": WIN_FILL, "loss": LOSS_FILL, "draw": DRAW_FILL}.get(bar_color, NEUTRAL_FILL)
|
||||
|
||||
|
||||
def _strip_platform(name: str) -> str:
|
||||
return name.replace("@live", "").replace("@psn", "").strip()
|
||||
|
||||
|
||||
def _create_scoreboard_sync(model: dict[str, Any], output_path: str, bar_color: str) -> None:
|
||||
map_name = model.get("map", "")
|
||||
map_image_path = _resolve_map_image(map_name)
|
||||
|
||||
if map_image_path:
|
||||
background = _load_map_cached(map_image_path, 2).copy()
|
||||
else:
|
||||
log.warning("[TSS-SB] No map image for %r; using flat background", map_name)
|
||||
background = Image.new("RGBA", (1920, 1080), (25, 28, 32, 255))
|
||||
|
||||
bg_w, bg_h = background.size
|
||||
|
||||
band = _make_vignette(bg_w, bg_h)
|
||||
overlay = Image.new("RGBA", (bg_w, bg_h), (0, 0, 0, 0))
|
||||
black = Image.new("RGBA", (bg_w, bg_h), (0, 0, 0, 255))
|
||||
overlay = Image.composite(black, overlay, band)
|
||||
draw = ImageDraw.Draw(overlay)
|
||||
|
||||
fonts = load_fonts(bg_w)
|
||||
BODY = int(bg_w * 0.020)
|
||||
STAT = int(bg_w * 0.022)
|
||||
|
||||
# ── Top-right metadata: timestamp + session id
|
||||
padding = 15
|
||||
ts_epoch = int(model.get("utc_timestamp") or 0)
|
||||
ts_text = datetime.fromtimestamp(ts_epoch, tz=timezone.utc).strftime("%H:%M:%S - %Y-%m-%d UTC")
|
||||
sid_text = model.get("session_id", "")
|
||||
for i, txt in enumerate((ts_text, sid_text)):
|
||||
bbox = draw.textbbox((0, 0), txt, font=fonts["info"])
|
||||
x = bg_w - (bbox[2] - bbox[0]) - padding - 10
|
||||
y = padding + 10 + i * ((bbox[3] - bbox[1]) + 20)
|
||||
draw.text((x, y), txt, font=fonts["info"], fill=GREY_FILL)
|
||||
|
||||
# ── Top-center titles: map name, then tournament · mode
|
||||
y = 50
|
||||
title = map_name or "Unknown Map"
|
||||
tb = draw.textbbox((0, 0), title, font=fonts["title"])
|
||||
draw.text(((bg_w - (tb[2] - tb[0])) // 2, y), title, font=fonts["title"], fill=NEUTRAL_FILL)
|
||||
# Gap off the title's full glyph box (not just measured height) so the subtitle
|
||||
# clears descenders instead of clipping into the map name.
|
||||
y += int(bg_w * 0.04) + 28
|
||||
|
||||
sub_parts = [p for p in (model.get("tournament_name"), model.get("mission_mode")) if p]
|
||||
if sub_parts:
|
||||
sub = " · ".join(sub_parts)
|
||||
sb = draw.textbbox((0, 0), sub, font=fonts["sub"])
|
||||
draw.text(((bg_w - (sb[2] - sb[0])) // 2, y), sub, font=fonts["sub"], fill=GREY_FILL)
|
||||
y += (sb[3] - sb[1]) + 10
|
||||
|
||||
y_start = y + 150
|
||||
|
||||
# ── Layout: two team columns + separator
|
||||
x_start = 55
|
||||
col_width = (bg_w - x_start * 2) // 2
|
||||
|
||||
sep_x = x_start + col_width
|
||||
draw.line([(sep_x, y_start - 60), (sep_x, bg_h - 50)], fill=_bar_fill(bar_color), width=5)
|
||||
|
||||
teams = model.get("teams", [])
|
||||
is_draw = model.get("is_draw", False)
|
||||
winner = model.get("winner")
|
||||
|
||||
def draw_team(idx: int, team: dict, start_x: int, section_width: int):
|
||||
flipped = idx == 1 # second team mirrored to the right
|
||||
players = sorted(team.get("players", []), key=lambda p: int(p.get("score") or 0), reverse=True)
|
||||
|
||||
team_name = team.get("team_name", "Team")
|
||||
if is_draw:
|
||||
header_fill = DRAW_FILL
|
||||
elif winner is not None and team_name == winner:
|
||||
header_fill = WIN_FILL
|
||||
else:
|
||||
header_fill = LOSS_FILL
|
||||
|
||||
header_y = y_start - 90
|
||||
nb = draw.textbbox((0, 0), team_name, font=fonts["team"])
|
||||
name_w = nb[2] - nb[0]
|
||||
name_x = start_x if not flipped else start_x + section_width - name_w
|
||||
draw.text((name_x, header_y), team_name, font=fonts["team"], fill=header_fill)
|
||||
|
||||
# W/L slot — only drawn once real per-tournament standings exist (stub returns
|
||||
# None today, so nothing renders under the team name).
|
||||
standings = get_tss_standings(team.get("team_id"))
|
||||
if standings:
|
||||
wl_text = f"{standings['wins']}W - {standings['losses']}L"
|
||||
wb = draw.textbbox((0, 0), wl_text, font=fonts["sub"])
|
||||
wl_x = start_x if not flipped else start_x + section_width - (wb[2] - wb[0])
|
||||
draw.text((wl_x, header_y + (nb[3] - nb[1]) + 18), wl_text, font=fonts["sub"], fill=GREY_FILL)
|
||||
|
||||
# Stat columns (rating now lives inline next to the player name).
|
||||
col_labels = ["Air", "Ground", "Assists", "Deaths", "Caps"]
|
||||
icon_map = {
|
||||
"Air": ICON_BASE_DIR / "fighter_icon.png",
|
||||
"Ground": ICON_BASE_DIR / "tank_icon.png",
|
||||
"Assists": ICON_BASE_DIR / "assists_icon.png",
|
||||
"Deaths": ICON_BASE_DIR / "deaths_icon.png",
|
||||
"Caps": ICON_BASE_DIR / "cap_icon.png",
|
||||
}
|
||||
# Column CENTERS — a tight stat block packed against the center separator
|
||||
# (like SRE). Center-anchoring keeps 1- and 4-digit values aligned. Team 2
|
||||
# mirrors across its own center axis so the board is symmetric.
|
||||
n_cols = len(col_labels)
|
||||
gutter = section_width * 0.035 # small gap off the center separator
|
||||
stat_area = section_width * 0.30 # tight column block
|
||||
right_edge = start_x + section_width - gutter
|
||||
step = stat_area / n_cols
|
||||
centers = [right_edge - stat_area + step * (i + 0.5) for i in range(n_cols)]
|
||||
if flipped:
|
||||
axis = start_x + section_width / 2.0
|
||||
centers = [2 * axis - c for c in centers]
|
||||
col_center = {lab: int(c) for lab, c in zip(col_labels, centers)}
|
||||
|
||||
ICON_SIZE = int(STAT * 1.1)
|
||||
head_icon_y = header_y + 72
|
||||
for label in col_labels:
|
||||
cx = col_center[label]
|
||||
ico = icon_map.get(label)
|
||||
if ico:
|
||||
try:
|
||||
img = load_icon(ico, (ICON_SIZE, ICON_SIZE))
|
||||
overlay.paste(img, (int(cx - ICON_SIZE // 2), int(head_icon_y)), img)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Player rows. Layout per row:
|
||||
# top line — player name + inline (rating ±delta) at the column edge; stats
|
||||
# lower line — vehicle icons next to their translated names
|
||||
body = fonts["body"]
|
||||
small = fonts["veh"]
|
||||
name_h = body.getbbox("Ag")[3]
|
||||
small_h = small.getbbox("Ag")[3]
|
||||
veh_icon = int(BODY * 1.6)
|
||||
y_off = header_y + ICON_SIZE + 95
|
||||
for p in players:
|
||||
name = _strip_platform(p.get("fake_nick") or p.get("nick") or "")
|
||||
units = [u for u in p.get("units", []) if u.get("used")]
|
||||
row_h = name_h + 12 + veh_icon + 12
|
||||
line_y = y_off # player-name line (at the column edge)
|
||||
id_mid = line_y + name_h // 2
|
||||
veh_y = line_y + name_h + 12 # vehicle line (icons + names together)
|
||||
vname_y = veh_y + (veh_icon - small_h) // 2
|
||||
name_w = draw.textbbox((0, 0), name, font=body)[2]
|
||||
rating_segs = _rating_segments(p.get("pvp_ratio"), p.get("pvp_ratio_delta"))
|
||||
rating_w = _segments_width(draw, rating_segs, body)
|
||||
|
||||
if not flipped:
|
||||
# name + rating at the left edge
|
||||
draw.text((start_x, line_y), name, font=body, fill=NAME_FILL)
|
||||
_draw_segments(draw, start_x + name_w + 12, line_y, rating_segs, body)
|
||||
# below: each icon directly next to its own vehicle name
|
||||
_draw_vehicle_pairs(overlay, draw, units, start_x, veh_y, vname_y, veh_icon, small)
|
||||
else:
|
||||
# name + rating at the right edge (rating to the left of the name)
|
||||
name_x = start_x + section_width - name_w
|
||||
draw.text((name_x, line_y), name, font=body, fill=NAME_FILL)
|
||||
_draw_segments(draw, name_x - 12 - rating_w, line_y, rating_segs, body)
|
||||
# below: icon+name pairs, the whole group right-aligned to the edge
|
||||
total = _vehicle_pairs_width(draw, units, veh_icon, small)
|
||||
_draw_vehicle_pairs(overlay, draw, units, start_x + section_width - total,
|
||||
veh_y, vname_y, veh_icon, small)
|
||||
|
||||
# stat values, vertically centered on the identity line
|
||||
stats = [
|
||||
("Air", p.get("air_kills", 0)),
|
||||
("Ground", p.get("ground_kills", 0)),
|
||||
("Assists", p.get("assists", 0)),
|
||||
("Deaths", p.get("deaths", 0)),
|
||||
("Caps", p.get("captures", 0)),
|
||||
]
|
||||
for label, val in stats:
|
||||
num = int(val)
|
||||
fill = NEUTRAL_FILL
|
||||
if label in ("Air", "Ground") and num > 0:
|
||||
fill = WIN_FILL
|
||||
elif label == "Deaths" and num > 0:
|
||||
fill = (255, 20, 20, 255)
|
||||
elif label == "Caps" and num > 0:
|
||||
fill = DRAW_FILL
|
||||
elif label == "Assists" and num > 0:
|
||||
fill = (230, 150, 90, 255)
|
||||
draw.text((col_center[label], id_mid), str(num), font=fonts["stat"], fill=fill, anchor="mm")
|
||||
|
||||
y_off += row_h + 14
|
||||
|
||||
draw_team(0, teams[0], x_start + 10, col_width)
|
||||
draw_team(1, teams[1], x_start + col_width, col_width)
|
||||
|
||||
# ── Composite + downsample
|
||||
final = Image.alpha_composite(background, overlay)
|
||||
scale = 0.5
|
||||
final = final.resize((int(bg_w * scale), int(bg_h * scale)), resample=Resampling.LANCZOS).convert("RGB")
|
||||
final.save(output_path, format="PNG", compress_level=1)
|
||||
|
||||
|
||||
RATING_FILL = (170, 200, 255, 255)
|
||||
|
||||
|
||||
def _rating_segments(rating, delta):
|
||||
"""Build colored (text, fill) segments for the inline ``(1468 +2)`` rating.
|
||||
|
||||
``delta`` is a signed change vs the player's previous rating; it's ``None`` until
|
||||
rating history is tracked (see wl.py), so for now only the current value shows.
|
||||
"""
|
||||
rating_str = f"{int(round(rating))}" if isinstance(rating, (int, float)) else "—"
|
||||
if isinstance(delta, (int, float)) and delta != 0:
|
||||
dcol = WIN_FILL if delta > 0 else LOSS_FILL
|
||||
return [(f"({rating_str} ", RATING_FILL), (f"{'+' if delta > 0 else ''}{int(delta)}", dcol), (")", RATING_FILL)]
|
||||
return [(f"({rating_str})", RATING_FILL)]
|
||||
|
||||
|
||||
def _segments_width(draw, segs, font):
|
||||
return sum(draw.textbbox((0, 0), s, font=font)[2] for s, _ in segs)
|
||||
|
||||
|
||||
def _draw_segments(draw, x, y, segs, font):
|
||||
for s, c in segs:
|
||||
draw.text((x, y), s, font=font, fill=c)
|
||||
x += draw.textbbox((0, 0), s, font=font)[2]
|
||||
return x
|
||||
|
||||
|
||||
def _paste_vehicle(overlay, unit, xy, size):
|
||||
"""Paste a vehicle icon (unused ones dimmed), falling back to not_found."""
|
||||
alpha = 255 if unit["used"] else 90
|
||||
for path in (VEHICLE_ICON_DIR / f"{unit['internal'].lower()}.png", ICON_BASE_DIR / "not_found.png"):
|
||||
try:
|
||||
img = load_icon(path, (size, size), alpha=alpha)
|
||||
overlay.paste(img, (int(xy[0]), int(xy[1])), img)
|
||||
return
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
|
||||
_VEH_PAIR_GAP = 22
|
||||
|
||||
|
||||
def _vehicle_pairs_width(draw, units, size, font):
|
||||
"""Total pixel width of the icon+name pairs (for right-aligning team 2)."""
|
||||
w = 0
|
||||
for u in units:
|
||||
w += size + 4 + draw.textbbox((0, 0), u["name"], font=font)[2] + _VEH_PAIR_GAP
|
||||
return max(0, w - _VEH_PAIR_GAP)
|
||||
|
||||
|
||||
def _draw_vehicle_pairs(overlay, draw, units, x, veh_y, vname_y, size, font):
|
||||
"""Draw each vehicle as an icon immediately followed by its translated name."""
|
||||
x = int(x)
|
||||
for u in units:
|
||||
_paste_vehicle(overlay, u, (x, veh_y), size)
|
||||
x += size + 4
|
||||
draw.text((x, vname_y), u["name"], font=font, fill=NEUTRAL_FILL)
|
||||
x += draw.textbbox((0, 0), u["name"], font=font)[2] + _VEH_PAIR_GAP
|
||||
return x
|
||||
|
||||
|
||||
async def create_scoreboard(model: dict[str, Any], output_path: str, bar_color: str = "") -> None:
|
||||
"""Render the TSS scoreboard for a game model and write it to ``output_path``."""
|
||||
await asyncio.to_thread(_create_scoreboard_sync, model, output_path, bar_color)
|
||||
Reference in New Issue
Block a user