From e3691a2319ea8592f9adc6319951a78f69a5cb97 Mon Sep 17 00:00:00 2001 From: Shu-Ying <1754798088@qq.com> Date: Sun, 12 Oct 2025 06:12:14 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20=E6=96=B0=E5=A2=9E=E5=86=9C?= =?UTF-8?q?=E5=9C=BA=E5=B8=AE=E5=8A=A9=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + __init__.py | 3 + farm/help.py | 206 ++++++------ resource/html/help.html | 696 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 757 insertions(+), 149 deletions(-) diff --git a/.gitignore b/.gitignore index 7b8470a..a5d4364 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /config/sign_in.json +/resource/* # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/__init__.py b/__init__.py index 77ba75b..f05e03e 100644 --- a/__init__.py +++ b/__init__.py @@ -11,6 +11,7 @@ from .database.database import g_pSqlManager from .dbService import g_pDBService from .event.event import g_pEventManager from .farm.farm import g_pFarmManager +from .farm.help import g_pHelpManager from .farm.shop import g_pShopManager from .json import g_pJsonManager from .request import g_pRequestManager @@ -90,6 +91,8 @@ async def start(): # 检查作物文件是否缺失 or 更新 await g_pRequestManager.initPlantDBFile() + await g_pHelpManager.createHelpImage() + # 析构函数 @driver.on_shutdown diff --git a/farm/help.py b/farm/help.py index 8094376..9c0823a 100644 --- a/farm/help.py +++ b/farm/help.py @@ -9,109 +9,115 @@ from zhenxun.services.log import logger from ..config import g_sResourcePath -def rendeerHtmlToFile(path: Path | str, context: dict, output: Path | str) -> None: - """ - 使用 Jinja2 渲染 HTML 模板并保存到指定文件,会自动创建父目录 +class CHelpManager: + @classmethod + def rendeerHtmlToFile( + cls, path: Path | str, context: dict, output: Path | str + ) -> None: + """ + 使用 Jinja2 渲染 HTML 模板并保存到指定文件,会自动创建父目录 - Args: - path (str): 模板 HTML 路径 - context (dict): 用于渲染的上下文字典 - output (str): 输出 HTML 文件路径 - """ - templatePath = str(path) - outputPath = str(output) + Args: + path (str): 模板 HTML 路径 + context (dict): 用于渲染的上下文字典 + output (str): 输出 HTML 文件路径 + """ + templatePath = str(path) + outputPath = str(output) - templateStr = Path(templatePath).read_text(encoding="utf-8") - template = Template(templateStr) - rendered = template.render(**context) + templateStr = Path(templatePath).read_text(encoding="utf-8") + template = Template(templateStr) + rendered = template.render(**context) - # 自动创建目录 - Path(outputPath).parent.mkdir(parents=True, exist_ok=True) + # 自动创建目录 + Path(outputPath).parent.mkdir(parents=True, exist_ok=True) - Path(outputPath).write_text(rendered, encoding="utf-8") + Path(outputPath).write_text(rendered, encoding="utf-8") + + @classmethod + async def screenshotHtmlToBytes(cls, path: str) -> bytes: + """ + 使用 Playwright 截图本地 HTML 文件并返回 PNG 图片字节数据 + + Args: + path (str): 本地 HTML 文件路径 + + Returns: + bytes: PNG 图片的原始字节内容 + """ + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page( + viewport={"width": 1200, "height": 900}, device_scale_factor=1 + ) + file_url = Path(path).resolve().as_uri() + await page.goto(file_url, wait_until="networkidle") + await page.evaluate("""() => { + return new Promise(r => setTimeout(r, 200)); + }""") + image_bytes = await page.screenshot(full_page=True) + await browser.close() + + return image_bytes + + @classmethod + async def screenshotSave( + cls, path: str, save: str, width: int, height: int + ) -> None: + """ + 使用 Playwright 渲染本地 HTML 并将截图保存到指定路径 + + Args: + path (str): HTML 文件路径 + save (str): PNG 保存路径(如 output/image.png) + width (int): 图片宽度 + height (int): 图片高度 + """ + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page( + viewport={"width": width, "height": height}, device_scale_factor=1 + ) + + file_url = Path(path).resolve().as_uri() + await page.goto(file_url, wait_until="networkidle") + await page.evaluate("""() => { + return new Promise(r => setTimeout(r, 200)); + }""") + + # 确保保存目录存在 + Path(save).parent.mkdir(parents=True, exist_ok=True) + + # 截图并保存到本地文件 + await page.screenshot(path=save, full_page=True) + await browser.close() + + @classmethod + async def createHelpImage(cls) -> bool: + templatePath = g_sResourcePath / "html/help.html" + outputPath = g_sResourcePath / "temp_html/help.html" + savePath = DATA_PATH / "farm_res/html/help.png" + + context = { + "main_title": "真寻农场帮助菜单", + "subtitle": "[]中为可选参数", + "page_title": "真寻农场帮助菜单", + "font_family": "MyFont", + "contents": [ + {"title": "主要指令", "commands": ["指令A", "指令B"]}, + {"title": "B", "commands": ["指令D", "指令E", "指令M", "指令i"]}, + ], + } + + try: + cls.rendeerHtmlToFile(templatePath, context, outputPath) + + bytes = await cls.screenshotSave(str(outputPath), str(savePath), 1500, 2300) + except Exception as e: + logger.warning("绘制农场帮助菜单失败", e=e) + return False + + return True -async def screenshotHtmlToBytes(path: str) -> bytes: - """ - 使用 Playwright 截图本地 HTML 文件并返回 PNG 图片字节数据 - - Args: - path (str): 本地 HTML 文件路径 - - Returns: - bytes: PNG 图片的原始字节内容 - """ - async with async_playwright() as p: - browser = await p.chromium.launch() - page = await browser.new_page() - file_url = Path(path).resolve().as_uri() - await page.goto(file_url) - image_bytes = await page.screenshot(full_page=True) - await browser.close() - - return image_bytes - - -async def screenshotSave(path: str, save: str) -> None: - """ - 使用 Playwright 渲染本地 HTML 并将截图保存到指定路径 - - Args: - path (str): HTML 文件路径 - save (str): PNG 保存路径(如 output/image.png) - """ - async with async_playwright() as p: - browser = await p.chromium.launch() - page = await browser.new_page() - - file_url = Path(path).resolve().as_uri() - await page.goto(file_url) - - # 确保保存目录存在 - Path(save).parent.mkdir(parents=True, exist_ok=True) - - # 截图并保存到本地文件 - await page.screenshot(path=save, full_page=True) - await browser.close() - - -async def createHelpImage() -> bool: - templatePath = g_sResourcePath / "html/help.html" - outputPath = DATA_PATH / "farm_res/html/help.html" - - context = { - "title": "功能指令总览", - "data": [ - { - "command": "开通农场", - "description": "首次进入游戏开通农场", - "tip": "", - }, - { - "command": "购买种子", - "description": "从商店中购买可用种子", - "tip": "", - }, - {"command": "播种", "description": "将种子种入土地中", "tip": "先开垦土地"}, - { - "command": "收获", - "description": "收获成熟作物获得收益", - "tip": "", - }, - { - "command": "偷菜", - "description": "从好友农场中偷取成熟作物", - "tip": "1", - }, - ], - } - - try: - rendeerHtmlToFile(templatePath, context, outputPath) - - image_bytes = await screenshot_html_to_bytes(html_output_path) - except Exception as e: - logger.warning("绘制农场帮助菜单失败", e=e) - return False - - return True +g_pHelpManager = CHelpManager() diff --git a/resource/html/help.html b/resource/html/help.html index 2c6dc6d..db64177 100644 --- a/resource/html/help.html +++ b/resource/html/help.html @@ -1,75 +1,673 @@ - + - {{ title }} + + 智能功能展示 - 参数可视化
-
-
{{ title }}
- - - - - - - - - - {% for entry in data %} - - - - - - {% endfor %} - -
指令描述Tip
{{ entry.command }}{{ entry.description }}{{ entry.tip }}
-
+
+

✨ 真寻农场帮助菜单 ✨

+

简单介绍一下农场的各个功能和食用方法

+ +
+
+
+ 必填参数 +
+
+
+ 可选参数 +
+
+
+ 条件参数 +
+
+
+ +
+
+ +
+
+
+ +
+
at 开通农场
+
+ +
+
条件逻辑
+
+ • 需要at小真寻
+
+
+
+ +
+
+
+ +
+
我的农场
+
+
+ +
+
+
+ +
+
农场详述
+
+
+ +
+
+
+ +
+
我的农场币
+
+
+ + +
+
+
+ +
+
种子商店
+
+ +
+
参数列表
+
+
+ 条件 + 参数 - 根据类型决定含义 +
+
+
+ +
+
条件逻辑
+
+ • 如果参数是中文 → 进入筛选模式,可接页码参数
+ • 如果参数是数字 → 直接作为页码使用
+
+
+ +
+ 使用示例1: 种子商店 胡萝
+ 使用示例2: 种子商店 2
+ 使用示例3: 种子商店 胡萝 3 +
+
+ + +
+
+
+ +
+
购买种子
+
+ +
+
参数列表
+
+
+ 必填 + 作物/种子名称 +
+
+ 可选 + 数量 +
+
+
+ +
+ 使用示例: 购买种子 胡萝卜 + 使用示例: 购买种子 胡萝卜 5 +
+
+ +
+
+
+ +
+
我的种子
+
+
+ +
+
+
+ +
+
播种
+
+ +
+
参数列表
+
+
+ 必填 + 作物/种子名称 +
+
+ 可选 + 数量 +
+
+
+ +
+
操作提示
+
+ • 数量不填默认将最大可能播种 +
+
+ +
+ 使用示例1: 种子商店 胡萝
+ 使用示例2: 种子商店 2
+ 使用示例3: 种子商店 胡萝 3 +
+
+ +
+
+
+ +
+
收获
+
+
+ +
+
+
+ +
+
铲除
+
+
+ +
+
+
+ +
+
我的作物
+
+
+ +
+
+
+ +
+
播种
+
+ +
+
参数列表
+
+
+ 可选 + 作物/种子名称 +
+
+ 可选 + 数量 +
+
+
+ +
+
操作提示
+
+ • 不填写作物名将售卖仓库种全部作物 + • 填作物名不填数量将指定作物全部出售 +
+
+
+ +
+
+
+ +
+
偷菜 at
+
+ +
+
条件逻辑
+
+ • 每人每天只能偷5次 + • 后续需要at目标且目标开通真寻农场 +
+
+
+ +
+
+
+ +
+
开垦
+
+
+ +
+
+
+ +
+
购买农场币
+
+ +
+
参数列表
+
+
+ 必填 + 数量 +
+
+
+ +
+
操作提示
+
+ • 数量为消耗金币的数量 +
+
+
+ +
+
+
+ +
+
更改农场名
+
+ +
+
参数列表
+
+
+ 必填 + 新的农场名 +
+
+
+ +
+
操作提示
+
+ • 仅支持部分特殊符号 +
+
+
+ +
+
+
+ +
+
农场签到
+
+
+ +
+
+
+ +
+
土地升级
+
+ +
+
参数列表
+
+
+ 必填 + 地块ID +
+
+
+ +
+
操作提示
+
+ • 地块ID通过农场详述获取 +
+
+
+
+
- + \ No newline at end of file