第12章 - 实战:开发一个新的典故
12.1 目标
前面十一章把 CHAGS 的每个零件都拆开讲过了。这一章把它们装回去:从零添加一个新的典故「孔融让梨」,走完内容、游戏包、注册、测试、验证的完整流程。
- id:
kongrong - 分组:
allusion(典故组) - 定性:信史(东汉),
yearApprox: 190 - 动词:让
- 玩法:复用
place模板(把东西放到它该去的地方)—— 三只大小不同的梨,一只只送到三位家人手里 - 预期改动:两份内容 JSON、两份英文补丁、四处登记、一个游戏包(5 个文件)、外壳注册两处、端到端用例一条
选 place 不是偷懒:模板库的设计目标就是「新条目优先复用,装不下再新增模板」,place 的注释里就写着「把东西放到它该去的地方(盘古化身、孔融让梨、完璧归赵)」。本章最后也给出「什么时候该新写模板」的判断。
注意:从 2026-10-15 起,内容目录叫
entries/而不是allusions/,每条内容拆成摘要与详情两个文件。这一章的路径与写法全部按当前源码给出。
12.2 第一步:摘要 JSON
新建 packages/content/src/entries/kongrong.json(摘要:大厅与图鉴要用的那一半):
{
"id": "kongrong",
"group": "allusion",
"kind": "history",
"title": "孔融让梨",
"pinyin": "Kǒng róng ràng lí",
"dynasty": "东汉",
"era": "东汉末年 · 约一千八百年前",
"yearApprox": 190,
"source": {
"book": "《后汉书·孔融传》",
"quote": "年四岁时,与诸兄共食梨,融辄引小者。大人问其故,答曰:我小儿,法当取小者。"
},
"moral": "把大的让出去,把小的留给自己。",
"keywords": ["谦让", "分享", "家人"],
"ageRange": [6, 9]
}
自查点:
group必填,schema 刻意不给默认值 —— 少写就在构建期报错;era写给孩子看(「东汉末年 · 约一千八百年前」),不要写年号;moral≤90 字,一句话说完;keywords2–6 个;yearApprox决定它在典故组里的位置(越小越靠前);- 典故组不许写
stratagem/thinkingTag/transfer(后两个只有三十六计才有;给典故写stratagem会被 schema 的 refine 拒绝)。
12.3 第二步:详情 JSON
新建 packages/content/src/entries/kongrong.game.json(详情:故事 / 抉择 / 复习题 / 关卡脚本):
{
"figures": ["孔融"],
"story": [
{ "id": "s1", "title": "一盘梨",
"text": "桌上有一盘梨,有大有小。",
"narration": "桌上有一盘梨,有大有小。" },
{ "id": "s2", "title": "谁来拿",
"text": "哥哥们看着孔融,他只有四岁。",
"narration": "哥哥们都看着孔融,他只有四岁。" }
],
"keyMoment": {
"beatId": "s2",
"question": "一盘梨有大有小,孔融会拿哪一个?",
"options": [
{ "id": "big", "text": "挑最大的", "isCanonical": false },
{ "id": "small", "text": "拿最小的,大的留给哥哥", "isCanonical": true },
{ "id": "none", "text": "都不吃", "isCanonical": false }
],
"reveal": "孔融拿起了最小的梨 —— 我年纪小,应该吃小的。"
},
"misconceptions": [
"谦让就是把自己喜欢的全都让出去",
"孔融想吃大的,只是不敢拿"
],
"quiz": [
{ "id": "q1", "kind": "recall",
"question": "孔融把什么样的梨留给了自己?",
"options": ["最大的", "最小的", "不好吃的"],
"answer": 1,
"explanation": "他把最大的让给哥哥,自己拿了最小的。" },
{ "id": "q2", "kind": "recall",
"question": "这件事发生在孔融几岁的时候?",
"options": ["四岁", "十岁", "十四岁"],
"answer": 0,
"explanation": "他四岁的时候,就知道把大的让给哥哥。" },
{ "id": "q3", "kind": "transfer",
"question": "下面哪件事和「让梨」是同一个道理?",
"options": ["把最后一个座位让给爷爷", "抢在前面拿玩具", "自己的东西紧紧抱住"],
"answer": 0,
"explanation": "都是把好的先让给别人。" }
],
"game": {
"template": "place",
"stages": [
{ "template": "place", "levelScript": {
"slots": [
{ "id": "elder", "x": 170, "y": 380, "kind": "elder", "radius": 78 },
{ "id": "second", "x": 375, "y": 330, "kind": "second", "radius": 78 },
{ "id": "kong", "x": 580, "y": 380, "kind": "kong", "radius": 78 }
],
"orbs": [
{ "id": "pearBig", "slotId": "elder", "x": 150, "y": 1226, "kind": "big",
"speak": "最大的给大哥" },
{ "id": "pearMid", "slotId": "second", "x": 375, "y": 1226, "kind": "mid",
"speak": "不大不小的给二哥" },
{ "id": "pearSmall", "slotId": "kong", "x": 600, "y": 1226, "kind": "small",
"speak": "最小的留给我自己" }
]
} }
]
}
}
对照第 6 章的 schema 自查一遍:
story恰好两幕(drawBeat只会画两幅),text每句 ≤90 字;keyMoment恰好一个isCanonical: true,beatId指向s2;quiz恰好三题,其中至少一道kind: "transfer";game.template与stages[].template都必须是TEMPLATE_IDS里真实存在的 id;place的硬约束(PlaceLevelSchema的 refine):光球数与槽位数一致、槽位 id 不重复、每个光球指向存在的槽位、每个槽位恰好被一个光球占用;- 每个光球都带
speak(归位时念出的一句话,用听不用看)。
12.4 第三步:四处登记 + 两半英文
一条内容要在四个地方各登记一行 —— 摘要、详情、摘要译文、详情译文分属四个文件。少登记一处,构建期或注册表单测会当场报错:
// 1) packages/content/src/entries/index.ts —— 摘要
import rawKongrong from './kongrong.json';
export const entrySummaries: EntrySummary[] = [
/* … */
summaryOf(rawKongrong),
];
// 2) packages/content/src/entries/detail.ts —— 详情
import rawKongrongGame from './kongrong.game.json';
export const entryDetails: Record<string, EntryDetail> = {
/* … */
kongrong: detailOf(rawKongrongGame),
};
// 3) packages/content/src/i18n/index.ts —— 摘要译文
import enKongrong from './en/kongrong.json';
// …
// 4) packages/content/src/i18n/game.ts —— 详情译文
import enKongrongGame from './en/kongrong.game.json';
export const gamePatches = { en: { /* … */ kongrong: enKongrongGame } };
英文补丁是结构镜像,只写要翻译的字段:
// i18n/en/kongrong.json(摘要补丁)
{
"title": "Kong Rong Shares the Pears",
"pinyin": "Kǒng róng ràng lí",
"dynasty": "Eastern Han",
"era": "Late Eastern Han · about 1,800 years ago",
"source": {
"book": "Book of the Later Han · Biography of Kong Rong",
"quote": "At four, sharing pears with his elder brothers, Kong Rong always took the smallest..."
},
"moral": "Give the big one away, keep the small one.",
"keywords": ["Yielding", "Sharing", "Family"]
}
// i18n/en/kongrong.game.json(详情补丁)
{
"figures": ["Kong Rong"],
"story": [
{ "title": "A Plate of Pears",
"text": "A plate of pears sat on the table, big ones and small ones.",
"narration": "A plate of pears sat on the table, big ones and small ones." },
{ "title": "Who Takes Which",
"text": "His brothers looked at Kong Rong. He was only four.",
"narration": "His brothers all looked at Kong Rong. He was only four years old." }
],
"keyMoment": {
"question": "The pears were big and small. Which one would Kong Rong take?",
"options": [{ "text": "The biggest one" }, { "text": "The smallest one" }, { "text": "None of them" }],
"reveal": "Kong Rong took the smallest pear — I am the youngest, so I should take the small one."
},
"misconceptions": ["Yielding means giving away everything you like"],
"quiz": [
{ "question": "Which pear did Kong Rong keep for himself?",
"options": ["The biggest", "The smallest", "The bad one"],
"explanation": "He left the biggest for his brothers and took the smallest." },
{ "question": "How old was Kong Rong when this happened?",
"options": ["Four", "Ten", "Fourteen"],
"explanation": "At four, he already knew to leave the big ones for his brothers." },
{ "question": "Which of these is the same idea as sharing the pears?",
"options": ["Giving the last seat to grandpa", "Rushing to grab first", "Holding your toys tight"],
"explanation": "Both are about letting others have the good thing first." }
],
"game": { "stages": [{ "levelScript": { "orbs": [
{ "speak": "The biggest one is for our eldest brother" },
{ "speak": "The middle one is for our second brother" },
{ "speak": "The smallest one is for me" }
] } }] }
}
两点必须注意:
- 数组按下标对齐:两幕故事写两项、三道题写三块、三个光球写三条 —— 否则
validatePatch报「数组长度不一致」; - 关卡脚本里的文案也要翻(
orbs[].speak)。漏了不会静默通过:content:check会在合并后扫中文,并报出具体路径。
12.5 第四步:跑内容校验
pnpm content:check
期望在新内容处看到(节选):
· 孔融让梨 (kongrong)
· 信史 / 2 幕故事 / 3 道复习题 / 1 段玩法:place
语言 en:
· Kong Rong Shares the Pears
如果 place 脚本写得不合法(少一个光球、槽位重复、两个光球指向同一个槽位),会在这里直接报错,例如:
✗ place 脚本 orbs: 光球数量必须与槽位数量一致
这一步永远在写游戏代码之前做 —— 内容先立住,游戏包只是「怎么画」。
12.6 第五步:游戏包
在 games/kongrong/ 下建 5 到 6 个文件。
package.json
{
"name": "@chags/game-kongrong",
"version": "0.1.0",
"private": true,
"type": "module",
"exports": { ".": "./src/index.ts" },
"scripts": {
"test": "vitest run",
"typecheck": "vue-tsc --noEmit -p ../../tsconfig.json"
},
"dependencies": {
"@chags/content": "workspace:*",
"@chags/game-kit": "workspace:*",
"@chags/phaser-kit": "workspace:*",
"@chags/templates": "workspace:*",
"phaser": "^4.2.1"
}
}
建完后在仓库根跑一次 pnpm install 让 workspace 链接生效。
src/manifest.ts
import type { GameManifest } from '@chags/game-kit';
import { DESIGN } from '@chags/templates/layout';
export const manifest: GameManifest = {
id: 'kongrong',
title: '孔融让梨',
version: '0.1.0',
template: 'place',
designSize: { width: DESIGN.width, height: DESIGN.height },
ageRange: [6, 9],
};
src/logic.ts
import { stageScript } from '@chags/content';
import type { ContentEntry } from '@chags/content';
import type { PlaceLevel } from '@chags/templates/schemas';
import { asLevel } from '@chags/game-kit';
/** 纯逻辑:不碰 Phaser,可以直接单测 */
export function placeLevel(content: ContentEntry): PlaceLevel {
return asLevel<PlaceLevel>(stageScript(content, 'place'));
}
asLevel 是断言而不是运行时校验:正确性由构建期的 content:check 与单测负例保证,生产包里不拖 zod(第 4 章)。
src/art.ts(全部画面由代码画)
import type Phaser from 'phaser';
import { THEME, circle, groundShadow, roundRect, shade } from '@chags/phaser-kit';
const PEAR_SIZE: Record<string, number> = { big: 54, mid: 42, small: 30 };
function drawPear(g: Phaser.GameObjects.Graphics, x: number, y: number, kind: string): void {
const r = PEAR_SIZE[kind] ?? 40;
g.fillStyle(THEME.leaf, 1);
g.fillEllipse(x, y - r * 1.3, r * 0.9, r * 0.55); // 叶子
g.fillStyle(0xf5d76e, 1);
g.fillEllipse(x, y, r * 1.7, r * 2.1); // 梨身
g.lineStyle(6, THEME.outline, 1);
g.strokeEllipse(x, y, r * 1.7, r * 2.1);
g.fillStyle(shade(0xf5d76e, 0.86), 1);
g.fillCircle(x - r * 0.4, y + r * 0.35, r * 0.28); // 腮部阴影
}
/** 光球:一只梨,大小按 kind(big / mid / small) */
export function drawOrb(g: Phaser.GameObjects.Graphics, kind: string): void {
drawPear(g, 0, 0, kind);
}
/** 槽位:三位家人;归位后手里多一只梨 */
export function drawPiece(
g: Phaser.GameObjects.Graphics, kind: string, x: number, y: number, filled: boolean,
): void {
const s = kind === 'second' ? 1.06 : 0.96;
groundShadow(g, x, y + 96, 150 * s, 26, 0.14);
roundRect(g, x - 54 * s, y - 60 * s, 108 * s, 150 * s, 44,
kind === 'kong' ? THEME.red : THEME.teal, THEME.outline, 6);
circle(g, x, y - 96 * s, 44 * s, THEME.sand, THEME.outline, 6);
if (filled) drawPear(g, x + 70, y - 34, kind === 'elder' ? 'big' : kind === 'second' ? 'mid' : 'small');
}
/** 分幕插图:第一幕一盘梨,第二幕三个孩子 */
export function drawBeat(
g: Phaser.GameObjects.Graphics,
index: 0 | 1,
rect: { x: number; y: number; w: number; h: number },
): void {
const cx = rect.x + rect.w / 2;
const cy = rect.y + rect.h / 2;
if (index === 0) {
roundRect(g, cx - 200, cy + 90, 400, 40, 20, THEME.wood, THEME.outline, 6);
drawPear(g, cx - 90, cy + 20, 'big');
drawPear(g, cx, cy + 52, 'mid');
drawPear(g, cx + 90, cy + 76, 'small');
return;
}
for (const [dx, h] of [[-170, 300], [-40, 330], [110, 190]] as const) {
groundShadow(g, cx + dx, cy + 170, h * 0.7, 30, 0.14);
roundRect(g, cx + dx - h * 0.18, cy + 170 - h, h * 0.36, h, 60, THEME.teal, THEME.outline, 6);
circle(g, cx + dx, cy + 170 - h - 46, h * 0.22, THEME.sand, THEME.outline, 6);
}
}
画法遵循第 8 章的工具箱约定:厚描边(THEME.outline)、落地阴影、三种尺寸的梨(视觉上能直接看出大小)。drawBeat 的 index 类型必须是 0 | 1 —— 契约只画两幕。
src/index.ts
import { defineGame, SCENE_KEYS } from '@chags/templates/core';
import { createPlaceScene } from '@chags/templates/place';
import { drawBeat, drawOrb, drawPiece } from './art';
import { placeLevel } from './logic';
import { manifest } from './manifest';
export default defineGame({
manifest,
drawBeat,
gameplay: (ctx, session) => ({
entry: SCENE_KEYS.place,
scenes: [
createPlaceScene(ctx, { level: placeLevel(ctx.content), session, drawPiece, drawOrb }),
],
}),
});
注意 import 全走子路径(@chags/templates/core / /place),不要 import 主 barrel —— 否则 24 个场景会一起进 chunk。nextSceneKey 与 reveal 可以不传:resolveEnding() 会默认成 SCENE_KEYS.reward 与 ctx.content.keyMoment.reveal。
至此游戏包完成:一共不到 150 行,其中一半是画面。defineGame 会自动给它装上分幕故事与典故卡(第 4 章),createPlaceScene 提供零失败归位、拿起即提示、念白播放与无头自动。
src/logic.test.ts
import { getEntry } from '@chags/content/full';
import { describe, expect, it } from 'vitest';
import { placeLevel } from './logic';
describe('孔融让梨 · 关卡数据', () => {
const raw = getEntry('kongrong');
it('三位家人、三只梨,一一对应', () => {
const level = placeLevel(raw!);
expect(level.slots.length).toBe(3);
expect(level.orbs.length).toBe(3);
expect(new Set(level.orbs.map((o) => o.slotId)).size).toBe(3);
});
it('最大的梨在大哥的槽位,最小的留给孔融', () => {
const level = placeLevel(raw!);
const orb = (id: string) => level.orbs.find((o) => o.id === id);
expect(orb('pearBig')?.slotId).toBe('elder');
expect(orb('pearSmall')?.slotId).toBe('kong');
});
it('每只梨归位时都有一句念白', () => {
const level = placeLevel(raw!);
for (const orb of level.orbs) expect(orb.speak.length).toBeGreaterThan(0);
});
});
单测从 @chags/content/full 取完整条目(Node 侧同步入口),不经过浏览器的懒加载路径。
12.7 第六步:外壳注册
两处改动:
// apps/site/src/registry.ts —— 加一行
export const registry = {
/* … */
kongrong: () => import('@chags/game-kongrong'),
};
// apps/site/package.json —— 加一条依赖
"dependencies": {
/* … */
"@chags/game-kongrong": "workspace:*"
}
改完 pnpm install 一次。此后 apps/site/src/registry.test.ts(第 13 章)会自动开始要求「内容目录、注册表、懒加载入口」三方一致 —— 哪个环节写漏了,pnpm test 直接红。
12.8 第七步:运行与测试
开发时直接直达:
http://127.0.0.1:5183/#/play/kongrong
http://127.0.0.1:5183/?autoplay=1#/play/kongrong
然后补一条端到端真实操作用例(e2e/smoke.spec.ts,模式与 nuwa / pangu 相同):
const kongrongPlace = loadContent('kongrong').game.stages[0]!
.levelScript as unknown as PlaceScript;
test('孔融让梨:真实拖拽把三只梨送到家人手里', { tag: '@real' }, async ({ page }) => {
await page.goto('/#/play/kongrong');
await expect(page.locator('canvas')).toBeVisible();
await expect.poll(() => events(page)).toContain('session:start');
// 两幕故事:等旁白念完(story:ready)再翻页,不靠固定 sleep
await waitEvent(page, 'story:ready:b1', 20_000);
await clickDesign(page, 375, 900);
await waitEvent(page, 'story:ready:b2', 20_000);
await clickDesign(page, 375, 900);
await expect.poll(() => events(page)).toContain('scene:enter:place');
// 三只梨各归各位:坐标直接从内容 JSON 读,不手抄
for (const orb of kongrongPlace.orbs) {
const slot = kongrongPlace.slots.find((s) => s.id === orb.slotId);
if (!slot) throw new Error('光球 ' + orb.id + ' 找不到槽位');
await dragDesign(page, { x: orb.x, y: orb.y }, { x: slot.x, y: slot.y });
await page.waitForTimeout(200);
}
await expect.poll(() => events(page), { timeout: 30_000 }).toContain('card:unlocked');
// 「收下」按钮中心 = REWARD.done.x + 168(两个按钮并排、各距卡片中心 168)
await clickDesign(page, REWARD.done.x + 168, REWARD.done.y);
await expect.poll(() => events(page)).toContain('session:exit');
});
两个细节来自最近的两次踩坑:
- 翻页等
story:ready,不要用固定waitForTimeout—— 旁白时长随文案变化; - 「收下」要点
REWARD.done.x + 168,不要点REWARD.done.x:那是两个按钮之间的空隙。旧命中区偏移半个按钮时这个空点会「漏」到收下按钮上,命中区修好之后就会失败(第 16 章)。
另外两条用例会自动覆盖新内容,不需要手写:双语载入用例会枚举内容目录里的全部 id × 两种语言;三十六计的 autoplay 冒烟清单也从内容目录驱动。建议再补一条 autoplay 用例,保证无头流程也过一遍。
12.9 第八步:完整验证
pnpm verify # 类型 / lint / 单测 / 内容体检 / 构建 / 体积(五步并行)
pnpm e2e # 端到端(含新加的用例)
预期:
- 单测从 1229 项增长到 1232 项(新内容 3 项)全部通过;
registry.test.ts的五条交叉断言通过(id / 标题 / 年龄 / 模板必须真的用在关卡脚本里 / 版本号);- 构建产物里多出
game-kongrong-*.js与content-kongrong-*.js两个小 chunk(几 KB),Phaser 仍是共享 chunk; - 体积报告的七项预算全部在限内。
最后跑一遍 pnpm audit:i18n,人工看一眼「孔融让梨」在两种语言下的每个画面(故事、归位、典故卡),确认没有排版问题 —— 英文比中文长的这个差异,是这类新内容最容易踩的坑。
12.10 如果要加的是第 37 计
流程与上面完全一样,只多三处「针对 stratagem 组」的差异:
- 摘要里写
"group": "stratagem"、"kind": "strategy"、"stratagem": { "number": 37, "set": 7 }—— 但套只有 6 套,所以要动STRATAGEM_SETS与groups.ts、以及stratagem.set.7的文案; - 登记顺序必须等于计策序号:
content:check会断言numbers.join(',') === expected.join(','); - 必须写
thinkingTag(2–6 字)与transfer(≥12 字,且英文也要有,否则 CJK 漏翻检查会红)。
换句话说:加一条典故是「三份文件 + 一个包」,加一计是「同样的流程 + 三条额外数据约束」。
12.11 什么时候该新写一个模板
复用优先,但遇到下面任一情况,就该按第 7 章的清单新增模板(而不是硬塞进现有模板):
- 判定语义不同:
place判「放对位置」,如果玩法核心是「在正确的时间点动手」(守株待兔、一鼓作气),这是新判定; - 教学核心需要新的强制约束:
sort的「先尝后分」是数据约束,不是表现效果 —— 塞进place会把教学核心降级成文案; - 输入原语不存在:棋盘对阵(田忌赛马)、剧情分支与现有任何模板都不共享判定;
- 同一段「定制代码」在两个条目里重复出现 —— 到第三个就该泛化,而不是复制。
判断标准可以归结为一句话:新模板的价值是让第二个条目变便宜,否则它只是一次性代码。 三十六计后三辑的做法可以作为反例参照:第五、六辑一个模板都没新增,全部是给已有模板加模式,老模式判定与单测一行未动。
12.12 小结
新增一个典故的完整清单:
| 步骤 | 文件 | 校验者 |
|---|---|---|
| 1. 摘要 JSON | packages/content/src/entries/kongrong.json |
pnpm typecheck(结构)+ content:check(取值) |
| 2. 详情 JSON | packages/content/src/entries/kongrong.game.json |
同上 + 模板 schema |
| 3. 四处登记 | entries/index.ts / entries/detail.ts / i18n/index.ts / i18n/game.ts |
registration.test.ts |
| 4. 英文补丁 ×2 | i18n/en/kongrong.json + kongrong.game.json |
content:check(结构 / 长度 / 漏翻) |
| 5. 游戏包 ×5 | games/kongrong/src/* |
vue-tsc / eslint / vitest |
| 6. 外壳注册 ×2 | registry.ts + apps/site/package.json |
registry.test.ts |
| 7. 端到端 | e2e/smoke.spec.ts |
Playwright(含 @real 标签) |
| 8. 全量验证 | — | pnpm verify + pnpm e2e + audit:i18n |
内容先行、画面后补、测试兜底 —— 这套流程对「第 18 个典故」和「第 100 个条目」是一样的。
下一章展开讲这些守护者的实现:测试体系与质量门禁。