import random
import secrets
from pathlib import Path
from typing import Any
from nonebot import require
# 顺序很重要:先 require 声明依赖,再 import(与 war_renderer 一致)
require("nonebot_plugin_htmlrender")
from nonebot_plugin_htmlrender import get_new_page # noqa: E402
from .war_renderer import _font_faces, _logo_data_uri, _rel
basic_path = Path(__file__).resolve().parents[1]
template_path = basic_path / "res" / "templates"
img_path = basic_path / "res" / "img"
# 战备类型 -> (标签, 主题色)
TYPE_STYLE: dict[str, dict[str, str]] = {
"blue": {"label": "武装·补给", "color": "#6bb7ea"},
"red": {"label": "轨道·飞鹰", "color": "#fe6d6a"},
"green": {"label": "战术·设备", "color": "#7ecb3f"},
}
def _load_template(name: str) -> str:
"""读取 templates/ 下的模板文件(UTF-8),每次渲染实时读取,便于改模板即时生效。"""
return (template_path / name).read_text(encoding="utf-8")
def _equip_icon(equip: dict[str, Any]) -> str:
"""战备图标:读取 img/helldivers/ 下的 PNG,配合 "
def _item_html(equip: dict[str, Any]) -> str:
name = equip.get("name") or "未知战备"
alias = (random.choice(equip["alias"]) if equip.get("alias") else None) or ""
type_key = equip.get("type") or ""
style = TYPE_STYLE.get(type_key, {"label": "其他", "color": "#cccccc"})
label = style["label"]
color = style["color"]
backpack = "背包位" if equip.get("backpack") else ""
alias_html = f"{alias}" if alias else ""
meta_tail = f" · {backpack}" if backpack else ""
return f"""