mm_datas 0.0.0-stage → 5.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example/custom_demo.js +237 -0
- package/example/model/item_model.json +46 -0
- package/example/model/map_model.json +53 -0
- package/example/model/player_model.json +65 -0
- package/example/model/role_model.json +61 -0
- package/example/model/skill_model.json +60 -0
- package/example/preset_demo.js +44 -0
- package/example/rpg_cache.js +179 -0
- package/example/rpg_crud.js +170 -0
- package/example/rpg_demo.js +61 -0
- package/example/rpg_model/bag_model.json +74 -0
- package/example/rpg_model/character_model.json +165 -0
- package/example/rpg_model/character_skill_model.json +61 -0
- package/example/rpg_model/drop_model.json +103 -0
- package/example/rpg_model/item_model.json +189 -0
- package/example/rpg_model/job_model.json +96 -0
- package/example/rpg_model/level_exp_model.json +111 -0
- package/example/rpg_model/map_model.json +166 -0
- package/example/rpg_model/map_state_model.json +63 -0
- package/example/rpg_model/monster_model.json +115 -0
- package/example/rpg_model/npc_model.json +103 -0
- package/example/rpg_model/quest_model.json +106 -0
- package/example/rpg_model/quest_progress_model.json +61 -0
- package/example/rpg_model/save_model.json +42 -0
- package/example/rpg_model/skill_model.json +124 -0
- package/example/rpg_persistence.js +151 -0
- package/example/rpg_relations.js +196 -0
- package/example/rpg_world.js +149 -0
- package/example/storage_demo.js +194 -0
- package/index.js +122 -0
- package/lib/backend.js +597 -0
- package/lib/errors.js +35 -0
- package/lib/manager.js +717 -0
- package/lib/model.js +540 -0
- package/lib/pool.js +277 -0
- package/lib/relation.js +175 -0
- package/lib/store.js +430 -0
- package/package.json +64 -5
- package/test.js +748 -0
- package/tool/data/index.js +107 -0
- package/tool/data/tool.json +211 -0
- package/README.md +0 -3
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "skill",
|
|
3
|
+
"kind": "static",
|
|
4
|
+
"version": 7,
|
|
5
|
+
"description": "技能模板(静态配置)",
|
|
6
|
+
"key": "skill_id",
|
|
7
|
+
"store": {
|
|
8
|
+
"db": "memory",
|
|
9
|
+
"table": "sys_skill"
|
|
10
|
+
},
|
|
11
|
+
"schema": {
|
|
12
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
13
|
+
"type": "object",
|
|
14
|
+
"properties": {
|
|
15
|
+
"skill_id": {
|
|
16
|
+
"type": "number",
|
|
17
|
+
"primary": true
|
|
18
|
+
},
|
|
19
|
+
"name": {
|
|
20
|
+
"type": "string"
|
|
21
|
+
},
|
|
22
|
+
"job_id": {
|
|
23
|
+
"type": "number",
|
|
24
|
+
"ref": "job",
|
|
25
|
+
"description": "限定职业"
|
|
26
|
+
},
|
|
27
|
+
"type": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"enum": [
|
|
30
|
+
"active",
|
|
31
|
+
"passive",
|
|
32
|
+
"ultimate"
|
|
33
|
+
],
|
|
34
|
+
"default": "active"
|
|
35
|
+
},
|
|
36
|
+
"mp_cost": {
|
|
37
|
+
"type": "number",
|
|
38
|
+
"default": 0
|
|
39
|
+
},
|
|
40
|
+
"damage": {
|
|
41
|
+
"type": "number",
|
|
42
|
+
"default": 0
|
|
43
|
+
},
|
|
44
|
+
"cooldown": {
|
|
45
|
+
"type": "number",
|
|
46
|
+
"default": 0
|
|
47
|
+
},
|
|
48
|
+
"level_req": {
|
|
49
|
+
"type": "number",
|
|
50
|
+
"default": 1
|
|
51
|
+
},
|
|
52
|
+
"description": {
|
|
53
|
+
"type": "string",
|
|
54
|
+
"default": ""
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"required": [
|
|
58
|
+
"name"
|
|
59
|
+
],
|
|
60
|
+
"additionalProperties": false
|
|
61
|
+
},
|
|
62
|
+
"seed": [
|
|
63
|
+
{
|
|
64
|
+
"skill_id": 101,
|
|
65
|
+
"name": "强力斩击",
|
|
66
|
+
"job_id": 1,
|
|
67
|
+
"type": "active",
|
|
68
|
+
"mp_cost": 8,
|
|
69
|
+
"damage": 35,
|
|
70
|
+
"cooldown": 3,
|
|
71
|
+
"level_req": 1
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"skill_id": 102,
|
|
75
|
+
"name": "旋风斩",
|
|
76
|
+
"job_id": 1,
|
|
77
|
+
"type": "ultimate",
|
|
78
|
+
"mp_cost": 30,
|
|
79
|
+
"damage": 120,
|
|
80
|
+
"cooldown": 20,
|
|
81
|
+
"level_req": 20
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
"skill_id": 201,
|
|
85
|
+
"name": "火球术",
|
|
86
|
+
"job_id": 2,
|
|
87
|
+
"type": "active",
|
|
88
|
+
"mp_cost": 12,
|
|
89
|
+
"damage": 48,
|
|
90
|
+
"cooldown": 2,
|
|
91
|
+
"level_req": 1
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"skill_id": 202,
|
|
95
|
+
"name": "暴风雪",
|
|
96
|
+
"job_id": 2,
|
|
97
|
+
"type": "ultimate",
|
|
98
|
+
"mp_cost": 45,
|
|
99
|
+
"damage": 160,
|
|
100
|
+
"cooldown": 25,
|
|
101
|
+
"level_req": 25
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"skill_id": 301,
|
|
105
|
+
"name": "穿透箭",
|
|
106
|
+
"job_id": 3,
|
|
107
|
+
"type": "active",
|
|
108
|
+
"mp_cost": 10,
|
|
109
|
+
"damage": 40,
|
|
110
|
+
"cooldown": 2,
|
|
111
|
+
"level_req": 1
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"skill_id": 401,
|
|
115
|
+
"name": "治愈术",
|
|
116
|
+
"job_id": 4,
|
|
117
|
+
"type": "active",
|
|
118
|
+
"mp_cost": 15,
|
|
119
|
+
"damage": 0,
|
|
120
|
+
"cooldown": 4,
|
|
121
|
+
"level_req": 1
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 第 4 章:持久化(存档、重启恢复、配置与玩家数据分工)
|
|
4
|
+
*
|
|
5
|
+
* 运行:node example/rpg_persistence.js
|
|
6
|
+
*
|
|
7
|
+
* 本章覆盖:
|
|
8
|
+
* - memory store(默认)vs json store(每表一个 .json + _tables.json)
|
|
9
|
+
* - autoSave:写入即落盘;以及手动 save() / load()
|
|
10
|
+
* - 重启模拟:新池 → 先 loadModels 恢复 schema → 再 load 恢复数据
|
|
11
|
+
* - 反例:不带模型定义直接 load,集合会退化成自由集合、schema 校验失效
|
|
12
|
+
* - 整包 dump() 写进业务存档集合(save),按角色 / 存档位读档
|
|
13
|
+
* - 静态配置(随代码发布)与动态数据(玩家存档)的持久化分工
|
|
14
|
+
*/
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const mm = require('../index.js');
|
|
19
|
+
const W = require('./rpg_world.js');
|
|
20
|
+
|
|
21
|
+
function fileList(dir) {
|
|
22
|
+
return fs.readdirSync(dir).sort();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function run() {
|
|
26
|
+
W.title('第 4 章 / 持久化:存档、重启恢复与配置分工');
|
|
27
|
+
const stamp = Date.now();
|
|
28
|
+
const autoDir = path.join(os.tmpdir(), 'mm_data_rpg_auto_' + stamp);
|
|
29
|
+
const manualDir = path.join(os.tmpdir(), 'mm_data_rpg_manual_' + stamp);
|
|
30
|
+
|
|
31
|
+
// ---------- 1. 两种 store ----------
|
|
32
|
+
// v5 起「装载了 *_model.json 的池」默认就是 hybrid(data/<集合>/000001.json),
|
|
33
|
+
// 本章想演示纯内存,必须显式写 store:'memory';v5 目录布局另见 storage_demo.js。
|
|
34
|
+
W.title('4.1 显式 memory 不落盘,json store 才写文件(v5 默认 hybrid 见 storage_demo)');
|
|
35
|
+
const mem = W.createWorld('rpg_persist_mem', { store: 'memory' });
|
|
36
|
+
W.show('内存池后端', mem.getServer().constructor.name);
|
|
37
|
+
W.show('内存池调用 save()', mem.data('character').save());
|
|
38
|
+
W.show('内存池目录不存在', fs.existsSync(path.join(autoDir, 'character.json')) === false);
|
|
39
|
+
|
|
40
|
+
const w = W.open('rpg_persist_auto', { store: 'json', dir: autoDir, autoSave: true });
|
|
41
|
+
const c = w.c;
|
|
42
|
+
const reg = w.reg;
|
|
43
|
+
const h = w.heroes;
|
|
44
|
+
W.show('json 池后端', w.pool.getServer().constructor.name + ' / autoSave=' + w.pool.getServer().autoSave);
|
|
45
|
+
W.show('落盘目录', autoDir);
|
|
46
|
+
const files = fileList(autoDir);
|
|
47
|
+
W.show('生成文件', files.length + ' 个:' + files.slice(0, 6).join('、') + ' …');
|
|
48
|
+
W.show('_tables.json', JSON.parse(fs.readFileSync(path.join(autoDir, '_tables.json'), 'utf8')).length + ' 张表');
|
|
49
|
+
W.show('character.json 前 60 字符', fs.readFileSync(path.join(autoDir, 'character.json'), 'utf8').slice(0, 60).replace(/\s+/g, ' '));
|
|
50
|
+
|
|
51
|
+
// ---------- 2. autoSave ----------
|
|
52
|
+
W.title('4.2 autoSave:每次写入自动落盘');
|
|
53
|
+
const charFile = path.join(autoDir, 'character.json');
|
|
54
|
+
const sizeBefore = fs.statSync(charFile).size;
|
|
55
|
+
const savedHero = c.character.add({
|
|
56
|
+
player_id: 4001, name: '存档战士', job_id: 1, map_id: 1001, level: 7, gold: 999
|
|
57
|
+
});
|
|
58
|
+
const sizeAfter = fs.statSync(charFile).size;
|
|
59
|
+
W.show('新增角色 #' + savedHero.id + ' 后文件变化', sizeBefore + ' → ' + sizeAfter + ' 字节');
|
|
60
|
+
const onDisk = JSON.parse(fs.readFileSync(charFile, 'utf8'));
|
|
61
|
+
W.show('磁盘上确实有这条记录', onDisk[onDisk.length - 1].name + ' / gold=' + onDisk[onDisk.length - 1].gold);
|
|
62
|
+
c.character.patch(savedHero.id, { $inc: { gold: 1 } });
|
|
63
|
+
W.show('patch 后磁盘金币', JSON.parse(fs.readFileSync(charFile, 'utf8'))[onDisk.length - 1].gold);
|
|
64
|
+
|
|
65
|
+
// ---------- 3. 手动 save / load + 重启模拟 ----------
|
|
66
|
+
W.title('4.3 手动存档与「重启」恢复(先定义,后数据)');
|
|
67
|
+
const w2 = W.open('rpg_persist_manual', { store: 'json', dir: manualDir });
|
|
68
|
+
const c2 = w2.c;
|
|
69
|
+
const reg2 = w2.reg;
|
|
70
|
+
const h2 = w2.heroes;
|
|
71
|
+
W.show('未 save 前目录存在?', fs.existsSync(manualDir));
|
|
72
|
+
const leveled = c2.character.add({
|
|
73
|
+
player_id: 5001, name: '手动存档法师', job_id: 2, map_id: 1003, level: 30, gold: 12345
|
|
74
|
+
});
|
|
75
|
+
c2.bag.add({ character_id: leveled.id, item_id: 1003, quantity: 1 });
|
|
76
|
+
W.show('写完后目录存在?', fs.existsSync(manualDir) + '(autoSave 关闭,需手动 save)');
|
|
77
|
+
W.show('reg.save()', reg2.save());
|
|
78
|
+
W.show('手动落盘文件数', fileList(manualDir).length);
|
|
79
|
+
W.show('存档里的角色数', JSON.parse(fs.readFileSync(path.join(manualDir, 'character.json'), 'utf8')).length);
|
|
80
|
+
|
|
81
|
+
// 模拟进程重启:一个全新的池,先加载模型定义,再恢复数据
|
|
82
|
+
const restored = W.createWorld('rpg_persist_restore', { store: 'json', dir: manualDir });
|
|
83
|
+
const regR = mm.dataAdmin.registry(restored);
|
|
84
|
+
W.show('新池恢复 load()', regR.load());
|
|
85
|
+
const rc = W.collections(restored);
|
|
86
|
+
W.show('恢复后角色数 / 背包数', rc.character.size + ' / ' + rc.bag.size);
|
|
87
|
+
W.show('恢复的那条角色', rc.character.get({ name: '手动存档法师' }).level + ' 级 / gold=' +
|
|
88
|
+
rc.character.get({ name: '手动存档法师' }).gold);
|
|
89
|
+
W.show('schema 是否仍生效', rc.character.constrained + ' / constrained,' + rc.character.schema().$schema);
|
|
90
|
+
try { rc.character.add({ name: '缺字段' }); W.show('恢复后写入校验', '未拦截(异常!)'); }
|
|
91
|
+
catch (e) { W.show('恢复后写入仍被校验', e.code + ' / ' + e.message.split('; ')[0]); }
|
|
92
|
+
W.show('恢复后引用完整性', regR.verify());
|
|
93
|
+
|
|
94
|
+
// 反例:不带模型定义直接 load
|
|
95
|
+
W.title('4.4 反例:不带模型定义直接 load 会丢失约束');
|
|
96
|
+
const bare = mm.createPool('rpg_persist_bare', { store: 'json', dir: manualDir });
|
|
97
|
+
const regB = mm.dataAdmin.registry(bare);
|
|
98
|
+
regB.load();
|
|
99
|
+
const bareChar = bare.data('character');
|
|
100
|
+
W.show('裸恢复后的 character', 'constrained=' + bareChar.constrained + ' / schema=' + bareChar.schema());
|
|
101
|
+
bareChar.add({ 随便一个字段: '会被放行' });
|
|
102
|
+
W.show('结论', '恢复玩家数据前必须先 loadModels(或 createPool({models})),否则集合退化成自由集合');
|
|
103
|
+
|
|
104
|
+
// ---------- 4. 业务存档:dump ----------
|
|
105
|
+
W.title('4.5 整包 dump() 写进业务存档集合(按角色 / 存档位)');
|
|
106
|
+
const dump = reg2.dump();
|
|
107
|
+
W.show('dump() 内容', Object.keys(dump).length + ' 张表 / 共 ' + reg2.stats().total + ' 行');
|
|
108
|
+
const slot1 = c2.save.add({
|
|
109
|
+
character_id: h2.arthur, slot: 1,
|
|
110
|
+
data: dump, saved_at: '2026-11-18 23:00:00'
|
|
111
|
+
});
|
|
112
|
+
const slot2 = c2.save.add({
|
|
113
|
+
character_id: h2.arthur, slot: 2,
|
|
114
|
+
data: { character: c2.character.all().filter(function (r) { return r.id === h2.arthur; }) },
|
|
115
|
+
saved_at: '2026-11-18 23:05:00'
|
|
116
|
+
});
|
|
117
|
+
W.show('写入两个存档位', '#' + slot1.id + '(整包) / #' + slot2.id + '(单角色)');
|
|
118
|
+
const read1 = c2.save.get({ character_id: h2.arthur, slot: 1 });
|
|
119
|
+
W.show('读档 slot1', Object.keys(read1.data).length + ' 张表,其中 character ' +
|
|
120
|
+
read1.data.character.length + ' 行、bag ' + read1.data.bag.length + ' 行');
|
|
121
|
+
const read2 = c2.save.get({ character_id: h2.arthur, slot: 2 });
|
|
122
|
+
W.show('读档 slot2', read2.data.character[0].name + ' / Lv.' + read2.data.character[0].level);
|
|
123
|
+
W.show('存档集合自身也可落盘', (function () {
|
|
124
|
+
reg2.save();
|
|
125
|
+
return fs.existsSync(path.join(manualDir, 'save.json')) ?
|
|
126
|
+
'save.json ' + fs.statSync(path.join(manualDir, 'save.json')).size + ' 字节' : '未生成';
|
|
127
|
+
})());
|
|
128
|
+
|
|
129
|
+
// ---------- 5. 边界与分工 ----------
|
|
130
|
+
W.title('4.6 边界与持久化分工');
|
|
131
|
+
const missingDir = path.join(os.tmpdir(), 'mm_data_rpg_no_such_' + stamp);
|
|
132
|
+
const missingPool = mm.createPool('rpg_persist_missing', { store: 'json', dir: missingDir });
|
|
133
|
+
W.show('load 不存在的目录', mm.dataAdmin.registry(missingPool).load());
|
|
134
|
+
W.show('换目录另存', reg2.save(path.join(os.tmpdir(), 'mm_data_rpg_copy_' + stamp)).dir);
|
|
135
|
+
W.show('静态配置(9 张)', '随模型 JSON 一起发布 → 启动 loadModels 即可,不需要玩家存档');
|
|
136
|
+
W.show('动态数据(6 张)', 'character / bag / character_skill / quest_progress / map_state / save → save/load 或 autoSave');
|
|
137
|
+
W.show('推荐组合', '静态表 = 代码里的模型定义 + seed;动态表 = json store + autoSave;跨版本靠 cfg_ver 钉扎');
|
|
138
|
+
|
|
139
|
+
W.title('第 4 章完成');
|
|
140
|
+
return { dir: autoDir, tables: fileList(autoDir).length };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (require.main === module) {
|
|
144
|
+
run().catch(function (e) {
|
|
145
|
+
console.error('演示失败: ' + (e.code || 'ERROR') + ' - ' + e.message);
|
|
146
|
+
console.error(e.stack);
|
|
147
|
+
process.exitCode = 1;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
module.exports = { run: run };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 第 2 章:RPG 数据之间的关联
|
|
4
|
+
*
|
|
5
|
+
* 运行:node example/rpg_relations.js
|
|
6
|
+
*
|
|
7
|
+
* 本章覆盖:
|
|
8
|
+
* - 多对一:角色 → 职业 / 地图;任务 → NPC / 怪物 / 奖励道具
|
|
9
|
+
* - 一对多:角色 → 背包 / 已学技能 / 任务进度
|
|
10
|
+
* - 多对多:角色 × 技能(中间表 character_skill);公会 × 成员(运行时声明)
|
|
11
|
+
* - 运行时声明关联:ref() / hasMany(),任意集合都能加
|
|
12
|
+
* - 写时引用校验 ref、读时批量展开 with、投影 select、反查 reverse、全库 verify
|
|
13
|
+
* - 用「后端查询次数」证明展开关联不会 N+1
|
|
14
|
+
* - 关联的两种策略:live 实时展开 vs snapshot 写时冗余(后者见第 3 章缓存)
|
|
15
|
+
*/
|
|
16
|
+
const W = require('./rpg_world.js');
|
|
17
|
+
const DRAFT = 'http://json-schema.org/draft-07/schema#';
|
|
18
|
+
|
|
19
|
+
function names(rows, field) {
|
|
20
|
+
return rows.map(function (r) { return r[field || 'name']; }).join('、');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function run() {
|
|
24
|
+
W.title('第 2 章 / 角色、职业、技能、地图、道具之间的关联');
|
|
25
|
+
const w = W.open('rpg_relations');
|
|
26
|
+
const c = w.c;
|
|
27
|
+
const reg = w.reg;
|
|
28
|
+
const h = w.heroes;
|
|
29
|
+
|
|
30
|
+
// ---------- 1. 模型里声明了哪些关联 ----------
|
|
31
|
+
W.title('2.1 关联清单(由 schema 的 ref 与 relations 自动生成)');
|
|
32
|
+
W.show('character 的关联', c.character.info().relations);
|
|
33
|
+
W.show('bag 的关联', c.bag.info().relations);
|
|
34
|
+
W.show('quest 的关联', c.quest.info().relations);
|
|
35
|
+
W.show('character.job_id 的 ref', c.character.definition().schema.properties.job_id.ref);
|
|
36
|
+
|
|
37
|
+
// ---------- 2. 多对一 / 一对多 ----------
|
|
38
|
+
W.title('2.2 一次读回完整角色档案(多对一 + 一对多)');
|
|
39
|
+
const full = c.character.get(h.arthur, {
|
|
40
|
+
with: ['job', 'map', 'items', 'skills', 'quests']
|
|
41
|
+
});
|
|
42
|
+
W.show('角色', full.name + ' / ' + full.job.name + ' / Lv.' + full.level + ' / ' + full.map.name +
|
|
43
|
+
' (' + (full.map.safe_zone ? '安全区' : '野外') + ')');
|
|
44
|
+
W.show('背包(with items)', full.items.map(function (b) {
|
|
45
|
+
return b.item_name + '×' + b.quantity + (b.equipped ? '[装备]' : '');
|
|
46
|
+
}).join('、'));
|
|
47
|
+
W.show('已学技能(with skills)', full.skills.map(function (s) {
|
|
48
|
+
return s.skill_name + ' Lv.' + s.level;
|
|
49
|
+
}).join('、'));
|
|
50
|
+
W.show('任务进度(with quests)', full.quests.map(function (q) {
|
|
51
|
+
return '#' + q.quest_id + ' ' + q.status + ' ' + q.progress;
|
|
52
|
+
}).join('、'));
|
|
53
|
+
|
|
54
|
+
// ---------- 3. 静态配置之间的关联 ----------
|
|
55
|
+
W.title('2.3 配置表之间也有关联:任务 / 掉落 / NPC');
|
|
56
|
+
const q1 = c.quest.get(1, { with: ['npc', 'monster', 'item'] });
|
|
57
|
+
W.show('任务「清理史莱姆」', q1.npc.name + ' 发布 → 打 ' + q1.monster.name + ' ×' + q1.target_count +
|
|
58
|
+
' → 奖励 ' + q1.item.name + ' / ' + q1.reward_exp + ' exp');
|
|
59
|
+
const bossDrop = c.drop.list({ monster_id: 99 }, { with: ['item'], limit: -1 });
|
|
60
|
+
W.show('红龙掉落表展开道具', bossDrop.list.map(function (d) { return d.item.name; }).join('、'));
|
|
61
|
+
const townNpcs = c.npc.list({ map_id: 1001 }, { with: ['map'], limit: -1 });
|
|
62
|
+
W.show('主城 NPC 所在地图', Array.from(new Set(townNpcs.list.map(function (n) { return n.map.name; }))).join('、'));
|
|
63
|
+
|
|
64
|
+
// ---------- 4. select 投影 / 别名 ----------
|
|
65
|
+
W.title('2.4 关联结果裁剪:select / as / 自定义 with');
|
|
66
|
+
const slim = c.character.get(h.merlin, {
|
|
67
|
+
with: [
|
|
68
|
+
{ ref: 'job', as: 'profession', select: ['name', 'base_mp'] },
|
|
69
|
+
{ ref: 'items', as: 'bag_list', select: ['item_name', 'quantity'] },
|
|
70
|
+
{ ref: 'skills', as: 'spellbook', select: ['skill_name', 'level'] }
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
W.show('职业裁剪', slim.profession);
|
|
74
|
+
W.show('背包裁剪', slim.bag_list);
|
|
75
|
+
W.show('技能裁剪', slim.spellbook);
|
|
76
|
+
|
|
77
|
+
// ---------- 5. 多对多 ----------
|
|
78
|
+
W.title('2.5 多对多:角色 × 技能(中间表 + 批量补全,无 N+1)');
|
|
79
|
+
const store = c.character.store;
|
|
80
|
+
store.calls.list = 0;
|
|
81
|
+
const learned = c.character_skill.list({ character_id: { $in: [h.arthur, h.merlin, h.robin] } }, { limit: -1 }).list;
|
|
82
|
+
const skillIds = Array.from(new Set(learned.map(function (r) { return r.skill_id; })));
|
|
83
|
+
const templates = c.skill.list({ skill_id: { $in: skillIds } }, { limit: -1 }).list;
|
|
84
|
+
const byId = new Map(templates.map(function (s) { return [s.skill_id, s]; }));
|
|
85
|
+
const calls = store.calls.list;
|
|
86
|
+
W.show('3 个角色共 ' + learned.length + ' 条已学技能 / ' + skillIds.length + ' 个技能模板',
|
|
87
|
+
'后端查询 ' + calls + ' 次(中间表 1 + 模板表 1,与角色数无关)');
|
|
88
|
+
W.show('亚瑟技能详情', learned.filter(function (r) { return r.character_id === h.arthur; }).map(function (r) {
|
|
89
|
+
const t = byId.get(r.skill_id);
|
|
90
|
+
return t.name + ' Lv.' + r.level + ' / 耗蓝 ' + t.mp_cost + ' / 伤害 ' + t.damage;
|
|
91
|
+
}).join(';'));
|
|
92
|
+
|
|
93
|
+
// 业务规则:职业限制不在 schema 里,用关联在应用层判定
|
|
94
|
+
function learn(characterId, skillId) {
|
|
95
|
+
const role = c.character.get(characterId);
|
|
96
|
+
const sk = c.skill.get(skillId);
|
|
97
|
+
if (!role || !sk) return '角色或技能不存在';
|
|
98
|
+
if (sk.job_id !== role.job_id) return '拒绝:' + c.job.get(role.job_id).name + '不能学「' + sk.name + '」';
|
|
99
|
+
if (role.level < sk.level_req) return '拒绝:等级不足(需 ' + sk.level_req + ')';
|
|
100
|
+
const r = c.character_skill.add({ character_id: characterId, skill_id: skillId });
|
|
101
|
+
return '已学会「' + c.skill.get(skillId).name + '」#' + r.id;
|
|
102
|
+
}
|
|
103
|
+
W.show('战士学火球术(跨职业)', learn(h.arthur, 201));
|
|
104
|
+
W.show('弓手学旋风斩(跨职业)', learn(h.robin, 102));
|
|
105
|
+
W.show('法师学暴风雪(Lv.21 < 需求 25)', learn(h.merlin, 202));
|
|
106
|
+
W.show('战士学旋风斩(合法)', learn(h.arthur, 102));
|
|
107
|
+
|
|
108
|
+
// ---------- 6. 运行时声明新关联 ----------
|
|
109
|
+
W.title('2.6 运行时给任意集合加关联:ref() / hasMany()');
|
|
110
|
+
const guild = c.character.collection('guild', {
|
|
111
|
+
key: 'id',
|
|
112
|
+
schema: {
|
|
113
|
+
$schema: DRAFT, type: 'object',
|
|
114
|
+
properties: {
|
|
115
|
+
name: { type: 'string', minLength: 1 },
|
|
116
|
+
leader_id: { type: 'number' },
|
|
117
|
+
notice: { type: 'string', default: '' }
|
|
118
|
+
},
|
|
119
|
+
required: ['name'], additionalProperties: false
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
guild.ref('leader_id', 'character', { as: 'leader', select: ['id', 'name', 'level'] });
|
|
123
|
+
const member = c.character.collection('guild_member', {
|
|
124
|
+
key: 'id',
|
|
125
|
+
schema: {
|
|
126
|
+
$schema: DRAFT, type: 'object',
|
|
127
|
+
properties: {
|
|
128
|
+
guild_id: { type: 'number' },
|
|
129
|
+
character_id: { type: 'number' },
|
|
130
|
+
rank: { type: 'string', enum: ['leader', 'officer', 'member'], default: 'member' }
|
|
131
|
+
},
|
|
132
|
+
required: ['guild_id', 'character_id'], additionalProperties: false
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
member.ref('character_id', 'character', { as: 'player', select: ['id', 'name', 'level'] });
|
|
136
|
+
member.ref('guild_id', 'guild', { as: 'guild_info', select: ['id', 'name'] });
|
|
137
|
+
guild.hasMany('members', { model: 'guild_member', via: 'guild_id', local: 'id' });
|
|
138
|
+
const g1 = guild.add({ name: '晨曦骑士团', leader_id: h.arthur, notice: '每周屠龙' });
|
|
139
|
+
member.addMany([
|
|
140
|
+
{ guild_id: g1.id, character_id: h.arthur, rank: 'leader' },
|
|
141
|
+
{ guild_id: g1.id, character_id: h.robin, rank: 'officer' }
|
|
142
|
+
]);
|
|
143
|
+
const gFull = guild.get(g1.id, { with: ['leader', 'members'] });
|
|
144
|
+
W.show('公会 + 会长 + 成员', gFull.name + ' / 会长 ' + gFull.leader.name + ' / 成员 ' +
|
|
145
|
+
names(gFull.members, 'character_id'));
|
|
146
|
+
W.show('guild 的关联清单', guild.info().relations);
|
|
147
|
+
W.show('悬空引用仍被拦截', (function () {
|
|
148
|
+
try { member.add({ guild_id: 999, character_id: h.arthur }); return '未拦截(异常!)'; }
|
|
149
|
+
catch (e) { return e.code; }
|
|
150
|
+
})());
|
|
151
|
+
|
|
152
|
+
// ---------- 7. 无 N+1 ----------
|
|
153
|
+
W.title('2.7 展开关联不会 N+1(对比不同结果行数的后端调用次数)');
|
|
154
|
+
function measure(query, withSpec) {
|
|
155
|
+
store.calls.list = 0;
|
|
156
|
+
const res = c.character.list(query, { with: withSpec, limit: -1 });
|
|
157
|
+
return { rows: res.list.length, calls: store.calls.list };
|
|
158
|
+
}
|
|
159
|
+
const spec = ['job', 'map', 'items', 'skills', 'quests'];
|
|
160
|
+
const one = measure({ id: h.arthur }, spec);
|
|
161
|
+
const many = measure({}, spec);
|
|
162
|
+
W.show('1 个角色 × 5 个关联', one);
|
|
163
|
+
W.show(many.rows + ' 个角色 × 5 个关联', many);
|
|
164
|
+
W.show('结论', '两次后端调用次数相同(1 次主表 + 5 次批量关联),与行数无关');
|
|
165
|
+
|
|
166
|
+
// ---------- 8. 反查 / 完整性 ----------
|
|
167
|
+
W.title('2.8 反查 reverse 与全库引用完整性 verify');
|
|
168
|
+
W.show('谁引用了战士职业(1)', c.job.reverse(1).list.map(function (r) {
|
|
169
|
+
return r.collection + '.' + r.field;
|
|
170
|
+
}).slice(0, 6).join('、') + ' …(共 ' + c.job.reverse(1).count + ' 条)');
|
|
171
|
+
W.show('谁在黑暗森林(1003)', c.map.reverse(1003).list.map(function (r) { return r.collection; }).join('、'));
|
|
172
|
+
W.show('谁引用了红龙(99)', c.monster.reverse(99).list.map(function (r) { return r.collection; }).join('、'));
|
|
173
|
+
W.show('谁持有小红药水(3001)', c.item.reverse(3001).count + ' 条');
|
|
174
|
+
W.show('全库 verify()', reg.verify());
|
|
175
|
+
|
|
176
|
+
// ---------- 9. 两种关联策略 ----------
|
|
177
|
+
W.title('2.9 live 展开 vs snapshot 写时冗余');
|
|
178
|
+
W.show('live(with item)', c.bag.first({ item_id: 1001 }).id + ' → ' +
|
|
179
|
+
c.bag.first({ item_id: 1001 }, { with: ['item'] }).item.name);
|
|
180
|
+
const snap = c.bag.first({ item_id: 1001 });
|
|
181
|
+
W.show('snapshot(写时冗余,不 join 直接读)', snap.item_name + ' / cfg_ver=' + snap.cfg_ver);
|
|
182
|
+
W.show('何时用哪种', '配置常变 → with 实时展开;战斗/结算高频且要留历史 → snapshot 冗余字段');
|
|
183
|
+
|
|
184
|
+
W.title('第 2 章完成');
|
|
185
|
+
return { guild_members: member.size, verify: reg.verify().ok };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (require.main === module) {
|
|
189
|
+
run().catch(function (e) {
|
|
190
|
+
console.error('演示失败: ' + (e.code || 'ERROR') + ' - ' + e.message);
|
|
191
|
+
console.error(e.stack);
|
|
192
|
+
process.exitCode = 1;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = { run: run };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* RPG 示例世界的构建器(可复用,不属于 mm_data 核心)
|
|
4
|
+
*
|
|
5
|
+
* 集合定义全部来自 example/rpg_model/*.json —— 那 15 个 JSON 只是「调用方提供的一份模型」,
|
|
6
|
+
* 换成自己的目录、自己的表名即可,mm_data 不会默认加载它们。
|
|
7
|
+
*
|
|
8
|
+
* 每个模型的 schema 都是标准 JSON Schema(draft-07 子集),
|
|
9
|
+
* 类型只用 string/number/boolean/object/array/null,整数一律 number,不出现 int/integer;
|
|
10
|
+
* 规则判定全部由 mm_validator 执行。
|
|
11
|
+
*
|
|
12
|
+
* 本文件只做两件事:
|
|
13
|
+
* 1) createWorld() —— 在独立的 mm_admin 实例池里加载模型(静态配置常驻内存)
|
|
14
|
+
* 2) seedWorld() —— 造一批可直接用于 CRUD / 关联 / 缓存 / 持久化演示的初始数据
|
|
15
|
+
*/
|
|
16
|
+
const mm = require('../index.js');
|
|
17
|
+
|
|
18
|
+
const MODEL_DIR = mm.EXAMPLE_MODEL_DIR;
|
|
19
|
+
|
|
20
|
+
/** 打印章节标题 */
|
|
21
|
+
function title(text) {
|
|
22
|
+
console.log('\n===== ' + text + ' =====');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 打印一行「标签: 值」 */
|
|
26
|
+
function show(label, value) {
|
|
27
|
+
console.log(' ' + label + ': ' + (typeof value === 'string' ? value : JSON.stringify(value)));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 建一个独立的 RPG 世界。
|
|
32
|
+
* namespace 不同 → 数据完全隔离;config 可带 store / dir / autoSave。
|
|
33
|
+
* @returns {object} mm_admin 实例池
|
|
34
|
+
*/
|
|
35
|
+
function createWorld(namespace, config) {
|
|
36
|
+
const cfg = Object.assign({ models: MODEL_DIR }, config || {});
|
|
37
|
+
return mm.createPool(namespace || 'rpg', cfg);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 把常用集合一次性取出,避免示例里反复写 pool.data('xxx') */
|
|
41
|
+
function collections(pool) {
|
|
42
|
+
return {
|
|
43
|
+
// 静态配置(kind:static,默认只读常驻内存)
|
|
44
|
+
job: pool.data('job'),
|
|
45
|
+
skill: pool.data('skill'),
|
|
46
|
+
map: pool.data('map'),
|
|
47
|
+
item: pool.data('item'),
|
|
48
|
+
monster: pool.data('monster'),
|
|
49
|
+
npc: pool.data('npc'),
|
|
50
|
+
quest: pool.data('quest'),
|
|
51
|
+
drop: pool.data('drop'),
|
|
52
|
+
level_exp: pool.data('level_exp'),
|
|
53
|
+
// 动态数据(玩家产生的读写)
|
|
54
|
+
character: pool.data('character'),
|
|
55
|
+
bag: pool.data('bag'),
|
|
56
|
+
character_skill: pool.data('character_skill'),
|
|
57
|
+
quest_progress: pool.data('quest_progress'),
|
|
58
|
+
map_state: pool.data('map_state'),
|
|
59
|
+
save: pool.data('save')
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 造初始数据:3 个主角 + 12 个路人群像 + 背包 / 已学技能 / 任务进度 / 地图刷怪。
|
|
65
|
+
* 所有写入都会经过 schema 校验与引用完整性校验(ref)。
|
|
66
|
+
*/
|
|
67
|
+
function seedWorld(pool) {
|
|
68
|
+
const c = collections(pool);
|
|
69
|
+
|
|
70
|
+
// 主角:战士 / 法师 / 弓手,分别落在不同地图、不同职业
|
|
71
|
+
const heroes = c.character.addMany([
|
|
72
|
+
{
|
|
73
|
+
player_id: 1001, name: '亚瑟', job_id: 1, level: 24, exp: 5200,
|
|
74
|
+
hp: 620, mp: 60, attack: 96, defense: 62, gold: 3800,
|
|
75
|
+
map_id: 1002, x: 1200, y: 800, online: true, created_at: '2026-11-18 20:00:00'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
player_id: 1002, name: '梅林', job_id: 2, level: 21, exp: 4100,
|
|
79
|
+
hp: 330, mp: 480, attack: 38, defense: 24, gold: 2150,
|
|
80
|
+
map_id: 1003, x: 300, y: 240, online: true, created_at: '2026-11-18 20:05:00'
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
player_id: 1003, name: '罗宾', job_id: 3, level: 18, exp: 3000,
|
|
84
|
+
hp: 410, mp: 180, attack: 74, defense: 38, gold: 980,
|
|
85
|
+
map_id: 1001, x: 320, y: 480, online: false, created_at: '2026-11-18 20:10:00'
|
|
86
|
+
}
|
|
87
|
+
]).ids;
|
|
88
|
+
const [arthur, merlin, robin] = heroes;
|
|
89
|
+
|
|
90
|
+
// 用模型自带的 generate 模板批量造路人群像(用于分页 / 排序 / 条件查询演示)
|
|
91
|
+
c.character.generate(12);
|
|
92
|
+
|
|
93
|
+
// 背包:item_id 指向静态道具模板;item_name / cfg_ver 由 mm_data 自动写快照
|
|
94
|
+
c.bag.addMany([
|
|
95
|
+
{ character_id: arthur, item_id: 1001, quantity: 1, equipped: true },
|
|
96
|
+
{ character_id: arthur, item_id: 2001, quantity: 1, equipped: true },
|
|
97
|
+
{ character_id: arthur, item_id: 3001, quantity: 20 },
|
|
98
|
+
{ character_id: arthur, item_id: 4001, quantity: 7 },
|
|
99
|
+
{ character_id: merlin, item_id: 3003, quantity: 35 },
|
|
100
|
+
{ character_id: merlin, item_id: 4002, quantity: 3 },
|
|
101
|
+
{ character_id: robin, item_id: 3001, quantity: 12 },
|
|
102
|
+
{ character_id: robin, item_id: 5001, quantity: 1 }
|
|
103
|
+
]);
|
|
104
|
+
|
|
105
|
+
// 多对多中间表:角色 × 技能模板,skill_name / cfg_ver 同样走快照
|
|
106
|
+
c.character_skill.addMany([
|
|
107
|
+
{ character_id: arthur, skill_id: 101, level: 5, exp: 820 },
|
|
108
|
+
{ character_id: arthur, skill_id: 102, level: 2, exp: 140 },
|
|
109
|
+
{ character_id: merlin, skill_id: 201, level: 6, exp: 1300 },
|
|
110
|
+
{ character_id: merlin, skill_id: 202, level: 1, exp: 0 },
|
|
111
|
+
{ character_id: robin, skill_id: 301, level: 4, exp: 560 }
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
// 任务进度
|
|
115
|
+
c.quest_progress.addMany([
|
|
116
|
+
{ character_id: arthur, quest_id: 1, status: 'done', progress: 10, custom_data: { kills: 10 }, updated_at: '2026-11-18 21:00:00' },
|
|
117
|
+
{ character_id: arthur, quest_id: 2, status: 'active', progress: 5, custom_data: { kills: 5 }, updated_at: '2026-11-18 21:30:00' },
|
|
118
|
+
{ character_id: merlin, quest_id: 2, status: 'active', progress: 3, custom_data: { kills: 3 }, updated_at: '2026-11-18 21:10:00' },
|
|
119
|
+
{ character_id: robin, quest_id: 1, status: 'active', progress: 2, custom_data: { kills: 2 }, updated_at: '2026-11-18 20:40:00' }
|
|
120
|
+
]);
|
|
121
|
+
|
|
122
|
+
// 地图实例里的怪物刷新状态(静态模板 × 运行时实体)
|
|
123
|
+
c.map_state.addMany([
|
|
124
|
+
{ map_id: 1002, monster_id: 1, x: 300, y: 400, hp: 40, alive: true },
|
|
125
|
+
{ map_id: 1002, monster_id: 2, x: 900, y: 620, hp: 90, alive: true },
|
|
126
|
+
{ map_id: 1003, monster_id: 3, x: 1200, y: 300, hp: 240, alive: true },
|
|
127
|
+
{ map_id: 1004, monster_id: 4, x: 600, y: 450, hp: 380, alive: true },
|
|
128
|
+
{ map_id: 1005, monster_id: 99, x: 900, y: 700, hp: 12000, alive: true, respawn_at: '2026-11-19 00:00:00' }
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
return { c: c, heroes: { arthur: arthur, merlin: merlin, robin: robin }, list: heroes };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** 一步到位:建池 + 造数据,返回后续章节需要的全部手柄 */
|
|
135
|
+
function open(namespace, config) {
|
|
136
|
+
const pool = createWorld(namespace, config);
|
|
137
|
+
const seeded = seedWorld(pool);
|
|
138
|
+
return Object.assign({ pool: pool, reg: mm.dataAdmin.registry(pool) }, seeded);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = {
|
|
142
|
+
MODEL_DIR: MODEL_DIR,
|
|
143
|
+
title: title,
|
|
144
|
+
show: show,
|
|
145
|
+
createWorld: createWorld,
|
|
146
|
+
collections: collections,
|
|
147
|
+
seedWorld: seedWorld,
|
|
148
|
+
open: open
|
|
149
|
+
};
|