refactor: restructure plugins per MTSS standard, consolidate assets to res/
Per docs/plugin-audit-report.md (plugins normalized to Trigger(handlers) → Service(services) → Model(repository/models) + utils): - Split monolithic __init__.py into handlers/services/utils across dailywife, deer_pipe, dice, galgame_card, helldivers_tools, huoziyinshua, learning_chat, makeaquote, mc_server_status, ncm_saying, picfinder_take, picstatus, random_jm_code, regif, steam_info, video_analysis, group_tools - Move static assets under res/: deer_pipe font/img, makeaquote font, helldivers img/templates, huoziyinshua HuoZiYinShua - Add config.py + register_config_items to ncm_saying, random_jm_code, group_tools; learning_chat unified config bridge - Remove deprecated: voice_trans plugin, bf_bot/test.py, dead code in dailywife/deer_pipe, empty dirs, debug scripts under helldivers temp - Disable brash_general_supercredits_tools (stub comment only) - bot.py: optional stdout/stderr redirect to log file for Web log viewer, force ANSI colorize on non-TTY sinks - Move runtime data (jm_code.json) out of plugin dir into hexi/data - Docs: plugin-audit-report.md; README reflects removed plugins Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# helldivers_tools —— Web 层。
|
||||
@@ -0,0 +1,114 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""战备管理 API:作为统一 Web 的一个模块,挂到 /api/helldivers。
|
||||
|
||||
- 不再自己出 HTML,页面由统一前端 (hexi/web) 渲染。
|
||||
- 鉴权统一走 hexi.web_auth(OAuth2 + SQLite)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||
from fastapi.responses import FileResponse, JSONResponse
|
||||
|
||||
from hexi.web_auth import require_admin
|
||||
|
||||
from ..services.equipment_store import (
|
||||
create_stratagem,
|
||||
delete_stratagem,
|
||||
export_items,
|
||||
get_stratagem,
|
||||
import_items,
|
||||
list_stratagems,
|
||||
list_stratagems_page,
|
||||
update_stratagem,
|
||||
)
|
||||
from ..utils.icon_utils import TYPE_COLORS, save_icon_png, svg_to_png
|
||||
|
||||
basic_path = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def _get_body(request: Request):
|
||||
return request.json()
|
||||
|
||||
|
||||
def build_admin_app() -> FastAPI | None:
|
||||
"""构建战备 API 子应用(挂载到 /api/helldivers)。"""
|
||||
app = FastAPI(title="Helldivers Stratagems API")
|
||||
auth = require_admin
|
||||
|
||||
@app.get("/stratagems")
|
||||
async def api_list(page: int = 1, page_size: int = 20, _: dict = auth):
|
||||
page = max(1, page)
|
||||
page_size = min(max(1, page_size), 100)
|
||||
items, total = list_stratagems_page(page, page_size)
|
||||
return {"items": items, "total": total, "page": page, "page_size": page_size}
|
||||
|
||||
@app.post("/stratagems")
|
||||
async def api_create(request: Request, _: dict = auth):
|
||||
body = await _get_body(request)
|
||||
try:
|
||||
item = create_stratagem(body)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return {"ok": True, "item": item}
|
||||
|
||||
@app.put("/stratagems/{seed}")
|
||||
async def api_update(seed: int, request: Request, _: dict = auth):
|
||||
body = await _get_body(request)
|
||||
item = update_stratagem(seed, body)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="未找到该战备")
|
||||
return {"ok": True, "item": item}
|
||||
|
||||
@app.delete("/stratagems/{seed}")
|
||||
async def api_delete(seed: int, _: dict = auth):
|
||||
if not delete_stratagem(seed):
|
||||
raise HTTPException(status_code=404, detail="未找到该战备")
|
||||
return {"ok": True}
|
||||
|
||||
@app.post("/import")
|
||||
async def api_import(request: Request, _: dict = auth):
|
||||
body = await _get_body(request)
|
||||
if isinstance(body, dict):
|
||||
items = body.get("items")
|
||||
else:
|
||||
items = body
|
||||
if not isinstance(items, list):
|
||||
raise HTTPException(status_code=400, detail="需要 JSON 数组或 {items: [...]}")
|
||||
result = import_items(items)
|
||||
return {"ok": True, **result}
|
||||
|
||||
@app.get("/export")
|
||||
async def api_export(_: dict = auth):
|
||||
return JSONResponse(export_items())
|
||||
|
||||
@app.get("/img/{path:path}")
|
||||
async def icon_file(path: str):
|
||||
base = (basic_path / "res" / "img").resolve()
|
||||
target = (basic_path / "res" / "img" / path).resolve()
|
||||
if not target.is_relative_to(base) or not target.exists():
|
||||
raise HTTPException(status_code=404, detail="图片不存在")
|
||||
return FileResponse(target)
|
||||
|
||||
@app.post("/stratagems/{seed}/icon")
|
||||
async def api_upload_icon(seed: int, request: Request, _: dict = auth):
|
||||
body = await _get_body(request)
|
||||
svg = (body.get("svg") or "").strip()
|
||||
framed = bool(body.get("framed"))
|
||||
item = get_stratagem(seed)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="未找到该战备")
|
||||
frame_color = TYPE_COLORS.get(item.get("type"), "#6bb7ea")
|
||||
try:
|
||||
png = await svg_to_png(svg, framed=framed, frame_color=frame_color)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"SVG 转换失败: {type(e).__name__}: {e}")
|
||||
rel = save_icon_png(png, item.get("name") or f"strategem-{seed}")
|
||||
update_stratagem(seed, {"icon": rel})
|
||||
return {"ok": True, "icon": rel}
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user