2 Commits

Author SHA1 Message Date
ded6f6ee2a Merge branch 'master' of http://82.157.175.59:3000/GenarrativeAI/Genarrative
Some checks failed
CI / verify (push) Has been cancelled
2026-04-27 14:23:33 +08:00
fa2dbb310b 1 2026-04-27 14:23:19 +08:00
74 changed files with 7358 additions and 1487 deletions

View File

@@ -142,7 +142,7 @@
落地规则:
1. 已发布拼图作品必须优先通过 `sourceSessionId` 恢复原 Agent session。
2. 恢复后的结果页沿用原草稿、候选图、正式图、标题、摘要和标签;创作者可以继续改标题、摘要、标签,并重新生成或切换图片。
2. 恢复后的结果页沿用原草稿、当前正式图、标题、摘要和标签;创作者可以继续改标题、摘要、标签,并重新生成图片。
3. 再次点击发布时不得创建新作品,必须覆盖同一个 `profileId / workId`
4. 覆盖发布只更新作品内容、更新时间、发布时间与广场投影;不得清零 `playCount`,不得改变作品归属。
5. 如果历史作品缺少 `sourceSessionId`,前端只能退回作品详情,不伪造编辑 session。
@@ -262,7 +262,7 @@ interface PuzzleAnchorPack {
1. 展示当前关卡名
2. 管理拼图图片生成
3. 展示并编辑题材标签
4. 预览作者信息
4. 执行作品测试
5. 执行发布
## 7.2 结果页必备字段
@@ -276,10 +276,28 @@ interface PuzzleAnchorPack {
本次建议同时加入:
1. `Agent 理解摘要`
2. `创作者署名预览`
3. `图片生成状态`
2. `图片生成状态`
3. `作品测试按钮`
4. `发布按钮`
## 7.2.1 2026-04-26 结果页收口规则
本轮拼图结果页必须对齐 RPG 结果页的工作台形态,不再保留右侧信息总表。
落地规则:
1. 页面顶部保留轻量返回区,主体使用页签式内容区,底部右侧使用操作区。
2. 结果页只分 `2` 个 Tab
- `基本信息`:只包含关卡名和题材标签。
- `拼图图片`:直接展示当前正式图、画面描述输入与生成入口。
3. 删除作者预览模块,不在结果页展示作者名、作者卡片或作者 HUD 说明。
4. 删除常驻发布校验模块,不在页面右侧或内容区预先展示阻断项。
5. 发布按钮放在屏幕右下角操作区,与 `作品测试` 并列;移动端保持底部可触达,页面内容区独立滚动。
6. 点击 `发布` 时才弹出发布面板;如果不满足发布条件,发布面板展示阻断项并拦截正式发布。
7. `作品测试` 不触发发布校验,直接用当前草稿进入拼图运行时。
8. 移动端需要保证 Tab 内容区、图片编辑区、发布面板都可以正常纵向滚动PC 端内容区使用更宽的预览与输入密度。
9. 题材标签不显示输入框常驻编辑态;只展示已有标签 chip支持删除已有标签并通过新增动作添加新标签。
## 7.3 关卡名规则
关卡名生成规则建议如下:
@@ -312,21 +330,45 @@ interface PuzzleAnchorPack {
1. 根据当前锚点生成正式拼图图片
2. 重新生成
3. 应用某一张候选
3. 上传一张可选参考图后进行图生
4. 从历史拼图素材库中选择一张素材作为参考图
第一版建议生成规则:
### 7.5.1 2026-04-27 单图替换规则
1. 默认一次生成 `2` 张候选图
2. 创作者选择 `1` 张作为正式图
3. 正式图确定后,写回作品主图
本轮起拼图结果页不再做多候选抽卡,而是采用“当前图直接替换”的轻量生成机制。
落地规则:
1. 每次点击生成只请求 `1` 张拼图图片。
2. 新图片生成成功后立即替换当前正式图,写回 `coverImageSrc / coverAssetId / selectedCandidateId`
3. `draft.candidates` 只保留当前这张图片对应的持久化记录,避免结果页继续展示历史候选池。
4. 不再提供“应用某一张候选图”的创作端交互;`select_puzzle_image` 只保留为兼容旧草稿的后端能力。
5. 再次生成失败时不得清空旧正式图,用户仍可发布或测试旧图。
6. 参考图只影响本次生成请求,不持久化为拼图作品字段。
7. 历史拼图素材库读取 `asset_kind = puzzle_cover_image` 的资产记录,只用于选择参考图,不直接替换正式图。
8. 从历史素材库选择素材后,前端把该素材的 `imageSrc` 作为 `referenceImageSrc` 传入下一次生成请求。
9. 本地上传参考图与历史素材参考图互斥;后选择者覆盖先选择者。
前端 UI 规则:
1. `拼图图片` Tab 左侧或上方展示当前正式图,右侧或下方展示画面描述输入与生成按钮。
2. 画面描述输入框上方必须显示标签 `画面描述`
3. `添加参考图` 按钮放入画面描述输入框右下角,作为小型 icon/button不单独占用输入框外的大按钮区。
4. 历史素材入口与添加参考图并列放在输入框右下角,打开独立素材选择面板,不在当前面板下方展开。
5. UI 不默认写玩法规则说明,只展示必要状态、预览和操作。
6. 移动端中输入框右下角按钮必须可点且不遮挡正文输入;输入框需要预留底部内边距。
后端落地契约:
1. `api-server` 写入 SpacetimeDB 的候选图 JSON 必须使用 `module-puzzle::PuzzleGeneratedImageCandidate` 持久化结构。
2. 持久化字段名保持 Rust 侧 `snake_case`,例如 `candidate_id``image_src``asset_id``actual_prompt``source_type`
3. 面向前端的 HTTP 响应仍由 `shared-contracts` 单独映射为 `camelCase`,不能把响应层字段名直接写入 SpacetimeDB JSON。
4. 多次生成候选图时必须追加到当前候选,不能清空已有候选图;已有正式选择保持不变,新追加候选图默认不抢占 `selected` 状态
5. 追加生成 `candidate_id` 必须按当前候选数量续号,避免前端列表 key 与后端选择动作命中旧候选图
4. 多次生成图片时必须替换当前候选记录,不能继续追加候选池;新记录默认 `selected = true`
5. 生成 `candidate_id` 可以继续按当前候选数量续号以保留调试可读性,但写入草稿时只保留新记录
6. 前端生成动作可携带 `referenceImageSrc`,字段语义对齐 RPG 场景图接口:支持单张 Data URL 或已有 `/generated-*` 旧路径。
7.`referenceImageSrc` 存在时,`api-server` 必须先把参考图归一化为 Data URL再对齐 RPG 场景图接口调用 DashScope `multimodal-generation/generation` 的 messages 图生图接口;无参考图时继续走现有 `text2image/image-synthesis` 文生图接口。
8. 图生图产物落盘、单图替换、正式图写回、发布校验与文生图完全复用同一套拼图图片链路。
9. `api-server` 的历史素材接口必须允许 `puzzle_cover_image`SpacetimeDB 侧历史素材 procedure 同步允许该类型。
## 7.6 拼图图片资产要求
@@ -343,9 +385,11 @@ interface PuzzleAnchorPack {
交互要求如下:
1. 生成图片时打开独立面板,不在当前卡片下方内联堆出大块内容
2. 标签编辑应为轻量标签编辑器,不做大表单。
3. 发布按钮必须固定清晰,不与图片生成操作混淆。
1. 图片生成不再通过作者预览旁的小弹窗承载,而是在 `拼图图片` Tab 内围绕当前正式图直接承载
2. 标签编辑应为轻量标签 chip 编辑器,不做大表单,不常驻显示标签输入框
3. 发布按钮必须固定在结果页右下操作区,不与图片生成操作混淆。
4. 点击发布后才展示发布阻断项;阻断项只存在于发布面板里。
5. `作品测试``发布` 同属右下操作区,测试动作不被发布阻断项拦截。
---
@@ -502,6 +546,9 @@ function resolvePuzzleGridSize(clearedLevelCount: number): 3 | 4 {
1. 初始局面不是已完成态
2. 初始局面至少存在可推进空间
3. 初始局面不能存在任何已经正确相邻的两块,避免玩家开局即看到自动合并块
初始化算法必须对候选打乱结果做正确相邻关系扫描:若任意两块在当前棋盘四向相邻,且它们在原图中的正确坐标也以相同方向相邻,则该候选布局无效,需要继续洗牌。多次随机尝试仍未得到合法布局时,使用确定性反序布局兜底;该布局等价于完整棋盘旋转 180 度,可保证原图相邻块不会以正确方向相邻。
## 9.5 交互规则总览

View File

@@ -120,3 +120,23 @@ Rust 首版返回:
1. `platform-llm` 单测能捕获开启搜索时上游 JSON 包含 `web_search_options`
2. `api-server` 配置单测能验证角色扮演搜索开关默认开启、环境变量可关闭。
3. 角色扮演剧情、NPC 对话、推理战斗文本请求都通过同一辅助函数设置搜索开关,避免漏接。
## 9. AgentSession 创作问答联网搜索补充2026-04-26
### 9.1 目标
AgentSession 页面中的 RPG 世界共创、拼图共创、大鱼吃小鱼共创都属于创作问答链路。用户在这些页面里会要求模型补充现实题材、历史文化、地理器物、玩法参照与美术风格依据,因此创作 Agent 的文本问答默认开启上游联网搜索能力。
### 9.2 落地范围
1. `api-server` 配置增加 `GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED` / `CREATION_AGENT_LLM_WEB_SEARCH_ENABLED`,默认 `true`
2. `creation_agent_llm_turn` 作为三类 Agent 共用 LLM 骨架,必须接收显式 `enable_web_search` 参数,并在 `LlmTextRequest` 上设置该值。
3. RPG 世界共创、拼图共创、大鱼吃小鱼共创的普通消息接口与 SSE 流式消息接口都传入同一配置值,避免只有某一种入口开启。
4. RPG 世界共创里的动态状态推断属于对当前聊天状态的结构化判断,不需要联网搜索,继续保持默认关闭。
5. `/api/llm/chat/completions` 通用代理继续不默认开启联网搜索。
### 9.3 验收
1. `api-server` 配置单测覆盖创作 Agent 联网搜索开关默认开启、环境变量可关闭。
2. 创作 Agent 的共用 LLM 单测覆盖开启搜索时 `LlmTextRequest.enable_web_search``true`
3. 三类 Agent turn request 均包含 `enable_web_search` 字段,调用点全部来自 `state.config.creation_agent_llm_web_search_enabled`

View File

@@ -0,0 +1,23 @@
# 资产历史接口补齐拼图封面素材类型
日期:`2026-04-27`
## 背景
拼图结果页会通过 `/api/assets/history?kind=puzzle_cover_image` 读取历史封面素材,供“生成或更换图片”面板复用旧图。
该链路与角色主视觉、场景图共用同一资产历史接口,因此后端白名单一旦漏掉 `puzzle_cover_image`,前端就会收到 `400 Bad Request`,表现为拼图封面历史素材列表无法打开。
## 本次口径
1. `server-rs/crates/api-server/src/assets.rs` 中的历史素材类型白名单统一收口为单一常量源。
2. HTTP 层错误文案与实际支持列表由同一函数生成,避免后续再出现“校验改了但提示文案还是旧口径”的漂移。
3. 增加 `puzzle_cover_image` 的回归测试,确保拼图封面素材不会再次被历史接口遗漏。
## 后续约束
1. 新增历史素材类型时,必须同时更新:
- `api-server``SUPPORTED_ASSET_HISTORY_KINDS`
- `spacetime-module` 的历史素材白名单
- 对应前端调用常量与测试
2. 如果运行态仍返回旧白名单错误,优先检查本地 `api-server.exe` 是否已按最新源码重新编译并重启,而不是先回退前端类型参数。

View File

@@ -253,6 +253,11 @@ function resolvePuzzleGridSize(clearedLevelCount: number): 3 | 4 {
1. 单块拖到单块位置:执行交换。
2. 合并块拖到任意目标锚点:保持内部相对布局整体重排。
3. 若合并块整体平移后覆盖到多个单块,被覆盖单块必须与合并块腾出的原格子做一对一交换,禁止把多个单块回填到同一个源格。
4. 一对一交换必须满足:
- 每个被覆盖单块只移动一次。
- 每个被腾出的源格只接收一个被覆盖单块。
- 若腾出的源格数量与被覆盖单块数量不一致,本次拖动视为非法,不更新棋盘。
3. 单块拖到合并块占据位置:先拆分目标合并块,再执行交换,最后重算合并。
### 7.5 通关
@@ -382,3 +387,58 @@ finalScore = tagSimilarityScore * 0.7 + sameAuthorScore * 0.3;
### 10.4 冻结说明
截至本次验收,拼图玩法已满足 PRD 要求的最小产品闭环;未继续扩展排行榜、提示、体力、异形拼块、倒计时、前端本地裁决等超出本轮需求的能力。
## 11. 2026-04-26 运行态机制补齐记录
本次按 PRD 第 9 章补齐拼图运行态的未完成机制,落点保持在 `server-rs/crates/module-puzzle` 领域层;前端本地兜底只同步表现和离线闭环,不改变后端真相源。
### 11.1 棋盘初始化
1. `build_initial_board_with_seed` 使用种子化洗牌生成初始棋盘,不再固定左移一格。
2. 正式 run 的种子由 `runId + profileId + levelIndex + gridSize` 派生;由于每次进入都会创建新的 `runId`,同一作品多次进入也会得到不同打乱样式。
3. 洗牌后若极端情况下仍为完成态,强制旋转一次,保证新关卡不是已完成局面。
4. 初始棋盘不得存在任何已经正确相邻的两块;初始化会多次洗牌筛选,若极端情况下未命中,则使用反序排列兜底,避免开局自动出现合并块。
5. `module-puzzle` 与本地 fallback 的测试都必须直接断言初始棋盘不存在正确相邻对,不能只检查 `mergedGroups = []`
### 11.2 局部重算与合并
1. 交换后只把源格、目标格和四向邻格纳入重算范围。
2. 拖动后把源格、目标格、被移动合并块边界格、被拆分目标合并块格子纳入重算范围。
3. 对受影响范围内的旧合并组先拆回单块,再按正确四向相邻关系重新生成合并组;未受影响的旧合并组保留。
4. 每次生成快照时统一重编号合并组,避免保留组与新组出现重复 `groupId`
### 11.3 拖动与拆分
1. 单块拖到单块位置时执行交换。
2. 合并块拖动时保持内部相对布局,以被拖动块作为锚点整体平移。
3. 单块拖入合并块占据位置时,先拆分目标合并块,再完成本次交换,最后按受影响范围重新合并。
### 11.4 本次新增验证
1. `cargo test -p module-puzzle` 覆盖:每次 run 不同打乱、正确相邻自动合并、单块拖入合并块拆分目标组。
2. `npm run test -- src/services/puzzle-runtime/puzzleLocalRuntime.test.ts` 覆盖:本地兜底每次启动不同打乱、交换后正确相邻自动合并、通关后推进下一关。
3. `npm run check:encoding` 已通过,确认中文文档未被编码写坏。
## 12. 2026-04-26 二次运行态缺口补齐
本次继续按 PRD 第 9.12 与第 14.4 节收敛两个剩余缺口:
1. 通关判定必须同时支持“所有拼块回到正确位置”和“所有拼块汇成一个覆盖全盘的大合并块”。领域层以 `all_tiles_resolved` 作为唯一对外真相,但其计算来源必须包含这两个条件。
2. 运行态底部不再常驻玩法说明文字,只保留短状态反馈、错误反馈和下一关动作;点击/拖动规则不写成长期 UI 文案。
### 12.1 合并块可见性修正
用户反馈“正确连接的块自动合并没有看到”后,确认原实现只把已合并格子染成绿色,仍按单块逐格渲染,视觉上无法形成“合并块”。本次运行态画布改为:
1. 根据 `mergedGroups` 计算合并块外接矩形。
2. 原单格位置让位为透明占位。
3. 在棋盘上叠加一个跨格整体层,内部仍按原图切片拼接,但外边框、阴影和拖动事件都属于同一个合并块。
4. 合并块整体层以组内第一块作为拖动锚点,继续沿用后端/本地运行态的合并块拖动裁决。
### 12.2 拖动可用性修正
用户反馈“没有办法拖动拼图块”后,确认原交互只在 pointer move 超过阈值后记录 `dragging = true`,没有持续记录当前指针位置,也没有把拖动中的块做视觉平移;移动端还可能被浏览器默认触控手势抢占。修正如下:
1. `dragState` 持续记录 `currentX/currentY`,拖动中按指针偏移对单块或合并块做 `translate3d` 跟手反馈。
2. 棋盘与合并块交互层增加 `touch-none select-none`,避免移动端滚动、选中文本等默认行为打断拖动。
3. 松手后仍只提交 `pieceId + targetRow + targetCol`,最终交换、合并、拆分和通关继续以后端/本地运行态快照为准。

View File

@@ -0,0 +1,31 @@
# 拼图生成图片资源代理修复
日期:`2026-04-27`
## 背景
拼图结果页的“生成或更换图片”会在 `api-server` 中调用 DashScope 生成图片,再把候选图上传到 OSS最终以 `/generated-puzzle-assets/...` 旧兼容路径写回 `PuzzleGeneratedImageCandidate.image_src` 与草稿封面字段。
本次排查发现拼图图片写入路径已经进入 `platform-oss::LegacyAssetPrefix::PuzzleAssets`,但后端 Axum 旧资源代理和 Vite 本地代理没有挂载 `/generated-puzzle-assets`。这会导致候选图或正式图无法读取;后续如果把已有候选图作为参考图继续更换图片,也会让参考图读取链路失效。
## 修复口径
1. `server-rs/crates/api-server/src/legacy_generated_assets.rs` 增加 `proxy_generated_puzzle_assets(...)`,复用统一的 OSS 签名读取逻辑。
2. `server-rs/crates/api-server/src/app.rs` 挂载 `/generated-puzzle-assets/{*path}`,与角色、大鱼、自定义世界图片资源前缀保持一致。
3. `vite.config.ts` 增加 `/generated-puzzle-assets` dev proxy保证本地网页端不会因为 Vite 代理缺口读不到后端资源。
## 后续约束
1. 任何新增 `LegacyAssetPrefix` 都必须同时检查:
- `platform-oss` 前缀枚举
- `api-server` 旧资源代理路由
- Vite dev proxy
- 前端 `isGeneratedLegacyPath(...)` 是否能识别
2. 拼图候选图 JSON 仍保持 SpacetimeDB 持久化结构 `PuzzleGeneratedImageCandidate` 的 snake_case 字段,不把 HTTP camelCase 响应结构写入 `draft_json`
3. 图片生成、OSS 读写和外部参考图解析继续留在 `api-server`,不能下沉到 SpacetimeDB reducer。
## 验收
1. `npm run check:encoding`
2. `cargo check -p api-server --manifest-path server-rs/Cargo.toml`
3. `npm run api-server:maincloud` 重启后,点击拼图结果页“生成或更换图片”,候选图应能写回并正常展示。

View File

@@ -0,0 +1,26 @@
# 拼图图片提示词脚本拆分
## 背景
拼图结果页的图片生成已经由 `server-rs/crates/api-server/src/puzzle.rs` 负责外部 I/O 编排、DashScope 请求、候选图落 OSS 与 SpacetimeDB 持久化。原先正式提示词和反向提示词也内联在同一文件里,后续调整拼图图片画面约束时容易误碰生成任务、资产绑定或候选池逻辑。
## 本轮落地边界
1. 拼图图片提示词统一放到 `server-rs/crates/api-server/src/prompt/puzzle_image.rs`
2. `puzzle.rs` 只负责读取提示词构建结果,并继续处理 DashScope、OSS、SpacetimeDB 写回。
3. 提示词模块只暴露:
- `build_puzzle_image_prompt(level_name, prompt)`
- `PUZZLE_DEFAULT_NEGATIVE_PROMPT`
4. 文生图和图生图继续共用同一份最终提示词,避免同一玩法下出现两套画面约束。
## 编码约束
1. 不把图片生成逻辑下沉到 SpacetimeDB reducer外部 I/O 必须留在 `api-server`
2. 不改候选图 JSON 持久化结构,仍使用 `module-puzzle::PuzzleGeneratedImageCandidate` 对应的 snake_case 字段。
3. 不改前端 UI 文案和交互;本轮只拆后端提示词脚本。
4. 后续若调整拼图图片风格、尺寸、禁止元素或切块可读性要求,优先修改 `prompt/puzzle_image.rs`,再按需补测试。
## 验收
1. `cargo test -p api-server puzzle_image` 通过。
2. `npm run check:encoding` 通过,确认新增中文文档和 Rust 注释仍是 UTF-8。

View File

@@ -0,0 +1,96 @@
# 拼图关卡通关弹窗与排行榜落地设计
更新时间:`2026-04-26`
## 1. 本次目标
玩家每完成拼图运行时的一关后,立即弹出独立结算弹窗。弹窗需要显示:
1. 本关通关时间。
2. 本关排行榜。
3. 排行榜条目包含名次、昵称、通关时间。
4. 下一关按钮,点击后进入下一关。
弹窗不能实现成当前面板下方追加内容,也不能在画布底部长期堆玩法说明。
## 2. 当前工程状态
拼图运行时已有两条能力边界:
1. 正式后端链路:`server-rs` 已有 `puzzle_runtime` 契约、`module-puzzle` 运行态模型与 `api-server` 路由。
2. 第一版单机运行态例外:前端当前实际通过 `startLocalPuzzleRun``swapLocalPuzzlePieces``dragLocalPuzzlePiece` 维护单次游玩内存快照,下一关通过 `advanceLocalPuzzleNextLevel` 交给 Rust HTTP 侧生成候选。
本次不新增旧 `server-node` 逻辑,不引入 PostgreSQL不从前端计算跨作品正式排行榜。
## 3. V1 排行榜边界
由于当前 PRD 已明确“第一版运行态采用单机本地版本”,本次排行榜采用可迁移的本地关卡榜结构:
1. 每个 `PuzzleRunSnapshot` 携带 `leaderboardEntries`
2. 每个 `PuzzleRuntimeLevelSnapshot` 携带:
- `startedAtMs`:本关开始时间。
- `clearedAtMs`:本关通关时间。
- `elapsedMs`:本关耗时。
- `leaderboardEntries`:当前关卡榜单。
3. 本地榜单生成规则只用于 V1 展示:
- 当前玩家昵称使用现有作者/玩家显示名兜底。
- 当前玩家通关后写入本关榜单。
- 追加少量稳定的系统样例成绩,按耗时升序排序。
- 名次由排序后顺序生成。
4. 后续正式迁移到 SpacetimeDB 时,字段可直接迁移为 `puzzle_level_clear_record` 或公共榜单 view前端弹窗不需要重做结构。
## 4. 数据结构
前端共享契约新增:
```ts
interface PuzzleLeaderboardEntry {
rank: number;
nickname: string;
elapsedMs: number;
isCurrentPlayer?: boolean;
}
```
运行态快照新增:
```ts
interface PuzzleRuntimeLevelSnapshot {
startedAtMs: number;
clearedAtMs: number | null;
elapsedMs: number | null;
leaderboardEntries: PuzzleLeaderboardEntry[];
}
interface PuzzleRunSnapshot {
leaderboardEntries: PuzzleLeaderboardEntry[];
}
```
Rust shared-contracts 与 api-server 映射同步补同名 camelCase 响应字段,确保 HTTP 本地下一关接口能透传这些字段。
## 5. 交互规则
1. 当前关卡从 `playing` 首次变为 `cleared` 后,弹出结算弹窗。
2. 弹窗打开时不允许点击背景关闭,避免误触跳过结算。
3. 弹窗保留关闭按钮,关闭后仍可通过底部下一关按钮继续。
4. 弹窗内“下一关”按钮直接调用现有 `onAdvanceNextLevel`
5. 下一关准备中时按钮禁用并显示加载态。
6. 进入下一关后,弹窗自动关闭,等待下一次通关再打开。
## 6. UI 要求
1. 移动端优先:弹窗最大宽度控制在窄屏内,榜单可纵向滚动。
2. PC 端:弹窗居中,信息密度保持克制。
3. 不在弹窗中写玩法规则说明。
4. 排行榜只展示名次、昵称、通关时间,当前玩家行用轻量高亮。
## 7. 验收点
1. 通关后能看到独立弹窗。
2. 弹窗显示本关耗时。
3. 排行榜显示名次、昵称、通关时间。
4. 点击弹窗内下一关按钮能触发进入下一关。
5. 进入下一关后弹窗消失。
6. `npm run check:encoding` 通过。
7. 拼图 runtime 相关单测通过。

View File

@@ -0,0 +1,37 @@
# 拼图结果页正式图刷新修复
日期:`2026-04-27`
## 背景
拼图结果页点击“生成并替换当前图片”后,后端会返回最新 session并把新图片写回 `draft.coverImageSrc`。但前端正式图展示仍可能命中旧的签名 URL 或浏览器图片缓存,导致当前正式图看起来没有变化。
这个问题在 `/generated-puzzle-assets/...` 私有资源链路下尤其明显:
1. 结果页展示依赖 `ResolvedAssetImage -> useResolvedAssetReadUrl -> /api/assets/read-url`
2. 读地址服务会缓存同一路径的签名 URL
3. 如果同一路径对应的资源内容在短时间内被更新,页面可能继续显示旧图
## 修复口径
1. `src/services/assetReadUrlService.ts`
-`resolveAssetReadUrl(...)` 增加 `refreshKey`
- 当调用方显式传入 `refreshKey` 时,跳过本地签名缓存
- 在最终图片 URL 上追加 `_v` 参数,强制浏览器拉取当前版本
2. `src/components/ResolvedAssetImage.tsx`
- 支持透传 `refreshKey`
3. `src/components/puzzle-result/PuzzleResultView.tsx`
- 拼图结果页正式图与发布弹窗正式图统一使用 `session.updatedAt + coverImageSrc` 作为刷新锚点
- 保证每次“生成并替换当前图片”回写新 session 后,结果页主图和发布预览图都会同步刷新
## 回归验证
1. `src/hooks/useResolvedAssetReadUrl.test.tsx`
- 新增 `refreshKey` 变化时重新获取签名地址并带 `_v` 参数的测试
2. `src/components/puzzle-result/PuzzleResultView.test.tsx`
- 新增 session 更新后正式图切换到新 `coverImageSrc` 的回归测试
## 后续约束
1. 任何使用 `/generated-*` 私有资源且存在“同一业务位重复生成替换”的界面,都应明确是否需要 `refreshKey`
2. 如果后端后续改成稳定对象键覆写,也不能移除前端结果页的主动刷新锚点

View File

@@ -0,0 +1,50 @@
# 拼图运行时拖动跟手延迟修复
日期:`2026-04-27`
## 1. 背景
拼图玩法运行时已经支持单块拖动、合并块整体拖动与拆分,但实测拖动时拼块会明显慢于手指或鼠标,表现为“拖着走但始终落后半拍”,尤其在移动端更明显。
本次目标只修复前端拖动跟手延迟,不改变拼图交换、合并、拆分、通关等玩法规则。
## 2. 根因
本轮定位到拖动延迟主要来自两个前端渲染问题:
1. `pointermove` 每一帧都调用 `setDragState`,导致整个 `PuzzleRuntimeShell` 和整盘拼图格子持续重渲染。
2. 拼块节点默认挂了 `transition`,拖动过程中的 `transform` 会被浏览器当成缓动动画处理,视觉上进一步放大“慢半拍”。
这两个问题叠加后,即使后端或本地运行态裁决没有延迟,前端拖动视觉仍然会滞后。
## 3. 修复口径
### 3.1 拖动视觉更新改为直写 DOM
拖动中的位移不再依赖 React state 持续驱动,而是改成:
1. `pointerdown` 只记录起点和 pointer 信息。
2. 超过拖动阈值后,只做一次 `setDraggingPieceId` 用于切换拖动态样式。
3. 后续 `pointermove` 通过 `requestAnimationFrame` 合帧,把位移直接写入目标拼块或合并块容器的 `style.transform`
这样可以避免每一帧都触发整盘 React 重渲染。
### 3.2 收紧过渡属性
拼块节点不再使用包含 `transform` 的通用 `transition`,只保留颜色、边框、阴影、透明度等非位移动画属性,避免拖动中的 transform 被浏览器插值缓动。
### 3.3 裁决边界保持不变
本次只优化拖动阶段的视觉反馈:
1. `pointerup` 后仍然走现有 `onDragPiece`
2. 单块交换、拖到合并块后拆分、合并块整体重排,继续沿用当前本地运行态规则。
3. 不新增前端本地裁决,不把玩法真相从既有运行态实现中分叉出去。
## 4. 验收标准
1. 单块拖动时拼块视觉位置应紧跟手指或鼠标,不再出现明显缓动拖尾。
2. 合并块整体拖动时,组容器应同步跟手移动。
3. 点击选中与拖动阈值判定仍保持原语义,不因为优化误触发交换。
4. 运行时现有结算弹窗、排行榜和下一关入口不受影响。
5. 定向测试覆盖拖动提交坐标的行为,并运行编码检查确保中文文档未被写坏。

View File

@@ -0,0 +1,152 @@
# RPG 运行态首段剧情启动修复记录2026-04-26
## 背景
点击 RPG 玩法测试作品进入游戏后,画布能正常显示地图、玩家和当前场景标题,但底部冒险区域为空,没有剧情文本和操作按钮。
进一步复查后确认,空白并不只是 `currentStory` 未生成。作品测试与正常进入游戏都应该走同一条链路:开局场景第一幕 -> 当前幕主 NPC 出现在对面 -> 直接开始聊天。正式运行态当时没有把第一幕的 NPC 配置合并进场景 NPC 列表,导致第一幕主 NPC 找不到。
## 问题定位
1. RPG 作品进入运行态后,`handleCharacterSelect()` 会把 `GameState.currentScene` 切到 `Story`,并设置玩家角色、场景和运行时状态。
2. 冒险面板挂载条件是 `visibleGameState.playerCharacter && visibleCurrentStory`,而 `currentStory``useRpgRuntimeStoryController` 管理。
3. 当前进入 `Story` 后没有自动请求首段剧情,导致 `visibleCurrentStory` 一直为 `null`,路由器不会挂载 `RpgRuntimePanelRouter`,底部区域因此保持空白。
4. 幕预览会显式构造 `previewScenePreset``previewEncounter``currentSceneActState`;正式运行态原先只从 `landmark.sceneNpcIds` 编译 `ScenePreset.npcs`,没有把 `sceneChapterBlueprints[].acts[].encounterNpcIds / primaryNpcId / oppositeNpcId` 合并进去,所以第一幕主角色不会稳定出现。
5. 首段普通剧情自动生成如果不避让 `currentEncounter`,会抢在 NPC 聊天流程前进入 loading导致“直接和当前幕主 NPC 聊天”的产品语义被破坏。
## 落地约束
1. 修复必须补齐真实运行态数据链路,不能只在 UI 上写静态提示文案。
2. 首段剧情仍使用现有 `generateInitialStory()``buildStoryFromResponse()` 处理,保持前端只负责表现和运行态装配。
3. 请求失败时使用现有 fallback story 生成逻辑,保证冒险面板仍有可交互选项。
4. 正常进入游戏和作品测试必须同源:优先从开局第一幕所在场景启动,并加载该幕的主 NPC / 遭遇。
5. 第一幕已有 `currentEncounter` 时,由 NPC 交互流接管首轮聊天,不再启动普通开局叙事。
## 本次修改
1. `src/hooks/rpg-runtime-story/useRpgRuntimeStoryController.ts`
- 增加 Story 场景启动 effect当存在 `playerCharacter``worldType``currentScene === 'Story'`,同时 `currentStory` 为空时,自动调用 `generateStoryForState()` 请求首段剧情。
- 增加 request key避免同一场景重复触发并发首段请求请求结束后释放 key允许失败后再次触发。
- 请求失败时设置 `aiError`,并回退到 `buildFallbackStoryForState()`
2. `src/hooks/rpg-runtime-story/useRpgRuntimeStoryController.test.tsx`
- 覆盖“进入 Story 场景且首段剧情为空时自动请求开局剧情”。
- 覆盖“已有当前幕 NPC 遭遇时不抢先请求普通开局剧情”,保证 NPC 交互流可以直接接管首轮聊天。
3. `src/hooks/rpg-session/useRpgSessionBootstrap.ts`
- 自定义世界选角进入游戏时,优先解析 `sceneChapterBlueprints` 的第一章第一幕所在场景。
- 开局场景选择优先绑定 `profile.landmarks[0]` 对应的章节/第一幕,避免章节数组顺序与场景列表顺序不一致时进入后续场景。
- 写入首个 `currentSceneActState`,让运行态背景、同幕角色和首个 encounter 使用同一套第一幕数据。
- 兼容旧作品:缺少多幕章节时,回退到第一个带场景角色的 landmark而不是停在空营地。
4. `src/data/scenePresets.ts`
- 场景编译时把多幕配置中的 `primaryNpcId``oppositeNpcId``encounterNpcIds` 合并进正式 `ScenePreset.npcs`
- 支持第一幕 NPC 只存在于多幕配置、不存在于旧 `landmark.sceneNpcIds` 的新作品数据。
5. `src/data/customWorldLibrary.ts`
- 保存档规范化时保留 `acts[].sceneId`,不再强制用章节 `sceneId` 覆盖,避免第一幕真实场景丢失。
6. `src/services/customWorldSceneActRuntime.ts`
- 场景章节匹配同时识别运行态场景 id、landmark id、章节 linked landmark 和 act scene id。
- 当前幕 NPC 集合同时包含 `primaryNpcId``oppositeNpcId``encounterNpcIds`,避免生成数据只写对面角色时被运行态漏掉。
- 正式场景遭遇的焦点 NPC 优先读取 `oppositeNpcId`,再回退到 `primaryNpcId` 和首个 encounter NPC。
7. `src/data/sceneEncounterPreviews.test.ts`
- 覆盖运行态场景 id 与 landmark id 不一致时仍能解析当前幕 NPC。
- 覆盖章节 `sceneId` 是抽象值、第一幕 `act.sceneId` 才是真实 landmark且只写 `oppositeNpcId` 时仍能解析当前幕 NPC。
- 覆盖当前幕对面 NPC 会优先成为正式场景 encounter。
8. `src/hooks/useGameFlow.customWorld.test.tsx`
- 覆盖正常进入自定义世界时会进入第一幕场景,并加载只存在于第一幕配置里的对面 NPC。
- 覆盖章节数组第一项不是开局场景时,仍以第一个 landmark 的第一幕作为开局。
## 2026-04-27 复查修正
用户复测后确认:开局场景本身可以是 `camp`,不能再把 `profile.landmarks[0]` 当作更高优先级的“真实开局”。运行态必须直接信任 `sceneChapterBlueprints[0].acts[0]`
1. `src/hooks/rpg-session/useRpgSessionBootstrap.ts`
- 自定义世界确认角色后,开局场景优先解析第一章第一幕的 `act.sceneId`,再回退到章节 `sceneId``linkedLandmarkIds`
- `custom-scene-camp` 可以作为正式开局场景进入,不再被第一个 landmark 覆盖。
- 只有缺少多幕章节时,才回退到旧作品的“带 NPC 地标 / 第一个地标”兼容逻辑。
2. `src/services/customWorldSceneActRuntime.ts`
- 当前幕解析兼容精简 profile 和旧快照,避免 `landmarks` 缺失时中断 NPC 聊天链路。
- 章节匹配继续同时识别运行态场景 id、camp id、landmark id、章节关联地标和 act scene id。
3. `src/hooks/useGameFlow.customWorld.test.tsx`
- 新增接近真实运行态的 hook 组合断言:选择世界、确认角色后进入 `custom-scene-camp` 的第一幕,当前 encounter 是 `oppositeNpcId` 对应的陆衡,并触发 NPC 主动开场聊天。
本轮修正后的产品语义:作品测试和正常进入游戏保持同源,进入游戏后以开局场景第一幕为准,直接加载当前幕对面 NPC并由 NPC 主动开启聊天。
## 验证
已执行:
```bash
npm test -- --run src/hooks/rpg-runtime-story/useRpgRuntimeStoryController.test.tsx
npm test -- --run src/hooks/useGameFlow.customWorld.test.tsx src/data/sceneEncounterPreviews.test.ts src/hooks/rpg-runtime-story/useRpgRuntimeStoryController.test.tsx
npx eslint src/data/customWorldLibrary.ts src/data/scenePresets.ts src/services/customWorldSceneActRuntime.ts src/hooks/rpg-session/useRpgSessionBootstrap.ts src/hooks/useGameFlow.customWorld.test.tsx src/data/sceneEncounterPreviews.test.ts src/hooks/rpg-runtime-story/useRpgRuntimeStoryController.ts src/hooks/rpg-runtime-story/useRpgRuntimeStoryController.test.tsx
npm run typecheck -- --pretty false
npm run check:encoding
```
局部测试、局部 ESLint、全量类型检查与编码检查均通过。后端代码未在本次任务中修改因此未执行 `npm run api-server:maincloud`
## 2026-04-27 第二轮复查修正
用户继续复测后发现两类问题:
1. 第一幕已经配置了 `oppositeNpcId`,但运行态对面角色仍可能不是第一幕主 NPC。
2. 战斗后 React 报错 `Encountered two children with the same key, monster-16`
本轮定位结论:
1. 第一幕 encounter 选择原先先走“友好 NPC 池”。如果第一幕 `oppositeNpcId` 是负好感或敌对标记角色,会被友好池过滤掉,随后可能回退到同幕其他角色,导致开局对面角色不对。
2. 负好感有限聊天原先只识别 `primaryNpcId`。当产品语义要求 `oppositeNpcId` 是第一幕正面对话角色时,敌意对面角色仍应先开聊天,而不是直接触发战斗。
3. 战斗奖励弹层按 `battleReward.id + hostileNpc.id` 作为 key同一场战斗击败两个同 preset 怪物时,两个条目都会是 `monster-16`,从而触发 React 重复 key 报错。
本轮修改:
1. `src/data/sceneEncounterPreviews.ts`
- 新增当前幕专用 NPC 池:只要角色属于 `primaryNpcId / oppositeNpcId / encounterNpcIds`,即使是负好感或敌对标记,也允许进入当前幕 encounter 候选。
- 只有非幕级随机遭遇继续使用原友好 NPC 池,避免误改普通野外战斗规则。
2. `src/services/customWorldSceneActRuntime.ts`
- 负好感有限聊天同时识别 `primaryNpcId``oppositeNpcId`
- 当前幕解析优先尊重 `currentSceneActState.chapterId/currentActId`,再回退到场景匹配,避免同一场景多章节时抢错当前幕。
3. `src/hooks/rpg-runtime-story/storyChoiceRuntime.ts`
- 战斗奖励中的 `defeatedHostileNpcs` 增加 `renderKey`,包含怪物 id、名称、位置与序号。
4. `src/components/rpg-runtime-panels/RpgAdventurePanelOverlays.tsx`
- 战斗奖励击败列表优先使用 `renderKey` 作为 React key。
本轮补充测试:
1. `src/data/sceneEncounterPreviews.test.ts`
- 覆盖第一幕 `oppositeNpcId` 是敌意角色时,仍作为正式 encounter且不会自动进入战斗。
2. `src/hooks/rpg-runtime-story/storyChoiceRuntime.test.ts`
- 覆盖同一场战斗击败两个 `monster-16` 时,奖励摘要生成唯一 `renderKey`
验证命令:
```bash
npm test -- --run src/data/sceneEncounterPreviews.test.ts src/hooks/rpg-runtime-story/storyChoiceRuntime.test.ts src/hooks/useGameFlow.customWorld.test.tsx
npx eslint src/data/sceneEncounterPreviews.ts src/data/sceneEncounterPreviews.test.ts src/services/customWorldSceneActRuntime.ts src/hooks/rpg-runtime-story/storyChoiceRuntime.ts src/hooks/rpg-runtime-story/storyChoiceRuntime.test.ts src/hooks/rpg-runtime-story/uiTypes.ts src/components/rpg-runtime-panels/RpgAdventurePanelOverlays.tsx
```
以上局部测试与局部 ESLint 已通过。后端代码未在本轮修改中触碰,因此不需要执行 `npm run api-server:maincloud`
## 2026-04-27 第三轮复查修正
用户再次复测后确认,作品测试选完角色仍没有稳定等价于“开局场景第一幕的幕测试”。本轮重新沿真实入口链路复查:作品测试结果页进入世界后,会先进入角色选择页;真正的开局状态是在 `handleCharacterSelect()` 中生成。上一轮主要修在场景遭遇预览层,仍让自定义世界选角后的 `currentEncounter` 先置空,再由 `ensureSceneEncounterPreview()` 推断第一幕 NPC因此一旦候选池、场景编译或角色敌意标记出现偏差开局对面角色仍可能漂移。
本轮修正:
1. `src/hooks/rpg-session/useRpgSessionBootstrap.ts`
- 在选角确认阶段显式解析 `sceneChapterBlueprints[0].acts[0]`,将作品测试开局直接绑定到第一章第一幕。
- 第一幕 encounter 选择顺序固定为 `oppositeNpcId -> primaryNpcId -> encounterNpcIds`,并跳过当前玩家角色。
- 优先从当前场景编译出的 `ScenePreset.npcs` 构造 encounter如果第一幕角色只存在于 `storyNpcs/playableNpcs`,也会直接从作品角色配置构造 encounter不再依赖预览兜底。
- 为构造出的开局 encounter 同步初始化 `npcStates`,保证后续 NPC 主动开场聊天可以读取正确关系状态。
2. `src/hooks/useGameFlow.customWorld.test.tsx`
- 增加断言:选角后当前 encounter 必须是第一幕 `oppositeNpcId` 对应的陆衡,不能回退到 `primaryNpcId`
本轮语义收敛为:作品测试选择角色完成后,不再只是“进入自定义世界后生成一个场景遭遇”,而是直接加载开局场景第一幕的运行态快照;对面角色由第一幕 `oppositeNpcId` 决定,并由 NPC 主动开启聊天。
验证命令:
```bash
npm test -- --run src/hooks/useGameFlow.customWorld.test.tsx src/data/sceneEncounterPreviews.test.ts src/hooks/rpg-runtime-story/storyChoiceRuntime.test.ts
npx eslint src/hooks/rpg-session/useRpgSessionBootstrap.ts src/hooks/useGameFlow.customWorld.test.tsx src/data/sceneEncounterPreviews.ts src/services/customWorldSceneActRuntime.ts src/hooks/rpg-runtime-story/storyChoiceRuntime.ts src/hooks/rpg-runtime-story/storyChoiceRuntime.test.ts src/hooks/rpg-runtime-story/uiTypes.ts src/components/rpg-runtime-panels/RpgAdventurePanelOverlays.tsx
npm run typecheck -- --pretty false
```
以上局部测试、局部 ESLint 与全量类型检查已通过。后端代码未在本轮修改中触碰,因此仍不需要执行 `npm run api-server:maincloud`

View File

@@ -0,0 +1,41 @@
# RPG 幕场景后排角色运行时展示修复2026-04-26
## 背景
多幕场景编辑器允许每一幕配置 `3` 个角色槽位:第一槽位是主角色,后两个槽位是后排同幕角色。问题出在配置已经写入 `sceneChapterBlueprints[*].acts[*].encounterNpcIds` 后,幕预览只稳定展示第一槽位主角色,后排两个角色没有进入真实游戏画布。
## 根因
1. 幕预览启动时只把 `encounterNpcIds[0]` 转成 `currentEncounter`
2. `GameCanvasEntityLayer` 原先只渲染玩家、同伴、战斗敌人和单个 `currentEncounter`,没有“当前幕环境角色”层。
3. 正式自定义世界运行时场景 id 使用 `custom-scene-landmark-*`,而 `sceneChapterBlueprints` 常保存原始 `landmark.id`,导致部分正式游戏场景不能稳定命中当前幕蓝图。
4. 场景相遇逻辑虽会读取当前幕 NPC 池,但会从池中随机选角色,可能让后排角色顶替主角色成为正式相遇对象。
## 修复口径
1. `customWorldSceneActRuntime` 增加自定义世界运行时场景别名匹配:
- `custom-scene-camp`
- `profile.camp.id`
- `custom-scene-landmark-{index}`
- `profile.landmarks[index].id`
2. `GameCanvasRuntime` 根据当前活跃幕的 `encounterNpcIds`,从 `currentScenePreset.npcs` 中解析除 `currentEncounter` 外的后排角色,并传给画布实体层。
3. `GameCanvasEntityLayer` 新增 `sceneActAmbientEncounters` 渲染分支:
- 仅在非战斗态展示;
- 使用同一列后排站位,上下错开;
- 不抢占 `currentEncounter`,因此聊天、战斗、有限聊天仍由主角色驱动;
- 后排角色仍可点击打开详情。
4. `sceneEncounterPreviews` 在当前幕存在 `primaryNpcId` 时,正式相遇优先选择主角色,后排角色保留为同幕可见实体。
## 正式游戏检查结论
后端草稿与发布档案中的 `encounterNpcIds` 没有丢失,本次问题主要在前端运行时装配与画布展示层。修复后:
1. 幕预览会展示主角色和后排两个角色。
2. 正式游戏进入对应自定义世界场景时,可通过运行时场景 id 命中原始幕蓝图。
3. 正式游戏的当前交互目标仍是 `primaryNpcId`,后排两个角色按当前幕环境实体展示。
## 回归
1. `GameCanvasEntityLayer.test.tsx` 覆盖后排两个 `sceneActAmbientEncounters` 与主角色同时渲染。
2. `sceneEncounterPreviews.test.ts` 覆盖运行时场景 id 对原始 landmark id 的幕蓝图匹配。
3. `sceneEncounterPreviews.test.ts` 覆盖正式相遇优先选择当前幕 `primaryNpcId`

View File

@@ -0,0 +1,20 @@
# 运行时预览与测试作品存档隔离2026-04-26
## 背景
幕预览和测试作品用于创作者检查玩法表现,不能被当作玩家正式游玩记录。若这类运行时复用正式 RPG 壳、story action 或 snapshot 接口,必须在进入个人存档页、游玩统计、作品游玩历史前被过滤。
## 落地约束
1. 前端预览态 `GameState` 必须写入 `runtimeMode: "preview"``runtimeMode: "test"`
2. 前端可同步写入 `runtimePersistenceDisabled: true` 作为更明确的禁存标记。
3. `useRpgSessionPersistence` 自动存档必须跳过上述预览/测试态。
4. runtime story 网关仍提交带禁存标记的 `snapshot`,避免服务端退回读取用户正式快照;服务端必须按禁存标记返回临时响应而不落库。
5. SpacetimeDB projection 层必须兜底识别上述标记:即便有旧入口误写 `runtime_snapshot`,也不刷新 `profile_save_archive``profile_played_world``profile_dashboard_state` 和钱包流水。
## 当前实现
1. 幕预览运行时在启动游戏壳时写入 `runtimeMode: "preview"``runtimePersistenceDisabled: true`
2. 前端自动存档会跳过预览/测试态。
3. runtime story 接口收到预览/测试快照时,只构造本次响应所需的临时 snapshot不写入 `runtime_snapshot`
4. `server-rs/crates/spacetime-module/src/runtime/profile.rs` 在 profile projection 同步前统一短路预览/测试快照。

View File

@@ -43,3 +43,17 @@
1. `custom_world_rpg_draft_prompts.rs` 只作为兼容 re-export后续不要在该文件新增提示词正文。
2. `runtime_story/compat/ai.rs` 只负责读取状态、调用 LLM 和组装返回,不再内联 NPC 对话或剧情导演提示词。
3. 后续所有 Agent 共创聊天、运行时角色聊天的提示词调整统一进入 `src/prompt/`
## 6. 运行时 NPC 聊天 Prompt 归并
2026-04-26 追加收口:
1. 删除 `server-rs/crates/api-server/src/runtime_chat_prompt.rs` 独立提示词脚本,避免 `runtime_chat` 相关提示词散落在 `src/` 根目录。
2. `server-rs/crates/api-server/src/prompt/runtime_chat.rs` 统一承接:
- 运行时剧情导演 system prompt 与 user prompt。
- NPC 对话导演 system prompt 与 user prompt。
- NPC 单轮聊天回复 system prompt 与 user prompt。
- NPC 下一轮 `suggestions` / `functionSuggestions` 的 JSON 输出约束。
- LLM 不可用时的聊天 reply、普通 choice、function choice 兜底生成。
3. `server-rs/crates/api-server/src/runtime_chat.rs` 只保留 Axum SSE、LLM 调用、解析、好感变化、结束聊天判断等流程编排,不再直接承载提示词正文或 choice 文案兜底。
4. 后续调整聊天 choice 语气、候选数量、`functionOptions` 描述方式、敌对聊天收束策略时,优先修改 `prompt/runtime_chat.rs`

View File

@@ -43,6 +43,7 @@ export type PuzzleAgentActionRequest =
| {
action: 'generate_puzzle_images';
promptText?: string | null;
referenceImageSrc?: string | null;
candidateCount?: number;
}
| {

View File

@@ -20,6 +20,13 @@ export interface PuzzleMergedGroupState {
occupiedCells: PuzzleCellPosition[];
}
export interface PuzzleLeaderboardEntry {
rank: number;
nickname: string;
elapsedMs: number;
isCurrentPlayer?: boolean;
}
export interface PuzzleBoardSnapshot {
rows: number;
cols: number;
@@ -40,6 +47,10 @@ export interface PuzzleRuntimeLevelSnapshot {
coverImageSrc: string | null;
board: PuzzleBoardSnapshot;
status: 'playing' | 'cleared';
startedAtMs: number;
clearedAtMs: number | null;
elapsedMs: number | null;
leaderboardEntries: PuzzleLeaderboardEntry[];
}
export interface PuzzleRunSnapshot {
@@ -52,6 +63,7 @@ export interface PuzzleRunSnapshot {
previousLevelTags: string[];
currentLevel: PuzzleRuntimeLevelSnapshot | null;
recommendedNextProfileId: string | null;
leaderboardEntries: PuzzleLeaderboardEntry[];
}
export interface StartPuzzleRunRequest {

View File

@@ -68,7 +68,7 @@ use crate::{
proxy_generated_animations, proxy_generated_big_fish_assets,
proxy_generated_character_drafts, proxy_generated_characters,
proxy_generated_custom_world_covers, proxy_generated_custom_world_scenes,
proxy_generated_qwen_sprites,
proxy_generated_puzzle_assets, proxy_generated_qwen_sprites,
},
llm::proxy_llm_chat_completions,
login_options::auth_login_options,
@@ -188,6 +188,10 @@ pub fn build_router(state: AppState) -> Router {
"/generated-big-fish-assets/{*path}",
get(proxy_generated_big_fish_assets),
)
.route(
"/generated-puzzle-assets/{*path}",
get(proxy_generated_puzzle_assets),
)
.route(
"/generated-custom-world-scenes/{*path}",
get(proxy_generated_custom_world_scenes),

View File

@@ -27,6 +27,13 @@ use crate::{
state::AppState,
};
// 历史素材类型需要与 SpacetimeDB 侧白名单保持同一口径,避免新增素材类型时 HTTP 门面漏同步。
const SUPPORTED_ASSET_HISTORY_KINDS: [&str; 3] = [
"character_visual",
"scene_image",
"puzzle_cover_image",
];
pub async fn create_direct_upload_ticket(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
@@ -118,11 +125,11 @@ pub async fn get_asset_history(
Query(query): Query<AssetHistoryQuery>,
) -> Result<Json<Value>, AppError> {
let asset_kind = query.kind.trim().to_string();
if asset_kind != "character_visual" && asset_kind != "scene_image" {
if !is_supported_asset_history_kind(asset_kind.as_str()) {
return Err(
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"field": "kind",
"message": "历史素材类型只支持 character_visual 或 scene_image",
"message": supported_asset_history_kind_message(),
})),
);
}
@@ -288,6 +295,17 @@ fn format_asset_owner_label(owner_user_id: Option<&str>) -> String {
format!("账号 {owner_user_id}")
}
fn is_supported_asset_history_kind(asset_kind: &str) -> bool {
SUPPORTED_ASSET_HISTORY_KINDS.contains(&asset_kind)
}
fn supported_asset_history_kind_message() -> String {
format!(
"历史素材类型只支持 {}",
SUPPORTED_ASSET_HISTORY_KINDS.join("")
)
}
async fn build_confirm_asset_object_upsert_input(
oss_client: &platform_oss::OssClient,
payload: ConfirmAssetObjectRequest,
@@ -457,6 +475,22 @@ mod tests {
type HmacSha1 = Hmac<Sha1>;
#[test]
fn asset_history_kind_support_includes_puzzle_cover_image() {
assert!(super::is_supported_asset_history_kind("character_visual"));
assert!(super::is_supported_asset_history_kind("scene_image"));
assert!(super::is_supported_asset_history_kind("puzzle_cover_image"));
assert!(!super::is_supported_asset_history_kind("puzzle_preview_image"));
}
#[test]
fn asset_history_kind_message_lists_all_supported_kinds() {
assert_eq!(
super::supported_asset_history_kind_message(),
"历史素材类型只支持 character_visual、scene_image、puzzle_cover_image"
);
}
#[tokio::test]
async fn direct_upload_ticket_returns_service_unavailable_when_oss_missing() {
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));

View File

@@ -261,6 +261,7 @@ pub async fn submit_big_fish_message(
llm_client: state.llm_client(),
session: &submitted_session,
quick_fill_requested: payload.quick_fill_requested.unwrap_or(false),
enable_web_search: state.config.creation_agent_llm_web_search_enabled,
},
move |text| {
draft_sink.persist_visible_text_async(text);
@@ -373,6 +374,7 @@ pub async fn stream_big_fish_message(
llm_client: state.llm_client(),
session: &submitted_session,
quick_fill_requested,
enable_web_search: state.config.creation_agent_llm_web_search_enabled,
},
move |text| {
let _ = reply_tx.send(text.to_string());

View File

@@ -19,6 +19,7 @@ pub(crate) struct BigFishAgentTurnRequest<'a> {
pub llm_client: Option<&'a LlmClient>,
pub session: &'a BigFishSessionRecord,
pub quick_fill_requested: bool,
pub enable_web_search: bool,
}
#[derive(Clone, Debug)]
@@ -122,6 +123,7 @@ where
request.llm_client,
format!("{BIG_FISH_AGENT_SYSTEM_PROMPT}\n\n{prompt}"),
"请按约定输出这一轮的 JSON。",
request.enable_web_search,
CreationAgentLlmTurnErrorMessages {
model_unavailable: "当前模型不可用,请稍后重试。",
generation_failed: "大鱼吃小鱼聊天生成失败,请稍后重试。",

View File

@@ -81,6 +81,7 @@ pub struct AppConfig {
pub llm_max_retries: u32,
pub llm_retry_backoff_ms: u64,
pub rpg_llm_web_search_enabled: bool,
pub creation_agent_llm_web_search_enabled: bool,
pub dashscope_base_url: String,
pub dashscope_api_key: Option<String>,
pub dashscope_scene_image_model: String,
@@ -170,6 +171,7 @@ impl Default for AppConfig {
llm_max_retries: DEFAULT_MAX_RETRIES,
llm_retry_backoff_ms: DEFAULT_RETRY_BACKOFF_MS,
rpg_llm_web_search_enabled: true,
creation_agent_llm_web_search_enabled: true,
dashscope_base_url: "https://dashscope.aliyuncs.com/api/v1".to_string(),
dashscope_api_key: None,
dashscope_scene_image_model: "wan2.2-t2i-flash".to_string(),
@@ -475,6 +477,13 @@ impl AppConfig {
config.rpg_llm_web_search_enabled = rpg_llm_web_search_enabled;
}
if let Some(creation_agent_llm_web_search_enabled) = read_first_bool_env(&[
"GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED",
"CREATION_AGENT_LLM_WEB_SEARCH_ENABLED",
]) {
config.creation_agent_llm_web_search_enabled = creation_agent_llm_web_search_enabled;
}
if let Some(dashscope_base_url) = read_first_non_empty_env(&["DASHSCOPE_BASE_URL"]) {
config.dashscope_base_url = dashscope_base_url;
}
@@ -843,4 +852,24 @@ mod tests {
std::env::remove_var("GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED");
}
}
#[test]
fn from_env_reads_creation_agent_llm_web_search_switch() {
let _guard = ENV_LOCK
.get_or_init(|| Mutex::new(()))
.lock()
.expect("env lock should not poison");
unsafe {
std::env::remove_var("GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED");
std::env::set_var("GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED", "false");
}
let config = AppConfig::from_env();
assert!(!config.creation_agent_llm_web_search_enabled);
unsafe {
std::env::remove_var("GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED");
}
}
}

View File

@@ -21,6 +21,7 @@ pub(crate) async fn stream_creation_agent_json_turn<F, E>(
llm_client: Option<&LlmClient>,
system_prompt: String,
user_prompt: impl Into<String>,
enable_web_search: bool,
messages: CreationAgentLlmTurnErrorMessages<'_>,
mut on_reply_update: F,
build_error: impl Fn(String) -> E,
@@ -33,10 +34,7 @@ where
let mut latest_reply_text = String::new();
let response = llm_client
.stream_text(
LlmTextRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt.into()),
]),
build_creation_agent_llm_request(system_prompt, user_prompt.into(), enable_web_search),
|delta: &LlmStreamDelta| {
if let Some(reply_progress) =
extract_reply_text_from_partial_json(delta.accumulated_text.as_str())
@@ -61,6 +59,19 @@ where
Ok(CreationAgentJsonTurnOutput { parsed })
}
fn build_creation_agent_llm_request(
system_prompt: String,
user_prompt: String,
enable_web_search: bool,
) -> LlmTextRequest {
// 创作 Agent 是否联网由 api-server 配置集中传入,避免各玩法各自散落默认值。
LlmTextRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_web_search(enable_web_search)
}
pub(crate) async fn request_creation_agent_json_turn<E>(
llm_client: &LlmClient,
system_prompt: String,
@@ -149,7 +160,10 @@ fn read_reply_text(parsed: &JsonValue) -> Option<String> {
#[cfg(test)]
mod tests {
use super::{extract_reply_text_from_partial_json, parse_json_response_text};
use super::{
build_creation_agent_llm_request, extract_reply_text_from_partial_json,
parse_json_response_text,
};
#[test]
fn extracts_reply_text_from_partial_json_with_chinese_text() {
@@ -167,4 +181,13 @@ mod tests {
assert_eq!(parsed["replyText"].as_str(), Some(""));
}
#[test]
fn builds_stream_request_with_web_search_when_enabled() {
let request =
build_creation_agent_llm_request("系统提示".to_string(), "用户提示".to_string(), true);
assert!(request.enable_web_search);
assert_eq!(request.messages.len(), 2);
}
}

View File

@@ -759,6 +759,7 @@ pub async fn submit_custom_world_agent_message(
session: &session,
quick_fill_requested: payload.quick_fill_requested.unwrap_or(false),
focus_card_id: payload.focus_card_id.clone(),
enable_web_search: state.config.creation_agent_llm_web_search_enabled,
},
move |text| {
draft_sink.persist_visible_text_async(text);
@@ -910,6 +911,7 @@ pub async fn stream_custom_world_agent_message(
session: &session,
quick_fill_requested,
focus_card_id,
enable_web_search: state.config.creation_agent_llm_web_search_enabled,
},
move |text| {
let _ = reply_tx.send(text.to_string());

View File

@@ -28,6 +28,7 @@ pub(crate) struct CustomWorldAgentTurnRequest<'a> {
pub session: &'a CustomWorldAgentSessionRecord,
pub quick_fill_requested: bool,
pub focus_card_id: Option<String>,
pub enable_web_search: bool,
}
#[derive(Clone, Debug)]
@@ -214,6 +215,7 @@ where
request.session.progress_percent,
request.quick_fill_requested,
&current_anchor_content,
request.enable_web_search,
on_reply_update,
)
.await?;
@@ -476,6 +478,7 @@ async fn stream_single_turn<F>(
progress_percent: u32,
quick_fill_requested: bool,
current_anchor_content: &EightAnchorContent,
enable_web_search: bool,
on_reply_update: F,
) -> Result<SingleTurnModelOutput, CustomWorldTurnError>
where
@@ -505,6 +508,7 @@ where
Some(llm_client),
prompt,
"请按约定输出这一轮的 JSON。",
enable_web_search,
CreationAgentLlmTurnErrorMessages {
model_unavailable: "当前模型不可用,请稍后重试。",
generation_failed: "这一轮设定生成失败,请稍后重试。",

View File

@@ -39,6 +39,13 @@ pub async fn proxy_generated_big_fish_assets(
proxy_legacy_generated_asset(state, LegacyAssetPrefix::BigFishAssets, path).await
}
pub async fn proxy_generated_puzzle_assets(
State(state): State<AppState>,
Path(path): Path<String>,
) -> Response {
proxy_legacy_generated_asset(state, LegacyAssetPrefix::PuzzleAssets, path).await
}
pub async fn proxy_generated_custom_world_scenes(
State(state): State<AppState>,
Path(path): Path<String>,

View File

@@ -46,7 +46,6 @@ mod request_context;
mod response_headers;
mod runtime_browse_history;
mod runtime_chat;
mod runtime_chat_prompt;
mod runtime_inventory;
mod runtime_profile;
mod runtime_save;

View File

@@ -2,5 +2,6 @@ pub(crate) mod agent_chat;
pub(crate) mod character_animation;
pub(crate) mod character_visual;
pub(crate) mod foundation_draft;
pub(crate) mod puzzle_image;
pub(crate) mod runtime_chat;
pub(crate) mod scene_background;

View File

@@ -0,0 +1,44 @@
/// 拼图图片生成的默认反向提示词。
///
/// 这里单独收口拼图图片提示词,避免图片生成链路、候选图持久化和 DashScope 请求编排
/// 混在同一个脚本里,后续调画风或资产约束时只需要改这一处。
pub(crate) const PUZZLE_DEFAULT_NEGATIVE_PROMPT: &str =
"低清晰度,低质量,文字水印,畸形构图,过度模糊,重复肢体,画面脏污";
/// 根据拼图关卡名和创作者输入构造最终发给图片模型的提示词。
pub(crate) fn build_puzzle_image_prompt(level_name: &str, prompt: &str) -> String {
format!(
concat!(
"请生成一张适合正方形拼图关卡的高清插画。",
"关卡名:{level_name}。",
"画面主体:{prompt}。",
"画面要求1:1 正方形画布,适配 3x3 或 4x4 拼图切块,",
"主体要清晰集中,前中后景层次明确,局部细节丰富但不要杂乱,",
"避免文字、水印、边框和 UI 元素。"
),
level_name = level_name,
prompt = prompt,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn build_puzzle_image_prompt_keeps_puzzle_asset_constraints() {
let prompt = build_puzzle_image_prompt("雨夜神庙", "猫咪在发光遗迹前寻找线索");
assert!(prompt.contains("雨夜神庙"));
assert!(prompt.contains("猫咪在发光遗迹前寻找线索"));
assert!(prompt.contains("正方形拼图关卡"));
assert!(prompt.contains("3x3 或 4x4"));
assert!(prompt.contains("避免文字、水印、边框和 UI 元素"));
}
#[test]
fn default_negative_prompt_blocks_text_and_low_quality_assets() {
assert!(PUZZLE_DEFAULT_NEGATIVE_PROMPT.contains("低清晰度"));
assert!(PUZZLE_DEFAULT_NEGATIVE_PROMPT.contains("文字水印"));
}
}

View File

@@ -112,3 +112,726 @@ pub(crate) fn build_runtime_reasoned_story_user_prompt(
"请基于以下运行时状态,为这一轮战斗结算生成一段 120 字以内的结果叙事,并自然引出下一组选项。\n{state_prompt}"
)
}
pub(crate) const NPC_CHAT_TURN_REPLY_SYSTEM_PROMPT: &str = r#"你是角色扮演 RPG 里的当前 NPC。
你只输出这名 NPC 此刻会对玩家说的一轮回复。
只输出纯中文口语回复正文不要输出角色名、引号、旁白、动作描写、Markdown、JSON 或解释。
- 如果这是第一次真正接触中的首轮回复,第一句必须先用自然招呼或开场判断起手,不能写成第三人称占位旁白。
回复长度控制在 1 到 3 句,必须紧接玩家刚说的话,自然推进气氛、情报或关系。"#;
pub(crate) const NPC_CHAT_TURN_SUGGESTION_SYSTEM_PROMPT: &str = r#"你要为 RPG NPC 聊天生成下一步候选,并判断敌对聊天是否已经收束。
只输出 JSON不要输出 Markdown 或解释。
JSON 结构:
{"shouldEndChat":false,"terminationReason":null,"suggestions":["温和共情台词","冷静追问台词","施压质疑台词"],"functionSuggestions":[{"functionId":"...","actionText":"玩家动作文本"}]}
- suggestions 是玩家下一轮可直接说出口的中文短句,每条 20 字以内;三条必须按顺序导向不同氛围和好感结果。
- suggestions 第 1 条温和共情,通常让气氛缓和、好感上升;第 2 条冷静追问或试探,通常保持中性但推进情报;第 3 条施压、质疑或立场冲突,通常让气氛变紧、好感下降或付出代价。
- functionSuggestions 只能从用户提示提供的 functionOptions 中挑选,不要发明 functionId。
- functionSuggestions 的 actionText 必须像玩家可点击动作,不暴露 functionId不写规则说明。
- 非敌对聊天 shouldEndChat 必须为 false。
- 敌对聊天可以随时 shouldEndChat=true且敌对 NPC 更偏好在话不投机、被威胁、玩家退出、底线被触碰时结束聊天。"#;
#[derive(Debug)]
pub(crate) struct NpcChatTurnPromptInput<'a> {
pub world_type: &'a str,
pub character: &'a Value,
pub encounter: &'a Value,
pub monsters: &'a [Value],
pub history: &'a [Value],
pub context: &'a Value,
pub conversation_history: &'a [Value],
pub dialogue: &'a [Value],
pub combat_context: Option<&'a Value>,
pub player_message: &'a str,
pub npc_state: &'a Value,
pub npc_initiates_conversation: bool,
pub chat_directive: Option<&'a Value>,
}
pub(crate) fn build_npc_chat_turn_reply_prompt(payload: &NpcChatTurnPromptInput<'_>) -> String {
let encounter = describe_encounter(payload.encounter);
let context = as_record(payload.context);
let npc_state = as_record(payload.npc_state);
let chat_directive = payload.chat_directive.and_then(as_record);
let conversation_history = if !payload.conversation_history.is_empty() {
payload.conversation_history
} else {
payload.dialogue
};
let opening_camp_background =
context.and_then(|record| read_string(record.get("openingCampBackground")));
let opening_camp_dialogue =
context.and_then(|record| read_string(record.get("openingCampDialogue")));
let allowed_topics = context
.and_then(|record| record.get("encounterAllowedTopics"))
.map(read_string_array)
.unwrap_or_default();
let blocked_topics = context
.and_then(|record| record.get("encounterBlockedTopics"))
.map(read_string_array)
.unwrap_or_default();
let is_first_meaningful_contact = context
.and_then(|record| read_bool(record.get("isFirstMeaningfulContact")))
.unwrap_or(false);
let affinity = npc_state
.and_then(|record| read_number(record.get("affinity")))
.unwrap_or(0.0);
let chatted_count = npc_state
.and_then(|record| read_number(record.get("chattedCount")))
.unwrap_or(0.0);
let limit_reason = chat_directive.and_then(|record| read_string(record.get("limitReason")));
let turn_limit = chat_directive
.and_then(|record| read_number(record.get("turnLimit")))
.unwrap_or(0.0)
.max(0.0);
let remaining_turns = chat_directive
.and_then(|record| read_number(record.get("remainingTurns")))
.unwrap_or(0.0)
.max(0.0);
let closing_mode = chat_directive.and_then(|record| read_string(record.get("closingMode")));
let is_limited_negative_affinity_chat =
limit_reason.as_deref() == Some("negative_affinity") && turn_limit > 0.0;
let is_hostile_model_chat = chat_directive
.and_then(|record| read_string(record.get("terminationMode")))
.as_deref()
== Some("hostile_model")
|| chat_directive
.and_then(|record| read_bool(record.get("isHostileChat")))
.unwrap_or(false);
let is_player_exit_turn = chat_directive
.and_then(|record| read_string(record.get("terminationReason")))
.as_deref()
== Some("player_exit");
let is_foreshadow_close_turn = closing_mode.as_deref() == Some("foreshadow_close")
|| chat_directive
.and_then(|record| read_bool(record.get("forceExitAfterTurn")))
.unwrap_or(false);
let has_npc_reply_in_history = conversation_history.iter().any(|item| {
as_record(item)
.and_then(|turn| read_string(turn.get("speaker")))
.is_some_and(|speaker| speaker == "npc")
});
let is_first_npc_spoken_turn =
is_first_meaningful_contact && !has_npc_reply_in_history && chatted_count <= 0.0;
let first_contact_relation_stance = describe_first_contact_relation_stance(
context.and_then(|record| record.get("firstContactRelationStance")),
);
let combat_context_block = payload.combat_context.and_then(describe_npc_combat_context);
[
Some(build_npc_dialogue_prompt_base(payload)),
Some(describe_npc_conversation_history(
conversation_history,
encounter.npc_name.as_str(),
)),
combat_context_block,
opening_camp_background.map(|text| format!("营地开场背景:{text}")),
opening_camp_dialogue.map(|text| format!("刚刚发生的第一段对话:{text}")),
Some(format!("当前关系值:{}", format_prompt_number(affinity))),
Some(format!("已聊天轮次:{}", format_prompt_number(chatted_count))),
if is_first_npc_spoken_turn {
Some(format!(
"当前接触阶段:第一次真正接触({first_contact_relation_stance})。这是这次聊天里 {} 第一次真正对玩家开口。",
encounter.npc_name
))
} else {
None
},
if is_first_npc_spoken_turn {
Some("第一句必须先用一句自然招呼或开场判断起手,再顺着玩家刚刚的话往下接。".to_string())
} else {
None
},
if is_first_npc_spoken_turn {
Some("不要写成“某人看着你,像是在等你把话接下去”这类第三人称占位旁白,也不要把整轮写成设定说明。".to_string())
} else {
None
},
if payload.npc_initiates_conversation {
Some(format!(
"当前要求:这是 {} 主动开口的第一句,不要假装玩家已经先说过话。",
encounter.npc_name
))
} else {
None
},
if allowed_topics.is_empty() {
None
} else {
Some(format!("当前更适合先谈:{}", allowed_topics.join("")))
},
if blocked_topics.is_empty() {
None
} else {
Some(format!("当前避免直接说破:{}", blocked_topics.join("")))
},
if is_limited_negative_affinity_chat {
Some(format!(
"当前相遇属于负好感主角色有限聊天,本次总上限 {} 轮。",
format_prompt_number(turn_limit)
))
} else {
None
},
if is_hostile_model_chat {
Some("当前是敌对或负好感聊天。对方不受固定回合限制,但随时可能不耐烦、结束谈话并把局势推向战斗或驱逐。".to_string())
} else {
None
},
if is_hostile_model_chat {
Some("敌对角色更偏好短促、戒备、带威胁的回应;如果玩家逼问、挑衅、退场或话题触到底线,回复应自然收束到对峙前一刻。".to_string())
} else {
None
},
if is_player_exit_turn {
Some("玩家正在主动结束这轮聊天。请对这个收束动作作出回应,并留下自然的下一步入口。回复后聊天会结束。".to_string())
} else {
None
},
if is_limited_negative_affinity_chat {
Some(format!(
"在你回复完这一轮之后,还剩 {} 轮可以继续聊。",
format_prompt_number(remaining_turns)
))
} else {
None
},
if is_limited_negative_affinity_chat && !is_foreshadow_close_turn {
Some("语气可以戒备、冷淡、带刺,但不要立刻转成开战,也不要把对话硬掐死。".to_string())
} else {
None
},
if is_foreshadow_close_turn {
Some("这是最后一轮回复。必须带有收束感,但不能只用“别问了”“滚开”之类的话把聊天粗暴截断。".to_string())
} else {
None
},
if is_foreshadow_close_turn {
Some("最后一轮必须抛出能推动后续剧情的明确铺垫,例如威胁、线索、条件、去处、人物、未说完的真相或下一步悬念。".to_string())
} else {
None
},
if is_foreshadow_close_turn {
Some("回复后这轮聊天会结束,所以不要邀请继续闲聊,也不要直接宣布已经开战。".to_string())
} else {
None
},
if payload.npc_initiates_conversation {
Some("玩家此刻还没有先说话,请直接写 NPC 主动开口时会说的第一轮回复。".to_string())
} else {
Some(format!("玩家刚刚说:{}", payload.player_message.trim()))
},
if payload.npc_initiates_conversation {
Some(format!(
"现在请只写 {} 主动开口时会说的话。",
encounter.npc_name
))
} else {
Some(format!(
"现在请只写 {} 这一轮会回复玩家的话。",
encounter.npc_name
))
},
]
.into_iter()
.flatten()
.filter(|text| !text.trim().is_empty())
.collect::<Vec<_>>()
.join("\n\n")
}
pub(crate) fn build_npc_chat_turn_suggestion_prompt(
payload: &NpcChatTurnPromptInput<'_>,
npc_reply: &str,
) -> String {
let encounter = describe_encounter(payload.encounter);
let conversation_history = if !payload.conversation_history.is_empty() {
payload.conversation_history
} else {
payload.dialogue
};
let combat_context_block = payload.combat_context.and_then(describe_npc_combat_context);
let chat_directive = payload.chat_directive.and_then(as_record);
let is_hostile_model_chat = chat_directive
.and_then(|record| read_string(record.get("terminationMode")))
.as_deref()
== Some("hostile_model")
|| chat_directive
.and_then(|record| read_bool(record.get("isHostileChat")))
.unwrap_or(false);
let is_player_exit_turn = chat_directive
.and_then(|record| read_string(record.get("terminationReason")))
.as_deref()
== Some("player_exit");
let function_options_block = chat_directive
.and_then(|record| record.get("functionOptions"))
.map(describe_function_options)
.filter(|text| !text.trim().is_empty());
[
Some(build_npc_dialogue_prompt_base(payload)),
Some(describe_npc_conversation_history(
conversation_history,
encounter.npc_name.as_str(),
)),
combat_context_block,
function_options_block,
if payload.npc_initiates_conversation {
Some("玩家尚未先开口,这一轮是 NPC 主动发起聊天。".to_string())
} else {
Some(format!("玩家刚刚说:{}", payload.player_message))
},
Some(format!("NPC 刚刚回复:{npc_reply}")),
if is_hostile_model_chat {
Some("这是敌对或负好感聊天。你需要判断这轮是否应该结束聊天;敌对角色更偏好随时终止并转入对峙。".to_string())
} else {
Some("这是非敌对聊天shouldEndChat 必须为 false。".to_string())
},
if is_player_exit_turn {
Some("玩家已经选择结束聊天shouldEndChat 必须为 trueterminationReason 必须为 player_exit。".to_string())
} else {
None
},
Some("suggestions 必须按顺序生成三种明显不同的玩家台词:温和共情、冷静追问或试探、施压质疑;不要给出同一种态度的近义句。".to_string()),
Some("functionSuggestions 从 functionOptions 中挑可触发动作并改写 actionText。".to_string()),
Some("只输出 JSON{\"shouldEndChat\":false,\"terminationReason\":null,\"suggestions\":[\"...\"],\"functionSuggestions\":[{\"functionId\":\"...\",\"actionText\":\"...\"}]}".to_string()),
]
.into_iter()
.flatten()
.filter(|text| !text.trim().is_empty())
.collect::<Vec<_>>()
.join("\n\n")
}
pub(crate) fn build_deterministic_npc_reply(
npc_name: &str,
player_message: &str,
npc_initiates_conversation: bool,
) -> String {
// LLM 不可用时仍由后端返回稳定中文对白,保证相遇和点击聊天链路不断。
if npc_initiates_conversation {
return format!("{npc_name}看向你,先开口说道:“你来了。先别急着走,我正有话想和你说。”");
}
format!("{npc_name}听完你的话,回应道:“{player_message}。我明白你的意思,我们继续说。”")
}
pub(crate) fn build_deterministic_chat_suggestions(
npc_name: &str,
player_message: &str,
) -> Vec<String> {
// 建议只承载玩家可点选的行动意图,不在 UI 里额外塞说明文案。
vec![
format!("{npc_name},我想先听你说"),
"这件事哪里不对劲".to_string(),
if player_message.contains('帮') || player_message.contains('忙') {
"先别绕,说清代价".to_string()
} else {
"你是不是还瞒着我".to_string()
},
]
}
pub(crate) fn build_fallback_npc_chat_suggestions(player_message: &str) -> Vec<String> {
let topic = player_message.trim().chars().take(8).collect::<String>();
let topic = if topic.is_empty() {
"刚才那句".to_string()
} else {
topic
};
vec![
"我愿意先听你说完".to_string(),
format!("这事和{topic}有关吗"),
"你别再避重就轻".to_string(),
]
}
pub(crate) fn build_fallback_function_suggestions(chat_directive: Option<&Value>) -> Vec<Value> {
read_function_options(chat_directive)
.into_iter()
.filter(|option| {
read_string_field(option, "functionId")
.as_deref()
.is_some_and(|function_id| function_id != "npc_chat")
})
.take(2)
.filter_map(|option| {
let function_id = read_string_field(option, "functionId")?;
let action_text = read_string_field(option, "actionText")?;
Some(json!({
"functionId": function_id,
"actionText": action_text,
}))
})
.collect()
}
fn describe_function_options(value: &Value) -> String {
let lines = value
.as_array()
.map(|items| {
items
.iter()
.take(8)
.filter_map(|item| {
let record = as_record(item)?;
let function_id = read_string(record.get("functionId"))?;
let action_text = read_string(record.get("actionText"))?;
let detail_text = read_string(record.get("detailText"));
let action = read_string(record.get("action"));
Some(format!(
"- functionId: {function_id}; actionText: {action_text}; action: {}; detail: {}",
action.unwrap_or_else(|| "unknown".to_string()),
detail_text.unwrap_or_else(|| "".to_string()),
))
})
.collect::<Vec<_>>()
})
.unwrap_or_default();
if lines.is_empty() {
return String::new();
}
let mut result = vec!["当前聊天中可改写为动作候选的 functionOptions".to_string()];
result.extend(lines);
result.join("\n")
}
fn build_npc_dialogue_prompt_base(payload: &NpcChatTurnPromptInput<'_>) -> String {
let encounter = describe_encounter(payload.encounter);
[
format!("世界:{}", describe_world(payload.world_type)),
describe_scene_context(payload.context),
describe_character("玩家 / ", payload.character),
encounter.block,
describe_monsters(payload.monsters),
describe_story_history(payload.history),
]
.into_iter()
.filter(|text| !text.trim().is_empty())
.collect::<Vec<_>>()
.join("\n\n")
}
struct EncounterDescription {
npc_name: String,
block: String,
}
fn describe_encounter(encounter: &Value) -> EncounterDescription {
let record = as_record(encounter);
let npc_name = record
.and_then(|item| read_string(item.get("npcName")))
.unwrap_or_else(|| "眼前角色".to_string());
let context_text = record
.and_then(|item| read_string(item.get("context")))
.or_else(|| record.and_then(|item| read_string(item.get("npcDescription"))))
.unwrap_or_else(|| "你们正在当前遭遇里继续对话。".to_string());
EncounterDescription {
npc_name: npc_name.clone(),
block: format!("当前对象:{npc_name}\n对象背景:{context_text}"),
}
}
fn describe_first_contact_relation_stance(value: Option<&Value>) -> String {
match value.and_then(|item| item.as_str()).map(str::trim) {
Some("guarded") => "戒备试探".to_string(),
Some("neutral") => "正常交流但仍不熟".to_string(),
Some("cooperative") => "已有善意,先确认合作节奏".to_string(),
Some("bonded") => "明显信任,但仍是第一次正式对上人".to_string(),
_ => "第一次真正接触".to_string(),
}
}
fn describe_world(world_type: &str) -> String {
match world_type {
"WUXIA" => "边城模板".to_string(),
"XIANXIA" => "灵潮模板".to_string(),
"CUSTOM" => "自定义世界".to_string(),
value if !value.trim().is_empty() => value.to_string(),
_ => "未知世界".to_string(),
}
}
fn describe_stats(label: &str, record: Option<&serde_json::Map<String, Value>>) -> String {
let hp = record
.and_then(|item| read_number(item.get("hp")))
.unwrap_or(0.0);
let max_hp = record
.and_then(|item| read_number(item.get("maxHp")))
.unwrap_or(hp)
.max(1.0);
let mana = record
.and_then(|item| read_number(item.get("mana")))
.unwrap_or(0.0);
let max_mana = record
.and_then(|item| read_number(item.get("maxMana")))
.unwrap_or(mana)
.max(1.0);
format!(
"{label}生命 {}/{},灵力 {}/{}",
format_prompt_number(hp),
format_prompt_number(max_hp),
format_prompt_number(mana),
format_prompt_number(max_mana)
)
}
fn describe_character(label: &str, value: &Value) -> String {
let record = as_record(value);
let name = record
.and_then(|item| read_string(item.get("name")))
.unwrap_or_else(|| "未知角色".to_string());
let title = record
.and_then(|item| read_string(item.get("title")))
.unwrap_or_else(|| "未知称号".to_string());
let description = record
.and_then(|item| read_string(item.get("description")))
.unwrap_or_else(|| "暂无额外描述".to_string());
let personality = record
.and_then(|item| read_string(item.get("personality")))
.unwrap_or_else(|| "性格信息未显式提供".to_string());
[
format!("{label}姓名:{name}"),
format!("{label}称号:{title}"),
format!("{label}描述:{description}"),
format!("{label}性格:{personality}"),
]
.join("\n")
}
fn describe_story_history(history: &[Value]) -> String {
if history.is_empty() {
return "近期剧情:暂无。".to_string();
}
let lines = history
.iter()
.rev()
.take(4)
.collect::<Vec<_>>()
.into_iter()
.rev()
.filter_map(|item| as_record(item).and_then(|record| read_string(record.get("text"))))
.collect::<Vec<_>>();
if lines.is_empty() {
"近期剧情:暂无。".to_string()
} else {
let mut result = vec!["近期剧情:".to_string()];
result.extend(lines.into_iter().map(|line| format!("- {line}")));
result.join("\n")
}
}
fn describe_npc_conversation_history(history: &[Value], npc_name: &str) -> String {
if history.is_empty() {
return "当前聊天记录:暂无。".to_string();
}
let lines = history
.iter()
.rev()
.take(10)
.collect::<Vec<_>>()
.into_iter()
.rev()
.filter_map(|item| {
let record = as_record(item)?;
let speaker = read_string(record.get("speaker"));
let speaker_name = read_string(record.get("speakerName"));
let text = read_string(record.get("text"))?;
match speaker.as_deref() {
Some("player") => Some(format!("- 玩家:{text}")),
Some("npc") => Some(format!(
"- {}{text}",
speaker_name.unwrap_or_else(|| npc_name.to_string())
)),
Some("system") => Some(format!("- 系统提示:{text}")),
_ => Some(format!(
"- {}{text}",
speaker_name.unwrap_or_else(|| "同伴".to_string())
)),
}
})
.collect::<Vec<_>>();
if lines.is_empty() {
"当前聊天记录:暂无。".to_string()
} else {
let mut result = vec!["当前聊天记录:".to_string()];
result.extend(lines);
result.join("\n")
}
}
fn describe_npc_combat_context(combat_context: &Value) -> Option<String> {
let record = as_record(combat_context)?;
let summary = read_string(record.get("summary"));
let battle_outcome = read_string(record.get("battleOutcome"));
let log_lines = record
.get("logLines")
.map(read_string_array)
.unwrap_or_default()
.into_iter()
.take(6)
.collect::<Vec<_>>();
if summary.is_none() && log_lines.is_empty() {
return None;
}
let outcome_text = match battle_outcome.as_deref() {
Some("spar_complete") => Some("切磋刚刚结束。".to_string()),
Some("victory") => Some("战斗刚刚分出胜负。".to_string()),
_ => None,
};
let mut lines = vec!["刚刚结束的交锋:".to_string()];
if let Some(text) = outcome_text {
lines.push(text);
}
if let Some(text) = summary {
lines.push(format!("- 结果摘要:{text}"));
}
if !log_lines.is_empty() {
lines.push("- 战斗日志:".to_string());
lines.extend(log_lines.into_iter().map(|line| format!(" - {line}")));
}
Some(lines.join("\n"))
}
fn describe_scene_context(context: &Value) -> String {
let record = as_record(context);
let scene_name = record
.and_then(|item| read_string(item.get("sceneName")))
.unwrap_or_else(|| "当前区域".to_string());
let scene_description = record
.and_then(|item| read_string(item.get("sceneDescription")))
.unwrap_or_else(|| "周围气氛仍未完全安定。".to_string());
let in_battle = if record
.and_then(|item| read_bool(item.get("inBattle")))
.unwrap_or(false)
{
"战斗中"
} else {
"非战斗"
};
let custom_world_profile = record
.and_then(|item| item.get("customWorldProfile"))
.and_then(as_record);
let custom_world_name = custom_world_profile.and_then(|item| read_string(item.get("name")));
let custom_world_summary =
custom_world_profile.and_then(|item| read_string(item.get("summary")));
[
Some(format!(
"世界补充:{}",
custom_world_name.unwrap_or_else(|| "".to_string())
)),
custom_world_summary.map(|text| format!("世界摘要:{text}")),
Some(format!("场景:{scene_name}")),
Some(format!("场景描述:{scene_description}")),
Some(format!("当前状态:{in_battle}")),
Some(describe_stats("玩家", record)),
]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.join("\n")
}
fn describe_monsters(monsters: &[Value]) -> String {
if monsters.is_empty() {
return "当前敌对目标:无。".to_string();
}
let lines = monsters
.iter()
.take(4)
.filter_map(|item| {
let record = as_record(item)?;
let name = read_string(record.get("name"))
.or_else(|| read_string(record.get("npcName")))
.or_else(|| read_string(record.get("id")))?;
let hp = read_number(record.get("hp")).unwrap_or(0.0);
let max_hp = read_number(record.get("maxHp")).unwrap_or(hp).max(1.0);
Some(format!(
"- {name}(生命 {}/{})",
format_prompt_number(hp),
format_prompt_number(max_hp)
))
})
.collect::<Vec<_>>();
if lines.is_empty() {
"当前敌对目标:无。".to_string()
} else {
let mut result = vec!["当前敌对目标:".to_string()];
result.extend(lines);
result.join("\n")
}
}
fn read_function_options(chat_directive: Option<&Value>) -> Vec<&Value> {
chat_directive
.and_then(|directive| directive.get("functionOptions"))
.and_then(Value::as_array)
.map(|items| items.iter().collect::<Vec<_>>())
.unwrap_or_default()
}
fn read_string_field(value: &Value, field: &str) -> Option<String> {
value
.get(field)
.and_then(Value::as_str)
.map(str::trim)
.filter(|text| !text.is_empty())
.map(ToOwned::to_owned)
}
fn read_string(value: Option<&Value>) -> Option<String> {
value
.and_then(Value::as_str)
.map(str::trim)
.filter(|text| !text.is_empty())
.map(ToOwned::to_owned)
}
fn read_number(value: Option<&Value>) -> Option<f64> {
value
.and_then(Value::as_f64)
.filter(|number| number.is_finite())
}
fn read_bool(value: Option<&Value>) -> Option<bool> {
value.and_then(Value::as_bool)
}
fn read_string_array(value: &Value) -> Vec<String> {
value
.as_array()
.map(|items| {
items
.iter()
.filter_map(|item| read_string(Some(item)))
.collect::<Vec<_>>()
})
.unwrap_or_default()
}
fn as_record(value: &Value) -> Option<&serde_json::Map<String, Value>> {
value.as_object()
}
fn format_prompt_number(value: f64) -> String {
if value.fract() == 0.0 {
format!("{}", value as i64)
} else {
value.to_string()
}
}

View File

@@ -12,12 +12,16 @@ use axum::{
sse::{Event, Sse},
},
};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
use module_assets::{
AssetObjectAccessPolicy, AssetObjectFieldError, build_asset_entity_binding_input,
build_asset_object_upsert_input, generate_asset_binding_id, generate_asset_object_id,
};
use module_puzzle::PuzzleGeneratedImageCandidate;
use platform_oss::{LegacyAssetPrefix, OssHeadObjectRequest, OssObjectAccess, OssPutObjectRequest};
use platform_oss::{
LegacyAssetPrefix, OssHeadObjectRequest, OssObjectAccess, OssPutObjectRequest,
OssSignedGetObjectUrlRequest,
};
use serde_json::{Map, Value, json};
use shared_contracts::{
puzzle_agent::{
@@ -64,6 +68,7 @@ use crate::{
api_response::json_success_body,
auth::AuthenticatedAccessToken,
http_error::AppError,
prompt::puzzle_image::{PUZZLE_DEFAULT_NEGATIVE_PROMPT, build_puzzle_image_prompt},
puzzle_agent_turn::{
PuzzleAgentTurnRequest, build_failed_finalize_record_input, build_finalize_record_input,
run_puzzle_agent_turn,
@@ -78,8 +83,6 @@ const PUZZLE_GALLERY_PROVIDER: &str = "puzzle-gallery";
const PUZZLE_RUNTIME_PROVIDER: &str = "puzzle-runtime";
const PUZZLE_TEXT_TO_IMAGE_MODEL: &str = "wan2.2-t2i-flash";
const PUZZLE_ENTITY_KIND: &str = "puzzle_work";
const PUZZLE_DEFAULT_NEGATIVE_PROMPT: &str =
"低清晰度,低质量,文字水印,畸形构图,过度模糊,重复肢体,画面脏污";
pub async fn create_puzzle_agent_session(
State(state): State<AppState>,
@@ -216,6 +219,7 @@ pub async fn submit_puzzle_agent_message(
llm_client: state.llm_client(),
session: &submitted_session,
quick_fill_requested: payload.quick_fill_requested.unwrap_or(false),
enable_web_search: state.config.creation_agent_llm_web_search_enabled,
},
|_| {},
)
@@ -320,6 +324,7 @@ pub async fn stream_puzzle_agent_message(
llm_client: state.llm_client(),
session: &session,
quick_fill_requested,
enable_web_search: state.config.creation_agent_llm_web_search_enabled,
},
move |text| {
let _ = reply_tx.send(text.to_string());
@@ -447,7 +452,7 @@ pub async fn execute_puzzle_agent_action(
(
"compile_puzzle_draft",
"完整拼图草稿",
"已编译草稿、生成候选图并应用正式图",
"已编译草稿、生成拼图图片并应用正式图。",
session,
)
}
@@ -468,7 +473,8 @@ pub async fn execute_puzzle_agent_action(
.clone()
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| draft.summary.clone());
let candidate_count = payload.candidate_count.unwrap_or(2).clamp(1, 2);
// 拼图结果页从多候选抽卡收口为单图替换,前端传入的旧 candidateCount 只做兼容忽略。
let candidate_count = 1;
let candidate_start_index = draft.candidates.len();
let candidates = generate_puzzle_image_candidates(
&state,
@@ -476,6 +482,7 @@ pub async fn execute_puzzle_agent_action(
&session.session_id,
&draft.level_name,
&prompt,
payload.reference_image_src.as_deref(),
candidate_count,
candidate_start_index,
)
@@ -521,8 +528,8 @@ pub async fn execute_puzzle_agent_action(
};
(
"generate_puzzle_images",
"候选图生成",
"已生成 2 张候选拼图图",
"拼图图片生成",
"已生成并替换当前拼图图",
session,
)
}
@@ -1296,6 +1303,7 @@ fn map_puzzle_run_response(run: PuzzleRunRecord) -> PuzzleRunSnapshotResponse {
previous_level_tags: run.previous_level_tags,
current_level: run.current_level.map(map_puzzle_runtime_level_response),
recommended_next_profile_id: run.recommended_next_profile_id,
leaderboard_entries: Vec::new(),
}
}
@@ -1381,6 +1389,10 @@ fn map_puzzle_runtime_level_response(
cover_image_src: level.cover_image_src,
board: map_puzzle_board_response(level.board),
status: level.status,
started_at_ms: 0,
cleared_at_ms: None,
elapsed_ms: None,
leaderboard_entries: Vec::new(),
}
}
@@ -1476,7 +1488,8 @@ async fn compile_puzzle_draft_with_initial_cover(
&compiled_session.session_id,
&draft.level_name,
&draft.summary,
2,
None,
1,
draft.candidates.len(),
)
.await
@@ -1619,23 +1632,53 @@ async fn generate_puzzle_image_candidates(
session_id: &str,
level_name: &str,
prompt: &str,
reference_image_src: Option<&str>,
candidate_count: u32,
candidate_start_index: usize,
) -> Result<Vec<PuzzleGeneratedImageCandidateRecord>, String> {
let count = candidate_count.clamp(1, 2);
let count = candidate_count.clamp(1, 1);
let settings =
require_puzzle_dashscope_settings(state).map_err(|error| error.message().to_string())?;
let http_client = build_puzzle_dashscope_http_client(&settings)
.map_err(|error| error.message().to_string())?;
let generated = create_puzzle_text_to_image_generation(
&http_client,
&settings,
build_puzzle_image_prompt(level_name, prompt).as_str(),
PUZZLE_DEFAULT_NEGATIVE_PROMPT,
"1024*1024",
count,
)
.await
let actual_prompt = build_puzzle_image_prompt(level_name, prompt);
let reference_image = match reference_image_src
.map(str::trim)
.filter(|value| !value.is_empty())
{
Some(source) => Some(
resolve_puzzle_reference_image_as_data_url(state, &http_client, source)
.await
.map_err(|error| error.message().to_string())?,
),
None => None,
};
// 中文注释SpacetimeDB reducer 不能做外部 I/O参考图读取与 DashScope 图生图都必须停留在 api-server。
let generated = match reference_image.as_deref() {
Some(reference_image) => {
create_puzzle_image_to_image_generation(
&http_client,
&settings,
actual_prompt.as_str(),
PUZZLE_DEFAULT_NEGATIVE_PROMPT,
"1024*1024",
count,
reference_image,
)
.await
}
None => {
create_puzzle_text_to_image_generation(
&http_client,
&settings,
actual_prompt.as_str(),
PUZZLE_DEFAULT_NEGATIVE_PROMPT,
"1024*1024",
count,
)
.await
}
}
.map_err(|error| error.message().to_string())?;
let mut items = Vec::with_capacity(generated.images.len());
@@ -1661,9 +1704,10 @@ async fn generate_puzzle_image_candidates(
image_src: asset.image_src,
asset_id: asset.asset_id,
prompt: prompt.to_string(),
actual_prompt: Some(prompt.to_string()),
actual_prompt: Some(actual_prompt.clone()),
source_type: "generated".to_string(),
selected: candidate_start_index == 0 && index == 0,
// 单图生成结果总是直接成为当前正式图。
selected: index == 0,
});
}
@@ -1740,7 +1784,8 @@ async fn build_local_next_puzzle_run(
&session.session_id,
&draft.level_name,
&draft.summary,
2,
None,
1,
draft.candidates.len(),
)
.await
@@ -1946,6 +1991,7 @@ fn build_local_puzzle_board(grid_size: u32) -> PuzzleBoardRecord {
struct PuzzleDashScopeSettings {
base_url: String,
api_key: String,
reference_image_model: String,
request_timeout_ms: u64,
}
@@ -1960,6 +2006,11 @@ struct PuzzleDownloadedImage {
bytes: Vec<u8>,
}
struct ParsedPuzzleImageDataUrl {
mime_type: String,
bytes: Vec<u8>,
}
struct GeneratedPuzzleAssetResponse {
image_src: String,
asset_id: String,
@@ -1994,6 +2045,7 @@ fn require_puzzle_dashscope_settings(
Ok(PuzzleDashScopeSettings {
base_url: base_url.to_string(),
api_key: api_key.to_string(),
reference_image_model: state.config.dashscope_reference_image_model.clone(),
request_timeout_ms: state.config.dashscope_image_request_timeout_ms.max(1),
})
}
@@ -2036,7 +2088,7 @@ async fn create_puzzle_text_to_image_generation(
candidate_count: u32,
) -> Result<PuzzleGeneratedImages, AppError> {
let mut parameters = Map::from_iter([
("n".to_string(), json!(candidate_count.clamp(1, 2))),
("n".to_string(), json!(candidate_count.clamp(1, 1))),
("size".to_string(), Value::String(size.to_string())),
("prompt_extend".to_string(), Value::Bool(true)),
("watermark".to_string(), Value::Bool(false)),
@@ -2127,7 +2179,7 @@ async fn create_puzzle_text_to_image_generation(
let mut images = Vec::with_capacity(image_urls.len());
for image_url in image_urls
.into_iter()
.take(candidate_count.clamp(1, 2) as usize)
.take(candidate_count.clamp(1, 1) as usize)
{
images.push(download_puzzle_remote_image(http_client, image_url.as_str()).await?);
}
@@ -2150,6 +2202,270 @@ async fn create_puzzle_text_to_image_generation(
)
}
async fn resolve_puzzle_reference_image_as_data_url(
state: &AppState,
http_client: &reqwest::Client,
source: &str,
) -> Result<String, AppError> {
let trimmed = source.trim();
if trimmed.is_empty() {
return Err(
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "puzzle",
"field": "referenceImageSrc",
"message": "参考图不能为空。",
})),
);
}
if let Some(parsed) = parse_puzzle_image_data_url(trimmed) {
return Ok(format!(
"data:{};base64,{}",
parsed.mime_type,
BASE64_STANDARD.encode(parsed.bytes)
));
}
if !trimmed.starts_with('/') {
return Err(
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "puzzle",
"field": "referenceImageSrc",
"message": "参考图必须是 Data URL 或 /generated-* 旧路径。",
})),
);
}
let object_key = trimmed.trim_start_matches('/');
if LegacyAssetPrefix::from_object_key(object_key).is_none() {
return Err(
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "puzzle",
"field": "referenceImageSrc",
"message": "参考图当前只支持 /generated-* 旧路径。",
})),
);
}
let oss_client = state.oss_client().ok_or_else(|| {
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_details(json!({
"provider": "aliyun-oss",
"reason": "OSS 未完成环境变量配置",
}))
})?;
let signed = oss_client
.sign_get_object_url(OssSignedGetObjectUrlRequest {
object_key: object_key.to_string(),
expire_seconds: Some(60),
})
.map_err(map_puzzle_asset_oss_error)?;
let response = http_client
.get(signed.signed_url)
.send()
.await
.map_err(|error| {
map_puzzle_dashscope_request_error(format!("读取拼图参考图失败:{error}"))
})?;
let status = response.status();
let content_type = response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.unwrap_or("image/png")
.to_string();
let body = response.bytes().await.map_err(|error| {
map_puzzle_dashscope_request_error(format!("读取拼图参考图内容失败:{error}"))
})?;
if !status.is_success() {
return Err(
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "aliyun-oss",
"message": format!("读取参考图失败,状态码:{status}"),
"objectKey": object_key,
})),
);
}
if body.is_empty() {
return Err(
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "aliyun-oss",
"message": "读取参考图失败:对象内容为空",
"objectKey": object_key,
})),
);
}
Ok(format!(
"data:{};base64,{}",
content_type,
BASE64_STANDARD.encode(body)
))
}
async fn create_puzzle_image_to_image_generation(
http_client: &reqwest::Client,
settings: &PuzzleDashScopeSettings,
prompt: &str,
negative_prompt: &str,
size: &str,
candidate_count: u32,
reference_image: &str,
) -> Result<PuzzleGeneratedImages, AppError> {
let mut content = vec![json!({ "image": reference_image })];
content.push(json!({ "text": prompt }));
let response = http_client
.post(format!(
"{}/services/aigc/multimodal-generation/generation",
settings.base_url
))
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", settings.api_key),
)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.json(&json!({
"model": settings.reference_image_model.as_str(),
"input": {
"messages": [
{
"role": "user",
"content": content,
}
],
},
"parameters": {
"n": candidate_count.clamp(1, 1),
"size": size,
"negative_prompt": negative_prompt,
"prompt_extend": true,
"watermark": false,
},
}))
.send()
.await
.map_err(|error| {
map_puzzle_dashscope_request_error(format!("创建拼图参考图生成任务失败:{error}"))
})?;
let status = response.status();
let response_text = response.text().await.map_err(|error| {
map_puzzle_dashscope_request_error(format!("读取拼图参考图生成响应失败:{error}"))
})?;
if !status.is_success() {
return Err(map_puzzle_dashscope_upstream_error(
response_text.as_str(),
"创建拼图参考图生成任务失败",
));
}
let payload = parse_puzzle_json_payload(response_text.as_str(), "解析拼图参考图生成响应失败")?;
let image_urls = extract_puzzle_image_urls(&payload);
if image_urls.is_empty() {
let task_id = extract_puzzle_task_id(&payload).ok_or_else(|| {
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "拼图参考图生成未返回 task_id 或图片地址",
}))
})?;
return wait_puzzle_generated_images(
http_client,
settings,
task_id.as_str(),
candidate_count,
"拼图参考图生成任务失败",
)
.await;
}
let mut images = Vec::with_capacity(candidate_count.clamp(1, 1) as usize);
for image_url in image_urls
.into_iter()
.take(candidate_count.clamp(1, 1) as usize)
{
images.push(download_puzzle_remote_image(http_client, image_url.as_str()).await?);
}
Ok(PuzzleGeneratedImages {
task_id: format!("puzzle-ref-{}", current_utc_micros()),
images,
})
}
async fn wait_puzzle_generated_images(
http_client: &reqwest::Client,
settings: &PuzzleDashScopeSettings,
task_id: &str,
candidate_count: u32,
failure_message: &str,
) -> Result<PuzzleGeneratedImages, AppError> {
let deadline = Instant::now() + Duration::from_millis(settings.request_timeout_ms);
while Instant::now() < deadline {
let poll_response = http_client
.get(format!("{}/tasks/{}", settings.base_url, task_id))
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", settings.api_key),
)
.send()
.await
.map_err(|error| {
map_puzzle_dashscope_request_error(format!("查询拼图图片生成任务失败:{error}"))
})?;
let poll_status = poll_response.status();
let poll_text = poll_response.text().await.map_err(|error| {
map_puzzle_dashscope_request_error(format!("读取拼图图片生成任务响应失败:{error}"))
})?;
if !poll_status.is_success() {
return Err(map_puzzle_dashscope_upstream_error(
poll_text.as_str(),
"查询拼图图片生成任务失败",
));
}
let poll_payload =
parse_puzzle_json_payload(poll_text.as_str(), "解析拼图图片生成任务响应失败")?;
let task_status = find_first_puzzle_string_by_key(&poll_payload, "task_status")
.unwrap_or_default()
.trim()
.to_string();
if task_status == "SUCCEEDED" {
let image_urls = extract_puzzle_image_urls(&poll_payload);
if image_urls.is_empty() {
return Err(
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "拼图图片生成成功但未返回图片地址",
})),
);
}
let mut images = Vec::with_capacity(image_urls.len());
for image_url in image_urls
.into_iter()
.take(candidate_count.clamp(1, 1) as usize)
{
images.push(download_puzzle_remote_image(http_client, image_url.as_str()).await?);
}
return Ok(PuzzleGeneratedImages {
task_id: task_id.to_string(),
images,
});
}
if matches!(task_status.as_str(), "FAILED" | "UNKNOWN") {
return Err(map_puzzle_dashscope_upstream_error(
poll_text.as_str(),
failure_message,
));
}
sleep(Duration::from_secs(2)).await;
}
Err(
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "拼图图片生成超时或未返回图片地址",
})),
)
}
async fn download_puzzle_remote_image(
http_client: &reqwest::Client,
image_url: &str,
@@ -2278,12 +2594,6 @@ async fn persist_puzzle_generated_asset(
})
}
fn build_puzzle_image_prompt(level_name: &str, prompt: &str) -> String {
format!(
"生成一张适合做正方形拼图关卡的高清插画。关卡名:{level_name}。画面要求:{prompt}。必须有清晰主体、丰富但不混乱的区域层次、适合被切成 3x3 或 4x4 拼图块,避免文字、水印、边框和 UI 元素。"
)
}
fn build_puzzle_asset_metadata(
owner_user_id: &str,
session_id: &str,
@@ -2307,6 +2617,46 @@ fn parse_puzzle_json_payload(raw_text: &str, fallback_message: &str) -> Result<V
})
}
fn parse_puzzle_image_data_url(value: &str) -> Option<ParsedPuzzleImageDataUrl> {
let body = value.strip_prefix("data:")?;
let (mime_type, data) = body.split_once(";base64,")?;
if !mime_type.starts_with("image/") {
return None;
}
let bytes = decode_puzzle_base64(data)?;
Some(ParsedPuzzleImageDataUrl {
mime_type: mime_type.to_string(),
bytes,
})
}
fn decode_puzzle_base64(value: &str) -> Option<Vec<u8>> {
let cleaned = value.trim().replace(char::is_whitespace, "");
let mut output = Vec::with_capacity(cleaned.len() * 3 / 4);
let mut buffer = 0u32;
let mut bits = 0u8;
for byte in cleaned.bytes() {
let value = match byte {
b'A'..=b'Z' => byte - b'A',
b'a'..=b'z' => byte - b'a' + 26,
b'0'..=b'9' => byte - b'0' + 52,
b'+' => 62,
b'/' => 63,
b'=' => break,
_ => return None,
} as u32;
buffer = (buffer << 6) | value;
bits += 6;
while bits >= 8 {
bits -= 8;
output.push(((buffer >> bits) & 0xFF) as u8);
}
}
Some(output)
}
fn extract_puzzle_task_id(payload: &Value) -> Option<String> {
find_first_puzzle_string_by_key(payload, "task_id")
}

View File

@@ -19,6 +19,7 @@ pub(crate) struct PuzzleAgentTurnRequest<'a> {
pub llm_client: Option<&'a LlmClient>,
pub session: &'a PuzzleAgentSessionRecord,
pub quick_fill_requested: bool,
pub enable_web_search: bool,
}
#[derive(Clone, Debug)]
@@ -128,6 +129,7 @@ where
request.llm_client,
format!("{PUZZLE_AGENT_SYSTEM_PROMPT}\n\n{prompt}"),
"请按约定输出这一轮的 JSON。",
request.enable_web_search,
CreationAgentLlmTurnErrorMessages {
model_unavailable: "当前模型不可用,请稍后重试。",
generation_failed: "拼图聊天生成失败,请稍后重试。",

View File

@@ -14,12 +14,14 @@ use std::convert::Infallible;
use crate::{
http_error::AppError,
request_context::RequestContext,
runtime_chat_prompt::{
prompt::runtime_chat::{
NPC_CHAT_TURN_REPLY_SYSTEM_PROMPT, NPC_CHAT_TURN_SUGGESTION_SYSTEM_PROMPT,
NpcChatTurnPromptInput, build_npc_chat_turn_reply_prompt,
NpcChatTurnPromptInput, build_deterministic_chat_suggestions,
build_deterministic_npc_reply, build_fallback_function_suggestions,
build_fallback_npc_chat_suggestions, build_npc_chat_turn_reply_prompt,
build_npc_chat_turn_suggestion_prompt,
},
request_context::RequestContext,
state::AppState,
};
@@ -256,66 +258,6 @@ where
Some((npc_reply, suggestions, function_suggestions, force_exit))
}
fn build_deterministic_npc_reply(
npc_name: &str,
player_message: &str,
npc_initiates_conversation: bool,
) -> String {
// Rust API 尚未迁入旧 Node 的完整 LLM NPC 聊天编排前,先由后端提供稳定兜底,保证相遇与选项聊天链路不断。
if npc_initiates_conversation {
return format!("{npc_name}看向你,先开口说道:“你来了。先别急着走,我正有话想和你说。”");
}
format!("{npc_name}听完你的话,回应道:“{player_message}。我明白你的意思,我们继续说。”")
}
fn build_deterministic_chat_suggestions(npc_name: &str, player_message: &str) -> Vec<String> {
// 建议只承载玩家可点选的行动意图,不在 UI 里额外塞说明文案。
vec![
format!("{npc_name},我想先听你说"),
"这件事哪里不对劲".to_string(),
if player_message.contains('帮') || player_message.contains('忙') {
"先别绕,说清代价".to_string()
} else {
"你是不是还瞒着我".to_string()
},
]
}
fn build_fallback_npc_chat_suggestions(player_message: &str) -> Vec<String> {
let topic = player_message.trim().chars().take(8).collect::<String>();
let topic = if topic.is_empty() {
"刚才那句".to_string()
} else {
topic
};
vec![
"我愿意先听你说完".to_string(),
format!("这事和{topic}有关吗"),
"你别再避重就轻".to_string(),
]
}
fn build_fallback_function_suggestions(chat_directive: Option<&Value>) -> Vec<Value> {
read_function_options(chat_directive)
.into_iter()
.filter(|option| {
read_string_field(option, "functionId")
.as_deref()
.is_some_and(|function_id| function_id != "npc_chat")
})
.take(2)
.filter_map(|option| {
let function_id = read_string_field(option, "functionId")?;
let action_text = read_string_field(option, "actionText")?;
Some(json!({
"functionId": function_id,
"actionText": action_text,
}))
})
.collect()
}
fn build_completion_directive(chat_directive: Option<&Value>, force_exit: bool) -> Value {
let Some(directive) = chat_directive else {
return Value::Null;

View File

@@ -1,644 +0,0 @@
use serde_json::Value;
pub(crate) const NPC_CHAT_TURN_REPLY_SYSTEM_PROMPT: &str = r#"你是角色扮演 RPG 里的当前 NPC。
你只输出这名 NPC 此刻会对玩家说的一轮回复。
只输出纯中文口语回复正文不要输出角色名、引号、旁白、动作描写、Markdown、JSON 或解释。
- 如果这是第一次真正接触中的首轮回复,第一句必须先用自然招呼或开场判断起手,不能写成第三人称占位旁白。
回复长度控制在 1 到 3 句,必须紧接玩家刚说的话,自然推进气氛、情报或关系。"#;
pub(crate) const NPC_CHAT_TURN_SUGGESTION_SYSTEM_PROMPT: &str = r#"你要为 RPG NPC 聊天生成下一步候选,并判断敌对聊天是否已经收束。
只输出 JSON不要输出 Markdown 或解释。
JSON 结构:
{"shouldEndChat":false,"terminationReason":null,"suggestions":["温和共情台词","冷静追问台词","施压质疑台词"],"functionSuggestions":[{"functionId":"...","actionText":"玩家动作文本"}]}
- suggestions 是玩家下一轮可直接说出口的中文短句,每条 20 字以内;三条必须按顺序导向不同氛围和好感结果。
- suggestions 第 1 条温和共情,通常让气氛缓和、好感上升;第 2 条冷静追问或试探,通常保持中性但推进情报;第 3 条施压、质疑或立场冲突,通常让气氛变紧、好感下降或付出代价。
- functionSuggestions 只能从用户提示提供的 functionOptions 中挑选,不要发明 functionId。
- functionSuggestions 的 actionText 必须像玩家可点击动作,不暴露 functionId不写规则说明。
- 非敌对聊天 shouldEndChat 必须为 false。
- 敌对聊天可以随时 shouldEndChat=true且敌对 NPC 更偏好在话不投机、被威胁、玩家退出、底线被触碰时结束聊天。"#;
#[derive(Debug)]
pub(crate) struct NpcChatTurnPromptInput<'a> {
pub world_type: &'a str,
pub character: &'a Value,
pub encounter: &'a Value,
pub monsters: &'a [Value],
pub history: &'a [Value],
pub context: &'a Value,
pub conversation_history: &'a [Value],
pub dialogue: &'a [Value],
pub combat_context: Option<&'a Value>,
pub player_message: &'a str,
pub npc_state: &'a Value,
pub npc_initiates_conversation: bool,
pub chat_directive: Option<&'a Value>,
}
pub(crate) fn build_npc_chat_turn_reply_prompt(payload: &NpcChatTurnPromptInput<'_>) -> String {
let encounter = describe_encounter(payload.encounter);
let context = as_record(payload.context);
let npc_state = as_record(payload.npc_state);
let chat_directive = payload.chat_directive.and_then(as_record);
let conversation_history = if !payload.conversation_history.is_empty() {
payload.conversation_history
} else {
payload.dialogue
};
let opening_camp_background =
context.and_then(|record| read_string(record.get("openingCampBackground")));
let opening_camp_dialogue =
context.and_then(|record| read_string(record.get("openingCampDialogue")));
let allowed_topics = context
.and_then(|record| record.get("encounterAllowedTopics"))
.map(read_string_array)
.unwrap_or_default();
let blocked_topics = context
.and_then(|record| record.get("encounterBlockedTopics"))
.map(read_string_array)
.unwrap_or_default();
let is_first_meaningful_contact = context
.and_then(|record| read_bool(record.get("isFirstMeaningfulContact")))
.unwrap_or(false);
let affinity = npc_state
.and_then(|record| read_number(record.get("affinity")))
.unwrap_or(0.0);
let chatted_count = npc_state
.and_then(|record| read_number(record.get("chattedCount")))
.unwrap_or(0.0);
let limit_reason = chat_directive.and_then(|record| read_string(record.get("limitReason")));
let turn_limit = chat_directive
.and_then(|record| read_number(record.get("turnLimit")))
.unwrap_or(0.0)
.max(0.0);
let remaining_turns = chat_directive
.and_then(|record| read_number(record.get("remainingTurns")))
.unwrap_or(0.0)
.max(0.0);
let closing_mode = chat_directive.and_then(|record| read_string(record.get("closingMode")));
let is_limited_negative_affinity_chat =
limit_reason.as_deref() == Some("negative_affinity") && turn_limit > 0.0;
let is_hostile_model_chat = chat_directive
.and_then(|record| read_string(record.get("terminationMode")))
.as_deref()
== Some("hostile_model")
|| chat_directive
.and_then(|record| read_bool(record.get("isHostileChat")))
.unwrap_or(false);
let is_player_exit_turn = chat_directive
.and_then(|record| read_string(record.get("terminationReason")))
.as_deref()
== Some("player_exit");
let is_foreshadow_close_turn = closing_mode.as_deref() == Some("foreshadow_close")
|| chat_directive
.and_then(|record| read_bool(record.get("forceExitAfterTurn")))
.unwrap_or(false);
let has_npc_reply_in_history = conversation_history.iter().any(|item| {
as_record(item)
.and_then(|turn| read_string(turn.get("speaker")))
.is_some_and(|speaker| speaker == "npc")
});
let is_first_npc_spoken_turn =
is_first_meaningful_contact && !has_npc_reply_in_history && chatted_count <= 0.0;
let first_contact_relation_stance = describe_first_contact_relation_stance(
context.and_then(|record| record.get("firstContactRelationStance")),
);
let combat_context_block = payload.combat_context.and_then(describe_npc_combat_context);
[
Some(build_npc_dialogue_prompt_base(payload)),
Some(describe_npc_conversation_history(
conversation_history,
encounter.npc_name.as_str(),
)),
combat_context_block,
opening_camp_background.map(|text| format!("营地开场背景:{text}")),
opening_camp_dialogue.map(|text| format!("刚刚发生的第一段对话:{text}")),
Some(format!("当前关系值:{}", format_prompt_number(affinity))),
Some(format!("已聊天轮次:{}", format_prompt_number(chatted_count))),
if is_first_npc_spoken_turn {
Some(format!(
"当前接触阶段:第一次真正接触({first_contact_relation_stance})。这是这次聊天里 {} 第一次真正对玩家开口。",
encounter.npc_name
))
} else {
None
},
if is_first_npc_spoken_turn {
Some("第一句必须先用一句自然招呼或开场判断起手,再顺着玩家刚刚的话往下接。".to_string())
} else {
None
},
if is_first_npc_spoken_turn {
Some("不要写成“某人看着你,像是在等你把话接下去”这类第三人称占位旁白,也不要把整轮写成设定说明。".to_string())
} else {
None
},
if payload.npc_initiates_conversation {
Some(format!(
"当前要求:这是 {} 主动开口的第一句,不要假装玩家已经先说过话。",
encounter.npc_name
))
} else {
None
},
if allowed_topics.is_empty() {
None
} else {
Some(format!("当前更适合先谈:{}", allowed_topics.join("")))
},
if blocked_topics.is_empty() {
None
} else {
Some(format!("当前避免直接说破:{}", blocked_topics.join("")))
},
if is_limited_negative_affinity_chat {
Some(format!(
"当前相遇属于负好感主角色有限聊天,本次总上限 {} 轮。",
format_prompt_number(turn_limit)
))
} else {
None
},
if is_hostile_model_chat {
Some("当前是敌对或负好感聊天。对方不受固定回合限制,但随时可能不耐烦、结束谈话并把局势推向战斗或驱逐。".to_string())
} else {
None
},
if is_hostile_model_chat {
Some("敌对角色更偏好短促、戒备、带威胁的回应;如果玩家逼问、挑衅、退场或话题触到底线,回复应自然收束到对峙前一刻。".to_string())
} else {
None
},
if is_player_exit_turn {
Some("玩家正在主动结束这轮聊天。请对这个收束动作作出回应,并留下自然的下一步入口。回复后聊天会结束。".to_string())
} else {
None
},
if is_limited_negative_affinity_chat {
Some(format!(
"在你回复完这一轮之后,还剩 {} 轮可以继续聊。",
format_prompt_number(remaining_turns)
))
} else {
None
},
if is_limited_negative_affinity_chat && !is_foreshadow_close_turn {
Some("语气可以戒备、冷淡、带刺,但不要立刻转成开战,也不要把对话硬掐死。".to_string())
} else {
None
},
if is_foreshadow_close_turn {
Some("这是最后一轮回复。必须带有收束感,但不能只用“别问了”“滚开”之类的话把聊天粗暴截断。".to_string())
} else {
None
},
if is_foreshadow_close_turn {
Some("最后一轮必须抛出能推动后续剧情的明确铺垫,例如威胁、线索、条件、去处、人物、未说完的真相或下一步悬念。".to_string())
} else {
None
},
if is_foreshadow_close_turn {
Some("回复后这轮聊天会结束,所以不要邀请继续闲聊,也不要直接宣布已经开战。".to_string())
} else {
None
},
if payload.npc_initiates_conversation {
Some("玩家此刻还没有先说话,请直接写 NPC 主动开口时会说的第一轮回复。".to_string())
} else {
Some(format!("玩家刚刚说:{}", payload.player_message.trim()))
},
if payload.npc_initiates_conversation {
Some(format!(
"现在请只写 {} 主动开口时会说的话。",
encounter.npc_name
))
} else {
Some(format!(
"现在请只写 {} 这一轮会回复玩家的话。",
encounter.npc_name
))
},
]
.into_iter()
.flatten()
.filter(|text| !text.trim().is_empty())
.collect::<Vec<_>>()
.join("\n\n")
}
pub(crate) fn build_npc_chat_turn_suggestion_prompt(
payload: &NpcChatTurnPromptInput<'_>,
npc_reply: &str,
) -> String {
let encounter = describe_encounter(payload.encounter);
let conversation_history = if !payload.conversation_history.is_empty() {
payload.conversation_history
} else {
payload.dialogue
};
let combat_context_block = payload.combat_context.and_then(describe_npc_combat_context);
let chat_directive = payload.chat_directive.and_then(as_record);
let is_hostile_model_chat = chat_directive
.and_then(|record| read_string(record.get("terminationMode")))
.as_deref()
== Some("hostile_model")
|| chat_directive
.and_then(|record| read_bool(record.get("isHostileChat")))
.unwrap_or(false);
let is_player_exit_turn = chat_directive
.and_then(|record| read_string(record.get("terminationReason")))
.as_deref()
== Some("player_exit");
let function_options_block = chat_directive
.and_then(|record| record.get("functionOptions"))
.map(describe_function_options)
.filter(|text| !text.trim().is_empty());
[
Some(build_npc_dialogue_prompt_base(payload)),
Some(describe_npc_conversation_history(
conversation_history,
encounter.npc_name.as_str(),
)),
combat_context_block,
function_options_block,
if payload.npc_initiates_conversation {
Some("玩家尚未先开口,这一轮是 NPC 主动发起聊天。".to_string())
} else {
Some(format!("玩家刚刚说:{}", payload.player_message))
},
Some(format!("NPC 刚刚回复:{npc_reply}")),
if is_hostile_model_chat {
Some("这是敌对或负好感聊天。你需要判断这轮是否应该结束聊天;敌对角色更偏好随时终止并转入对峙。".to_string())
} else {
Some("这是非敌对聊天shouldEndChat 必须为 false。".to_string())
},
if is_player_exit_turn {
Some("玩家已经选择结束聊天shouldEndChat 必须为 trueterminationReason 必须为 player_exit。".to_string())
} else {
None
},
Some("suggestions 必须按顺序生成三种明显不同的玩家台词:温和共情、冷静追问或试探、施压质疑;不要给出同一种态度的近义句。".to_string()),
Some("functionSuggestions 从 functionOptions 中挑可触发动作并改写 actionText。".to_string()),
Some("只输出 JSON{\"shouldEndChat\":false,\"terminationReason\":null,\"suggestions\":[\"...\"],\"functionSuggestions\":[{\"functionId\":\"...\",\"actionText\":\"...\"}]}".to_string()),
]
.into_iter()
.flatten()
.filter(|text| !text.trim().is_empty())
.collect::<Vec<_>>()
.join("\n\n")
}
fn describe_function_options(value: &Value) -> String {
let lines = value
.as_array()
.map(|items| {
items
.iter()
.take(8)
.filter_map(|item| {
let record = as_record(item)?;
let function_id = read_string(record.get("functionId"))?;
let action_text = read_string(record.get("actionText"))?;
let detail_text = read_string(record.get("detailText"));
let action = read_string(record.get("action"));
Some(format!(
"- functionId: {function_id}; actionText: {action_text}; action: {}; detail: {}",
action.unwrap_or_else(|| "unknown".to_string()),
detail_text.unwrap_or_else(|| "".to_string()),
))
})
.collect::<Vec<_>>()
})
.unwrap_or_default();
if lines.is_empty() {
return String::new();
}
let mut result = vec!["当前聊天中可改写为动作候选的 functionOptions".to_string()];
result.extend(lines);
result.join("\n")
}
fn build_npc_dialogue_prompt_base(payload: &NpcChatTurnPromptInput<'_>) -> String {
let encounter = describe_encounter(payload.encounter);
[
format!("世界:{}", describe_world(payload.world_type)),
describe_scene_context(payload.context),
describe_character("玩家 / ", payload.character),
encounter.block,
describe_monsters(payload.monsters),
describe_story_history(payload.history),
]
.into_iter()
.filter(|text| !text.trim().is_empty())
.collect::<Vec<_>>()
.join("\n\n")
}
struct EncounterDescription {
npc_name: String,
block: String,
}
fn describe_encounter(encounter: &Value) -> EncounterDescription {
let record = as_record(encounter);
let npc_name = record
.and_then(|item| read_string(item.get("npcName")))
.unwrap_or_else(|| "眼前角色".to_string());
let context_text = record
.and_then(|item| read_string(item.get("context")))
.or_else(|| record.and_then(|item| read_string(item.get("npcDescription"))))
.unwrap_or_else(|| "你们正在当前遭遇里继续对话。".to_string());
EncounterDescription {
npc_name: npc_name.clone(),
block: format!("当前对象:{npc_name}\n对象背景:{context_text}"),
}
}
fn describe_first_contact_relation_stance(value: Option<&Value>) -> String {
match value.and_then(|item| item.as_str()).map(str::trim) {
Some("guarded") => "戒备试探".to_string(),
Some("neutral") => "正常交流但仍不熟".to_string(),
Some("cooperative") => "已有善意,先确认合作节奏".to_string(),
Some("bonded") => "明显信任,但仍是第一次正式对上人".to_string(),
_ => "第一次真正接触".to_string(),
}
}
fn describe_world(world_type: &str) -> String {
match world_type {
"WUXIA" => "边城模板".to_string(),
"XIANXIA" => "灵潮模板".to_string(),
"CUSTOM" => "自定义世界".to_string(),
value if !value.trim().is_empty() => value.to_string(),
_ => "未知世界".to_string(),
}
}
fn describe_stats(label: &str, record: Option<&serde_json::Map<String, Value>>) -> String {
let hp = record
.and_then(|item| read_number(item.get("hp")))
.unwrap_or(0.0);
let max_hp = record
.and_then(|item| read_number(item.get("maxHp")))
.unwrap_or(hp)
.max(1.0);
let mana = record
.and_then(|item| read_number(item.get("mana")))
.unwrap_or(0.0);
let max_mana = record
.and_then(|item| read_number(item.get("maxMana")))
.unwrap_or(mana)
.max(1.0);
format!(
"{label}生命 {}/{},灵力 {}/{}",
format_prompt_number(hp),
format_prompt_number(max_hp),
format_prompt_number(mana),
format_prompt_number(max_mana)
)
}
fn describe_character(label: &str, value: &Value) -> String {
let record = as_record(value);
let name = record
.and_then(|item| read_string(item.get("name")))
.unwrap_or_else(|| "未知角色".to_string());
let title = record
.and_then(|item| read_string(item.get("title")))
.unwrap_or_else(|| "未知称号".to_string());
let description = record
.and_then(|item| read_string(item.get("description")))
.unwrap_or_else(|| "暂无额外描述".to_string());
let personality = record
.and_then(|item| read_string(item.get("personality")))
.unwrap_or_else(|| "性格信息未显式提供".to_string());
[
format!("{label}姓名:{name}"),
format!("{label}称号:{title}"),
format!("{label}描述:{description}"),
format!("{label}性格:{personality}"),
]
.join("\n")
}
fn describe_story_history(history: &[Value]) -> String {
if history.is_empty() {
return "近期剧情:暂无。".to_string();
}
let lines = history
.iter()
.rev()
.take(4)
.collect::<Vec<_>>()
.into_iter()
.rev()
.filter_map(|item| as_record(item).and_then(|record| read_string(record.get("text"))))
.collect::<Vec<_>>();
if lines.is_empty() {
"近期剧情:暂无。".to_string()
} else {
let mut result = vec!["近期剧情:".to_string()];
result.extend(lines.into_iter().map(|line| format!("- {line}")));
result.join("\n")
}
}
fn describe_npc_conversation_history(history: &[Value], npc_name: &str) -> String {
if history.is_empty() {
return "当前聊天记录:暂无。".to_string();
}
let lines = history
.iter()
.rev()
.take(10)
.collect::<Vec<_>>()
.into_iter()
.rev()
.filter_map(|item| {
let record = as_record(item)?;
let speaker = read_string(record.get("speaker"));
let speaker_name = read_string(record.get("speakerName"));
let text = read_string(record.get("text"))?;
match speaker.as_deref() {
Some("player") => Some(format!("- 玩家:{text}")),
Some("npc") => Some(format!(
"- {}{text}",
speaker_name.unwrap_or_else(|| npc_name.to_string())
)),
Some("system") => Some(format!("- 系统提示:{text}")),
_ => Some(format!(
"- {}{text}",
speaker_name.unwrap_or_else(|| "同伴".to_string())
)),
}
})
.collect::<Vec<_>>();
if lines.is_empty() {
"当前聊天记录:暂无。".to_string()
} else {
let mut result = vec!["当前聊天记录:".to_string()];
result.extend(lines);
result.join("\n")
}
}
fn describe_npc_combat_context(combat_context: &Value) -> Option<String> {
let record = as_record(combat_context)?;
let summary = read_string(record.get("summary"));
let battle_outcome = read_string(record.get("battleOutcome"));
let log_lines = record
.get("logLines")
.map(read_string_array)
.unwrap_or_default()
.into_iter()
.take(6)
.collect::<Vec<_>>();
if summary.is_none() && log_lines.is_empty() {
return None;
}
let outcome_text = match battle_outcome.as_deref() {
Some("spar_complete") => Some("切磋刚刚结束。".to_string()),
Some("victory") => Some("战斗刚刚分出胜负。".to_string()),
_ => None,
};
let mut lines = vec!["刚刚结束的交锋:".to_string()];
if let Some(text) = outcome_text {
lines.push(text);
}
if let Some(text) = summary {
lines.push(format!("- 结果摘要:{text}"));
}
if !log_lines.is_empty() {
lines.push("- 战斗日志:".to_string());
lines.extend(log_lines.into_iter().map(|line| format!(" - {line}")));
}
Some(lines.join("\n"))
}
fn describe_scene_context(context: &Value) -> String {
let record = as_record(context);
let scene_name = record
.and_then(|item| read_string(item.get("sceneName")))
.unwrap_or_else(|| "当前区域".to_string());
let scene_description = record
.and_then(|item| read_string(item.get("sceneDescription")))
.unwrap_or_else(|| "周围气氛仍未完全安定。".to_string());
let in_battle = if record
.and_then(|item| read_bool(item.get("inBattle")))
.unwrap_or(false)
{
"战斗中"
} else {
"非战斗"
};
let custom_world_profile = record
.and_then(|item| item.get("customWorldProfile"))
.and_then(as_record);
let custom_world_name = custom_world_profile.and_then(|item| read_string(item.get("name")));
let custom_world_summary =
custom_world_profile.and_then(|item| read_string(item.get("summary")));
[
Some(format!(
"世界补充:{}",
custom_world_name.unwrap_or_else(|| "".to_string())
)),
custom_world_summary.map(|text| format!("世界摘要:{text}")),
Some(format!("场景:{scene_name}")),
Some(format!("场景描述:{scene_description}")),
Some(format!("当前状态:{in_battle}")),
Some(describe_stats("玩家", record)),
]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.join("\n")
}
fn describe_monsters(monsters: &[Value]) -> String {
if monsters.is_empty() {
return "当前敌对目标:无。".to_string();
}
let lines = monsters
.iter()
.take(4)
.filter_map(|item| {
let record = as_record(item)?;
let name = read_string(record.get("name"))
.or_else(|| read_string(record.get("npcName")))
.or_else(|| read_string(record.get("id")))?;
let hp = read_number(record.get("hp")).unwrap_or(0.0);
let max_hp = read_number(record.get("maxHp")).unwrap_or(hp).max(1.0);
Some(format!(
"- {name}(生命 {}/{})",
format_prompt_number(hp),
format_prompt_number(max_hp)
))
})
.collect::<Vec<_>>();
if lines.is_empty() {
"当前敌对目标:无。".to_string()
} else {
let mut result = vec!["当前敌对目标:".to_string()];
result.extend(lines);
result.join("\n")
}
}
fn read_string(value: Option<&Value>) -> Option<String> {
value
.and_then(Value::as_str)
.map(str::trim)
.filter(|text| !text.is_empty())
.map(ToOwned::to_owned)
}
fn read_number(value: Option<&Value>) -> Option<f64> {
value
.and_then(Value::as_f64)
.filter(|number| number.is_finite())
}
fn read_bool(value: Option<&Value>) -> Option<bool> {
value.and_then(Value::as_bool)
}
fn read_string_array(value: &Value) -> Vec<String> {
value
.as_array()
.map(|items| {
items
.iter()
.filter_map(|item| read_string(Some(item)))
.collect::<Vec<_>>()
})
.unwrap_or_default()
}
fn as_record(value: &Value) -> Option<&serde_json::Map<String, Value>> {
value.as_object()
}
fn format_prompt_number(value: f64) -> String {
if value.fract() == 0.0 {
format!("{}", value as i64)
} else {
value.to_string()
}
}

View File

@@ -4,6 +4,7 @@ use axum::{
http::StatusCode,
response::Response,
};
use module_runtime::{SAVE_SNAPSHOT_VERSION, format_utc_micros};
use serde::Deserialize;
use serde_json::{Value, json};
use shared_contracts::runtime::{
@@ -70,8 +71,8 @@ pub async fn put_runtime_snapshot(
let updated_at_micros = offset_datetime_to_unix_micros(now);
let saved_at_micros = offset_datetime_to_unix_micros(saved_at);
let record = state
.put_runtime_snapshot_record(
let record = if is_non_persistent_runtime_snapshot(&payload.game_state) {
build_transient_runtime_snapshot_record(
user_id,
saved_at_micros,
payload.bottom_tab,
@@ -79,10 +80,21 @@ pub async fn put_runtime_snapshot(
payload.current_story,
updated_at_micros,
)
.await
.map_err(|error| {
runtime_save_error_response(&request_context, map_runtime_save_client_error(error))
})?;
} else {
state
.put_runtime_snapshot_record(
user_id,
saved_at_micros,
payload.bottom_tab,
payload.game_state,
payload.current_story,
updated_at_micros,
)
.await
.map_err(|error| {
runtime_save_error_response(&request_context, map_runtime_save_client_error(error))
})?
};
Ok(json_success_body(
Some(&request_context),
@@ -184,6 +196,52 @@ fn build_saved_game_snapshot_response(
}
}
fn build_transient_runtime_snapshot_record(
user_id: String,
saved_at_micros: i64,
bottom_tab: String,
game_state: Value,
current_story: Option<Value>,
updated_at_micros: i64,
) -> module_runtime::RuntimeSnapshotRecord {
// 中文注释:预览/测试入口可得到本次响应,但不能覆盖用户正式当前快照。
module_runtime::RuntimeSnapshotRecord {
user_id,
version: SAVE_SNAPSHOT_VERSION,
saved_at: format_utc_micros(saved_at_micros),
saved_at_micros,
bottom_tab,
game_state_json: game_state.to_string(),
current_story_json: current_story.as_ref().map(Value::to_string),
game_state,
current_story,
created_at_micros: updated_at_micros,
updated_at_micros,
}
}
fn is_non_persistent_runtime_snapshot(game_state: &Value) -> bool {
let Some(game_state) = game_state.as_object() else {
return false;
};
if game_state
.get("runtimePersistenceDisabled")
.and_then(Value::as_bool)
.unwrap_or(false)
{
return true;
}
matches!(
game_state
.get("runtimeMode")
.and_then(Value::as_str)
.map(str::trim),
Some("preview") | Some("test")
)
}
fn build_profile_save_archive_summary_response(
record: &module_runtime::RuntimeProfileSaveArchiveRecord,
) -> ProfileSaveArchiveSummaryResponse {

View File

@@ -8,7 +8,7 @@ use module_npc::{
NpcRelationStance, build_initial_stance_profile as build_module_npc_initial_stance_profile,
build_relation_state as build_module_npc_relation_state,
};
use module_runtime::RuntimeSnapshotRecord;
use module_runtime::{RuntimeSnapshotRecord, SAVE_SNAPSHOT_VERSION, format_utc_micros};
use module_runtime_story_compat::{
CONTINUE_ADVENTURE_FUNCTION_ID, CurrentEncounterNpcQuestContext, GeneratedStoryPayload,
PendingQuestOfferContext, RuntimeStoryActionResponseParts, StoryResolution,
@@ -376,15 +376,28 @@ async fn persist_runtime_story_snapshot(
)
})?
.unwrap_or(now);
let saved_at_micros = offset_datetime_to_unix_micros(saved_at);
let updated_at_micros = offset_datetime_to_unix_micros(now);
if is_non_persistent_runtime_story_snapshot(&snapshot) {
return Ok(build_transient_runtime_snapshot_record(
user_id,
saved_at_micros,
snapshot.bottom_tab,
snapshot.game_state,
snapshot.current_story,
updated_at_micros,
));
}
state
.put_runtime_snapshot_record(
user_id,
offset_datetime_to_unix_micros(saved_at),
saved_at_micros,
snapshot.bottom_tab,
snapshot.game_state,
snapshot.current_story,
offset_datetime_to_unix_micros(now),
updated_at_micros,
)
.await
.map_err(|error| {
@@ -392,6 +405,52 @@ async fn persist_runtime_story_snapshot(
})
}
fn build_transient_runtime_snapshot_record(
user_id: String,
saved_at_micros: i64,
bottom_tab: String,
game_state: Value,
current_story: Option<Value>,
updated_at_micros: i64,
) -> RuntimeSnapshotRecord {
// 中文注释:预览/测试只需要本次响应里的 hydrated snapshot不能写入正式存档表。
RuntimeSnapshotRecord {
user_id,
version: SAVE_SNAPSHOT_VERSION,
saved_at: format_utc_micros(saved_at_micros),
saved_at_micros,
bottom_tab,
game_state_json: game_state.to_string(),
current_story_json: current_story.as_ref().map(Value::to_string),
game_state,
current_story,
created_at_micros: updated_at_micros,
updated_at_micros,
}
}
fn is_non_persistent_runtime_story_snapshot(snapshot: &RuntimeStorySnapshotPayload) -> bool {
let Some(game_state) = snapshot.game_state.as_object() else {
return false;
};
if game_state
.get("runtimePersistenceDisabled")
.and_then(Value::as_bool)
.unwrap_or(false)
{
return true;
}
matches!(
game_state
.get("runtimeMode")
.and_then(Value::as_str)
.map(str::trim),
Some("preview") | Some("test")
)
}
fn validate_snapshot_payload(snapshot: &RuntimeStorySnapshotPayload) -> Result<(), String> {
if normalize_required_string(snapshot.bottom_tab.as_str()).is_none() {
return Err("snapshot.bottomTab 不能为空".to_string());

View File

@@ -191,6 +191,133 @@ async fn runtime_story_routes_resolve_through_rust_route_boundary() {
);
}
#[tokio::test]
async fn runtime_story_preview_snapshot_returns_transient_response_without_overwriting_save() {
let state = seed_authenticated_state().await;
let token = issue_access_token(&state);
let app = build_router(state);
let formal_response = app
.clone()
.oneshot(
Request::builder()
.method("PUT")
.uri("/api/runtime/save/snapshot")
.header("authorization", format!("Bearer {token}"))
.header("content-type", "application/json")
.header("x-genarrative-response-envelope", "v1")
.body(Body::from(
json!({
"bottomTab": "adventure",
"gameState": {
"runtimeSessionId": "runtime-main",
"runtimeActionVersion": 1,
"worldType": "WUXIA",
"playerCharacter": { "id": "hero" },
"currentScene": "Story",
"runtimeStats": { "playTimeMs": 0 },
"storyHistory": []
},
"currentStory": {
"text": "正式存档里的故事。",
"options": []
}
})
.to_string(),
))
.expect("request should build"),
)
.await
.expect("request should succeed");
assert_eq!(formal_response.status(), StatusCode::OK);
let preview_response = app
.clone()
.oneshot(
Request::builder()
.method("POST")
.uri("/api/runtime/story/actions/resolve")
.header("authorization", format!("Bearer {token}"))
.header("content-type", "application/json")
.header("x-genarrative-response-envelope", "v1")
.body(Body::from(
json!({
"sessionId": "runtime-main",
"clientVersion": 3,
"action": {
"type": "story_choice",
"functionId": "idle_rest_focus"
},
"snapshot": {
"bottomTab": "adventure",
"gameState": {
"runtimeSessionId": "runtime-main",
"runtimeActionVersion": 3,
"runtimeMode": "preview",
"runtimePersistenceDisabled": true,
"playerHp": 10,
"playerMaxHp": 30,
"playerMana": 2,
"playerMaxMana": 12,
"storyHistory": []
},
"currentStory": {
"text": "幕预览里的临时故事。",
"options": []
}
}
})
.to_string(),
))
.expect("request should build"),
)
.await
.expect("request should succeed");
assert_eq!(preview_response.status(), StatusCode::OK);
let preview_payload: Value = serde_json::from_slice(
&preview_response
.into_body()
.collect()
.await
.expect("body should collect")
.to_bytes(),
)
.expect("response should be json");
assert_eq!(
preview_payload["data"]["snapshot"]["gameState"]["runtimeMode"],
json!("preview")
);
let saved_response = app
.oneshot(
Request::builder()
.method("GET")
.uri("/api/runtime/save/snapshot")
.header("authorization", format!("Bearer {token}"))
.header("x-genarrative-response-envelope", "v1")
.body(Body::empty())
.expect("request should build"),
)
.await
.expect("request should succeed");
assert_eq!(saved_response.status(), StatusCode::OK);
let saved_payload: Value = serde_json::from_slice(
&saved_response
.into_body()
.collect()
.await
.expect("body should collect")
.to_bytes(),
)
.expect("response should be json");
assert_eq!(
saved_payload["data"]["currentStory"]["text"],
json!("正式存档里的故事。")
);
assert!(saved_payload["data"]["gameState"]["runtimeMode"].is_null());
}
#[tokio::test]
async fn runtime_story_action_resolve_rejects_client_version_conflict() {
let state = seed_authenticated_state().await;

View File

@@ -15,6 +15,7 @@ pub const PUZZLE_PROFILE_ID_PREFIX: &str = "puzzle-profile-";
pub const PUZZLE_RUN_ID_PREFIX: &str = "puzzle-run-";
pub const PUZZLE_MIN_TAG_COUNT: usize = 3;
pub const PUZZLE_MAX_TAG_COUNT: usize = 6;
const PUZZLE_INITIAL_SHUFFLE_ATTEMPTS: u64 = 64;
#[cfg_attr(feature = "spacetime-types", derive(SpacetimeType))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -680,7 +681,7 @@ pub fn build_generated_candidates(
) -> Result<Vec<PuzzleGeneratedImageCandidate>, PuzzleFieldError> {
let session_id =
normalize_required_string(session_id).ok_or(PuzzleFieldError::MissingSessionId)?;
let count = candidate_count.max(1).min(2);
let count = candidate_count.max(1).min(1);
let prompt = normalize_required_string(prompt_text.unwrap_or(&draft.summary))
.unwrap_or_else(|| draft.summary.clone());
@@ -690,7 +691,7 @@ pub fn build_generated_candidates(
let candidate_id = format!("{session_id}-candidate-{}", index + 1);
PuzzleGeneratedImageCandidate {
candidate_id: candidate_id.clone(),
// 拼图候选图的正式持久化由 api-server 上传 OSS这里仅保留 reducer
// 拼图图的正式持久化由 api-server 上传 OSS这里仅保留 reducer
// 单测/保底路径构造,前缀必须与 OSS 兼容路由一致,不能再指向 public 目录。
image_src: format!(
"/generated-puzzle-assets/{session_id}/{candidate_seed}/cover.svg"
@@ -884,37 +885,18 @@ pub fn resolve_puzzle_grid_size(cleared_level_count: u32) -> u32 {
}
pub fn build_initial_board(grid_size: u32) -> Result<PuzzleBoardSnapshot, PuzzleFieldError> {
build_initial_board_with_seed(grid_size, 0)
}
pub fn build_initial_board_with_seed(
grid_size: u32,
shuffle_seed: u64,
) -> Result<PuzzleBoardSnapshot, PuzzleFieldError> {
if !matches!(grid_size, 3 | 4) {
return Err(PuzzleFieldError::InvalidGridSize);
}
let total = grid_size * grid_size;
let mut positions = (0..total)
.map(|index| PuzzleCellPosition {
row: index / grid_size,
col: index % grid_size,
})
.collect::<Vec<_>>();
if total > 1 {
positions.rotate_left(1);
}
let pieces = (0..total)
.map(|index| {
let correct_row = index / grid_size;
let correct_col = index % grid_size;
let current = &positions[index as usize];
PuzzlePieceState {
piece_id: format!("piece-{index}"),
correct_row,
correct_col,
current_row: current.row,
current_col: current.col,
merged_group_id: None,
}
})
.collect::<Vec<_>>();
let pieces = build_initial_pieces_without_correct_neighbors(grid_size, shuffle_seed);
Ok(rebuild_board_snapshot(grid_size, pieces, None))
}
@@ -925,7 +907,23 @@ pub fn start_run(
cleared_level_count: u32,
) -> Result<PuzzleRunSnapshot, PuzzleFieldError> {
let grid_size = resolve_puzzle_grid_size(cleared_level_count);
let board = build_initial_board(grid_size)?;
let shuffle_seed = puzzle_shuffle_seed(
&run_id,
&entry_profile.profile_id,
cleared_level_count + 1,
grid_size,
);
start_run_with_shuffle_seed(run_id, entry_profile, cleared_level_count, shuffle_seed)
}
pub fn start_run_with_shuffle_seed(
run_id: String,
entry_profile: &PuzzleWorkProfile,
cleared_level_count: u32,
shuffle_seed: u64,
) -> Result<PuzzleRunSnapshot, PuzzleFieldError> {
let grid_size = resolve_puzzle_grid_size(cleared_level_count);
let board = build_initial_board_with_seed(grid_size, shuffle_seed)?;
Ok(PuzzleRunSnapshot {
run_id: run_id.clone(),
entry_profile_id: entry_profile.profile_id.clone(),
@@ -989,7 +987,23 @@ pub fn swap_pieces(
pieces[second_index].current_row = first_row;
pieces[second_index].current_col = first_col;
let next_board = rebuild_board_snapshot(current_level.grid_size, pieces, None);
let affected_cells = [
PuzzleCellPosition {
row: first_row,
col: first_col,
},
PuzzleCellPosition {
row: second_row,
col: second_col,
},
];
let next_board = rebuild_board_snapshot_for_affected_cells(
current_level.grid_size,
&current_level.board,
pieces,
affected_cells,
None,
);
Ok(with_next_board(run, next_board))
}
@@ -1019,13 +1033,91 @@ pub fn drag_piece_or_group(
.ok_or(PuzzleFieldError::MissingPieceId)?;
let source_group_id = pieces[piece_index].merged_group_id.clone();
match source_group_id {
let operation_cells = match source_group_id {
Some(group_id) => drag_group(&mut pieces, &group_id, target_row, target_col, grid_size)?,
None => drag_single_piece(&mut pieces, piece_index, target_row, target_col)?,
};
let next_board = rebuild_board_snapshot_for_affected_cells(
grid_size,
&current_level.board,
pieces,
operation_cells,
None,
);
Ok(with_next_board(run, next_board))
}
pub fn rebuild_board_snapshot_for_affected_cells(
grid_size: u32,
previous_board: &PuzzleBoardSnapshot,
pieces: Vec<PuzzlePieceState>,
affected_cells: impl IntoIterator<Item = PuzzleCellPosition>,
selected_piece_id: Option<String>,
) -> PuzzleBoardSnapshot {
let affected_scope = expand_affected_cells(grid_size, affected_cells);
if affected_scope.is_empty() || previous_board.merged_groups.is_empty() {
return rebuild_board_snapshot(grid_size, pieces, selected_piece_id);
}
let next_board = rebuild_board_snapshot(grid_size, pieces, None);
Ok(with_next_board(run, next_board))
let mut recalculated_piece_ids = pieces
.iter()
.filter(|piece| affected_scope.contains(&(piece.current_row, piece.current_col)))
.map(|piece| piece.piece_id.clone())
.collect::<BTreeSet<_>>();
let previous_piece_by_id = previous_board
.pieces
.iter()
.map(|piece| (piece.piece_id.clone(), piece))
.collect::<BTreeMap<_, _>>();
for piece_id in recalculated_piece_ids.clone() {
if let Some(previous_piece) = previous_piece_by_id.get(&piece_id)
&& let Some(group_id) = previous_piece.merged_group_id.as_deref()
{
add_previous_group_piece_ids(previous_board, group_id, &mut recalculated_piece_ids);
}
}
let mut preserved_groups = Vec::new();
for group in &previous_board.merged_groups {
if group
.piece_ids
.iter()
.any(|piece_id| recalculated_piece_ids.contains(piece_id))
{
continue;
}
let occupied_cells = group
.piece_ids
.iter()
.filter_map(|piece_id| {
pieces
.iter()
.find(|piece| piece.piece_id == *piece_id)
.map(|piece| PuzzleCellPosition {
row: piece.current_row,
col: piece.current_col,
})
})
.collect::<Vec<_>>();
if occupied_cells.len() == group.piece_ids.len() {
preserved_groups.push(PuzzleMergedGroupState {
group_id: group.group_id.clone(),
piece_ids: group.piece_ids.clone(),
occupied_cells,
});
}
}
let recalculated_pieces = pieces
.iter()
.filter(|piece| recalculated_piece_ids.contains(&piece.piece_id))
.cloned()
.collect::<Vec<_>>();
let mut next_groups = preserved_groups;
next_groups.extend(resolve_merged_groups(&recalculated_pieces));
rebuild_board_snapshot_with_groups(grid_size, pieces, next_groups, selected_piece_id)
}
pub fn advance_next_level(
@@ -1042,7 +1134,13 @@ pub fn advance_next_level(
let next_cleared_count = run.cleared_level_count;
let next_grid_size = resolve_puzzle_grid_size(next_cleared_count);
let next_board = build_initial_board(next_grid_size)?;
let shuffle_seed = puzzle_shuffle_seed(
&run.run_id,
&next_profile.profile_id,
run.current_level_index + 1,
next_grid_size,
);
let next_board = build_initial_board_with_seed(next_grid_size, shuffle_seed)?;
let mut played_profile_ids = run.played_profile_ids.clone();
played_profile_ids.push(next_profile.profile_id.clone());
@@ -1258,12 +1356,146 @@ fn split_phrase_list(value: &str) -> Vec<String> {
.collect()
}
fn puzzle_shuffle_seed(run_id: &str, profile_id: &str, level_index: u32, grid_size: u32) -> u64 {
let mut hash = 0xcbf2_9ce4_8422_2325_u64;
for byte in run_id
.bytes()
.chain(profile_id.bytes())
.chain(level_index.to_le_bytes())
.chain(grid_size.to_le_bytes())
{
hash ^= u64::from(byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
hash
}
fn shuffle_positions(positions: &mut [PuzzleCellPosition], seed: u64) {
if positions.len() <= 1 {
return;
}
let mut state = seed ^ ((positions.len() as u64) << 32) ^ 0x9e37_79b9_7f4a_7c15;
for index in (1..positions.len()).rev() {
state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
let swap_index = (state % ((index + 1) as u64)) as usize;
positions.swap(index, swap_index);
}
}
fn build_initial_pieces_without_correct_neighbors(
grid_size: u32,
shuffle_seed: u64,
) -> Vec<PuzzlePieceState> {
let total = grid_size * grid_size;
let base_positions = build_correct_positions(grid_size);
for attempt in 0..PUZZLE_INITIAL_SHUFFLE_ATTEMPTS {
let mut positions = base_positions.clone();
shuffle_positions(
&mut positions,
shuffle_seed.wrapping_add(attempt.wrapping_mul(0x9e37_79b9_7f4a_7c15)),
);
ensure_board_is_not_solved(&mut positions, grid_size);
let pieces = build_pieces_from_positions(grid_size, &positions);
if !has_any_correct_neighbor_pair(&pieces) {
return pieces;
}
}
// 反序布局等价于把完整棋盘旋转 180 度;任意原图相邻块在当前棋盘中的方向都会反向,
// 因此可作为“开局没有正确相邻块”的确定性兜底。
let fallback_pieces =
build_pieces_from_positions(grid_size, &build_reverse_positions(total, grid_size));
debug_assert!(!has_any_correct_neighbor_pair(&fallback_pieces));
fallback_pieces
}
fn build_correct_positions(grid_size: u32) -> Vec<PuzzleCellPosition> {
let total = grid_size * grid_size;
(0..total)
.map(|index| PuzzleCellPosition {
row: index / grid_size,
col: index % grid_size,
})
.collect()
}
fn build_reverse_positions(total: u32, grid_size: u32) -> Vec<PuzzleCellPosition> {
(0..total)
.rev()
.map(|index| PuzzleCellPosition {
row: index / grid_size,
col: index % grid_size,
})
.collect()
}
fn build_pieces_from_positions(
grid_size: u32,
positions: &[PuzzleCellPosition],
) -> Vec<PuzzlePieceState> {
positions
.iter()
.enumerate()
.map(|(index, current)| {
let index = index as u32;
PuzzlePieceState {
piece_id: format!("piece-{index}"),
correct_row: index / grid_size,
correct_col: index % grid_size,
current_row: current.row,
current_col: current.col,
merged_group_id: None,
}
})
.collect()
}
fn ensure_board_is_not_solved(positions: &mut [PuzzleCellPosition], grid_size: u32) {
if positions.len() <= 1 {
return;
}
let is_solved = positions.iter().enumerate().all(|(index, position)| {
position.row == index as u32 / grid_size && position.col == index as u32 % grid_size
});
if is_solved {
positions.rotate_left(1);
}
}
fn has_any_correct_neighbor_pair(pieces: &[PuzzlePieceState]) -> bool {
let pieces_by_cell = pieces
.iter()
.map(|piece| ((piece.current_row, piece.current_col), piece))
.collect::<BTreeMap<_, _>>();
pieces.iter().any(|piece| {
neighbor_cells(piece.current_row, piece.current_col)
.into_iter()
.filter_map(|cell| pieces_by_cell.get(&cell))
.any(|neighbor| are_correct_neighbors(piece, neighbor))
})
}
fn rebuild_board_snapshot(
grid_size: u32,
mut pieces: Vec<PuzzlePieceState>,
pieces: Vec<PuzzlePieceState>,
selected_piece_id: Option<String>,
) -> PuzzleBoardSnapshot {
let merged_groups = resolve_merged_groups(&pieces);
rebuild_board_snapshot_with_groups(grid_size, pieces, merged_groups, selected_piece_id)
}
fn rebuild_board_snapshot_with_groups(
grid_size: u32,
mut pieces: Vec<PuzzlePieceState>,
merged_groups: Vec<PuzzleMergedGroupState>,
selected_piece_id: Option<String>,
) -> PuzzleBoardSnapshot {
let merged_groups = normalize_group_ids(merged_groups);
let group_by_piece = merged_groups
.iter()
.flat_map(|group| {
@@ -1279,9 +1511,13 @@ fn rebuild_board_snapshot(
piece.merged_group_id = group_by_piece.get(&piece.piece_id).cloned();
}
let all_tiles_resolved = pieces.iter().all(|piece| {
let all_pieces_in_correct_cells = pieces.iter().all(|piece| {
piece.correct_row == piece.current_row && piece.correct_col == piece.current_col
});
let all_pieces_merged_into_one_group = merged_groups
.iter()
.any(|group| group.piece_ids.len() == pieces.len() && pieces.len() > 1);
let all_tiles_resolved = all_pieces_in_correct_cells || all_pieces_merged_into_one_group;
PuzzleBoardSnapshot {
rows: grid_size,
@@ -1293,6 +1529,50 @@ fn rebuild_board_snapshot(
}
}
fn normalize_group_ids(groups: Vec<PuzzleMergedGroupState>) -> Vec<PuzzleMergedGroupState> {
groups
.into_iter()
.enumerate()
.map(|(index, group)| PuzzleMergedGroupState {
group_id: format!("group-{}", index + 1),
..group
})
.collect()
}
fn expand_affected_cells(
grid_size: u32,
cells: impl IntoIterator<Item = PuzzleCellPosition>,
) -> BTreeSet<(u32, u32)> {
let mut scope = BTreeSet::new();
for cell in cells {
if cell.row >= grid_size || cell.col >= grid_size {
continue;
}
scope.insert((cell.row, cell.col));
for (row, col) in neighbor_cells(cell.row, cell.col) {
if row < grid_size && col < grid_size {
scope.insert((row, col));
}
}
}
scope
}
fn add_previous_group_piece_ids(
previous_board: &PuzzleBoardSnapshot,
group_id: &str,
piece_ids: &mut BTreeSet<String>,
) {
if let Some(group) = previous_board
.merged_groups
.iter()
.find(|group| group.group_id == group_id)
{
piece_ids.extend(group.piece_ids.iter().cloned());
}
}
fn resolve_merged_groups(pieces: &[PuzzlePieceState]) -> Vec<PuzzleMergedGroupState> {
let pieces_by_cell = pieces
.iter()
@@ -1385,17 +1665,32 @@ fn drag_single_piece(
piece_index: usize,
target_row: u32,
target_col: u32,
) -> Result<(), PuzzleFieldError> {
) -> Result<Vec<PuzzleCellPosition>, PuzzleFieldError> {
let target_index = pieces
.iter()
.position(|piece| piece.current_row == target_row && piece.current_col == target_col)
.ok_or(PuzzleFieldError::InvalidTargetCell)?;
let mut affected_cells = vec![
PuzzleCellPosition {
row: pieces[piece_index].current_row,
col: pieces[piece_index].current_col,
},
PuzzleCellPosition {
row: target_row,
col: target_col,
},
];
if let Some(target_group_id) = pieces[target_index].merged_group_id.clone() {
for piece in pieces
.iter_mut()
.filter(|piece| piece.merged_group_id.as_deref() == Some(target_group_id.as_str()))
{
affected_cells.push(PuzzleCellPosition {
row: piece.current_row,
col: piece.current_col,
});
piece.merged_group_id = None;
}
}
@@ -1410,7 +1705,7 @@ fn drag_single_piece(
pieces[target_index].current_row = source_row;
pieces[target_index].current_col = source_col;
}
Ok(())
Ok(affected_cells)
}
fn drag_group(
@@ -1419,7 +1714,7 @@ fn drag_group(
target_row: u32,
target_col: u32,
grid_size: u32,
) -> Result<(), PuzzleFieldError> {
) -> Result<Vec<PuzzleCellPosition>, PuzzleFieldError> {
let group_indices = pieces
.iter()
.enumerate()
@@ -1456,8 +1751,19 @@ fn drag_group(
.iter()
.map(|index| (pieces[*index].current_row, pieces[*index].current_col))
.collect::<Vec<_>>();
let mut affected_cells = source_positions
.iter()
.map(|(row, col)| PuzzleCellPosition {
row: *row,
col: *col,
})
.collect::<Vec<_>>();
for (index, next_row, next_col) in &target_positions {
affected_cells.push(PuzzleCellPosition {
row: *next_row,
col: *next_col,
});
if let Some(target_piece_index) = pieces.iter().position(|piece| {
piece.current_row == *next_row
&& piece.current_col == *next_col
@@ -1473,6 +1779,14 @@ fn drag_group(
.copied()
.ok_or(PuzzleFieldError::InvalidOperation)?;
pieces[target_piece_index].merged_group_id = None;
affected_cells.push(PuzzleCellPosition {
row: pieces[target_piece_index].current_row,
col: pieces[target_piece_index].current_col,
});
affected_cells.push(PuzzleCellPosition {
row: fallback.0,
col: fallback.1,
});
pieces[target_piece_index].current_row = fallback.0;
pieces[target_piece_index].current_col = fallback.1;
}
@@ -1480,7 +1794,7 @@ fn drag_group(
pieces[*index].current_col = *next_col;
}
Ok(())
Ok(affected_cells)
}
fn with_next_board(run: &PuzzleRunSnapshot, next_board: PuzzleBoardSnapshot) -> PuzzleRunSnapshot {
@@ -1553,13 +1867,13 @@ mod tests {
}
#[test]
fn generated_candidates_use_oss_compatible_prefix() {
fn generated_candidate_uses_oss_compatible_prefix_and_single_image() {
let anchor_pack = infer_anchor_pack("雨夜猫咪", Some("雨夜猫咪"));
let draft = compile_result_draft(&anchor_pack, &[]);
let candidates = build_generated_candidates("session-1", None, &draft, 2, 1_000)
.expect("candidates should build");
assert_eq!(candidates.len(), 2);
assert_eq!(candidates.len(), 1);
assert!(
candidates[0]
.image_src
@@ -1611,6 +1925,281 @@ mod tests {
);
}
#[test]
fn initial_board_shuffle_changes_by_run_id() {
let profile = build_published_profile("entry", "owner-a", vec!["蒸汽城市", "雨夜", "猫咪"]);
let first = start_run("run-random-a".to_string(), &profile, 0).expect("first run");
let second = start_run("run-random-b".to_string(), &profile, 0).expect("second run");
let first_positions = first
.current_level
.expect("first level")
.board
.pieces
.into_iter()
.map(|piece| (piece.current_row, piece.current_col))
.collect::<Vec<_>>();
let second_positions = second
.current_level
.expect("second level")
.board
.pieces
.into_iter()
.map(|piece| (piece.current_row, piece.current_col))
.collect::<Vec<_>>();
assert_ne!(first_positions, second_positions);
}
#[test]
fn initial_board_has_no_correct_neighbor_pairs() {
for grid_size in [3, 4] {
for shuffle_seed in 0..128 {
let board = build_initial_board_with_seed(grid_size, shuffle_seed).expect("board");
assert!(board.merged_groups.is_empty());
assert!(
!has_any_correct_neighbor_pair(&board.pieces),
"grid_size={grid_size}, shuffle_seed={shuffle_seed}"
);
}
}
}
#[test]
fn correct_neighbors_auto_merge_after_swap() {
let profile = build_published_profile("entry", "owner-a", vec!["蒸汽城市", "雨夜", "猫咪"]);
let mut run =
start_run_with_shuffle_seed("run-merge".to_string(), &profile, 0, 7).expect("run");
let current_level = run.current_level.as_mut().expect("level");
current_level.board = rebuild_board_snapshot(
3,
vec![
PuzzlePieceState {
piece_id: "piece-0".to_string(),
correct_row: 0,
correct_col: 0,
current_row: 1,
current_col: 1,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-1".to_string(),
correct_row: 0,
correct_col: 1,
current_row: 0,
current_col: 1,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-2".to_string(),
correct_row: 0,
correct_col: 2,
current_row: 2,
current_col: 2,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-3".to_string(),
correct_row: 1,
correct_col: 0,
current_row: 0,
current_col: 2,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-4".to_string(),
correct_row: 1,
correct_col: 1,
current_row: 1,
current_col: 0,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-5".to_string(),
correct_row: 1,
correct_col: 2,
current_row: 2,
current_col: 0,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-6".to_string(),
correct_row: 2,
correct_col: 0,
current_row: 0,
current_col: 0,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-7".to_string(),
correct_row: 2,
correct_col: 1,
current_row: 1,
current_col: 2,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-8".to_string(),
correct_row: 2,
correct_col: 2,
current_row: 2,
current_col: 1,
merged_group_id: None,
},
],
None,
);
let swapped = swap_pieces(&run, "piece-0", "piece-6").expect("swap");
let board = &swapped.current_level.as_ref().expect("level").board;
let group = board
.merged_groups
.iter()
.find(|group| {
group.piece_ids.contains(&"piece-0".to_string())
&& group.piece_ids.contains(&"piece-1".to_string())
})
.expect("piece-0 and piece-1 should merge");
assert_eq!(group.piece_ids.len(), 2);
}
#[test]
fn single_piece_dragging_into_group_splits_target_group() {
let profile = build_published_profile("entry", "owner-a", vec!["蒸汽城市", "雨夜", "猫咪"]);
let mut run =
start_run_with_shuffle_seed("run-split".to_string(), &profile, 0, 9).expect("run");
let current_level = run.current_level.as_mut().expect("level");
current_level.board = rebuild_board_snapshot(
3,
vec![
PuzzlePieceState {
piece_id: "piece-0".to_string(),
correct_row: 0,
correct_col: 0,
current_row: 0,
current_col: 0,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-1".to_string(),
correct_row: 0,
correct_col: 1,
current_row: 0,
current_col: 1,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-2".to_string(),
correct_row: 0,
correct_col: 2,
current_row: 2,
current_col: 2,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-3".to_string(),
correct_row: 1,
correct_col: 0,
current_row: 1,
current_col: 0,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-4".to_string(),
correct_row: 1,
correct_col: 1,
current_row: 1,
current_col: 1,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-5".to_string(),
correct_row: 1,
correct_col: 2,
current_row: 1,
current_col: 2,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-6".to_string(),
correct_row: 2,
correct_col: 0,
current_row: 2,
current_col: 0,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-7".to_string(),
correct_row: 2,
correct_col: 1,
current_row: 2,
current_col: 1,
merged_group_id: None,
},
PuzzlePieceState {
piece_id: "piece-8".to_string(),
correct_row: 2,
correct_col: 2,
current_row: 0,
current_col: 2,
merged_group_id: None,
},
],
None,
);
let dragged = drag_piece_or_group(&run, "piece-8", 0, 1).expect("drag");
let board = &dragged.current_level.as_ref().expect("level").board;
assert_eq!(
board
.pieces
.iter()
.find(|piece| piece.piece_id == "piece-8")
.map(|piece| (piece.current_row, piece.current_col)),
Some((0, 1))
);
assert!(
board
.merged_groups
.iter()
.all(|group| !(group.piece_ids.contains(&"piece-0".to_string())
&& group.piece_ids.contains(&"piece-1".to_string())))
);
}
#[test]
fn one_full_board_group_marks_level_cleared() {
let pieces = (0..9)
.map(|index| PuzzlePieceState {
piece_id: format!("piece-{index}"),
correct_row: index / 3,
correct_col: index % 3,
current_row: index / 3,
current_col: (index + 1) % 3,
merged_group_id: None,
})
.collect::<Vec<_>>();
let board = rebuild_board_snapshot_with_groups(
3,
pieces,
vec![PuzzleMergedGroupState {
group_id: "group-full".to_string(),
piece_ids: (0..9).map(|index| format!("piece-{index}")).collect(),
occupied_cells: (0..9)
.map(|index| PuzzleCellPosition {
row: index / 3,
col: (index + 1) % 3,
})
.collect(),
}],
None,
);
assert!(board.all_tiles_resolved);
}
#[test]
fn apply_publish_overrides_updates_draft_truth() {
let anchor_pack = infer_anchor_pack("雨夜猫咪神庙", Some("雨夜猫咪神庙"));

View File

@@ -23,6 +23,8 @@ pub struct ExecutePuzzleAgentActionRequest {
#[serde(default)]
pub prompt_text: Option<String>,
#[serde(default)]
pub reference_image_src: Option<String>,
#[serde(default)]
pub candidate_count: Option<u32>,
#[serde(default)]
pub candidate_id: Option<String>,

View File

@@ -56,6 +56,16 @@ pub struct PuzzleMergedGroupStateResponse {
pub occupied_cells: Vec<PuzzleCellPositionResponse>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PuzzleLeaderboardEntryResponse {
pub rank: u32,
pub nickname: String,
pub elapsed_ms: u64,
#[serde(default)]
pub is_current_player: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PuzzleBoardSnapshotResponse {
@@ -82,6 +92,14 @@ pub struct PuzzleRuntimeLevelSnapshotResponse {
pub cover_image_src: Option<String>,
pub board: PuzzleBoardSnapshotResponse,
pub status: String,
#[serde(default)]
pub started_at_ms: u64,
#[serde(default)]
pub cleared_at_ms: Option<u64>,
#[serde(default)]
pub elapsed_ms: Option<u64>,
#[serde(default)]
pub leaderboard_entries: Vec<PuzzleLeaderboardEntryResponse>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
@@ -98,6 +116,8 @@ pub struct PuzzleRunSnapshotResponse {
pub current_level: Option<PuzzleRuntimeLevelSnapshotResponse>,
#[serde(default)]
pub recommended_next_profile_id: Option<String>,
#[serde(default)]
pub leaderboard_entries: Vec<PuzzleLeaderboardEntryResponse>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]

View File

@@ -3,6 +3,7 @@ use crate::*;
const ASSET_HISTORY_MAX_LIMIT: usize = 120;
const ASSET_HISTORY_CHARACTER_VISUAL_KIND: &str = "character_visual";
const ASSET_HISTORY_SCENE_IMAGE_KIND: &str = "scene_image";
const ASSET_HISTORY_PUZZLE_COVER_IMAGE_KIND: &str = "puzzle_cover_image";
#[spacetimedb::table(
accessor = asset_object,
@@ -199,8 +200,11 @@ fn list_asset_history(
let asset_kind = input.asset_kind.trim();
if asset_kind != ASSET_HISTORY_CHARACTER_VISUAL_KIND
&& asset_kind != ASSET_HISTORY_SCENE_IMAGE_KIND
&& asset_kind != ASSET_HISTORY_PUZZLE_COVER_IMAGE_KIND
{
return Err("历史素材类型只支持 character_visual 或 scene_image".to_string());
return Err(
"历史素材类型只支持 character_visual、scene_image 或 puzzle_cover_image".to_string(),
);
}
let limit = usize::try_from(input.limit)

View File

@@ -679,7 +679,7 @@ fn save_puzzle_generated_images_tx(
if candidates.is_empty() {
return Err("拼图候选图不能为空".to_string());
}
append_generated_candidates(&mut draft, candidates);
replace_generated_candidate(&mut draft, candidates);
draft.generation_status = "ready".to_string();
if let Some(selected) = draft
.candidates
@@ -718,7 +718,7 @@ fn save_puzzle_generated_images_tx(
stage: next_stage,
anchor_pack_json: row.anchor_pack_json.clone(),
draft_json: Some(serialize_json(&draft)),
last_assistant_reply: Some("候选图已经生成,请选择正式拼图图片".to_string()),
last_assistant_reply: Some("拼图图片已经生成,并已替换当前正式图".to_string()),
published_profile_id: row.published_profile_id.clone(),
created_at: row.created_at,
updated_at: saved_at,
@@ -1485,21 +1485,19 @@ fn increment_puzzle_profile_play_count(
);
}
fn append_generated_candidates(
fn replace_generated_candidate(
draft: &mut PuzzleResultDraft,
candidates: Vec<PuzzleGeneratedImageCandidate>,
) {
let has_selected_candidate = draft.candidates.iter().any(|entry| entry.selected);
// 再次生成图片是扩充候选池,不覆盖创作者已经看到或已经选择的候选图。
// 若已有正式选择,新追加候选图保持未选中,避免同一草稿出现多个 selected。
draft
.candidates
.extend(candidates.into_iter().map(|mut candidate| {
if has_selected_candidate {
candidate.selected = false;
}
// 结果页生图采用单图替换:每次只保留最新图片,并立即作为正式图。
draft.candidates = candidates
.into_iter()
.take(1)
.map(|mut candidate| {
candidate.selected = true;
candidate
}));
})
.collect();
}
fn list_published_puzzle_profiles(ctx: &TxContext) -> Result<Vec<PuzzleWorkProfile>, String> {
@@ -1609,7 +1607,7 @@ mod tests {
}
#[test]
fn puzzle_generated_images_are_appended_without_clearing_existing_candidates() {
fn puzzle_generated_images_replace_existing_candidate() {
let anchor_pack = infer_anchor_pack("蒸汽城市雨夜猫咪", Some("蒸汽城市雨夜猫咪"));
let mut draft = compile_result_draft(&anchor_pack, &[]);
draft.candidates = vec![PuzzleGeneratedImageCandidate {
@@ -1622,7 +1620,7 @@ mod tests {
selected: true,
}];
append_generated_candidates(
replace_generated_candidate(
&mut draft,
vec![PuzzleGeneratedImageCandidate {
candidate_id: "session-1-candidate-2".to_string(),
@@ -1635,11 +1633,9 @@ mod tests {
}],
);
assert_eq!(draft.candidates.len(), 2);
assert_eq!(draft.candidates[0].candidate_id, "session-1-candidate-1");
assert_eq!(draft.candidates.len(), 1);
assert_eq!(draft.candidates[0].candidate_id, "session-1-candidate-2");
assert!(draft.candidates[0].selected);
assert_eq!(draft.candidates[1].candidate_id, "session-1-candidate-2");
assert!(!draft.candidates[1].selected);
}
#[test]

View File

@@ -434,6 +434,10 @@ pub(crate) fn sync_profile_projections_from_snapshot(
let game_state_object = game_state.as_object();
let saved_at = Timestamp::from_micros_since_unix_epoch(snapshot.saved_at_micros);
if is_non_persistent_runtime_snapshot(&game_state) {
return Ok(());
}
sync_profile_dashboard_from_snapshot(ctx, snapshot, game_state_object, saved_at);
sync_profile_save_archive_from_snapshot(ctx, snapshot, &game_state, saved_at)?;
@@ -740,6 +744,10 @@ fn resolve_profile_save_archive_meta(
game_state: &JsonValue,
current_story_json: Option<&str>,
) -> Option<ProfileSaveArchiveMeta> {
if is_non_persistent_runtime_snapshot(game_state) {
return None;
}
let game_state_object = game_state.as_object();
let world_meta = resolve_profile_world_snapshot_meta(game_state_object)?;
let story_engine_memory = game_state_object
@@ -813,6 +821,25 @@ fn resolve_profile_save_archive_meta(
})
}
fn is_non_persistent_runtime_snapshot(game_state: &JsonValue) -> bool {
let Some(game_state) = game_state.as_object() else {
return false;
};
if game_state
.get("runtimePersistenceDisabled")
.and_then(JsonValue::as_bool)
.unwrap_or(false)
{
return true;
}
matches!(
read_string_from_json(game_state.get("runtimeMode")).as_deref(),
Some("preview") | Some("test")
)
}
fn build_builtin_world_title(world_type: &str) -> String {
match world_type {
"WUXIA" => "武侠世界".to_string(),

View File

@@ -926,27 +926,6 @@ export function CustomWorldEntityCatalog({
</div>
</Section>
<Section
title="角色维度"
subtitle={profile.attributeSchema?.schemaName}
>
<div className="grid grid-cols-2 gap-2 sm:grid-cols-3 xl:grid-cols-2 2xl:grid-cols-3">
{attributeSlots.map((slot) => (
<div
key={slot.slotId}
className="platform-subpanel rounded-xl px-3 py-3"
>
<div className="text-sm font-semibold text-white">
{slot.name}
</div>
<div className="mt-1 line-clamp-2 text-[11px] leading-5 text-zinc-400">
{buildAttributeSlotSummary(slot) || slot.definition}
</div>
</div>
))}
</div>
</Section>
<Section
title="世界概述"
actions={
@@ -1000,6 +979,33 @@ export function CustomWorldEntityCatalog({
}
>
<div className="space-y-3">
<div className="platform-subpanel rounded-2xl px-4 py-4">
<div className="flex flex-wrap items-end justify-between gap-2">
<div className="text-[11px] font-bold tracking-[0.18em] text-zinc-500">
</div>
{profile.attributeSchema?.schemaName ? (
<div className="text-xs leading-5 text-zinc-500">
{profile.attributeSchema.schemaName}
</div>
) : null}
</div>
<div className="mt-3 grid grid-cols-2 gap-2 sm:grid-cols-3 xl:grid-cols-6">
{attributeSlots.map((slot) => (
<div
key={slot.slotId}
className="rounded-xl border border-white/10 bg-black/15 px-3 py-3"
>
<div className="text-sm font-semibold text-white">
{slot.name}
</div>
<div className="mt-1 line-clamp-2 text-[11px] leading-5 text-zinc-400">
{buildAttributeSlotSummary(slot) || slot.definition}
</div>
</div>
))}
</div>
</div>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
{structuredFoundationEntries.map((entry) => (
<div

View File

@@ -8,15 +8,19 @@ type ResolvedAssetImageProps = Omit<
> & {
src?: string | null;
fallbackSrc?: string | null;
refreshKey?: string | number | null;
};
export function ResolvedAssetImage({
src,
fallbackSrc,
alt,
refreshKey,
...rest
}: ResolvedAssetImageProps) {
const { resolvedUrl } = useResolvedAssetReadUrl(src);
const { resolvedUrl } = useResolvedAssetReadUrl(src, {
refreshKey,
});
const finalSrc = resolvedUrl || fallbackSrc?.trim() || '';
if (!finalSrc) {

View File

@@ -78,6 +78,7 @@ function renderEntityLayer(effectNpcId: string | null) {
return renderToStaticMarkup(
<GameCanvasEntityLayer
companions={[]}
sceneActAmbientEncounters={[]}
currentScenePreset={null}
sceneTransitionToken={0}
isSceneTransitionEntering={false}
@@ -185,4 +186,52 @@ describe('GameCanvasEntityLayer', () => {
expect(html).not.toContain('npc-affinity-effect-npc-liu');
expect(html).not.toContain('好感度变化 +3');
});
it('renders scene act back-row encounters alongside the primary encounter', () => {
const html = renderToStaticMarkup(
<GameCanvasEntityLayer
companions={[]}
sceneActAmbientEncounters={[
createEncounter({ id: 'npc-back-1', npcName: '后排甲' }),
createEncounter({ id: 'npc-back-2', npcName: '后排乙' }),
]}
currentScenePreset={null}
sceneTransitionToken={0}
isSceneTransitionEntering={false}
isSceneTransitionExiting={false}
transitionSweepPx={320}
sceneTransitionExitDurationS={0.2}
sceneTransitionEntryDurationS={0.2}
companionAnchorLeft="10%"
companionAnchorBottom="20%"
playerBottomOffsetPx={0}
sceneTransitionPhase="idle"
inBattle={false}
onEntitySelect={null}
playerLeft="20%"
playerCharacter={createCharacter()}
playerHp={100}
playerMaxHp={100}
effectivePlayerFacing="right"
effectivePlayerAnimationState={AnimationState.IDLE}
shouldShowPlayerDialogueIcon={false}
dialogueIndicator={null}
npcAffinityEffect={null}
sceneCombatants={[]}
monsters={[]}
getHostileNpcOuterLeft={() => '70%'}
groundBottom="18%"
stageLiftPx={68}
encounter={createEncounter({ id: 'npc-primary', npcName: '主角色' })}
sideAnchor="15%"
cameraAnchorX={0}
monsterAnchorMeters={3.2}
playerX={0}
/>,
);
expect(html).toContain('查看主角色详情');
expect(html).toContain('查看后排甲详情');
expect(html).toContain('查看后排乙详情');
});
});

View File

@@ -51,6 +51,7 @@ type MonsterSpriteConfig = (typeof MONSTERS_BY_WORLD)[WorldType.WUXIA][number];
interface GameCanvasEntityLayerProps {
companions: CompanionRenderState[];
sceneActAmbientEncounters: Encounter[];
currentScenePreset: ScenePresetInfo | null;
sceneTransitionToken: number;
isSceneTransitionEntering: boolean;
@@ -93,6 +94,13 @@ interface GameCanvasEntityLayerProps {
playerX: number;
}
const SCENE_ACT_BACK_ROW_ANCHOR_X_METERS = RESOLVED_ENTITY_X_METERS + 1.08;
const SCENE_ACT_BACK_ROW_OFFSET_PX = [62, -46] as const;
function addCssPxOffset(value: string, offsetPx: number) {
return offsetPx === 0 ? value : `calc(${value} + ${offsetPx}px)`;
}
function CombatFloatingNumber({
event,
onDone,
@@ -177,6 +185,7 @@ function CombatReactiveSpriteFrame({
export function GameCanvasEntityLayer({
companions,
sceneActAmbientEncounters,
currentScenePreset,
sceneTransitionToken,
isSceneTransitionEntering,
@@ -415,9 +424,16 @@ export function GameCanvasEntityLayer({
</div>
</motion.div>
{sceneCombatants.map(hostileNpc => {
{sceneCombatants.map((hostileNpc, index) => {
const npcEncounter = hostileNpc.encounter;
if (!npcEncounter) return null;
const hostileRenderKey = [
hostileNpc.id,
npcEncounter.id ?? npcEncounter.npcName,
hostileNpc.xMeters,
hostileNpc.yOffset ?? 0,
index,
].join(':');
const config = monsters.find(item => item.id === hostileNpc.id);
const renderOffset = MONSTER_RENDER_OFFSETS[hostileNpc.id] ?? {x: 0, y: 0};
const npcCharacter = npcEncounter?.characterId ? getCharacterById(npcEncounter.characterId) : null;
@@ -453,7 +469,7 @@ export function GameCanvasEntityLayer({
return (
<div
key={hostileNpc.id}
key={hostileRenderKey}
className="absolute"
style={{
left: getHostileNpcOuterLeft(hostileNpc),
@@ -628,6 +644,111 @@ export function GameCanvasEntityLayer({
</div>
);
})()}
{!inBattle &&
sceneActAmbientEncounters.map((ambientEncounter, index) => {
const ambientOffsetPx = SCENE_ACT_BACK_ROW_OFFSET_PX[index];
if (ambientOffsetPx === undefined) {
return null;
}
const ambientResolvedCharacter =
ambientEncounter.kind !== 'treasure' && ambientEncounter.characterId
? getCharacterById(ambientEncounter.characterId)
: null;
const ambientMonsterConfig =
!ambientResolvedCharacter &&
ambientEncounter.kind === 'npc' &&
ambientEncounter.monsterPresetId
? monsters.find(item => item.id === ambientEncounter.monsterPresetId) ?? null
: null;
const ambientHostileBottomOffsetPx = ambientMonsterConfig
? getHostileNpcSceneBottomOffsetPx(ambientMonsterConfig)
: getSceneNpcVisualBottomOffsetPx(ambientEncounter);
const ambientBottomOffsetPx = ambientResolvedCharacter
? getEncounterCharacterBottomOffsetPx(
stageLiftPx,
ambientEncounter,
ambientResolvedCharacter,
ambientOffsetPx,
)
: stageLiftPx + ambientHostileBottomOffsetPx + ambientOffsetPx;
const ambientFacing = getFacingTowardPlayer(
SCENE_ACT_BACK_ROW_ANCHOR_X_METERS,
playerX,
);
const ambientBottom = ambientEncounter.characterId
? getEncounterCharacterOpponentBottom(
groundBottom,
stageLiftPx,
ambientEncounter,
getCharacterById(ambientEncounter.characterId),
)
: `calc(${groundBottom} + ${stageLiftPx + ambientHostileBottomOffsetPx}px)`;
return (
<div
key={`scene-act-ambient-${ambientEncounter.id ?? ambientEncounter.npcName}-${index}`}
className="absolute"
style={{
left: getMonsterWorldLeft(
sideAnchor,
SCENE_ACT_BACK_ROW_ANCHOR_X_METERS,
cameraAnchorX,
monsterAnchorMeters,
),
bottom: addCssPxOffset(ambientBottom, ambientOffsetPx),
zIndex: getSceneEntityZIndex(ambientBottomOffsetPx),
transition: 'left 260ms linear, bottom 180ms ease',
}}
>
<SceneEntityButton
onClick={
ambientEncounter.kind === 'npc'
? () => onEntitySelect?.({kind: 'npc', encounter: ambientEncounter})
: null
}
ariaLabel={
ambientEncounter.kind === 'npc'
? `查看${ambientEncounter.npcName}详情`
: undefined
}
className="relative flex w-28 flex-col items-center"
>
<div className={ROLE_CHARACTER_FRAME_CLASS}>
{ambientResolvedCharacter &&
!ambientEncounter.visual &&
!ambientEncounter.imageSrc?.trim() ? (
<RoleCharacterSprite
state={AnimationState.IDLE}
character={ambientResolvedCharacter}
facing={ambientFacing}
/>
) : ambientMonsterConfig ? (
<HostileNpcAnimator
hostileNpc={ambientMonsterConfig}
animation="idle"
flip={ambientFacing === 'right'}
className="scale-[1.82] origin-bottom"
/>
) : (
<SceneEncounterNpcSprite
encounter={ambientEncounter}
state={AnimationState.IDLE}
facing={ambientFacing}
className="drop-shadow-[0_8px_14px_rgba(0,0,0,0.32)]"
/>
)}
</div>
{/* 幕后排角色只是同幕可见实体,不抢占当前交互目标。 */}
{npcAffinityEffect?.npcId ===
(ambientEncounter.id ?? ambientEncounter.npcName) ? (
<NpcAffinityEffectBadge effect={npcAffinityEffect} />
) : null}
</SceneEntityButton>
</div>
);
})}
</>
);
}

View File

@@ -2,8 +2,12 @@ import {useEffect, useLayoutEffect, useRef, useState} from 'react';
import {resolveCompatibilityTemplateWorldType} from '../../data/customWorldRuntime';
import {MONSTERS_BY_WORLD, PLAYER_BASE_X_METERS} from '../../data/hostileNpcs';
import {resolveActiveSceneActBackgroundImage} from '../../services/customWorldSceneActRuntime';
import {AnimationState, WorldType} from '../../types';
import {buildEncounterFromSceneNpc} from '../../data/scenePresets';
import {
resolveActiveSceneActBackgroundImage,
resolveActiveSceneActEncounterNpcIds,
} from '../../services/customWorldSceneActRuntime';
import {AnimationState, type Encounter, type SceneNpc, WorldType} from '../../types';
import {GameCanvasEffectLayer} from './GameCanvasEffectLayer';
import {GameCanvasEntityLayer} from './GameCanvasEntityLayer';
import {GameCanvasOverlayLayer} from './GameCanvasOverlayLayer';
@@ -66,6 +70,42 @@ export function GameCanvasRuntime({
const backgroundSrc = activeSceneActBackground
|| currentScenePreset?.imageSrc
|| (resolvedWorldType === WorldType.WUXIA ? '/scene_bg/45_PixelSky.png' : '/scene_bg/47_PixelSky.png');
const activeSceneActEncounterNpcIds =
currentScenePreset?.id
? resolveActiveSceneActEncounterNpcIds({
profile: customWorldProfile,
sceneId: currentScenePreset.id,
storyEngineMemory,
})
: [];
const activeSceneActNpcIdSet = new Set(activeSceneActEncounterNpcIds);
const sceneActAmbientEncounters = (currentScenePreset?.npcs ?? [])
.filter((npc: SceneNpc) => {
if (activeSceneActNpcIdSet.size === 0) {
return false;
}
const candidateIds = [npc.id, npc.characterId].filter(
(value): value is string => Boolean(value),
);
const encounterIds = [encounter?.id, encounter?.characterId].filter(
(value): value is string => Boolean(value),
);
return (
candidateIds.some((id) => activeSceneActNpcIdSet.has(id)) &&
!candidateIds.some((id) => encounterIds.includes(id))
);
})
.slice(0, 2)
.map((npc: SceneNpc, index): Encounter => {
const npcEncounter = buildEncounterFromSceneNpc(npc);
return {
...npcEncounter,
xMeters: 3.2 + 1.08,
id: npcEncounter.id ?? `${npc.id}:ambient-${index}`,
};
});
const monsters = resolvedWorldType ? MONSTERS_BY_WORLD[resolvedWorldType] : [];
const groundBottom = '18%';
const stageLiftPx = 68;
@@ -181,6 +221,7 @@ export function GameCanvasRuntime({
/>
<GameCanvasEntityLayer
companions={companions}
sceneActAmbientEncounters={sceneActAmbientEncounters}
currentScenePreset={currentScenePreset}
sceneTransitionToken={sceneTransitionToken}
isSceneTransitionEntering={isSceneTransitionEntering}

View File

@@ -23,6 +23,7 @@ import type {
PuzzleAgentActionRequest,
PuzzleAgentOperationRecord,
} from '../../../packages/shared/src/contracts/puzzleAgentActions';
import type { PuzzleResultDraft } from '../../../packages/shared/src/contracts/puzzleAgentDraft';
import type {
PuzzleAgentSessionSnapshot,
SendPuzzleAgentMessageRequest,
@@ -132,6 +133,8 @@ type PuzzleDetailReturnTarget = {
tab: PlatformHomeTab;
};
type PuzzleRuntimeReturnStage = 'puzzle-result' | 'puzzle-gallery-detail';
type AgentResultBlockerView = {
code?: string;
message: string;
@@ -423,6 +426,8 @@ export function PlatformEntryFlowShellImpl({
useState<PuzzleWorkSummary | null>(null);
const [puzzleDetailReturnTarget, setPuzzleDetailReturnTarget] =
useState<PuzzleDetailReturnTarget | null>(null);
const [puzzleRuntimeReturnStage, setPuzzleRuntimeReturnStage] =
useState<PuzzleRuntimeReturnStage>('puzzle-gallery-detail');
const [puzzleRun, setPuzzleRun] = useState<PuzzleRunSnapshot | null>(null);
const [isPuzzleLoadingLibrary, setIsPuzzleLoadingLibrary] = useState(false);
const [puzzleGenerationState, setPuzzleGenerationState] =
@@ -995,6 +1000,7 @@ export function PlatformEntryFlowShellImpl({
setPuzzleOperation(null);
setPuzzleWorks([]);
setSelectedPuzzleDetail(null);
setPuzzleRuntimeReturnStage('puzzle-gallery-detail');
setPuzzleRun(null);
setPuzzleGenerationState(null);
setIsPuzzleNextLevelGenerating(false);
@@ -1166,6 +1172,7 @@ export function PlatformEntryFlowShellImpl({
const { item } = await getPuzzleGalleryDetail(profileId);
setSelectedPuzzleDetail(item);
setPuzzleRun(startLocalPuzzleRun(item));
setPuzzleRuntimeReturnStage('puzzle-gallery-detail');
setSelectionStage('puzzle-runtime');
} catch (error) {
setPuzzleError(resolvePuzzleErrorMessage(error, '启动拼图玩法失败。'));
@@ -1176,6 +1183,57 @@ export function PlatformEntryFlowShellImpl({
[isPuzzleBusy, resolvePuzzleErrorMessage, setSelectionStage],
);
const buildPuzzleTestWork = useCallback(
(draft: PuzzleResultDraft) => {
const profileId =
puzzleSession?.publishedProfileId ??
`draft-${puzzleSession?.sessionId ?? 'puzzle'}-test`;
const now = new Date().toISOString();
return {
workId: `test-${profileId}`,
profileId,
ownerUserId: authUi?.user?.id ?? 'current-user',
sourceSessionId: puzzleSession?.sessionId ?? null,
authorDisplayName: authUi?.user?.displayName ?? '玩家',
levelName: draft.levelName,
summary: draft.summary,
themeTags: draft.themeTags,
coverImageSrc: draft.coverImageSrc,
coverAssetId: draft.coverAssetId,
publicationStatus: 'draft',
updatedAt: now,
publishedAt: null,
playCount: 0,
publishReady: Boolean(puzzleSession?.resultPreview?.publishReady),
} satisfies PuzzleWorkSummary;
},
[
authUi?.user?.displayName,
authUi?.user?.id,
puzzleSession?.publishedProfileId,
puzzleSession?.resultPreview?.publishReady,
puzzleSession?.sessionId,
],
);
const startPuzzleTestRunFromDraft = useCallback(
(draft: PuzzleResultDraft) => {
if (!draft.coverImageSrc) {
setPuzzleError('请先选择一张正式拼图图片。');
return;
}
const testWork = buildPuzzleTestWork(draft);
setSelectedPuzzleDetail(testWork);
setPuzzleRun(startLocalPuzzleRun(testWork));
setPuzzleRuntimeReturnStage('puzzle-result');
setPuzzleError(null);
setSelectionStage('puzzle-runtime');
},
[buildPuzzleTestWork, setSelectionStage],
);
const submitBigFishInput = useCallback(
(payload: SubmitBigFishInputRequest) => {
if (
@@ -2342,7 +2400,6 @@ export function PlatformEntryFlowShellImpl({
>
<PuzzleResultView
session={puzzleSession}
author={authUi?.user ?? null}
isBusy={isPuzzleBusy}
error={puzzleError}
onBack={() => {
@@ -2351,6 +2408,7 @@ export function PlatformEntryFlowShellImpl({
onExecuteAction={(payload) => {
void executePuzzleAction(payload);
}}
onStartTestRun={startPuzzleTestRunFromDraft}
/>
</Suspense>
</motion.div>
@@ -2414,7 +2472,7 @@ export function PlatformEntryFlowShellImpl({
isBusy={isPuzzleBusy || isPuzzleNextLevelGenerating}
error={puzzleError}
onBack={() => {
setSelectionStage('puzzle-gallery-detail');
setSelectionStage(puzzleRuntimeReturnStage);
}}
onSwapPieces={(payload) => {
void swapPuzzlePiecesInRun(payload);

View File

@@ -0,0 +1,425 @@
// @vitest-environment jsdom
import {
fireEvent,
render,
screen,
waitFor,
within,
} from '@testing-library/react';
import { describe, expect, test, vi } from 'vitest';
import type { PuzzleAgentSessionSnapshot } from '../../../packages/shared/src/contracts/puzzleAgentSession';
import { puzzleAssetClient } from '../../services/puzzle-works/puzzleAssetClient';
import { PuzzleResultView } from './PuzzleResultView';
vi.mock('../ResolvedAssetImage', () => ({
ResolvedAssetImage: ({
src,
alt,
className,
}: {
src?: string | null;
alt?: string;
className?: string;
}) => (src ? <img src={src} alt={alt} className={className} /> : null),
}));
vi.mock('../../services/puzzle-works/puzzleAssetClient', () => ({
puzzleAssetClient: {
listHistoryAssets: vi.fn(),
},
}));
function createSession(
overrides: Partial<PuzzleAgentSessionSnapshot> = {},
): PuzzleAgentSessionSnapshot {
const baseSession: PuzzleAgentSessionSnapshot = {
sessionId: 'puzzle-session-1',
currentTurn: 2,
progressPercent: 88,
stage: 'ready_to_publish',
anchorPack: {
themePromise: {
key: 'themePromise',
label: '题材承诺',
value: '雨夜猫咪',
status: 'confirmed',
},
visualSubject: {
key: 'visualSubject',
label: '画面主体',
value: '屋檐下的猫',
status: 'confirmed',
},
visualMood: {
key: 'visualMood',
label: '视觉气质',
value: '温暖',
status: 'confirmed',
},
compositionHooks: {
key: 'compositionHooks',
label: '拼图记忆点',
value: '雨滴与灯牌',
status: 'confirmed',
},
tagsAndForbidden: {
key: 'tagsAndForbidden',
label: '标签与禁忌',
value: '猫咪、雨夜',
status: 'confirmed',
},
},
draft: {
levelName: '雨夜猫街',
summary: '屋檐下的猫与暖灯街角。',
themeTags: ['猫咪', '雨夜'],
forbiddenDirectives: [],
creatorIntent: null,
anchorPack: {
themePromise: {
key: 'themePromise',
label: '题材承诺',
value: '雨夜猫咪',
status: 'confirmed',
},
visualSubject: {
key: 'visualSubject',
label: '画面主体',
value: '屋檐下的猫',
status: 'confirmed',
},
visualMood: {
key: 'visualMood',
label: '视觉气质',
value: '温暖',
status: 'confirmed',
},
compositionHooks: {
key: 'compositionHooks',
label: '拼图记忆点',
value: '雨滴与灯牌',
status: 'confirmed',
},
tagsAndForbidden: {
key: 'tagsAndForbidden',
label: '标签与禁忌',
value: '猫咪、雨夜',
status: 'confirmed',
},
},
candidates: [
{
candidateId: 'candidate-1',
imageSrc: '/puzzle/candidate-1.png',
assetId: 'asset-1',
prompt: '雨夜猫咪',
actualPrompt: null,
sourceType: 'generated',
selected: true,
},
],
selectedCandidateId: 'candidate-1',
coverImageSrc: '/puzzle/candidate-1.png',
coverAssetId: 'asset-1',
generationStatus: 'ready',
metadata: null,
},
messages: [],
lastAssistantReply: null,
publishedProfileId: null,
suggestedActions: [],
resultPreview: null,
updatedAt: '2026-04-26T10:00:00.000Z',
};
const session = {
...baseSession,
resultPreview: {
draft: baseSession.draft!,
publishReady: true,
blockers: [],
qualityFindings: [],
},
...overrides,
} satisfies PuzzleAgentSessionSnapshot;
return session;
}
describe('PuzzleResultView', () => {
test('uses two tabs without author preview or persistent publish validation', () => {
render(
<PuzzleResultView
session={createSession()}
onBack={() => {}}
onExecuteAction={() => {}}
onStartTestRun={() => {}}
/>,
);
expect(screen.getByRole('button', { name: '基本信息' })).toBeTruthy();
expect(screen.getByRole('button', { name: '拼图图片' })).toBeTruthy();
expect(screen.queryByText('作者预览')).toBeNull();
expect(screen.queryByText('发布校验')).toBeNull();
expect(screen.getByRole('button', { name: //u })).toBeTruthy();
expect(screen.getByRole('button', { name: //u })).toBeTruthy();
});
test('edits theme tags with chips instead of a persistent tag input', () => {
render(
<PuzzleResultView
session={createSession()}
onBack={() => {}}
onExecuteAction={() => {}}
/>,
);
expect(screen.queryByLabelText('新题材标签')).toBeNull();
fireEvent.click(screen.getByLabelText('删除标签 猫咪'));
expect(screen.queryByText('猫咪')).toBeNull();
expect(screen.getByText('雨夜')).toBeTruthy();
fireEvent.click(screen.getByLabelText('新增题材标签'));
fireEvent.change(screen.getByLabelText('新题材标签'), {
target: { value: '暖灯' },
});
fireEvent.click(screen.getByRole('button', { name: '添加' }));
expect(screen.getByText('暖灯')).toBeTruthy();
expect(screen.queryByLabelText('新题材标签')).toBeNull();
});
test('shows blockers only after clicking publish and blocks publish action', () => {
const onExecuteAction = vi.fn();
render(
<PuzzleResultView
session={createSession({
resultPreview: {
draft: createSession().draft!,
publishReady: false,
blockers: [
{
id: 'missing-cover',
code: 'missing-cover',
message: '请先选择正式图',
},
],
qualityFindings: [],
},
})}
onBack={() => {}}
onExecuteAction={onExecuteAction}
/>,
);
expect(screen.queryByText('请先选择正式图')).toBeNull();
fireEvent.click(screen.getByRole('button', { name: //u }));
const dialog = screen.getByRole('dialog', { name: '发布拼图作品' });
expect(within(dialog).getByText('请先选择正式图')).toBeTruthy();
fireEvent.click(within(dialog).getByRole('button', { name: '发布到广场' }));
expect(onExecuteAction).not.toHaveBeenCalled();
});
test('starts work test from the current editable draft', () => {
const onStartTestRun = vi.fn();
render(
<PuzzleResultView
session={createSession()}
onBack={() => {}}
onExecuteAction={() => {}}
onStartTestRun={onStartTestRun}
/>,
);
fireEvent.change(screen.getByDisplayValue('雨夜猫街'), {
target: { value: '暖灯猫街' },
});
fireEvent.click(screen.getByLabelText('新增题材标签'));
fireEvent.change(screen.getByLabelText('新题材标签'), {
target: { value: '暖灯' },
});
fireEvent.click(screen.getByRole('button', { name: '添加' }));
fireEvent.click(screen.getByRole('button', { name: //u }));
expect(onStartTestRun).toHaveBeenCalledWith(
expect.objectContaining({
levelName: '暖灯猫街',
themeTags: ['猫咪', '雨夜', '暖灯'],
}),
);
});
test('generates one image from the picture description and replaces current image', () => {
const onExecuteAction = vi.fn();
render(
<PuzzleResultView
session={createSession()}
onBack={() => {}}
onExecuteAction={onExecuteAction}
/>,
);
fireEvent.click(screen.getByRole('button', { name: '拼图图片' }));
expect(screen.getByText('画面描述')).toBeTruthy();
expect(screen.queryByText(//u)).toBeNull();
fireEvent.change(screen.getByLabelText('画面描述'), {
target: { value: '一只猫在雨夜灯牌下回头。' },
});
fireEvent.click(screen.getByRole('button', { name: //u }));
expect(onExecuteAction).toHaveBeenCalledWith({
action: 'generate_puzzle_images',
promptText: '一只猫在雨夜灯牌下回头。',
referenceImageSrc: undefined,
candidateCount: 1,
});
});
test('selects a history puzzle asset as reference image for the next generation', async () => {
const onExecuteAction = vi.fn();
vi.mocked(puzzleAssetClient.listHistoryAssets).mockResolvedValue([
{
assetObjectId: 'asset-history-1',
assetKind: 'puzzle_cover_image',
imageSrc: '/generated-puzzle-assets/history/image.png',
ownerUserId: 'user-1',
ownerLabel: '账号 user-1',
profileId: null,
entityId: 'puzzle-session-1',
createdAt: '2026-04-27T10:00:00.000Z',
updatedAt: '2026-04-27T10:00:00.000Z',
},
]);
render(
<PuzzleResultView
session={createSession()}
onBack={() => {}}
onExecuteAction={onExecuteAction}
/>,
);
fireEvent.click(screen.getByRole('button', { name: '拼图图片' }));
fireEvent.click(screen.getByLabelText('从历史拼图素材库选择'));
const dialog = await screen.findByRole('dialog', {
name: '选择历史拼图素材',
});
fireEvent.click(within(dialog).getByRole('button', { name: / user-1/u }));
await waitFor(() => {
expect(screen.queryByRole('dialog', { name: '选择历史拼图素材' })).toBeNull();
});
fireEvent.click(screen.getByRole('button', { name: //u }));
expect(onExecuteAction).toHaveBeenLastCalledWith({
action: 'generate_puzzle_images',
promptText: '屋檐下的猫与暖灯街角。',
referenceImageSrc: '/generated-puzzle-assets/history/image.png',
candidateCount: 1,
});
});
test('refreshes the current formal image when session cover image changes', async () => {
const { rerender } = render(
<PuzzleResultView
session={createSession()}
onBack={() => {}}
onExecuteAction={() => {}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: '拼图图片' }));
expect(screen.getByRole('img', { name: '雨夜猫街' }).getAttribute('src')).toBe(
'/puzzle/candidate-1.png',
);
rerender(
<PuzzleResultView
session={createSession({
draft: {
...createSession().draft!,
candidates: [
{
candidateId: 'candidate-2',
imageSrc: '/puzzle/candidate-2.png',
assetId: 'asset-2',
prompt: '新图',
actualPrompt: '新图',
sourceType: 'generated',
selected: true,
},
],
selectedCandidateId: 'candidate-2',
coverImageSrc: '/puzzle/candidate-2.png',
coverAssetId: 'asset-2',
},
updatedAt: '2026-04-27T11:11:11.000Z',
})}
onBack={() => {}}
onExecuteAction={() => {}}
/>,
);
await waitFor(() => {
expect(screen.getByRole('img', { name: '雨夜猫街' }).getAttribute('src')).toBe(
'/puzzle/candidate-2.png',
);
});
});
test('prefers the selected latest candidate image when coverImageSrc lags behind', async () => {
render(
<PuzzleResultView
session={createSession({
draft: {
...createSession().draft!,
candidates: [
{
candidateId: 'candidate-1',
imageSrc: '/puzzle/candidate-1.png',
assetId: 'asset-1',
prompt: '旧图',
actualPrompt: '旧图',
sourceType: 'generated',
selected: false,
},
{
candidateId: 'candidate-2',
imageSrc: '/puzzle/candidate-2.png',
assetId: 'asset-2',
prompt: '新图',
actualPrompt: '新图',
sourceType: 'generated',
selected: true,
},
],
selectedCandidateId: 'candidate-2',
coverImageSrc: '/puzzle/candidate-1.png',
coverAssetId: 'asset-1',
},
})}
onBack={() => {}}
onExecuteAction={() => {}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: '拼图图片' }));
await waitFor(() => {
expect(screen.getByRole('img', { name: '雨夜猫街' }).getAttribute('src')).toBe(
'/puzzle/candidate-2.png',
);
});
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,126 @@
/* @vitest-environment jsdom */
import { fireEvent, render, screen, within } from '@testing-library/react';
import { expect, test, vi } from 'vitest';
import type { PuzzleRunSnapshot } from '../../../packages/shared/src/contracts/puzzleRuntimeSession';
import { PuzzleRuntimeShell } from './PuzzleRuntimeShell';
vi.mock('../../hooks/useResolvedAssetReadUrl', () => ({
useResolvedAssetReadUrl: () => ({
resolvedUrl: '',
isResolving: false,
shouldResolve: false,
}),
}));
vi.mock('../ResolvedAssetImage', () => ({
ResolvedAssetImage: () => null,
}));
const clearedRun: PuzzleRunSnapshot = {
runId: 'run-1',
entryProfileId: 'profile-1',
clearedLevelCount: 1,
currentLevelIndex: 1,
currentGridSize: 3,
playedProfileIds: ['profile-1'],
previousLevelTags: ['奇幻'],
recommendedNextProfileId: 'profile-2',
leaderboardEntries: [
{
rank: 1,
nickname: '测试作者',
elapsedMs: 12_340,
isCurrentPlayer: true,
},
{
rank: 2,
nickname: '星桥旅人',
elapsedMs: 18_120,
},
],
currentLevel: {
runId: 'run-1',
levelIndex: 1,
gridSize: 3,
profileId: 'profile-1',
levelName: '潮雾拼图',
authorDisplayName: '测试作者',
themeTags: ['奇幻'],
coverImageSrc: null,
status: 'cleared',
startedAtMs: 1000,
clearedAtMs: 13_340,
elapsedMs: 12_340,
leaderboardEntries: [
{
rank: 1,
nickname: '测试作者',
elapsedMs: 12_340,
isCurrentPlayer: true,
},
{
rank: 2,
nickname: '星桥旅人',
elapsedMs: 18_120,
},
],
board: {
rows: 3,
cols: 3,
selectedPieceId: null,
allTilesResolved: true,
mergedGroups: [],
pieces: Array.from({ length: 9 }, (_, index) => ({
pieceId: `piece-${index}`,
correctRow: Math.floor(index / 3),
correctCol: index % 3,
currentRow: Math.floor(index / 3),
currentCol: index % 3,
mergedGroupId: null,
})),
},
},
};
test('通关后显示结算弹窗、排行榜和下一关按钮', () => {
const onAdvanceNextLevel = vi.fn();
render(
<PuzzleRuntimeShell
run={clearedRun}
onBack={vi.fn()}
onSwapPieces={vi.fn()}
onDragPiece={vi.fn()}
onAdvanceNextLevel={onAdvanceNextLevel}
/>,
);
const dialog = screen.getByRole('dialog', { name: '通关完成' });
expect(within(dialog).getAllByText('0:12.34').length).toBeGreaterThan(0);
expect(within(dialog).getByText('排行榜')).toBeTruthy();
expect(within(dialog).getByText('#1')).toBeTruthy();
expect(within(dialog).getByText('测试作者')).toBeTruthy();
fireEvent.click(within(dialog).getByRole('button', { name: '下一关' }));
expect(onAdvanceNextLevel).toHaveBeenCalledTimes(1);
});
test('关闭通关弹窗后保留底部下一关入口', () => {
render(
<PuzzleRuntimeShell
run={clearedRun}
onBack={vi.fn()}
onSwapPieces={vi.fn()}
onDragPiece={vi.fn()}
onAdvanceNextLevel={vi.fn()}
/>,
);
fireEvent.click(screen.getByRole('button', { name: '关闭通关弹窗' }));
expect(screen.queryByRole('dialog', { name: '通关完成' })).toBeNull();
expect(screen.getByRole('button', { name: //u })).toBeTruthy();
});

View File

@@ -1,10 +1,11 @@
import { ArrowLeft, ArrowRight, Loader2 } from 'lucide-react';
import { useMemo, useRef, useState } from 'react';
import { ArrowLeft, ArrowRight, Clock, Loader2, Trophy, X } from 'lucide-react';
import { useEffect, useMemo, useRef, useState } from 'react';
import type {
DragPuzzlePieceRequest,
PuzzleBoardSnapshot,
PuzzleCellPosition,
PuzzleMergedGroupState,
PuzzleRunSnapshot,
SwapPuzzlePiecesRequest,
} from '../../../packages/shared/src/contracts/puzzleRuntimeSession';
@@ -27,9 +28,26 @@ type PuzzleBoardPieceViewModel = {
col: number;
correctRow: number;
correctCol: number;
mergedGroupId: string | null;
label: string;
};
type PuzzleMergedGroupViewModel = {
groupId: string;
pieceIds: string[];
anchorPieceId: string;
minRow: number;
minCol: number;
rowSpan: number;
colSpan: number;
pieces: Array<
PuzzleBoardPieceViewModel & {
localRow: number;
localCol: number;
}
>;
};
function boardCellKey(position: PuzzleCellPosition) {
return `${position.row}:${position.col}`;
}
@@ -46,6 +64,59 @@ function buildPieceLabel(pieceId: string) {
return fallback || '块';
}
function buildMergedGroupViewModels(
groups: PuzzleMergedGroupState[],
pieces: PuzzleBoardPieceViewModel[],
) {
const pieceById = new Map(pieces.map((piece) => [piece.pieceId, piece]));
return groups
.map<PuzzleMergedGroupViewModel | null>((group) => {
const groupPieces = group.pieceIds
.map((pieceId) => pieceById.get(pieceId) ?? null)
.filter((piece): piece is PuzzleBoardPieceViewModel => Boolean(piece));
if (groupPieces.length <= 1) {
return null;
}
const rows = groupPieces.map((piece) => piece.row);
const cols = groupPieces.map((piece) => piece.col);
const minRow = Math.min(...rows);
const maxRow = Math.max(...rows);
const minCol = Math.min(...cols);
const maxCol = Math.max(...cols);
const anchorPiece = groupPieces[0];
if (!anchorPiece) {
return null;
}
return {
groupId: group.groupId,
pieceIds: group.pieceIds,
anchorPieceId: anchorPiece.pieceId,
minRow,
minCol,
rowSpan: maxRow - minRow + 1,
colSpan: maxCol - minCol + 1,
pieces: groupPieces.map((piece) => ({
...piece,
localRow: piece.row - minRow,
localCol: piece.col - minCol,
})),
};
})
.filter((group): group is PuzzleMergedGroupViewModel => Boolean(group));
}
function formatElapsedMs(elapsedMs: number | null | undefined) {
const normalizedMs = Math.max(0, Math.round(elapsedMs ?? 0));
const totalSeconds = Math.floor(normalizedMs / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
const centiseconds = Math.floor((normalizedMs % 1000) / 10);
return `${minutes}:${seconds.toString().padStart(2, '0')}.${centiseconds
.toString()
.padStart(2, '0')}`;
}
/**
* 拼图运行时壳层。
* 前端仅维护轻量选中态与拖拽目标,交换、合并、拆分与通关全部以后端快照为准。
@@ -60,13 +131,24 @@ export function PuzzleRuntimeShell({
onAdvanceNextLevel,
}: PuzzleRuntimeShellProps) {
const [selectedPieceId, setSelectedPieceId] = useState<string | null>(null);
const [dragState, setDragState] = useState<{
const dragSessionRef = useRef<{
pieceId: string;
pointerId: number;
dragging: boolean;
startX: number;
startY: number;
currentX: number;
currentY: number;
} | null>(null);
const dragVisualTargetRef = useRef<{
pieceId: string;
groupId: string | null;
} | null>(null);
const dragVisualFrameRef = useRef<number | null>(null);
const dragOffsetRef = useRef<{ x: number; y: number } | null>(null);
const pieceElementRefMap = useRef(new Map<string, HTMLDivElement>());
const groupElementRefMap = useRef(new Map<string, HTMLDivElement>());
const [dismissedClearKey, setDismissedClearKey] = useState<string | null>(null);
const boardRef = useRef<HTMLDivElement | null>(null);
const currentLevel = run?.currentLevel ?? null;
const board = currentLevel?.board ?? null;
@@ -84,20 +166,27 @@ export function PuzzleRuntimeShell({
col: piece.currentCol,
correctRow: piece.correctRow,
correctCol: piece.correctCol,
mergedGroupId: piece.mergedGroupId,
label: buildPieceLabel(piece.pieceId),
}));
}, [board]);
const mergedCellKeys = useMemo(() => {
const mergedGroups = useMemo(() => {
if (!board) {
return new Set<string>();
return [];
}
return new Set(
board.mergedGroups.flatMap((group) =>
group.occupiedCells.map((cell) => boardCellKey(cell)),
return buildMergedGroupViewModels(board.mergedGroups, pieces);
}, [board, pieces]);
const mergedCellKeys = useMemo(
() =>
new Set(
mergedGroups.flatMap((group) =>
group.pieces.map((piece) => boardCellKey(piece)),
),
),
);
}, [board]);
[mergedGroups],
);
const pieceByCell = useMemo(() => {
const map = new Map<string, PuzzleBoardPieceViewModel>();
@@ -106,6 +195,119 @@ export function PuzzleRuntimeShell({
}
return map;
}, [pieces]);
const pieceById = useMemo(
() => new Map(pieces.map((piece) => [piece.pieceId, piece])),
[pieces],
);
const resetDragVisualTarget = () => {
const dragVisualTarget = dragVisualTargetRef.current;
if (!dragVisualTarget) {
return;
}
const pieceElement = pieceElementRefMap.current.get(dragVisualTarget.pieceId);
if (pieceElement) {
pieceElement.style.transform = '';
pieceElement.style.willChange = '';
pieceElement.style.zIndex = '';
pieceElement.style.opacity = '';
}
if (dragVisualTarget.groupId) {
const groupElement = groupElementRefMap.current.get(dragVisualTarget.groupId);
if (groupElement) {
groupElement.style.transform = '';
groupElement.style.willChange = '';
groupElement.style.zIndex = '';
groupElement.style.opacity = '';
}
}
dragVisualTargetRef.current = null;
};
const cancelDragVisualFrame = () => {
if (dragVisualFrameRef.current === null) {
return;
}
window.cancelAnimationFrame(dragVisualFrameRef.current);
dragVisualFrameRef.current = null;
};
const resetDragInteraction = () => {
cancelDragVisualFrame();
dragOffsetRef.current = null;
dragSessionRef.current = null;
resetDragVisualTarget();
};
const flushDragVisual = () => {
dragVisualFrameRef.current = null;
const dragSession = dragSessionRef.current;
if (!dragSession || !dragSession.dragging) {
resetDragVisualTarget();
return;
}
const piece = pieceById.get(dragSession.pieceId) ?? null;
const groupId = piece?.mergedGroupId ?? null;
const nextTarget = {
pieceId: dragSession.pieceId,
groupId,
};
const previousTarget = dragVisualTargetRef.current;
if (
previousTarget &&
(previousTarget.pieceId !== nextTarget.pieceId ||
previousTarget.groupId !== nextTarget.groupId)
) {
resetDragVisualTarget();
}
dragVisualTargetRef.current = nextTarget;
const offsetX = dragSession.currentX - dragSession.startX;
const offsetY = dragSession.currentY - dragSession.startY;
dragOffsetRef.current = { x: offsetX, y: offsetY };
if (groupId) {
const groupElement = groupElementRefMap.current.get(groupId);
if (groupElement) {
groupElement.style.willChange = 'transform';
groupElement.style.transform = `translate3d(${offsetX}px, ${offsetY}px, 0) scale(1.02)`;
groupElement.style.zIndex = '80';
groupElement.style.opacity = '0.95';
}
const pieceElement = pieceElementRefMap.current.get(dragSession.pieceId);
if (pieceElement) {
pieceElement.style.transform = '';
pieceElement.style.willChange = '';
pieceElement.style.zIndex = '';
pieceElement.style.opacity = '';
}
return;
}
const pieceElement = pieceElementRefMap.current.get(dragSession.pieceId);
if (pieceElement) {
pieceElement.style.willChange = 'transform';
pieceElement.style.transform = `translate3d(${offsetX}px, ${offsetY}px, 0) scale(1.03)`;
pieceElement.style.zIndex = '70';
pieceElement.style.opacity = '0.95';
}
};
const scheduleDragVisual = () => {
if (dragVisualFrameRef.current !== null) {
return;
}
dragVisualFrameRef.current = window.requestAnimationFrame(flushDragVisual);
};
useEffect(() => () => {
cancelDragVisualFrame();
resetDragVisualTarget();
}, []);
if (!run || !currentLevel || !board) {
return (
@@ -174,18 +376,19 @@ export function PuzzleRuntimeShell({
pieceId: string,
event: React.PointerEvent<HTMLDivElement>,
) => {
const currentDragState = dragState;
if (!currentDragState || currentDragState.pieceId !== pieceId) {
const currentDragSession = dragSessionRef.current;
if (!currentDragSession || currentDragSession.pieceId !== pieceId) {
return;
}
event.currentTarget.releasePointerCapture(event.pointerId);
event.currentTarget.releasePointerCapture?.(event.pointerId);
if (currentDragState.dragging) {
if (currentDragSession.dragging) {
const targetCell = resolveBoardCellFromPointer(
event.clientX,
event.clientY,
);
resetDragInteraction();
if (targetCell) {
onDragPiece({
pieceId,
@@ -194,18 +397,73 @@ export function PuzzleRuntimeShell({
});
}
setSelectedPieceId(null);
setDragState(null);
return;
}
setDragState(null);
resetDragInteraction();
handlePieceClick(pieceId);
};
const handlePiecePointerDown = (
pieceId: string,
event: React.PointerEvent<HTMLDivElement>,
) => {
if (isBusy) {
return;
}
event.preventDefault();
resetDragInteraction();
event.currentTarget.setPointerCapture?.(event.pointerId);
dragSessionRef.current = {
pieceId,
pointerId: event.pointerId,
dragging: false,
startX: event.clientX,
startY: event.clientY,
currentX: event.clientX,
currentY: event.clientY,
};
};
const handlePiecePointerMove = (
pieceId: string,
event: React.PointerEvent<HTMLDivElement>,
) => {
const dragSession = dragSessionRef.current;
if (
!dragSession ||
dragSession.pieceId !== pieceId ||
dragSession.pointerId !== event.pointerId
) {
return;
}
event.preventDefault();
const deltaX = event.clientX - dragSession.startX;
const deltaY = event.clientY - dragSession.startY;
const dragging = dragSession.dragging || Math.hypot(deltaX, deltaY) >= 8;
dragSession.dragging = dragging;
dragSession.currentX = event.clientX;
dragSession.currentY = event.clientY;
if (!dragging) {
return;
}
// 拖动中的视觉更新直接写入 DOM transform避免 pointermove 触发整盘 React 重渲染导致跟手延迟。
scheduleDragVisual();
};
const statusLabel =
currentLevel.status === 'cleared' ? '已通关' : `${board.rows}x${board.cols}`;
const nextAvailable =
currentLevel.status === 'cleared' && Boolean(run.recommendedNextProfileId);
const clearResultKey = `${run.runId}:${currentLevel.profileId}:${currentLevel.levelIndex}`;
const leaderboardEntries =
(currentLevel.leaderboardEntries ?? []).length > 0
? currentLevel.leaderboardEntries
: (run.leaderboardEntries ?? []);
const isClearResultOpen =
currentLevel.status === 'cleared' && dismissedClearKey !== clearResultKey;
return (
<div className="fixed inset-0 z-[100] flex justify-center bg-slate-950 text-white">
@@ -246,7 +504,8 @@ export function PuzzleRuntimeShell({
<div className="absolute inset-0 flex items-center justify-center p-4 pt-24 pb-28">
<div
ref={boardRef}
className="grid aspect-square w-full max-w-[min(92vw,92vh)] rounded-[1.7rem] border border-white/12 bg-white/8 p-2 shadow-[0_30px_80px_rgba(0,0,0,0.35)] backdrop-blur-sm"
data-testid="puzzle-board"
className="relative grid aspect-square w-full max-w-[min(92vw,92vh)] touch-none select-none rounded-[1.7rem] border border-white/12 bg-white/8 p-2 shadow-[0_30px_80px_rgba(0,0,0,0.35)] backdrop-blur-sm"
style={{
gridTemplateColumns: `repeat(${board.cols}, minmax(0, 1fr))`,
}}
@@ -263,64 +522,55 @@ export function PuzzleRuntimeShell({
className="relative p-1"
>
<div
ref={(node) => {
if (!piece) {
return;
}
if (node) {
pieceElementRefMap.current.set(piece.pieceId, node);
return;
}
pieceElementRefMap.current.delete(piece.pieceId);
}}
data-piece-id={piece?.pieceId ?? undefined}
className={`flex h-full min-h-[4.5rem] items-center justify-center rounded-[1rem] border text-sm font-black transition ${
occupied
? isSelected
? 'border-amber-200 bg-amber-400/84 text-slate-950 shadow-[0_12px_30px_rgba(251,191,36,0.22)]'
: isMerged
? 'border-emerald-200/55 bg-emerald-300/26 text-white'
? 'border-transparent bg-transparent text-white'
: 'border-white/18 bg-white/12 text-white'
: 'border-white/8 bg-black/18 text-white/20'
} ${
isMerged ? 'transition-colors' : 'transition-[background-color,border-color,box-shadow,opacity]'
}`}
onPointerDown={(event) => {
if (!piece || isBusy) {
if (!piece || isMerged) {
return;
}
event.currentTarget.setPointerCapture(event.pointerId);
setDragState({
pieceId: piece.pieceId,
pointerId: event.pointerId,
dragging: false,
startX: event.clientX,
startY: event.clientY,
});
handlePiecePointerDown(piece.pieceId, event);
}}
onPointerMove={(event) => {
if (
!piece ||
!dragState ||
dragState.pieceId !== piece.pieceId ||
dragState.pointerId !== event.pointerId ||
dragState.dragging
) {
if (!piece || isMerged) {
return;
}
const deltaX = event.clientX - dragState.startX;
const deltaY = event.clientY - dragState.startY;
if (Math.hypot(deltaX, deltaY) >= 8) {
setDragState((current) =>
current && current.pieceId === piece.pieceId
? {
...current,
dragging: true,
}
: current,
);
}
handlePiecePointerMove(piece.pieceId, event);
}}
onPointerUp={(event) => {
if (piece) {
if (piece && !isMerged) {
handlePiecePointerUp(piece.pieceId, event);
}
}}
onPointerCancel={() => {
setDragState(null);
resetDragInteraction();
}}
onLostPointerCapture={() => {
resetDragInteraction();
}}
>
{piece ? (
<div className="relative h-full w-full overflow-hidden rounded-[0.92rem]">
{resolvedCoverImage ? (
{isMerged ? null : resolvedCoverImage ? (
<div
className="absolute inset-0"
style={{
@@ -341,9 +591,11 @@ export function PuzzleRuntimeShell({
<div className="absolute inset-0 bg-[linear-gradient(145deg,rgba(251,191,36,0.4),rgba(76,29,19,0.72))]" />
)}
<div className="absolute inset-0 bg-black/10" />
<div className="absolute bottom-1 right-1 rounded-full bg-black/38 px-1.5 py-0.5 text-[10px] font-black text-white/86">
{!isMerged ? (
<div className="absolute bottom-1 right-1 rounded-full bg-black/38 px-1.5 py-0.5 text-[10px] font-black text-white/86">
{piece.label}
</div>
</div>
) : null}
</div>
) : (
''
@@ -352,22 +604,97 @@ export function PuzzleRuntimeShell({
</div>
);
})}
{mergedGroups.map((group) => (
<div
key={group.groupId}
ref={(node) => {
if (node) {
groupElementRefMap.current.set(group.groupId, node);
return;
}
groupElementRefMap.current.delete(group.groupId);
}}
className="pointer-events-none absolute z-10 p-1"
style={{
left: `${(group.minCol / board.cols) * 100}%`,
top: `${(group.minRow / board.rows) * 100}%`,
width: `${(group.colSpan / board.cols) * 100}%`,
height: `${(group.rowSpan / board.rows) * 100}%`,
}}
>
<div
className="pointer-events-none relative grid h-full w-full touch-none overflow-visible active:scale-[0.992]"
style={{
gridTemplateColumns: `repeat(${group.colSpan}, minmax(0, 1fr))`,
gridTemplateRows: `repeat(${group.rowSpan}, minmax(0, 1fr))`,
}}
>
{group.pieces.map((piece) => (
<div
key={piece.pieceId}
className="pointer-events-auto relative touch-none overflow-hidden bg-emerald-300/10"
style={{
gridColumn: piece.localCol + 1,
gridRow: piece.localRow + 1,
}}
onPointerDown={(event) => {
handlePiecePointerDown(piece.pieceId, event);
}}
onPointerMove={(event) => {
handlePiecePointerMove(piece.pieceId, event);
}}
onPointerUp={(event) => {
handlePiecePointerUp(piece.pieceId, event);
}}
onPointerCancel={() => {
resetDragInteraction();
}}
onLostPointerCapture={() => {
resetDragInteraction();
}}
>
{resolvedCoverImage ? (
<div
className="absolute inset-0"
style={{
backgroundImage: `url("${resolvedCoverImage}")`,
backgroundSize: `${board.cols * 100}% ${board.rows * 100}%`,
backgroundPosition: `${
board.cols > 1
? (piece.correctCol / (board.cols - 1)) * 100
: 0
}% ${
board.rows > 1
? (piece.correctRow / (board.rows - 1)) * 100
: 0
}%`,
}}
/>
) : (
<div className="absolute inset-0 bg-[linear-gradient(145deg,rgba(52,211,153,0.38),rgba(6,78,59,0.68))]" />
)}
<div className="absolute inset-0 bg-black/8" />
</div>
))}
<div className="pointer-events-none absolute inset-0 rounded-[1rem] ring-2 ring-emerald-100/58 shadow-[0_0_0_1px_rgba(16,185,129,0.2),0_14px_32px_rgba(6,78,59,0.24)]" />
</div>
</div>
))}
</div>
</div>
<div className="absolute bottom-0 left-0 z-20 flex w-full items-end justify-between gap-3 px-4 py-4">
<div className="max-w-[18rem] rounded-[1.1rem] bg-black/28 px-4 py-3 text-xs leading-6 text-white/74 backdrop-blur">
{selectedPieceId
? '已选择一块,再点另一块可交换;也可以直接拖到目标位置。'
: '点击两块可交换,拖动单块或合并块到目标格继续推进。'}
</div>
<div className="absolute bottom-0 left-0 z-20 flex w-full items-end justify-end gap-3 px-4 py-4">
<div className="flex flex-col items-end gap-2">
{error ? (
<div className="rounded-full bg-red-500/20 px-3 py-1 text-xs text-red-100">
{error}
</div>
) : null}
{selectedPieceId && currentLevel.status !== 'cleared' ? (
<div className="rounded-full bg-black/28 px-3 py-1 text-xs text-white/72 backdrop-blur">
</div>
) : null}
{nextAvailable ? (
<button
type="button"
@@ -389,6 +716,107 @@ export function PuzzleRuntimeShell({
)}
</div>
</div>
{isClearResultOpen ? (
<div className="absolute inset-0 z-40 flex items-center justify-center bg-slate-950/68 px-4 py-6 backdrop-blur-sm">
<section
role="dialog"
aria-modal="true"
aria-labelledby="puzzle-clear-result-title"
className="flex max-h-[min(92vh,42rem)] w-full max-w-[28rem] flex-col overflow-hidden rounded-[1.5rem] border border-white/14 bg-slate-950/94 shadow-[0_28px_90px_rgba(0,0,0,0.5)]"
>
<header className="flex items-start justify-between gap-3 border-b border-white/10 px-5 py-4">
<div className="min-w-0">
<div className="mb-2 inline-flex h-9 w-9 items-center justify-center rounded-full bg-amber-200 text-slate-950">
<Trophy className="h-4 w-4" />
</div>
<h2
id="puzzle-clear-result-title"
className="truncate text-lg font-black text-white"
>
</h2>
<div className="mt-1 line-clamp-1 text-xs text-white/62">
{currentLevel.levelName}
</div>
</div>
<button
type="button"
aria-label="关闭通关弹窗"
className="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-white/8 text-white/72 transition hover:bg-white/14 hover:text-white"
onClick={() => {
setDismissedClearKey(clearResultKey);
}}
>
<X className="h-4 w-4" />
</button>
</header>
<div className="min-h-0 flex-1 overflow-y-auto px-5 py-4">
<div className="flex items-center justify-between gap-4 rounded-[1rem] border border-amber-200/24 bg-amber-200/10 px-4 py-3">
<div className="flex items-center gap-3">
<span className="inline-flex h-9 w-9 items-center justify-center rounded-full bg-black/24 text-amber-100">
<Clock className="h-4 w-4" />
</span>
<span className="text-sm font-semibold text-white/72">
</span>
</div>
<span className="font-mono text-xl font-black text-amber-100">
{formatElapsedMs(currentLevel.elapsedMs)}
</span>
</div>
<div className="mt-4">
<div className="mb-2 text-sm font-bold text-white"></div>
<div className="overflow-hidden rounded-[1rem] border border-white/10">
<div className="grid grid-cols-[3.5rem_minmax(0,1fr)_6rem] bg-white/6 px-3 py-2 text-[11px] font-bold text-white/48">
<span></span>
<span></span>
<span className="text-right"></span>
</div>
<div className="max-h-56 overflow-y-auto">
{leaderboardEntries.map((entry) => (
<div
key={`${entry.rank}:${entry.nickname}:${entry.elapsedMs}`}
className={`grid grid-cols-[3.5rem_minmax(0,1fr)_6rem] items-center px-3 py-2.5 text-sm ${
entry.isCurrentPlayer
? 'bg-amber-200/14 text-amber-50'
: 'border-t border-white/8 text-white/78'
}`}
>
<span className="font-mono font-black">#{entry.rank}</span>
<span className="truncate font-semibold">
{entry.nickname}
</span>
<span className="text-right font-mono text-xs font-bold">
{formatElapsedMs(entry.elapsedMs)}
</span>
</div>
))}
</div>
</div>
</div>
</div>
<footer className="flex items-center justify-end border-t border-white/10 px-5 py-4">
<button
type="button"
disabled={isBusy || !nextAvailable}
onClick={onAdvanceNextLevel}
className="inline-flex items-center gap-2 rounded-full bg-amber-200 px-5 py-2.5 text-sm font-black text-slate-950 transition hover:bg-amber-100 disabled:cursor-not-allowed disabled:opacity-45"
>
{isBusy ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<ArrowRight className="h-4 w-4" />
)}
</button>
</footer>
</section>
</div>
) : null}
</div>
</div>
);

View File

@@ -2070,6 +2070,9 @@ function SceneActPreviewRuntime({
...current,
worldType: WorldType.CUSTOM,
customWorldProfile: profile,
// 中文注释:幕预览只复用运行时表现,不应进入正式存档和个人游玩记录。
runtimeMode: 'preview',
runtimePersistenceDisabled: true,
currentScene: 'Story',
currentScenePreset: previewScenePreset,
currentEncounter: previewEncounter,
@@ -4873,42 +4876,6 @@ export function WorldEditor({
rows={3}
/>
</Field>
<Field label="玩家原始设定">
<TextArea
value={draft.settingText}
onChange={(value) =>
setDraft((current) => ({
...current,
settingText: value,
creatorIntent: current.creatorIntent
? {
...current.creatorIntent,
rawSettingText: value,
}
: current.creatorIntent,
}))
}
rows={4}
/>
</Field>
<WorldAttributeSchemaEditor
value={draft.attributeSchema}
onChange={(attributeSchema) =>
setDraft((current) => ({
...current,
attributeSchema,
ownedSettingLayers: current.ownedSettingLayers
? {
...current.ownedSettingLayers,
ruleProfile: {
...current.ownedSettingLayers.ruleProfile,
attributeSchema,
},
}
: current.ownedSettingLayers,
}))
}
/>
<SaveBar
onClose={onClose}
onSave={() => {
@@ -5134,6 +5101,9 @@ export function WorldFoundationEditor({
}) {
const initialDraft = useMemo(() => buildFoundationDraft(profile), [profile]);
const [draft, setDraft] = useDraft(initialDraft);
const [attributeSchemaDraft, setAttributeSchemaDraft] = useDraft(
profile.attributeSchema,
);
return (
<ModalShell
@@ -5170,10 +5140,27 @@ export function WorldFoundationEditor({
</div>
</Field>
))}
<WorldAttributeSchemaEditor
value={attributeSchemaDraft}
onChange={setAttributeSchemaDraft}
/>
<SaveBar
onClose={onClose}
onSave={() => {
onSave(applyFoundationDraftToProfile(profile, draft));
const nextProfile = applyFoundationDraftToProfile(profile, draft);
onSave({
...nextProfile,
attributeSchema: attributeSchemaDraft,
ownedSettingLayers: nextProfile.ownedSettingLayers
? {
...nextProfile.ownedSettingLayers,
ruleProfile: {
...nextProfile.ownedSettingLayers.ruleProfile,
attributeSchema: attributeSchemaDraft,
},
}
: nextProfile.ownedSettingLayers,
});
onClose();
}}
/>

View File

@@ -883,7 +883,6 @@ export function RpgAdventurePanelOverlays({
setIsSettingsPanelOpen,
isStatsPanelOpen,
setIsStatsPanelOpen,
chapterState,
journeyBeat,
goalStack,
goalPulse,
@@ -1645,7 +1644,7 @@ export function RpgAdventurePanelOverlays({
<div className="mt-3 flex flex-wrap gap-2">
{battleReward.defeatedHostileNpcs.map((hostileNpc) => (
<span
key={`${battleReward.id}-${hostileNpc.id}`}
key={`${battleReward.id}-${hostileNpc.renderKey ?? hostileNpc.id}`}
className="rounded-full border border-emerald-300/20 bg-emerald-500/10 px-3 py-1 text-xs text-emerald-50"
>
{hostileNpc.name}

View File

@@ -38,9 +38,9 @@ import {
ItemUseProfile,
KnowledgeFact,
RoleAttributeProfile,
SceneNarrativeResidue,
SceneActBlueprint,
SceneChapterBlueprint,
SceneNarrativeResidue,
ThemePack,
ThreadContract,
WorldStoryGraph,
@@ -990,7 +990,7 @@ function normalizeSceneActBlueprint(
return {
id: toText(value.id, `saved-scene-act-${sceneId}-${index + 1}`),
sceneId,
sceneId: toText(value.sceneId, sceneId),
title: title || `${index + 1}`,
summary: summary || title || `围绕${sceneId}继续推进`,
stageCoverage:

View File

@@ -1,16 +1,20 @@
import { describe, expect, it } from 'vitest';
import { resolveActiveSceneActEncounterNpcIds } from '../services/customWorldSceneActRuntime';
import {
AnimationState,
type Character,
type CustomWorldProfile,
type Encounter,
type GameState,
type SceneNpc,
WorldType,
} from '../types';
import { getMonsterPresetsByWorld } from './hostileNpcPresets';
import { createSceneHostileNpc } from './hostileNpcs';
import { buildInitialNpcState } from './npcInteractions';
import {
createSceneEncounterPreview,
hasAutoBattleSceneEncounter,
resolveSceneEncounterPreview,
} from './sceneEncounterPreviews';
@@ -150,5 +154,345 @@ describe('sceneEncounterPreviews', () => {
expect(monster?.encounter?.hostile).toBe(true);
expect(monster?.encounter?.initialAffinity).toBe(-40);
});
});
it('resolves active act npc ids when runtime scene id differs from landmark id', () => {
const profile = {
id: 'custom-profile',
name: '测试世界',
settingText: '',
subtitle: '',
summary: '',
tone: '',
playerGoal: '',
templateWorldType: WorldType.WUXIA,
majorFactions: [],
coreConflicts: [],
attributeSchema: {
attributes: [],
},
playableNpcs: [],
storyNpcs: [],
items: [],
landmarks: [
{
id: 'landmark-raw-1',
name: '旧桥',
description: '旧桥',
sceneNpcIds: ['npc-front', 'npc-back-1', 'npc-back-2'],
connections: [],
},
],
sceneChapterBlueprints: [
{
id: 'chapter-1',
sceneId: 'landmark-raw-1',
title: '旧桥章节',
summary: '',
sceneTaskDescription: '',
linkedThreadIds: [],
linkedLandmarkIds: ['landmark-raw-1'],
acts: [
{
id: 'act-1',
sceneId: 'landmark-raw-1',
title: '第一幕',
summary: '',
stageCoverage: ['opening'],
encounterNpcIds: ['npc-front', 'npc-back-1', 'npc-back-2'],
primaryNpcId: 'npc-front',
oppositeNpcId: 'npc-front',
eventDescription: '',
linkedThreadIds: [],
advanceRule: 'after_primary_contact',
actGoal: '',
transitionHook: '',
},
],
},
],
} as CustomWorldProfile;
expect(
resolveActiveSceneActEncounterNpcIds({
profile,
sceneId: 'custom-scene-landmark-1',
}),
).toEqual(['npc-front', 'npc-back-1', 'npc-back-2']);
});
it('resolves active act npc ids from act scene id even when chapter scene id is abstract', () => {
const profile = {
id: 'custom-profile',
name: '测试世界',
settingText: '',
subtitle: '',
summary: '',
tone: '',
playerGoal: '',
templateWorldType: WorldType.WUXIA,
majorFactions: [],
coreConflicts: [],
attributeSchema: {
attributes: [],
},
playableNpcs: [],
storyNpcs: [],
items: [],
landmarks: [
{
id: 'landmark-raw-1',
name: '旧桥',
description: '旧桥',
sceneNpcIds: [],
connections: [],
},
],
sceneChapterBlueprints: [
{
id: 'chapter-1',
sceneId: 'chapter-abstract-scene',
title: '旧桥章节',
summary: '',
sceneTaskDescription: '',
linkedThreadIds: [],
linkedLandmarkIds: [],
acts: [
{
id: 'act-1',
sceneId: 'landmark-raw-1',
title: '第一幕',
summary: '',
stageCoverage: ['opening'],
encounterNpcIds: [],
primaryNpcId: '',
oppositeNpcId: 'npc-front',
eventDescription: '',
linkedThreadIds: [],
advanceRule: 'after_primary_contact',
actGoal: '',
transitionHook: '',
},
],
},
],
} as CustomWorldProfile;
expect(
resolveActiveSceneActEncounterNpcIds({
profile,
sceneId: 'custom-scene-landmark-1',
}),
).toEqual(['npc-front']);
});
it('uses the active act opposite npc as the formal scene encounter', () => {
const state = {
...createBaseState(),
worldType: WorldType.CUSTOM,
customWorldProfile: {
id: 'custom-profile',
name: '测试世界',
settingText: '',
subtitle: '',
summary: '',
tone: '',
playerGoal: '',
templateWorldType: WorldType.WUXIA,
majorFactions: [],
coreConflicts: [],
attributeSchema: {
attributes: [],
},
playableNpcs: [],
storyNpcs: [],
items: [],
landmarks: [
{
id: 'landmark-raw-1',
name: '旧桥',
description: '旧桥',
sceneNpcIds: ['npc-front', 'npc-back-1', 'npc-back-2'],
connections: [],
},
],
sceneChapterBlueprints: [
{
id: 'chapter-1',
sceneId: 'landmark-raw-1',
title: '旧桥章节',
summary: '',
sceneTaskDescription: '',
linkedThreadIds: [],
linkedLandmarkIds: ['landmark-raw-1'],
acts: [
{
id: 'act-1',
sceneId: 'landmark-raw-1',
title: '第一幕',
summary: '',
stageCoverage: ['opening'],
encounterNpcIds: ['npc-front', 'npc-back-1', 'npc-back-2'],
primaryNpcId: 'npc-back-1',
oppositeNpcId: 'npc-front',
eventDescription: '',
linkedThreadIds: [],
advanceRule: 'after_primary_contact',
actGoal: '',
transitionHook: '',
},
],
},
],
} as CustomWorldProfile,
currentEncounter: null,
currentScenePreset: {
id: 'custom-scene-landmark-1',
name: '旧桥',
description: '旧桥',
imageSrc: '/bridge.png',
connectedSceneIds: [],
npcs: [
{
id: 'hostile-side',
name: '旁路敌人',
description: '旁路敌人',
avatar: '敌',
role: '敌对角色',
monsterPresetId: 'monster-01',
initialAffinity: -40,
hostile: true,
},
{
id: 'npc-back-1',
name: '后排甲',
description: '后排甲',
avatar: '甲',
role: '同幕角色',
},
{
id: 'npc-front',
name: '主角色',
description: '主角色',
avatar: '主',
role: '主角色',
},
{
id: 'npc-back-2',
name: '后排乙',
description: '后排乙',
avatar: '乙',
role: '同幕角色',
},
] satisfies SceneNpc[],
treasureHints: [],
},
} satisfies GameState;
const preview = createSceneEncounterPreview(state);
expect(preview.currentEncounter?.id).toBe('npc-front');
expect(preview.currentEncounter?.npcName).toBe('主角色');
});
it('uses active act opposite npc even when that npc is hostile', () => {
const state = {
...createBaseState(),
worldType: WorldType.CUSTOM,
customWorldProfile: {
id: 'custom-profile',
name: '测试世界',
settingText: '',
subtitle: '',
summary: '',
tone: '',
playerGoal: '',
templateWorldType: WorldType.WUXIA,
majorFactions: [],
coreConflicts: [],
attributeSchema: {
attributes: [],
},
playableNpcs: [],
storyNpcs: [],
items: [],
landmarks: [],
sceneChapterBlueprints: [
{
id: 'chapter-1',
sceneId: 'custom-scene-camp',
title: '开局章节',
summary: '',
sceneTaskDescription: '',
linkedThreadIds: [],
linkedLandmarkIds: [],
acts: [
{
id: 'act-1',
sceneId: 'custom-scene-camp',
title: '第一幕',
summary: '',
stageCoverage: ['opening'],
encounterNpcIds: ['npc-hostile-opposite', 'npc-back'],
primaryNpcId: 'npc-back',
oppositeNpcId: 'npc-hostile-opposite',
eventDescription: '',
linkedThreadIds: [],
advanceRule: 'after_primary_contact',
actGoal: '',
transitionHook: '',
},
],
},
],
} as CustomWorldProfile,
currentEncounter: null,
currentScenePreset: {
id: 'custom-scene-camp',
name: '营地',
description: '营地',
imageSrc: '/camp.png',
connectedSceneIds: [],
npcs: [
{
id: 'npc-hostile-opposite',
name: '敌意对面角色',
description: '第一幕先开口的敌意角色',
avatar: '敌',
role: '第一幕对面角色',
initialAffinity: -20,
hostile: true,
},
{
id: 'npc-back',
name: '后排角色',
description: '同幕后排角色',
avatar: '后',
role: '同幕角色',
},
] satisfies SceneNpc[],
treasureHints: [],
},
} satisfies GameState;
const preview = createSceneEncounterPreview(state);
const resolved = resolveSceneEncounterPreview({
...state,
...preview,
npcStates: {
'npc-hostile-opposite': {
...buildInitialNpcState(
preview.currentEncounter!,
WorldType.CUSTOM,
state,
),
affinity: -20,
},
},
});
expect(preview.currentEncounter?.id).toBe('npc-hostile-opposite');
expect(preview.currentEncounter?.npcName).toBe('敌意对面角色');
expect(resolved.inBattle).toBe(false);
expect(resolved.currentEncounter?.id).toBe('npc-hostile-opposite');
});
});

View File

@@ -1,3 +1,8 @@
import {
canUseLimitedPrimaryNpcChat,
resolveActiveSceneActEncounterFocusNpcId,
resolveActiveSceneActEncounterNpcIds,
} from '../services/customWorldSceneActRuntime';
import { AnimationState, Encounter, GameState, SceneNpc, WorldType } from '../types';
import { getRecruitedNpcIds } from './companionRoster';
import {
@@ -15,10 +20,6 @@ import {
getSceneHostileNpcs,
getWorldCampScenePreset,
} from './scenePresets';
import {
canUseLimitedPrimaryNpcChat,
resolveActiveSceneActEncounterNpcIds,
} from '../services/customWorldSceneActRuntime';
export const EXPLORE_APPROACH_DURATION_MS = 4000;
export const PREVIEW_ENTITY_X_METERS = 12;
@@ -115,7 +116,11 @@ function getAvailableFriendlySceneNpcs(state: GameState) {
const activeActNpcIdSet = new Set(activeActNpcIds);
return getSceneFriendlyNpcs(state.currentScenePreset)
.filter(candidate => !isCampScene || Boolean(candidate.characterId))
.filter(candidate =>
!isCampScene ||
Boolean(candidate.characterId) ||
activeActNpcIdSet.has(candidate.id),
)
.filter(candidate => candidate.characterId !== state.playerCharacter?.id)
.filter(candidate => !recruitedNpcIds.has(candidate.id))
.filter(candidate =>
@@ -126,6 +131,29 @@ function getAvailableFriendlySceneNpcs(state: GameState) {
);
}
function getAvailableActiveSceneActNpcs(state: GameState) {
const recruitedNpcIds = getRecruitedNpcIds(state);
const activeActNpcIds = resolveActiveSceneActEncounterNpcIds({
profile: state.customWorldProfile,
sceneId: state.currentScenePreset?.id ?? null,
storyEngineMemory: state.storyEngineMemory,
});
const activeActNpcIdSet = new Set(activeActNpcIds);
if (activeActNpcIdSet.size === 0) {
return [];
}
return (state.currentScenePreset?.npcs ?? [])
.filter(candidate => {
const candidateIds = [candidate.id, candidate.characterId].filter(
(value): value is string => Boolean(value),
);
return candidateIds.some(id => activeActNpcIdSet.has(id));
})
.filter(candidate => candidate.characterId !== state.playerCharacter?.id)
.filter(candidate => !recruitedNpcIds.has(candidate.id));
}
function getAvailableHostileSceneNpcs(state: GameState) {
const recruitedNpcIds = getRecruitedNpcIds(state);
@@ -142,6 +170,54 @@ function pickEncounterHostileNpcs(hostileNpcs: Array<SceneNpc & { monsterPresetI
return hostileNpcs.filter(npc => selectedMonsterIds.has(npc.monsterPresetId));
}
function pickFriendlySceneNpcForActiveAct(state: GameState, npcs: SceneNpc[]) {
const focusNpcId = resolveActiveSceneActEncounterFocusNpcId({
profile: state.customWorldProfile,
sceneId: state.currentScenePreset?.id ?? null,
storyEngineMemory: state.storyEngineMemory,
});
return (
npcs.find(
(npc) =>
npc.id === focusNpcId ||
(npc.characterId ? npc.characterId === focusNpcId : false),
) ?? pickRandomItem(npcs)
);
}
function hasActiveSceneActEncounterTarget(state: GameState) {
return resolveActiveSceneActEncounterNpcIds({
profile: state.customWorldProfile,
sceneId: state.currentScenePreset?.id ?? null,
storyEngineMemory: state.storyEngineMemory,
}).length > 0;
}
function buildEmptyEncounterPreview() {
return {
sceneHostileNpcs: [],
currentEncounter: null,
npcInteractionActive: false,
inBattle: false,
};
}
function buildActiveSceneActNpcEncounter(
state: GameState,
availableNpcs: SceneNpc[],
xMeters: number,
) {
const npc = pickFriendlySceneNpcForActiveAct(state, availableNpcs);
return {
sceneHostileNpcs: [],
currentEncounter: npc ? buildFriendlyEncounter(npc, xMeters) : null,
npcInteractionActive: false,
inBattle: false,
};
}
function buildHostileEncounterGroup(
state: GameState,
entryX: number,
@@ -218,12 +294,15 @@ function buildResolvedHostileBattleState(state: GameState, hostileEncounters: En
export function createSceneEncounterPreview(state: GameState) {
if (!state.worldType || !state.currentScenePreset) {
return {
sceneHostileNpcs: [],
currentEncounter: null,
npcInteractionActive: false,
inBattle: false,
};
return buildEmptyEncounterPreview();
}
if (hasActiveSceneActEncounterTarget(state)) {
return buildActiveSceneActNpcEncounter(
state,
getAvailableActiveSceneActNpcs(state),
PREVIEW_ENTITY_X_METERS,
);
}
const availableNpcs = getAvailableFriendlySceneNpcs(state);
@@ -237,12 +316,7 @@ export function createSceneEncounterPreview(state: GameState) {
const kind = pickRandomItem(availableKinds);
if (!kind) {
return {
sceneHostileNpcs: [],
currentEncounter: null,
npcInteractionActive: false,
inBattle: false,
};
return buildEmptyEncounterPreview();
}
if (kind === 'hostile') {
@@ -255,7 +329,7 @@ export function createSceneEncounterPreview(state: GameState) {
}
if (kind === 'npc') {
const npc = pickRandomItem(availableNpcs);
const npc = pickFriendlySceneNpcForActiveAct(state, availableNpcs);
return {
sceneHostileNpcs: [],
@@ -276,19 +350,22 @@ export function createSceneEncounterPreview(state: GameState) {
export function createSceneCallOutEncounter(state: GameState) {
if (!state.worldType || !state.currentScenePreset) {
return {
sceneHostileNpcs: [],
currentEncounter: null,
npcInteractionActive: false,
inBattle: false,
};
return buildEmptyEncounterPreview();
}
if (hasActiveSceneActEncounterTarget(state)) {
return buildActiveSceneActNpcEncounter(
state,
getAvailableActiveSceneActNpcs(state),
CALL_OUT_ENTRY_X_METERS,
);
}
const availableNpcs = getAvailableFriendlySceneNpcs(state);
const availableKinds: Array<'hostile' | 'npc' | 'treasure'> = [];
const availableHostiles = getAvailableHostileSceneNpcs(state);
if (availableHostiles.length > 0) availableKinds.push('hostile');
const availableNpcs = getAvailableFriendlySceneNpcs(state);
if (availableNpcs.length > 0) availableKinds.push('npc');
if (TREASURE_ENCOUNTERS_ENABLED && (state.currentScenePreset.treasureHints?.length ?? 0) > 0) {
availableKinds.push('treasure');
@@ -305,7 +382,7 @@ export function createSceneCallOutEncounter(state: GameState) {
}
if (kind === 'npc') {
const npc = pickRandomItem(availableNpcs);
const npc = pickFriendlySceneNpcForActiveAct(state, availableNpcs);
return {
sceneHostileNpcs: [],
currentEncounter: npc ? buildFriendlyEncounter(npc, CALL_OUT_ENTRY_X_METERS) : null,

View File

@@ -381,6 +381,40 @@ function buildCustomSceneId(kind: 'camp' | 'landmark', index = 0) {
return kind === 'camp' ? 'custom-scene-camp' : `custom-scene-landmark-${index + 1}`;
}
function collectSceneActNpcIdsForScene(
profile: CustomWorldProfile,
sceneAliases: string[],
) {
const aliasSet = new Set(sceneAliases.map((alias) => alias.trim()).filter(Boolean));
const npcIds: string[] = [];
const pushNpcId = (npcId: string | null | undefined) => {
const normalizedNpcId = npcId?.trim() ?? '';
if (normalizedNpcId && !npcIds.includes(normalizedNpcId)) {
npcIds.push(normalizedNpcId);
}
};
profile.sceneChapterBlueprints?.forEach((chapter) => {
const chapterSceneIds = [
chapter.sceneId,
...chapter.linkedLandmarkIds,
...chapter.acts.map((act) => act.sceneId),
].map((sceneId) => sceneId.trim()).filter(Boolean);
if (!chapterSceneIds.some((sceneId) => aliasSet.has(sceneId))) {
return;
}
chapter.acts.forEach((act) => {
pushNpcId(act.primaryNpcId);
pushNpcId(act.oppositeNpcId);
act.encounterNpcIds.forEach(pushNpcId);
});
});
return npcIds;
}
function buildCustomScenePresets(profile: CustomWorldProfile): ScenePreset[] {
const campSceneProfile = resolveCustomWorldCampScene(profile);
const landmarkImageMap = resolveCustomWorldLandmarkImageMap(profile);
@@ -403,6 +437,37 @@ function buildCustomScenePresets(profile: CustomWorldProfile): ScenePreset[] {
const customStoryNpcById = new Map(
profile.storyNpcs.map((npc) => [npc.id, npc]),
);
const buildCustomSceneNpcByRoleId = (roleId: string) => {
const storyNpc = customStoryNpcById.get(roleId);
if (storyNpc) {
return buildCustomSceneNpc(storyNpc, profile);
}
const playableNpc = profile.playableNpcs.find((npc) => npc.id === roleId);
if (!playableNpc) {
return null;
}
return buildCharacterNpc(playableNpc.id, WorldType.CUSTOM, profile);
};
const pushUniqueSceneNpc = (sceneNpcs: SceneNpc[], npc: SceneNpc | null) => {
if (!npc) {
return;
}
const candidateIds = [npc.id, npc.characterId].filter(Boolean);
if (
sceneNpcs.some((sceneNpc) =>
[sceneNpc.id, sceneNpc.characterId]
.filter(Boolean)
.some((sceneNpcId) => candidateIds.includes(sceneNpcId)),
)
) {
return;
}
sceneNpcs.push(npc);
};
const campNpcs = playableCharacters.slice(1).map(character => {
const npc = buildCharacterNpc(character.id, WorldType.CUSTOM, profile);
return npc
@@ -413,6 +478,12 @@ function buildCustomScenePresets(profile: CustomWorldProfile): ScenePreset[] {
}
: null;
}).filter(Boolean) as SceneNpc[];
collectSceneActNpcIdsForScene(profile, [
campSceneId,
profile.camp?.id ?? '',
]).forEach((npcId) =>
pushUniqueSceneNpc(campNpcs, buildCustomSceneNpcByRoleId(npcId)),
);
const campConnections = profile.landmarks
.slice(0, 3)
@@ -445,12 +516,20 @@ function buildCustomScenePresets(profile: CustomWorldProfile): ScenePreset[] {
npcs: campNpcs,
},
...profile.landmarks.map((landmark, index): ScenePreset => {
const sceneNpcs = landmark.sceneNpcIds
const runtimeSceneId = buildCustomSceneId('landmark', index);
const sceneActNpcIds = collectSceneActNpcIdsForScene(profile, [
landmark.id,
runtimeSceneId,
]);
const sceneNpcs = [...sceneActNpcIds, ...landmark.sceneNpcIds]
.map((npcId) => customStoryNpcById.get(npcId))
.filter(Boolean)
.map((npc) =>
buildCustomSceneNpc(npc!, profile),
);
sceneActNpcIds.forEach((npcId) =>
pushUniqueSceneNpc(sceneNpcs, buildCustomSceneNpcByRoleId(npcId)),
);
if (sceneNpcs.length < 3) {
profile.storyNpcs
.filter(
@@ -499,12 +578,12 @@ function buildCustomScenePresets(profile: CustomWorldProfile): ScenePreset[] {
const monsterSliceStart = (index * 2) % Math.max(1, fallbackMonsterIds.length || 1);
const seedMonsterIds: string[] = fallbackMonsterIds.slice(monsterSliceStart, monsterSliceStart + 2);
const hostileNpcs = seedMonsterIds
.map((monsterId: string) => buildHostileSceneNpc(buildCustomSceneId('landmark', index), WorldType.CUSTOM, monsterId))
.map((monsterId: string) => buildHostileSceneNpc(runtimeSceneId, WorldType.CUSTOM, monsterId))
.filter(Boolean) as SceneNpc[];
const combinedNpcs = [...sceneNpcs, ...hostileNpcs];
return {
id: buildCustomSceneId('landmark', index),
id: runtimeSceneId,
name: landmark.name,
description: landmark.description,
worldType: WorldType.CUSTOM,
@@ -521,7 +600,7 @@ function buildCustomScenePresets(profile: CustomWorldProfile): ScenePreset[] {
landmark.narrativeResidues && landmark.narrativeResidues.length > 0
? landmark.narrativeResidues
: buildSceneNarrativeResidues({
sceneId: buildCustomSceneId('landmark', index),
sceneId: runtimeSceneId,
sceneName: landmark.name,
profile,
}),

View File

@@ -272,6 +272,35 @@ describe('storyChoiceRuntime', () => {
);
});
it('keeps defeated hostile reward render keys unique for duplicate monster ids', async () => {
rollHostileNpcLootMock.mockResolvedValue([]);
const reward = await buildHostileNpcBattleReward(
createState({
inBattle: true,
sceneHostileNpcs: [
{ id: 'monster-16', name: '雷翼甲' },
{ id: 'monster-16', name: '雷翼乙', xMeters: 4.1, yOffset: 32 },
] as GameState['sceneHostileNpcs'],
currentBattleNpcId: null,
}),
createState({
inBattle: false,
sceneHostileNpcs: [],
}),
'battle',
(state) => state.sceneHostileNpcs,
);
expect(reward?.defeatedHostileNpcs).toHaveLength(2);
expect(reward?.defeatedHostileNpcs.map((npc) => npc.id)).toEqual([
'monster-16',
'monster-16',
]);
expect(new Set(reward?.defeatedHostileNpcs.map((npc) => npc.renderKey)).size)
.toBe(2);
});
it('applies server runtime responses and falls back locally when the request fails', async () => {
const gameState = createState();
const currentStory = createStory('当前故事');

View File

@@ -165,9 +165,17 @@ export async function buildHostileNpcBattleReward(
id: `battle-reward-${Date.now()}-${Math.random()
.toString(36)
.slice(2, 8)}`,
defeatedHostileNpcs: defeatedHostileNpcs.map((hostileNpc) => ({
defeatedHostileNpcs: defeatedHostileNpcs.map((hostileNpc, index) => ({
id: hostileNpc.id,
name: hostileNpc.name,
// 中文注释:同一场战斗可能击败多个同 preset 怪物,奖励弹层 key 不能只用怪物 id。
renderKey: [
hostileNpc.id,
hostileNpc.name,
hostileNpc.xMeters,
hostileNpc.yOffset ?? 0,
index,
].join(':'),
})),
items: addInventoryItems([], rolledItems),
};

View File

@@ -95,7 +95,7 @@ export interface GoalFlowUi {
export interface BattleRewardSummary {
id: string;
defeatedHostileNpcs: Array<{ id: string; name: string }>;
defeatedHostileNpcs: Array<{ id: string; name: string; renderKey?: string }>;
items: InventoryItem[];
}

View File

@@ -0,0 +1,213 @@
/* @vitest-environment jsdom */
import { render, screen, waitFor } from '@testing-library/react';
import { useState } from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { StoryGenerationContext } from '../../services/aiTypes';
import type { Character, Encounter, GameState, StoryMoment } from '../../types';
import { AnimationState, WorldType } from '../../types';
import { useRpgRuntimeStoryController } from './useRpgRuntimeStoryController';
const aiServiceMocks = vi.hoisted(() => ({
generateInitialStory: vi.fn(),
generateNextStep: vi.fn(),
}));
vi.mock('../../services/aiService', async () => {
const actual =
await vi.importActual<typeof import('../../services/aiService')>(
'../../services/aiService',
);
return {
...actual,
generateInitialStory: aiServiceMocks.generateInitialStory,
generateNextStep: aiServiceMocks.generateNextStep,
};
});
function createCharacter(): Character {
return {
id: 'hero',
name: '沈行',
title: '试剑客',
description: '在风声里辨认危险的旅人。',
personality: '谨慎而果断',
skills: [],
} as unknown as Character;
}
function createGameState(params: {
currentEncounter?: Encounter | null;
} = {}): GameState {
return {
worldType: WorldType.CUSTOM,
customWorldProfile: null,
playerCharacter: createCharacter(),
currentScene: 'Story',
storyHistory: [],
animationState: AnimationState.IDLE,
currentEncounter: params.currentEncounter ?? null,
npcInteractionActive: false,
currentScenePreset: {
id: 'scene-opening',
name: '证券交易所大厅',
description: '高耸大厅里仍残留着开盘前的低声交谈。',
imageSrc: '',
npcs: [],
hostileNpcIds: [],
},
sceneHostileNpcs: [],
playerX: 0,
playerOffsetY: 0,
playerFacing: 'right',
playerActionMode: 'idle',
scrollWorld: false,
inBattle: false,
playerHp: 100,
playerMaxHp: 100,
playerMana: 20,
playerMaxMana: 20,
playerSkillCooldowns: {},
activeCombatEffects: [],
playerInventory: [],
playerEquipment: {
weapon: null,
armor: null,
relic: null,
},
playerCurrency: 0,
npcStates: {},
quests: [],
roster: [],
companions: [],
} as unknown as GameState;
}
function buildStoryContextFromState(
_state: GameState,
): StoryGenerationContext {
return {
playerHp: 100,
playerMaxHp: 100,
playerMana: 20,
playerMaxMana: 20,
inBattle: false,
playerX: 0,
playerFacing: 'right',
playerAnimation: 'idle',
skillCooldowns: {},
sceneId: 'scene-opening',
sceneName: '证券交易所大厅',
sceneDescription: '高耸大厅里仍残留着开盘前的低声交谈。',
pendingSceneEncounter: false,
} as StoryGenerationContext;
}
function Harness() {
const [gameState, setGameState] = useState<GameState>(() =>
createGameState(),
);
const controller = useRpgRuntimeStoryController({
gameState,
setGameState,
buildStoryContextFromState,
});
return (
<div>
<div data-testid="loading">{controller.isLoading ? 'yes' : 'no'}</div>
<div data-testid="story">{controller.currentStory?.text ?? ''}</div>
<button
type="button"
onClick={() =>
setGameState((current) => ({
...current,
currentScene: 'Selection',
playerCharacter: null,
}))
}
>
</button>
</div>
);
}
function HarnessWithEncounter() {
const encounter = {
id: 'npc-luheng',
kind: 'npc',
npcName: '陆衡',
npcDescription: '正在核对异常账本的人。',
npcAvatar: '',
context: '第一幕主NPC',
} satisfies Encounter;
const [gameState, setGameState] = useState<GameState>(() =>
createGameState({
currentEncounter: encounter,
}),
);
const controller = useRpgRuntimeStoryController({
gameState,
setGameState,
buildStoryContextFromState,
});
return (
<div>
<div data-testid="loading">{controller.isLoading ? 'yes' : 'no'}</div>
<div data-testid="story">{controller.currentStory?.text ?? ''}</div>
</div>
);
}
describe('useRpgRuntimeStoryController', () => {
beforeEach(() => {
vi.clearAllMocks();
aiServiceMocks.generateInitialStory.mockResolvedValue({
storyText: '大厅里的报价屏忽明忽暗,一条异常交易记录浮了上来。',
options: [],
} satisfies { storyText: string; options: StoryMoment['options'] });
aiServiceMocks.generateNextStep.mockResolvedValue({
storyText: '下一步剧情',
options: [],
});
});
it('进入 Story 场景且首段剧情为空时自动请求开局剧情', async () => {
render(<Harness />);
await waitFor(() => {
expect(aiServiceMocks.generateInitialStory).toHaveBeenCalledTimes(1);
});
expect(aiServiceMocks.generateInitialStory).toHaveBeenCalledWith(
WorldType.CUSTOM,
expect.objectContaining({ id: 'hero' }),
[],
expect.objectContaining({
sceneId: 'scene-opening',
sceneName: '证券交易所大厅',
}),
undefined,
);
await waitFor(() => {
expect(screen.getByTestId('story').textContent).toContain(
'异常交易记录',
);
});
expect(screen.getByTestId('loading').textContent).toBe('no');
});
it('已有当前幕 NPC 遭遇时不抢先请求普通开局剧情', async () => {
render(<HarnessWithEncounter />);
await new Promise((resolve) => window.setTimeout(resolve, 20));
expect(aiServiceMocks.generateInitialStory).not.toHaveBeenCalled();
expect(screen.getByTestId('story').textContent).toBe('');
expect(screen.getByTestId('loading').textContent).toBe('no');
});
});

View File

@@ -1,4 +1,12 @@
import { useCallback, useMemo, useState, type Dispatch, type SetStateAction } from 'react';
import {
type Dispatch,
type SetStateAction,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { generateInitialStory, generateNextStep } from '../../services/aiService';
import type { StoryGenerationContext } from '../../services/aiTypes';
@@ -7,6 +15,7 @@ import {
appendStoryHistory,
createStoryProgressionActions,
} from './progressionActions';
import type { StoryContextBuilderExtras } from './storyContextBuilder';
import {
createStoryStateResolvers,
getStoryGenerationHostileNpcs,
@@ -16,9 +25,8 @@ import {
buildStoryFromResponse as buildStoryFromResponseFromPresentation,
getTypewriterDelay,
} from './storyPresentation';
import { buildNpcStory } from './storyRuntimeSupport';
import { createGenerateStoryForState } from './storyRequestRuntime';
import type { StoryContextBuilderExtras } from './storyContextBuilder';
import { buildNpcStory } from './storyRuntimeSupport';
type BuildStoryContextFromState = (
state: GameState,
@@ -39,6 +47,7 @@ export function useRpgRuntimeStoryController(params: {
const [currentStory, setCurrentStory] = useState<StoryMoment | null>(null);
const [aiError, setAiError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const openingStoryRequestKeyRef = useRef<string | null>(null);
const { getAvailableOptionsForState, buildFallbackStoryForState } = useMemo(
() =>
@@ -104,6 +113,80 @@ export function useRpgRuntimeStoryController(params: {
buildFallbackStoryForState,
});
useEffect(() => {
const playerCharacter = gameState.playerCharacter;
if (
!playerCharacter ||
!gameState.worldType ||
gameState.currentScene !== 'Story' ||
gameState.currentEncounter
) {
openingStoryRequestKeyRef.current = null;
return;
}
if (currentStory) {
return;
}
const requestKey = [
gameState.runtimeSessionId ?? 'local',
playerCharacter.id,
gameState.currentScenePreset?.id ?? 'scene',
gameState.storyHistory.length,
].join(':');
if (openingStoryRequestKeyRef.current === requestKey) {
return;
}
openingStoryRequestKeyRef.current = requestKey;
let cancelled = false;
// 首段剧情属于运行态启动数据;这里补齐后,冒险面板才会按真实 story 挂载。
setAiError(null);
setIsLoading(true);
void generateStoryForState({
state: gameState,
character: playerCharacter,
history: gameState.storyHistory,
})
.then((openingStory) => {
if (!cancelled) {
setCurrentStory(openingStory);
}
})
.catch((error) => {
if (cancelled) {
return;
}
console.error('Failed to start opening RPG story:', error);
setAiError(error instanceof Error ? error.message : '未知智能生成错误');
setCurrentStory(buildFallbackStoryForState(gameState, playerCharacter));
})
.finally(() => {
if (!cancelled) {
if (openingStoryRequestKeyRef.current === requestKey) {
openingStoryRequestKeyRef.current = null;
}
setIsLoading(false);
}
});
return () => {
cancelled = true;
if (openingStoryRequestKeyRef.current === requestKey) {
openingStoryRequestKeyRef.current = null;
}
};
}, [
buildFallbackStoryForState,
currentStory,
gameState,
generateStoryForState,
]);
return {
currentStory,
setCurrentStory,

View File

@@ -24,7 +24,13 @@ import {
ensureSceneEncounterPreview,
RESOLVED_ENTITY_X_METERS,
} from '../../data/sceneEncounterPreviews';
import { getScenePreset, getWorldCampScenePreset } from '../../data/scenePresets';
import {
buildEncounterFromSceneNpc,
getScenePreset,
getScenePresetById,
getWorldCampScenePreset,
} from '../../data/scenePresets';
import { buildInitialSceneActRuntimeState } from '../../services/customWorldSceneActRuntime';
import { createEmptyStoryEngineMemoryState } from '../../services/storyEngine/visibilityEngine';
import {
AnimationState,
@@ -34,6 +40,8 @@ import {
EquipmentLoadout,
GameState,
InventoryItem,
SceneActBlueprint,
SceneChapterBlueprint,
SceneNpc,
WorldType,
} from '../../types';
@@ -208,6 +216,220 @@ function createInitialGameState(): GameState {
};
}
function resolveOpeningActScenePreset(
profile: CustomWorldProfile | null,
): NonNullable<GameState['currentScenePreset']> | null {
if (!profile) {
return null;
}
const openingChapter = profile.sceneChapterBlueprints?.[0] ?? null;
const openingSceneIds = [
openingChapter?.acts[0]?.sceneId,
openingChapter?.sceneId,
...(openingChapter?.linkedLandmarkIds ?? []),
]
.map((sceneId) => sceneId?.trim() ?? '')
.filter(Boolean);
for (const sceneId of openingSceneIds) {
const directScene = resolveCustomWorldScenePresetByConfiguredId(
profile,
sceneId,
);
if (directScene) {
return directScene;
}
}
const fallbackLandmarkIndex = profile.landmarks.findIndex(
(landmark) => landmark.sceneNpcIds.length > 0,
);
if (fallbackLandmarkIndex >= 0) {
return getScenePresetById(
WorldType.CUSTOM,
`custom-scene-landmark-${fallbackLandmarkIndex + 1}`,
);
}
const firstLandmarkId = profile.landmarks[0]?.id?.trim() ?? '';
if (firstLandmarkId) {
const firstLandmarkScene = getScenePresetById(
WorldType.CUSTOM,
'custom-scene-landmark-1',
);
if (firstLandmarkScene) {
return firstLandmarkScene;
}
}
return profile.landmarks.length > 0
? getScenePresetById(WorldType.CUSTOM, 'custom-scene-landmark-1')
: null;
}
function resolveOpeningSceneActBlueprint(
profile: CustomWorldProfile | null,
): { chapter: SceneChapterBlueprint; act: SceneActBlueprint } | null {
const openingChapter = profile?.sceneChapterBlueprints?.[0] ?? null;
const openingAct = openingChapter?.acts[0] ?? null;
return openingChapter && openingAct
? { chapter: openingChapter, act: openingAct }
: null;
}
function resolveCustomWorldScenePresetByConfiguredId(
profile: CustomWorldProfile,
sceneId: string | null | undefined,
): NonNullable<GameState['currentScenePreset']> | null {
const normalizedSceneId = sceneId?.trim() ?? '';
if (!normalizedSceneId) {
return null;
}
const directScene = getScenePresetById(WorldType.CUSTOM, normalizedSceneId);
if (directScene) {
return directScene;
}
const campId = profile.camp?.id?.trim() ?? '';
if (
normalizedSceneId === campId ||
normalizedSceneId === 'custom-scene-camp'
) {
return getScenePresetById(WorldType.CUSTOM, 'custom-scene-camp');
}
const landmarkIndex = profile.landmarks.findIndex(
(landmark) => landmark.id === normalizedSceneId,
);
if (landmarkIndex < 0) {
return null;
}
return getScenePresetById(
WorldType.CUSTOM,
`custom-scene-landmark-${landmarkIndex + 1}`,
);
}
function resolveOpeningActNpcIdPriority(openingAct: SceneActBlueprint) {
return [
openingAct.oppositeNpcId,
openingAct.primaryNpcId,
...openingAct.encounterNpcIds,
]
.map((npcId) => npcId.trim())
.filter((npcId, index, list) => npcId && list.indexOf(npcId) === index);
}
function findSceneNpcByRuntimeRoleId(
scenePreset: GameState['currentScenePreset'],
roleId: string,
) {
return (
scenePreset?.npcs?.find(
(npc) => npc.id === roleId || npc.characterId === roleId,
) ?? null
);
}
function buildOpeningEncounterFromCustomWorldRole(
profile: CustomWorldProfile,
roleId: string,
): Encounter | null {
const role =
profile.storyNpcs.find((npc) => npc.id === roleId) ??
profile.playableNpcs.find((npc) => npc.id === roleId) ??
null;
if (!role) {
return null;
}
const isHostile = role.initialAffinity < 0;
return {
id: role.id,
kind: 'npc',
characterId: role.id,
npcName: role.name,
npcDescription: role.description,
npcAvatar: role.imageSrc ?? role.name.slice(0, 1) ?? '?',
context: role.role,
xMeters: RESOLVED_ENTITY_X_METERS,
initialAffinity: role.initialAffinity,
hostile: isHostile,
title: role.title,
backstory: role.backstory,
personality: role.personality,
motivation: role.motivation,
combatStyle: role.combatStyle,
relationshipHooks: [...role.relationshipHooks],
tags: [...role.tags],
backstoryReveal: role.backstoryReveal,
skills: role.skills.map((skill) => ({ ...skill })),
initialItems: role.initialItems.map((item) => ({
...item,
tags: [...item.tags],
})),
imageSrc: role.imageSrc,
visual: (role as { visual?: Encounter['visual'] }).visual,
narrativeProfile: role.narrativeProfile,
attributeProfile: role.attributeProfile,
};
}
function resolveOpeningActEncounter(params: {
profile: CustomWorldProfile | null;
scenePreset: GameState['currentScenePreset'];
playerCharacter: Character;
}) {
const opening = resolveOpeningSceneActBlueprint(params.profile);
if (!opening || !params.profile) {
return null;
}
for (const npcId of resolveOpeningActNpcIdPriority(opening.act)) {
if (npcId === params.playerCharacter.id) {
continue;
}
const sceneNpc = findSceneNpcByRuntimeRoleId(params.scenePreset, npcId);
if (sceneNpc && sceneNpc.characterId !== params.playerCharacter.id) {
return {
...buildEncounterFromSceneNpc(sceneNpc, RESOLVED_ENTITY_X_METERS),
xMeters: RESOLVED_ENTITY_X_METERS,
};
}
const roleEncounter = buildOpeningEncounterFromCustomWorldRole(
params.profile,
npcId,
);
if (roleEncounter) {
return roleEncounter;
}
}
return null;
}
function buildOpeningStoryEngineMemory(
profile: CustomWorldProfile | null,
sceneId: string | null | undefined,
) {
const storyEngineMemory = createEmptyStoryEngineMemoryState();
return {
...storyEngineMemory,
currentSceneActState:
buildInitialSceneActRuntimeState({
profile,
sceneId,
storyEngineMemory,
}) ?? storyEngineMemory.currentSceneActState ?? null,
};
}
/**
* RPG session bootstrap 主实现。
* 工作包 C 起由新域 hook 承载世界选择、选角确认与新开局初始化。
@@ -287,14 +509,23 @@ export function useRpgSessionBootstrap() {
setGameState((prev) => {
const resolvedWorldType = prev.worldType;
const resolvedCustomWorldProfile = prev.customWorldProfile;
const initialScenePreset = resolvedWorldType
? (getWorldCampScenePreset(resolvedWorldType) ??
getScenePreset(resolvedWorldType, 0))
: null;
const initialEncounter = createInitialCampEncounter(
resolvedWorldType,
character,
);
const initialScenePreset =
resolvedWorldType === WorldType.CUSTOM
? (resolveOpeningActScenePreset(resolvedCustomWorldProfile) ??
getWorldCampScenePreset(resolvedWorldType) ??
getScenePreset(resolvedWorldType, 0))
: resolvedWorldType
? (getWorldCampScenePreset(resolvedWorldType) ??
getScenePreset(resolvedWorldType, 0))
: null;
const initialEncounter =
resolvedWorldType === WorldType.CUSTOM
? resolveOpeningActEncounter({
profile: resolvedCustomWorldProfile,
scenePreset: initialScenePreset,
playerCharacter: character,
})
: createInitialCampEncounter(resolvedWorldType, character);
const initialNpcState = initialEncounter
? buildInitialNpcState(initialEncounter, resolvedWorldType, prev)
: null;
@@ -330,7 +561,13 @@ export function useRpgSessionBootstrap() {
playerProgression: createInitialPlayerProgressionState(),
currentScene: 'Story',
storyHistory: [],
storyEngineMemory: createEmptyStoryEngineMemoryState(),
storyEngineMemory:
resolvedWorldType === WorldType.CUSTOM
? buildOpeningStoryEngineMemory(
resolvedCustomWorldProfile,
initialScenePreset?.id,
)
: createEmptyStoryEngineMemoryState(),
chapterState: null,
campaignState: null,
activeScenarioPackId:

View File

@@ -11,6 +11,9 @@ const AUTO_SAVE_DELAY_MS = 400;
function canPersistSnapshot(gameState: GameState, story: StoryMoment | null) {
return (
gameState.runtimePersistenceDisabled !== true &&
gameState.runtimeMode !== 'preview' &&
gameState.runtimeMode !== 'test' &&
gameState.currentScene === 'Story' &&
Boolean(gameState.worldType) &&
Boolean(gameState.playerCharacter) &&

View File

@@ -3,17 +3,34 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useMemo } from 'react';
import { afterEach, expect, test } from 'vitest';
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import {
buildCustomWorldPlayableCharacters,
setRuntimeCharacterOverrides,
} from '../data/characterPresets';
import { setRuntimeCustomWorldProfile } from '../data/customWorldRuntime';
import { normalizeCustomWorldProfileRecord } from '../data/customWorldLibrary';
import { setRuntimeCustomWorldProfile } from '../data/customWorldRuntime';
import { WorldType } from '../types';
import { useRpgRuntimeStory } from './rpg-runtime-story/useRpgRuntimeStory';
import { useRpgSessionBootstrap } from './rpg-session';
const aiServiceMocks = vi.hoisted(() => ({
streamNpcChatTurn: vi.fn(),
}));
vi.mock('../services/aiService', async () => {
const actual =
await vi.importActual<typeof import('../services/aiService')>(
'../services/aiService',
);
return {
...actual,
streamNpcChatTurn: aiServiceMocks.streamNpcChatTurn,
};
});
function buildBackstoryReveal(label: string) {
return {
publicSummary: `${label}的公开背景`,
@@ -208,6 +225,40 @@ function buildSavedProfile() {
reactionHooks: ['原始灯册', '封灯令'],
},
},
{
id: 'story-primary-only',
name: '沈砺旧识',
title: '旧潮案记录员',
role: '第一幕主线记录者',
description: '负责整理旧潮案脉络的人。',
backstory: '他知道异常账本的来源,但不会第一时间正面对话。',
personality: '沉默、谨慎。',
motivation: '保住旧案原始记录。',
combatStyle: '以防守和牵制为主。',
initialAffinity: 8,
relationshipHooks: ['旧案记录'],
tags: ['记录', '主线'],
backstoryReveal: buildBackstoryReveal('沈砺旧识'),
skills: [],
initialItems: [],
},
{
id: 'story-act-only',
name: '陆衡',
title: '航运公会审计员',
role: '第一幕主NPC',
description: '正在交易所大厅核对异常账本的人。',
backstory: '他掌握着旧航路资金流向的第一份实证。',
personality: '克制、警惕,习惯先观察再开口。',
motivation: '确认谁在开盘前转移了旧案资金。',
combatStyle: '用短杖和账册压制对手节奏。',
initialAffinity: 6,
relationshipHooks: ['异常账本'],
tags: ['审计', '第一幕'],
backstoryReveal: buildBackstoryReveal('陆衡'),
skills: [],
initialItems: [],
},
],
items: [],
camp: {
@@ -219,7 +270,7 @@ function buildSavedProfile() {
id: 'landmark-1',
name: '回潮旧灯塔',
description: '被海雾啃旧的石塔仍在夜里维持着微弱灯火。',
sceneNpcIds: ['story-1'],
sceneNpcIds: [],
connections: [
{
targetLandmarkId: 'landmark-2',
@@ -251,6 +302,60 @@ function buildSavedProfile() {
],
},
],
sceneChapterBlueprints: [
{
id: 'chapter-1',
sceneId: 'custom-scene-camp',
title: '交易所第一幕',
summary: '玩家在交易大厅被异常账本牵住。',
sceneTaskDescription: '查清异常账本指向谁。',
linkedThreadIds: [],
linkedLandmarkIds: [],
acts: [
{
id: 'act-1',
sceneId: 'custom-scene-camp',
title: '第一幕',
summary: '陆衡先开口试探玩家。',
stageCoverage: ['opening'],
encounterNpcIds: ['story-primary-only', 'story-act-only'],
primaryNpcId: 'story-primary-only',
oppositeNpcId: 'story-act-only',
eventDescription: '陆衡拿着异常账本,在开盘前拦住玩家。',
linkedThreadIds: [],
advanceRule: 'after_primary_contact',
actGoal: '确认异常账本的第一条线索。',
transitionHook: '账本指向旧灯塔的潮痕。',
},
],
},
{
id: 'chapter-late',
sceneId: 'landmark-2',
title: '雾栈后续幕',
summary: '后续场景不应抢走开局。',
sceneTaskDescription: '处理雾栈尽头的后续问题。',
linkedThreadIds: [],
linkedLandmarkIds: ['landmark-2'],
acts: [
{
id: 'act-late',
sceneId: 'landmark-2',
title: '后续幕',
summary: '雾栈里有人影闪过。',
stageCoverage: ['aftermath'],
encounterNpcIds: ['story-1'],
primaryNpcId: 'story-1',
oppositeNpcId: 'story-1',
eventDescription: '后续角色在雾栈尽头等待。',
linkedThreadIds: [],
advanceRule: 'after_active_step_complete',
actGoal: '后续推进。',
transitionHook: '继续深入雾栈。',
},
],
},
],
scenarioPackId: 'scenario-pack:tide',
campaignPackId: 'campaign-pack:tide',
generationMode: 'full',
@@ -275,6 +380,13 @@ function readSnapshot() {
currentScenePresetId: string | null;
currentScenePresetName: string | null;
currentSceneConnectedIds: string[];
currentSceneActId: string | null;
currentEncounterId: string | null;
currentEncounterName: string | null;
currentStoryDisplayMode: string | null;
currentStoryNpcName: string | null;
currentStoryDialogueTexts: string[];
isStoryLoading: boolean;
firstLandmarkResidueTitle: string | null;
playerCharacterName: string | null;
playerInventoryNames: string[];
@@ -293,8 +405,19 @@ function GameFlowHarness() {
[profile],
);
const selectedCharacter = playableCharacters[0] ?? null;
const { gameState, handleCustomWorldSelect, handleCharacterSelect } =
const {
gameState,
setGameState,
handleCustomWorldSelect,
handleCharacterSelect,
} =
useRpgSessionBootstrap();
const story = useRpgRuntimeStory({
gameState,
setGameState,
buildResolvedChoiceState: () => ({}) as never,
playResolvedChoice: async (state) => state,
});
const snapshot = {
worldType: gameState.worldType,
@@ -305,6 +428,15 @@ function GameFlowHarness() {
currentScenePresetId: gameState.currentScenePreset?.id ?? null,
currentScenePresetName: gameState.currentScenePreset?.name ?? null,
currentSceneConnectedIds: gameState.currentScenePreset?.connectedSceneIds ?? [],
currentSceneActId:
gameState.storyEngineMemory?.currentSceneActState?.currentActId ?? null,
currentEncounterId: gameState.currentEncounter?.id ?? null,
currentEncounterName: gameState.currentEncounter?.npcName ?? null,
currentStoryDisplayMode: story.currentStory?.displayMode ?? null,
currentStoryNpcName: story.currentStory?.npcChatState?.npcName ?? null,
currentStoryDialogueTexts:
story.currentStory?.dialogue?.map((entry) => entry.text) ?? [],
isStoryLoading: story.isLoading,
firstLandmarkResidueTitle:
gameState.customWorldProfile?.landmarks[0]?.narrativeResidues?.[0]
?.title ?? null,
@@ -345,6 +477,16 @@ afterEach(() => {
setRuntimeCharacterOverrides(null);
});
beforeEach(() => {
aiServiceMocks.streamNpcChatTurn.mockReset();
aiServiceMocks.streamNpcChatTurn.mockResolvedValue({
affinityDelta: 0,
affinityText: '这轮对话暂时没有带来明显关系变化。',
npcReply: '开盘前别靠近账本。你先告诉我,是谁让你来查这笔异常资金?',
suggestions: ['我先说明来意', '你先说账本哪里异常', '我不是来抢账本的'],
});
});
test('saved custom world result settings flow into game state after entering the world', async () => {
const user = userEvent.setup();
@@ -378,4 +520,30 @@ test('saved custom world result settings flow into game state after entering the
expect(readSnapshot().playerEquipment.weapon).toBe('旧潮短刃');
expect(readSnapshot().playerEquipment.relic).toBe('旧潮图残页');
expect(readSnapshot().playerEquipment.armor).toBeTruthy();
expect(readSnapshot().currentScenePresetId).toBe('custom-scene-camp');
expect(readSnapshot().currentSceneActId).toBe('act-1');
expect(readSnapshot().currentEncounterId).toBe('story-act-only');
expect(readSnapshot().currentEncounterName).toBe('陆衡');
expect(readSnapshot().currentEncounterId).not.toBe('story-primary-only');
await waitFor(() => {
expect(readSnapshot().currentStoryNpcName).toBe('陆衡');
});
expect(readSnapshot().currentStoryDisplayMode).toBe('dialogue');
expect(readSnapshot().currentStoryDialogueTexts).toContain(
'开盘前别靠近账本。你先告诉我,是谁让你来查这笔异常资金?',
);
expect(aiServiceMocks.streamNpcChatTurn).toHaveBeenCalledWith(
WorldType.CUSTOM,
expect.objectContaining({ name: '沈砺' }),
expect.objectContaining({ id: 'story-act-only', npcName: '陆衡' }),
expect.anything(),
expect.anything(),
expect.anything(),
[],
'',
expect.anything(),
expect.objectContaining({
npcInitiatesConversation: true,
}),
);
});

View File

@@ -66,6 +66,63 @@ describe('useResolvedAssetReadUrl', () => {
);
});
test('refreshKey changes force a refreshed signed image url', async () => {
vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
new Response(
JSON.stringify({
ok: true,
data: {
read: {
objectKey:
'generated-puzzle-assets/puzzle-session-1/candidate-1/asset-1/image.png',
signedUrl: 'https://signed.example.com/puzzle.png',
expiresAt: '2099-01-01T00:10:00Z',
},
},
error: null,
meta: {
apiVersion: '2026-04-08',
routeVersion: '2026-04-08',
latencyMs: 1,
timestamp: '2099-01-01T00:00:00Z',
},
}),
{
status: 200,
headers: {
'Content-Type': 'application/json',
},
},
),
);
const { rerender } = render(
<ResolvedAssetImage
src="/generated-puzzle-assets/puzzle-session-1/candidate-1/asset-1/image.png"
refreshKey="first-version"
alt="候选图"
/>,
);
const firstImage = await screen.findByRole('img', { name: '候选图' });
expect(firstImage.getAttribute('src')).toContain('_v=first-version');
rerender(
<ResolvedAssetImage
src="/generated-puzzle-assets/puzzle-session-1/candidate-1/asset-1/image.png"
refreshKey="second-version"
alt="候选图"
/>,
);
await waitFor(() => {
expect(screen.getByRole('img', { name: '候选图' }).getAttribute('src')).toContain(
'_v=second-version',
);
});
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
});
test('generated 私有资源签名失败时保持空图像而不是回退裸路径', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(
@@ -105,4 +162,3 @@ describe('useResolvedAssetReadUrl', () => {
expect(screen.queryByRole('img', { name: '候选图' })).toBeNull();
});
});

View File

@@ -8,6 +8,7 @@ import {
type UseResolvedAssetReadUrlOptions = {
enabled?: boolean;
expireSeconds?: number;
refreshKey?: string | number | null;
};
export function useResolvedAssetReadUrl(
@@ -39,6 +40,7 @@ export function useResolvedAssetReadUrl(
void resolveAssetReadUrl(normalizedSource, {
expireSeconds: options.expireSeconds,
refreshKey: options.refreshKey,
})
.then((nextUrl) => {
if (!cancelled) {
@@ -55,7 +57,12 @@ export function useResolvedAssetReadUrl(
return () => {
cancelled = true;
};
}, [normalizedSource, options.expireSeconds, shouldResolve]);
}, [
normalizedSource,
options.expireSeconds,
options.refreshKey,
shouldResolve,
]);
return {
resolvedUrl,

View File

@@ -6,6 +6,17 @@ export type AssetReadUrlRequest = {
expireSeconds?: number;
};
type AssetReadUrlResolveOptions = {
signal?: AbortSignal;
expireSeconds?: number;
/**
* 图片内容可能在同一路径下被重新写入。
* 这时需要显式跳过本地签名缓存,并在最终 URL 上追加一次性参数,
* 避免结果页仍命中旧签名地址或浏览器图片缓存。
*/
refreshKey?: string | number | null;
};
export type AssetReadUrlResponse = {
read?: {
objectKey?: string;
@@ -100,21 +111,26 @@ function shouldReuseCachedReadUrlFailure(
export async function getSignedAssetReadUrl(
request: AssetReadUrlRequest,
signal?: AbortSignal,
options: {
bypassCache?: boolean;
} = {},
) {
const cacheKey = buildCacheKey(request);
const cached = cacheKey ? signedReadUrlCache.get(cacheKey) : undefined;
const bypassCache = options.bypassCache === true;
const cached =
!bypassCache && cacheKey ? signedReadUrlCache.get(cacheKey) : undefined;
if (cached && shouldReuseCachedReadUrl(cached)) {
return cached.signedUrl;
}
const cachedFailure = cacheKey
const cachedFailure = !bypassCache && cacheKey
? signedReadUrlFailureCache.get(cacheKey)
: undefined;
if (cachedFailure && shouldReuseCachedReadUrlFailure(cachedFailure)) {
throw new Error('资源不存在或暂时不可读取');
}
if (cacheKey) {
if (cacheKey && !bypassCache) {
const pendingRequest = pendingSignedReadUrlRequests.get(cacheKey);
if (pendingRequest) {
return pendingRequest;
@@ -178,26 +194,48 @@ export async function getSignedAssetReadUrl(
}
})();
if (cacheKey) {
if (cacheKey && !bypassCache) {
pendingSignedReadUrlRequests.set(cacheKey, requestPromise);
}
try {
return await requestPromise;
} finally {
if (cacheKey) {
if (cacheKey && !bypassCache) {
pendingSignedReadUrlRequests.delete(cacheKey);
}
}
}
function appendCacheBustParam(
url: string,
refreshKey: string | number | null | undefined,
) {
const normalizedRefreshKey =
refreshKey === null || refreshKey === undefined
? ''
: String(refreshKey).trim();
if (!normalizedRefreshKey) {
return url;
}
try {
const parsedUrl = new URL(url, globalThis.location?.origin ?? 'http://localhost');
parsedUrl.searchParams.set('_v', normalizedRefreshKey);
if (/^(?:https?:)?\/\//u.test(url)) {
return parsedUrl.toString();
}
return `${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`;
} catch {
const separator = url.includes('?') ? '&' : '?';
return `${url}${separator}_v=${encodeURIComponent(normalizedRefreshKey)}`;
}
}
// 兼容层:普通 http(s)/data/blob 路径原样返回;历史 generated-* 路径自动换签名读 URL。
export async function resolveAssetReadUrl(
source: string | null | undefined,
options: {
signal?: AbortSignal;
expireSeconds?: number;
} = {},
options: AssetReadUrlResolveOptions = {},
) {
const value = source?.trim() ?? '';
if (!value) {
@@ -209,20 +247,25 @@ export async function resolveAssetReadUrl(
value.startsWith('data:') ||
value.startsWith('blob:')
) {
return value;
return appendCacheBustParam(value, options.refreshKey);
}
if (isGeneratedLegacyPath(value)) {
return getSignedAssetReadUrl(
const signedUrl = await getSignedAssetReadUrl(
{
legacyPublicPath: value,
expireSeconds: options.expireSeconds,
},
options.signal,
{
bypassCache:
options.refreshKey !== null && options.refreshKey !== undefined,
},
);
return appendCacheBustParam(signedUrl, options.refreshKey);
}
return value;
return appendCacheBustParam(value, options.refreshKey);
}
export function clearSignedAssetReadUrlCache() {

View File

@@ -13,6 +13,44 @@ function toSet(values: string[]) {
return new Set(values.map((value) => value.trim()).filter(Boolean));
}
function resolveCustomWorldRuntimeSceneAliases(
profile: CustomWorldProfile,
sceneId: string,
) {
const aliases = toSet([sceneId]);
const campId = profile.camp?.id?.trim() || 'custom-scene-camp';
if (sceneId === 'custom-scene-camp' || sceneId === campId) {
aliases.add(campId);
aliases.add('custom-scene-camp');
}
// 中文注释:部分单元测试和旧快照会传入精简 profile运行态解析不能假设 landmarks 始终存在。
(profile.landmarks ?? []).forEach((landmark, index) => {
const runtimeSceneId = `custom-scene-landmark-${index + 1}`;
if (sceneId === runtimeSceneId || sceneId === landmark.id) {
aliases.add(runtimeSceneId);
aliases.add(landmark.id);
}
});
return aliases;
}
function doesSceneMatchChapter(
profile: CustomWorldProfile,
sceneId: string,
chapter: SceneChapterBlueprint,
) {
const sceneAliases = resolveCustomWorldRuntimeSceneAliases(profile, sceneId);
const chapterSceneIds = toSet([
chapter.sceneId,
...(chapter.linkedLandmarkIds ?? []),
...(chapter.acts ?? []).map((act) => act.sceneId),
]);
return [...sceneAliases].some((id) => chapterSceneIds.has(id));
}
export function resolveSceneChapterBlueprint(
profile: CustomWorldProfile | null | undefined,
sceneId: string | null | undefined,
@@ -22,8 +60,8 @@ export function resolveSceneChapterBlueprint(
}
return (
profile.sceneChapterBlueprints?.find(
(entry) => entry.sceneId === sceneId || entry.linkedLandmarkIds.includes(sceneId),
profile.sceneChapterBlueprints?.find((entry) =>
doesSceneMatchChapter(profile, sceneId, entry),
) ?? null
);
}
@@ -33,15 +71,24 @@ export function resolveActiveSceneActBlueprint(params: {
sceneId: string | null | undefined;
storyEngineMemory?: StoryEngineMemoryState | null;
}): SceneActBlueprint | null {
const chapter = resolveSceneChapterBlueprint(params.profile, params.sceneId);
const runtimeState = params.storyEngineMemory?.currentSceneActState;
const runtimeChapter =
params.profile && runtimeState?.chapterId
? params.profile.sceneChapterBlueprints?.find(
(entry) =>
entry.id === runtimeState.chapterId &&
Boolean(params.sceneId) &&
doesSceneMatchChapter(params.profile!, params.sceneId!, entry),
) ?? null
: null;
const chapter =
runtimeChapter ?? resolveSceneChapterBlueprint(params.profile, params.sceneId);
if (!chapter || chapter.acts.length === 0) {
return null;
}
const runtimeState = params.storyEngineMemory?.currentSceneActState;
if (
runtimeState &&
runtimeState.sceneId === chapter.sceneId &&
runtimeState.chapterId === chapter.id
) {
const matchedAct = chapter.acts.find((entry) => entry.id === runtimeState.currentActId);
@@ -132,15 +179,23 @@ export function buildInitialSceneActRuntimeState(params: {
sceneId: string | null | undefined;
storyEngineMemory?: StoryEngineMemoryState | null;
}): SceneActRuntimeState | null {
const chapter = resolveSceneChapterBlueprint(params.profile, params.sceneId);
const runtimeState = params.storyEngineMemory?.currentSceneActState;
const runtimeChapter =
params.profile && params.sceneId && runtimeState?.chapterId
? params.profile.sceneChapterBlueprints?.find(
(entry) =>
entry.id === runtimeState.chapterId &&
doesSceneMatchChapter(params.profile!, params.sceneId!, entry),
) ?? null
: null;
const chapter =
runtimeChapter ?? resolveSceneChapterBlueprint(params.profile, params.sceneId);
if (!chapter || chapter.acts.length === 0) {
return null;
}
const runtimeState = params.storyEngineMemory?.currentSceneActState;
if (
runtimeState &&
runtimeState.sceneId === chapter.sceneId &&
runtimeState.chapterId === chapter.id &&
chapter.acts.some((entry) => entry.id === runtimeState.currentActId)
) {
@@ -167,11 +222,22 @@ export function resolveActiveSceneActEncounterNpcIds(params: {
sceneId: string | null | undefined;
storyEngineMemory?: StoryEngineMemoryState | null;
}) {
return (
resolveActiveSceneActBlueprint(params)?.encounterNpcIds
.map((entry) => entry.trim())
.filter(Boolean) ?? []
);
const activeAct = resolveActiveSceneActBlueprint(params);
if (!activeAct) {
return [];
}
return [
...new Set(
[
activeAct.primaryNpcId,
activeAct.oppositeNpcId,
...activeAct.encounterNpcIds,
]
.map((entry) => entry.trim())
.filter(Boolean),
),
];
}
export function resolveActiveSceneActPrimaryNpcId(params: {
@@ -182,6 +248,28 @@ export function resolveActiveSceneActPrimaryNpcId(params: {
return resolveActiveSceneActBlueprint(params)?.primaryNpcId?.trim() || null;
}
export function resolveActiveSceneActOppositeNpcId(params: {
profile: CustomWorldProfile | null | undefined;
sceneId: string | null | undefined;
storyEngineMemory?: StoryEngineMemoryState | null;
}) {
return resolveActiveSceneActBlueprint(params)?.oppositeNpcId?.trim() || null;
}
export function resolveActiveSceneActEncounterFocusNpcId(params: {
profile: CustomWorldProfile | null | undefined;
sceneId: string | null | undefined;
storyEngineMemory?: StoryEngineMemoryState | null;
}) {
const activeAct = resolveActiveSceneActBlueprint(params);
return (
activeAct?.oppositeNpcId?.trim() ||
activeAct?.primaryNpcId?.trim() ||
activeAct?.encounterNpcIds[0]?.trim() ||
null
);
}
export function resolveActiveSceneActBackgroundImage(params: {
profile: CustomWorldProfile | null | undefined;
sceneId: string | null | undefined;
@@ -201,6 +289,22 @@ export function canUseLimitedPrimaryNpcChat(params: {
return false;
}
const activeAct = resolveActiveSceneActBlueprint({
profile: params.profile,
sceneId: params.sceneId,
storyEngineMemory: params.storyEngineMemory,
});
const limitedChatNpcIds = toSet([
activeAct?.primaryNpcId ?? '',
activeAct?.oppositeNpcId ?? '',
]);
// 中文注释:第一幕对面角色即使是负好感,也必须先进入剧情对话;普通敌人仍按战斗处理。
if (limitedChatNpcIds.has(params.npcId)) {
return true;
}
return (
resolveActiveSceneActPrimaryNpcId({
profile: params.profile,

View File

@@ -1,10 +1,12 @@
import { describe, expect, test } from 'vitest';
import type { PuzzlePieceState } from '../../../packages/shared/src/contracts/puzzleRuntimeSession';
import type { PuzzleWorkSummary } from '../../../packages/shared/src/contracts/puzzleWorkSummary';
import {
advanceLocalPuzzleLevel,
dragLocalPuzzlePiece,
startLocalPuzzleRun,
swapLocalPuzzlePieces,
} from './puzzleLocalRuntime';
const baseWork: PuzzleWorkSummary = {
@@ -25,6 +27,25 @@ const baseWork: PuzzleWorkSummary = {
publishReady: true,
};
function hasAnyCorrectNeighborPair(pieces: PuzzlePieceState[]) {
return pieces.some((piece) =>
pieces.some((candidate) => {
if (piece.pieceId === candidate.pieceId) {
return false;
}
const currentRowDelta = candidate.currentRow - piece.currentRow;
const currentColDelta = candidate.currentCol - piece.currentCol;
const correctRowDelta = candidate.correctRow - piece.correctRow;
const correctColDelta = candidate.correctCol - piece.correctCol;
return (
Math.abs(currentRowDelta) + Math.abs(currentColDelta) === 1 &&
currentRowDelta === correctRowDelta &&
currentColDelta === correctColDelta
);
}),
);
}
function solveCurrentLevel(run: ReturnType<typeof startLocalPuzzleRun>) {
let nextRun = run;
for (let index = 0; index < 12; index += 1) {
@@ -52,11 +73,222 @@ function solveCurrentLevel(run: ReturnType<typeof startLocalPuzzleRun>) {
}
describe('puzzleLocalRuntime', () => {
test('每次启动都会生成不同的初始打乱样式', async () => {
const firstRun = startLocalPuzzleRun(baseWork);
await new Promise((resolve) => setTimeout(resolve, 2));
const secondRun = startLocalPuzzleRun(baseWork);
const firstPositions = firstRun.currentLevel?.board.pieces.map((piece) => [
piece.currentRow,
piece.currentCol,
]);
const secondPositions = secondRun.currentLevel?.board.pieces.map((piece) => [
piece.currentRow,
piece.currentCol,
]);
expect(firstPositions).not.toEqual(secondPositions);
});
test('初始棋盘没有任何自动合并块', () => {
for (let index = 0; index < 12; index += 1) {
const run = startLocalPuzzleRun(baseWork);
const board = run.currentLevel?.board;
expect(board?.mergedGroups).toEqual([]);
expect(hasAnyCorrectNeighborPair(board?.pieces ?? [])).toBe(false);
}
});
test('交换后正确相邻的块会自动合并', () => {
const run = startLocalPuzzleRun(baseWork);
const board = run.currentLevel?.board;
expect(board).toBeTruthy();
if (!run.currentLevel || !board) {
return;
}
const nextRun = {
...run,
currentLevel: {
...run.currentLevel,
board: {
...board,
pieces: board.pieces.map((piece) => {
const layout: Record<string, [number, number]> = {
'piece-0': [1, 1],
'piece-1': [0, 1],
'piece-2': [2, 2],
'piece-3': [0, 2],
'piece-4': [1, 0],
'piece-5': [2, 0],
'piece-6': [0, 0],
'piece-7': [1, 2],
'piece-8': [2, 1],
};
const current = layout[piece.pieceId] ?? [piece.currentRow, piece.currentCol];
return {
...piece,
currentRow: current[0],
currentCol: current[1],
mergedGroupId: null,
};
}),
mergedGroups: [],
allTilesResolved: false,
},
},
};
const swapped = swapLocalPuzzlePieces(nextRun, {
firstPieceId: 'piece-0',
secondPieceId: 'piece-6',
});
expect(
swapped.currentLevel?.board.mergedGroups.some(
(group) =>
group.pieceIds.includes('piece-0') &&
group.pieceIds.includes('piece-1'),
),
).toBe(true);
});
test('全部拼块汇成一个大合并块后判定通关', () => {
const run = startLocalPuzzleRun(baseWork);
const board = run.currentLevel?.board;
expect(board).toBeTruthy();
if (!run.currentLevel || !board) {
return;
}
const solvedByOneGroup = {
...run,
currentLevel: {
...run.currentLevel,
board: {
...board,
pieces: board.pieces.map((piece, index) => ({
...piece,
currentRow: Math.floor(index / board.cols),
currentCol: (index + 1) % board.cols,
mergedGroupId: 'group-full',
})),
mergedGroups: [
{
groupId: 'group-full',
pieceIds: board.pieces.map((piece) => piece.pieceId),
occupiedCells: board.pieces.map((_, index) => ({
row: Math.floor(index / board.cols),
col: (index + 1) % board.cols,
})),
},
],
allTilesResolved: true,
},
},
};
expect(solvedByOneGroup.currentLevel.board.allTilesResolved).toBe(true);
});
test('大合并块覆盖多个小块时会与被覆盖块逐一交换,不会出现小块消失', () => {
const run = startLocalPuzzleRun(baseWork);
const board = run.currentLevel?.board;
expect(board).toBeTruthy();
if (!run.currentLevel || !board) {
return;
}
const preparedRun = {
...run,
currentLevel: {
...run.currentLevel,
board: {
...board,
pieces: board.pieces.map((piece) => {
const layout: Record<string, [number, number, string | null]> = {
'piece-0': [0, 0, 'group-1'],
'piece-1': [0, 1, 'group-1'],
'piece-2': [0, 2, null],
'piece-3': [1, 0, 'group-1'],
'piece-4': [1, 1, 'group-1'],
'piece-5': [1, 2, null],
'piece-6': [2, 0, null],
'piece-7': [2, 1, null],
'piece-8': [2, 2, null],
};
const current = layout[piece.pieceId] ?? [
piece.currentRow,
piece.currentCol,
piece.mergedGroupId,
];
return {
...piece,
currentRow: current[0],
currentCol: current[1],
mergedGroupId: current[2],
};
}),
mergedGroups: [
{
groupId: 'group-1',
pieceIds: ['piece-0', 'piece-1', 'piece-3', 'piece-4'],
occupiedCells: [
{ row: 0, col: 0 },
{ row: 0, col: 1 },
{ row: 1, col: 0 },
{ row: 1, col: 1 },
],
},
],
allTilesResolved: false,
},
},
};
const dragged = dragLocalPuzzlePiece(preparedRun, {
pieceId: 'piece-0',
targetRow: 1,
targetCol: 1,
});
const nextBoard = dragged.currentLevel?.board;
expect(nextBoard).toBeTruthy();
if (!nextBoard) {
return;
}
const occupiedCells = nextBoard.pieces.map((piece) => `${piece.currentRow}:${piece.currentCol}`);
expect(new Set(occupiedCells).size).toBe(nextBoard.pieces.length);
expect(
nextBoard.pieces.find((piece) => piece.pieceId === 'piece-5'),
).toMatchObject({ currentRow: 0, currentCol: 0 });
expect(
nextBoard.pieces.find((piece) => piece.pieceId === 'piece-7'),
).toMatchObject({ currentRow: 0, currentCol: 1 });
expect(
nextBoard.pieces.find((piece) => piece.pieceId === 'piece-8'),
).toMatchObject({ currentRow: 1, currentCol: 0 });
expect(
nextBoard.pieces.find((piece) => piece.pieceId === 'piece-0'),
).toMatchObject({ currentRow: 1, currentCol: 1 });
expect(
nextBoard.pieces.find((piece) => piece.pieceId === 'piece-4'),
).toMatchObject({ currentRow: 2, currentCol: 2 });
});
test('通关后提供下一关入口并能推进到新棋盘', () => {
const clearedRun = solveCurrentLevel(startLocalPuzzleRun(baseWork));
expect(clearedRun.currentLevel?.status).toBe('cleared');
expect(clearedRun.recommendedNextProfileId).toBe('profile-1::local-level-2');
expect(clearedRun.currentLevel?.elapsedMs).toBeGreaterThan(0);
expect(clearedRun.currentLevel?.leaderboardEntries.length).toBeGreaterThan(0);
expect(
clearedRun.currentLevel?.leaderboardEntries.some(
(entry) => entry.isCurrentPlayer && entry.nickname === '测试作者',
),
).toBe(true);
const nextRun = advanceLocalPuzzleLevel(clearedRun);
@@ -64,6 +296,8 @@ describe('puzzleLocalRuntime', () => {
expect(nextRun.currentLevel?.status).toBe('playing');
expect(nextRun.currentLevel?.levelName).toBe('测试拼图 · 第 2 关');
expect(nextRun.currentLevel?.board.allTilesResolved).toBe(false);
expect(nextRun.currentLevel?.elapsedMs).toBeNull();
expect(nextRun.currentLevel?.leaderboardEntries).toEqual([]);
expect(nextRun.recommendedNextProfileId).toBeNull();
});
});

View File

@@ -1,7 +1,10 @@
import type {
DragPuzzlePieceRequest,
PuzzleBoardSnapshot,
PuzzleCellPosition,
PuzzleGridSize,
PuzzleLeaderboardEntry,
PuzzleMergedGroupState,
PuzzlePieceState,
PuzzleRunSnapshot,
SwapPuzzlePiecesRequest,
@@ -12,72 +15,276 @@ function resolvePuzzleGridSize(clearedLevelCount: number): PuzzleGridSize {
return clearedLevelCount >= 3 ? 4 : 3;
}
function buildInitialPositions(gridSize: PuzzleGridSize) {
const PUZZLE_INITIAL_SHUFFLE_ATTEMPTS = 64;
function buildShuffleSeed(...parts: Array<string | number>) {
let hash = 0x811c9dc5;
for (const part of parts.join('|')) {
hash ^= part.charCodeAt(0);
hash = Math.imul(hash, 16777619) >>> 0;
}
return hash || 1;
}
function shufflePositions(
positions: PuzzleCellPosition[],
seed: number,
): PuzzleCellPosition[] {
const shuffled = positions.map((position) => ({ ...position }));
let state = seed >>> 0;
for (let index = shuffled.length - 1; index > 0; index -= 1) {
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
const swapIndex = state % (index + 1);
const currentPosition = shuffled[index];
const swapPosition = shuffled[swapIndex];
if (!currentPosition || !swapPosition) {
continue;
}
shuffled[index] = swapPosition;
shuffled[swapIndex] = currentPosition;
}
return shuffled;
}
function ensureBoardIsNotSolved(
positions: PuzzleCellPosition[],
gridSize: PuzzleGridSize,
) {
const solved = positions.every(
(position, index) =>
position.row === Math.floor(index / gridSize) &&
position.col === index % gridSize,
);
if (solved && positions.length > 1) {
const first = positions.shift();
if (first) {
positions.push(first);
}
}
}
function buildInitialPositions(gridSize: PuzzleGridSize, seed: number) {
const positions = Array.from({ length: gridSize * gridSize }, (_, index) => ({
row: Math.floor(index / gridSize),
col: index % gridSize,
}));
return positions.slice(1).concat(positions.slice(0, 1));
for (let attempt = 0; attempt < PUZZLE_INITIAL_SHUFFLE_ATTEMPTS; attempt += 1) {
const shuffled = shufflePositions(
positions,
(seed + Math.imul(attempt, 2654435761)) >>> 0,
);
ensureBoardIsNotSolved(shuffled, gridSize);
const pieces = buildPiecesFromPositions(gridSize, shuffled);
if (!hasAnyCorrectNeighborPair(pieces)) {
return shuffled;
}
}
return positions.slice().reverse();
}
function boardCellKey(row: number, col: number) {
return `${row}:${col}`;
}
function clampElapsedMs(value: number) {
return Math.max(1_000, Math.round(value));
}
function rankLeaderboardEntries(
entries: Omit<PuzzleLeaderboardEntry, 'rank'>[],
): PuzzleLeaderboardEntry[] {
return entries
.map((entry) => ({ ...entry }))
.sort((left, right) => left.elapsedMs - right.elapsedMs)
.map((entry, index) => ({
...entry,
rank: index + 1,
}));
}
// V1 本地榜单只用于单次游玩闭环展示;正式榜单后续迁移到 SpacetimeDB 表或 view。
function buildLocalLeaderboardEntries(
elapsedMs: number,
playerNickname: string,
levelIndex: number,
gridSize: PuzzleGridSize,
): PuzzleLeaderboardEntry[] {
const normalizedElapsedMs = clampElapsedMs(elapsedMs);
const baseOffsetMs = gridSize === 3 ? 4_000 : 8_000;
return rankLeaderboardEntries([
{
nickname: playerNickname.trim() || '玩家',
elapsedMs: normalizedElapsedMs,
isCurrentPlayer: true,
},
{
nickname: '星桥旅人',
elapsedMs: normalizedElapsedMs + baseOffsetMs + levelIndex * 700,
},
{
nickname: '月港拼图手',
elapsedMs: Math.max(1_000, normalizedElapsedMs - baseOffsetMs / 2),
},
{
nickname: '雾灯收藏家',
elapsedMs: normalizedElapsedMs + baseOffsetMs * 2 + levelIndex * 900,
},
]);
}
function neighborCells(row: number, col: number): PuzzleCellPosition[] {
return [
row > 0 ? { row: row - 1, col } : null,
{ row: row + 1, col },
col > 0 ? { row, col: col - 1 } : null,
{ row, col: col + 1 },
].filter((cell): cell is PuzzleCellPosition => Boolean(cell));
}
function areCorrectNeighbors(left: PuzzlePieceState, right: PuzzlePieceState) {
const currentRowDelta = right.currentRow - left.currentRow;
const currentColDelta = right.currentCol - left.currentCol;
const correctRowDelta = right.correctRow - left.correctRow;
const correctColDelta = right.correctCol - left.correctCol;
return (
Math.abs(currentRowDelta) + Math.abs(currentColDelta) === 1 &&
currentRowDelta === correctRowDelta &&
currentColDelta === correctColDelta
);
}
function buildPiecesFromPositions(
gridSize: PuzzleGridSize,
positions: PuzzleCellPosition[],
): PuzzlePieceState[] {
return positions.map((current, index) => ({
pieceId: `piece-${index}`,
correctRow: Math.floor(index / gridSize),
correctCol: index % gridSize,
currentRow: current.row,
currentCol: current.col,
mergedGroupId: null,
}));
}
function hasAnyCorrectNeighborPair(pieces: PuzzlePieceState[]) {
const piecesByCell = new Map(
pieces.map((piece) => [boardCellKey(piece.currentRow, piece.currentCol), piece]),
);
return pieces.some((piece) =>
neighborCells(piece.currentRow, piece.currentCol).some((neighbor) => {
const neighborPiece = piecesByCell.get(boardCellKey(neighbor.row, neighbor.col));
return Boolean(neighborPiece && areCorrectNeighbors(piece, neighborPiece));
}),
);
}
function resolveMergedGroups(
pieces: PuzzlePieceState[],
): PuzzleMergedGroupState[] {
const piecesByCell = new Map(
pieces.map((piece) => [boardCellKey(piece.currentRow, piece.currentCol), piece]),
);
const piecesById = new Map(pieces.map((piece) => [piece.pieceId, piece]));
const visited = new Set<string>();
const groups: PuzzleMergedGroupState[] = [];
for (const piece of pieces) {
if (visited.has(piece.pieceId)) {
continue;
}
const queue = [piece.pieceId];
const pieceIds: string[] = [];
while (queue.length) {
const currentPieceId = queue.shift();
if (!currentPieceId || visited.has(currentPieceId)) {
continue;
}
visited.add(currentPieceId);
const currentPiece = piecesById.get(currentPieceId);
if (!currentPiece) {
continue;
}
pieceIds.push(currentPieceId);
for (const neighbor of neighborCells(
currentPiece.currentRow,
currentPiece.currentCol,
)) {
const neighborPiece = piecesByCell.get(boardCellKey(neighbor.row, neighbor.col));
if (neighborPiece && areCorrectNeighbors(currentPiece, neighborPiece)) {
queue.push(neighborPiece.pieceId);
}
}
}
if (pieceIds.length <= 1) {
continue;
}
groups.push({
groupId: `group-${groups.length + 1}`,
pieceIds,
occupiedCells: pieceIds
.map((pieceId) => piecesById.get(pieceId))
.filter((value): value is PuzzlePieceState => Boolean(value))
.map((value) => ({ row: value.currentRow, col: value.currentCol })),
});
}
return groups;
}
function rebuildBoardSnapshot(
gridSize: PuzzleGridSize,
pieces: PuzzlePieceState[],
): PuzzleBoardSnapshot {
const resolvedPieceIds = new Set(
pieces
.filter(
(piece) =>
piece.currentRow === piece.correctRow &&
piece.currentCol === piece.correctCol,
)
.map((piece) => piece.pieceId),
const mergedGroups = resolveMergedGroups(pieces).map((group, index) => ({
...group,
groupId: `group-${index + 1}`,
}));
const groupByPiece = new Map(
mergedGroups.flatMap((group) =>
group.pieceIds.map((pieceId) => [pieceId, group.groupId] as const),
),
);
const allTilesResolved = resolvedPieceIds.size === pieces.length;
const nextPieces = pieces.map((piece) => ({
...piece,
mergedGroupId: groupByPiece.get(piece.pieceId) ?? null,
}));
const allPiecesInCorrectCells = nextPieces.every(
(piece) =>
piece.currentRow === piece.correctRow &&
piece.currentCol === piece.correctCol,
);
const allPiecesMergedIntoOneGroup = mergedGroups.some(
(group) => group.pieceIds.length === nextPieces.length && nextPieces.length > 1,
);
const allTilesResolved =
allPiecesInCorrectCells || allPiecesMergedIntoOneGroup;
return {
rows: gridSize,
cols: gridSize,
pieces: pieces.map((piece) => ({
...piece,
mergedGroupId: resolvedPieceIds.has(piece.pieceId)
? 'resolved-main'
: null,
})),
mergedGroups: resolvedPieceIds.size
? [
{
groupId: 'resolved-main',
pieceIds: Array.from(resolvedPieceIds),
occupiedCells: pieces
.filter((piece) => resolvedPieceIds.has(piece.pieceId))
.map((piece) => ({
row: piece.currentRow,
col: piece.currentCol,
})),
},
]
: [],
pieces: nextPieces,
mergedGroups,
selectedPieceId: null,
allTilesResolved,
};
}
function buildInitialBoard(gridSize: PuzzleGridSize): PuzzleBoardSnapshot {
const shuffledPositions = buildInitialPositions(gridSize);
const pieces = Array.from({ length: gridSize * gridSize }, (_, index) => {
const correctRow = Math.floor(index / gridSize);
const correctCol = index % gridSize;
const current = shuffledPositions[index] ?? { row: correctRow, col: correctCol };
return {
pieceId: `piece-${index}`,
correctRow,
correctCol,
currentRow: current.row,
currentCol: current.col,
mergedGroupId: null,
};
});
function buildInitialBoard(
gridSize: PuzzleGridSize,
runId: string,
profileId: string,
levelIndex: number,
): PuzzleBoardSnapshot {
const shuffledPositions = buildInitialPositions(
gridSize,
buildShuffleSeed(runId, profileId, levelIndex, Date.now()),
);
const pieces = buildPiecesFromPositions(gridSize, shuffledPositions);
return rebuildBoardSnapshot(gridSize, pieces);
}
@@ -93,6 +300,21 @@ function applyNextBoard(
status === 'cleared' && run.currentLevel.status !== 'cleared'
? run.clearedLevelCount + 1
: run.clearedLevelCount;
const justCleared = status === 'cleared' && run.currentLevel.status !== 'cleared';
const nowMs = Date.now();
const clearedAtMs = justCleared ? nowMs : (run.currentLevel.clearedAtMs ?? null);
const elapsedMs = justCleared
? clampElapsedMs(nowMs - run.currentLevel.startedAtMs)
: (run.currentLevel.elapsedMs ?? null);
const leaderboardEntries =
justCleared && elapsedMs
? buildLocalLeaderboardEntries(
elapsedMs,
run.currentLevel.authorDisplayName,
run.currentLevel.levelIndex,
run.currentLevel.gridSize,
)
: run.currentLevel.leaderboardEntries;
return {
...run,
clearedLevelCount: nextClearedLevelCount,
@@ -100,7 +322,11 @@ function applyNextBoard(
...run.currentLevel,
board: nextBoard,
status,
clearedAtMs,
elapsedMs,
leaderboardEntries,
},
leaderboardEntries,
recommendedNextProfileId:
status === 'cleared'
? buildLocalNextProfileId(run.entryProfileId, nextClearedLevelCount + 1)
@@ -129,6 +355,7 @@ function buildFallbackLocalLevel(run: PuzzleRunSnapshot): PuzzleRunSnapshot {
const nextProfileId =
run.recommendedNextProfileId ??
buildLocalNextProfileId(run.entryProfileId, nextLevelIndex);
const startedAtMs = Date.now();
return {
...run,
@@ -145,17 +372,24 @@ function buildFallbackLocalLevel(run: PuzzleRunSnapshot): PuzzleRunSnapshot {
gridSize,
profileId: nextProfileId,
levelName: buildLocalLevelName(currentLevel.levelName, nextLevelIndex),
board: buildInitialBoard(gridSize),
board: buildInitialBoard(gridSize, run.runId, nextProfileId, nextLevelIndex),
status: 'playing',
startedAtMs,
clearedAtMs: null,
elapsedMs: null,
leaderboardEntries: [],
},
recommendedNextProfileId: null,
leaderboardEntries: [],
};
}
export function startLocalPuzzleRun(item: PuzzleWorkSummary): PuzzleRunSnapshot {
const gridSize = resolvePuzzleGridSize(0);
const runId = `local-puzzle-run-${item.profileId}-${Date.now()}`;
const startedAtMs = Date.now();
return {
runId: `local-puzzle-run-${item.profileId}-${Date.now()}`,
runId,
entryProfileId: item.profileId,
clearedLevelCount: 0,
currentLevelIndex: 1,
@@ -163,7 +397,7 @@ export function startLocalPuzzleRun(item: PuzzleWorkSummary): PuzzleRunSnapshot
playedProfileIds: [item.profileId],
previousLevelTags: item.themeTags,
currentLevel: {
runId: `local-puzzle-run-${item.profileId}`,
runId,
levelIndex: 1,
gridSize,
profileId: item.profileId,
@@ -171,10 +405,15 @@ export function startLocalPuzzleRun(item: PuzzleWorkSummary): PuzzleRunSnapshot
authorDisplayName: item.authorDisplayName,
themeTags: item.themeTags,
coverImageSrc: item.coverImageSrc,
board: buildInitialBoard(gridSize),
board: buildInitialBoard(gridSize, runId, item.profileId, 1),
status: 'playing',
startedAtMs,
clearedAtMs: null,
elapsedMs: null,
leaderboardEntries: [],
},
recommendedNextProfileId: null,
leaderboardEntries: [],
};
}
@@ -201,6 +440,120 @@ export function swapLocalPuzzlePieces(
return applyNextBoard(run, rebuildBoardSnapshot(currentLevel.gridSize, pieces));
}
function dragSinglePiece(
pieces: PuzzlePieceState[],
moving: PuzzlePieceState,
targetRow: number,
targetCol: number,
) {
const occupying = pieces.find(
(piece) =>
piece.pieceId !== moving.pieceId &&
piece.currentRow === targetRow &&
piece.currentCol === targetCol,
);
if (occupying?.mergedGroupId) {
for (const piece of pieces) {
if (piece.mergedGroupId === occupying.mergedGroupId) {
piece.mergedGroupId = null;
}
}
}
const source = { row: moving.currentRow, col: moving.currentCol };
moving.currentRow = targetRow;
moving.currentCol = targetCol;
if (occupying) {
occupying.currentRow = source.row;
occupying.currentCol = source.col;
}
}
function dragGroup(
pieces: PuzzlePieceState[],
moving: PuzzlePieceState,
targetRow: number,
targetCol: number,
gridSize: PuzzleGridSize,
) {
if (!moving.mergedGroupId) {
return false;
}
const groupPieces = pieces.filter(
(piece) => piece.mergedGroupId === moving.mergedGroupId,
);
const rowOffset = targetRow - moving.currentRow;
const colOffset = targetCol - moving.currentCol;
const targetPositions = groupPieces.map((piece) => ({
piece,
row: piece.currentRow + rowOffset,
col: piece.currentCol + colOffset,
}));
if (
targetPositions.some(
(position) =>
position.row < 0 ||
position.col < 0 ||
position.row >= gridSize ||
position.col >= gridSize,
)
) {
return false;
}
const movingIds = new Set(groupPieces.map((piece) => piece.pieceId));
const targetCellKeys = new Set(
targetPositions.map((position) => boardCellKey(position.row, position.col)),
);
// 大块整体平移后,所有被覆盖的小块必须一对一交换到真正腾出来的格子里,
// 不能重复写回同一个源格,否则会出现多个小块重叠并在渲染上“消失”。
const vacatedPositions = groupPieces
.map((piece) => ({
row: piece.currentRow,
col: piece.currentCol,
}))
.filter(
(position) => !targetCellKeys.has(boardCellKey(position.row, position.col)),
)
.sort((left, right) => left.row - right.row || left.col - right.col);
const occupyingPieces = targetPositions
.map(
(target) =>
pieces.find(
(piece) =>
!movingIds.has(piece.pieceId) &&
piece.currentRow === target.row &&
piece.currentCol === target.col,
) ?? null,
)
.filter((piece): piece is PuzzlePieceState => Boolean(piece))
.sort(
(left, right) =>
left.currentRow - right.currentRow || left.currentCol - right.currentCol,
);
if (occupyingPieces.length !== vacatedPositions.length) {
return false;
}
for (let index = 0; index < occupyingPieces.length; index += 1) {
const occupying = occupyingPieces[index];
const fallback = vacatedPositions[index];
if (!occupying || !fallback) {
return false;
}
occupying.mergedGroupId = null;
occupying.currentRow = fallback.row;
occupying.currentCol = fallback.col;
}
for (const target of targetPositions) {
target.piece.currentRow = target.row;
target.piece.currentCol = target.col;
}
return true;
}
export function dragLocalPuzzlePiece(
run: PuzzleRunSnapshot,
payload: DragPuzzlePieceRequest,
@@ -222,18 +575,20 @@ export function dragLocalPuzzlePiece(
if (!moving) {
return run;
}
const occupying = pieces.find(
(piece) =>
piece.pieceId !== payload.pieceId &&
piece.currentRow === payload.targetRow &&
piece.currentCol === payload.targetCol,
);
const source = { row: moving.currentRow, col: moving.currentCol };
moving.currentRow = payload.targetRow;
moving.currentCol = payload.targetCol;
if (occupying) {
occupying.currentRow = source.row;
occupying.currentCol = source.col;
if (moving.mergedGroupId) {
const moved = dragGroup(
pieces,
moving,
payload.targetRow,
payload.targetCol,
currentLevel.gridSize,
);
if (!moved) {
return run;
}
} else {
dragSinglePiece(pieces, moving, payload.targetRow, payload.targetCol);
}
return applyNextBoard(run, rebuildBoardSnapshot(currentLevel.gridSize, pieces));

View File

@@ -0,0 +1,37 @@
import { ASSET_API_PATHS } from '../../editor/shared/editorApiClient';
import { requestJson } from '../apiClient';
export type PuzzleHistoryAsset = {
assetObjectId: string;
assetKind: 'puzzle_cover_image';
imageSrc: string;
ownerUserId?: string | null;
ownerLabel: string;
profileId?: string | null;
entityId?: string | null;
createdAt: string;
updatedAt: string;
};
/**
* 读取历史拼图图片素材。结果页只把它们作为参考图来源,
* 不直接替换当前正式图,正式图仍由后端单图生成链路写回。
*/
export async function listPuzzleHistoryAssets(payload: { limit?: number }) {
const params = new URLSearchParams({ kind: 'puzzle_cover_image' });
if (payload.limit) {
params.set('limit', String(payload.limit));
}
const response = await requestJson<{ assets: PuzzleHistoryAsset[] }>(
`${ASSET_API_PATHS.assetHistory}?${params.toString()}`,
{ method: 'GET' },
'读取历史拼图素材失败',
);
return response.assets;
}
export const puzzleAssetClient = {
listHistoryAssets: listPuzzleHistoryAssets,
};

View File

@@ -35,6 +35,7 @@ export interface GameRuntimeStats {
}
export type PlayerProgressionGrantSource = 'quest' | 'hostile_npc';
export type GameRuntimeMode = 'play' | 'preview' | 'test';
export interface PlayerProgressionState {
level: number;
@@ -51,6 +52,8 @@ export interface GameState {
playerCharacter: Character | null;
runtimeSessionId?: string | null;
runtimeActionVersion?: number;
runtimeMode?: GameRuntimeMode;
runtimePersistenceDisabled?: boolean;
runtimeStats: GameRuntimeStats;
playerProgression?: PlayerProgressionState | null;
currentScene: string;

View File

@@ -77,6 +77,11 @@ export default defineConfig(({mode}) => {
changeOrigin: true,
secure: false,
},
'/generated-puzzle-assets': {
target: runtimeServerTarget,
changeOrigin: true,
secure: false,
},
'/generated-custom-world-scenes': {
target: runtimeServerTarget,
changeOrigin: true,