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,237 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 主演示:基于 mm_admin 实例池,完全自定义地构造一套游戏数据
|
|
4
|
+
*
|
|
5
|
+
* model.schema 是一份标准 JSON Schema(draft-07 子集):
|
|
6
|
+
* { $schema, type:'object', properties:{...}, required:[...], additionalProperties:false }
|
|
7
|
+
* 类型只用 string / number / boolean / object / array / null,不出现 int / integer。
|
|
8
|
+
* 规则判定全部委托 mm_validator,mm_datas 只负责关键词翻译与错误路径。
|
|
9
|
+
*
|
|
10
|
+
* 运行:node example/custom_demo.js
|
|
11
|
+
*/
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const os = require('os');
|
|
14
|
+
const mm = require('../index.js');
|
|
15
|
+
const tool = require('../tool/data/index.js');
|
|
16
|
+
|
|
17
|
+
const DRAFT = 'http://json-schema.org/draft-07/schema#';
|
|
18
|
+
|
|
19
|
+
function title(t) { console.log('\n===== ' + t + ' ====='); }
|
|
20
|
+
|
|
21
|
+
async function run() {
|
|
22
|
+
mm.dataAdmin.reset();
|
|
23
|
+
title('0. 起点:mm_admin 实例池 + 空注册表');
|
|
24
|
+
const pool = mm.dataAdmin();
|
|
25
|
+
console.log('池类型:', pool.constructor.name, '/ 绑定的管理类:', pool.getClass().name);
|
|
26
|
+
console.log('mm_admin 全局注册表:', mm.list(), '/ 集合数:', mm.dataAdmin.registry().models().length);
|
|
27
|
+
const vi = mm.validator();
|
|
28
|
+
console.log('schema 校验器:', vi.package, 'v' + vi.version, '/ 可用工具:', vi.available_tools.length);
|
|
29
|
+
console.log('schema 规范:', vi.schema_draft, '/ 允许类型:', vi.allowed_types.join(' / '));
|
|
30
|
+
console.log('禁用类型:', vi.forbidden_types.join(' / '), '(一律改用 number)');
|
|
31
|
+
|
|
32
|
+
// ---- 1. 静态配置集合:名字、字段、种子全部自定义 ----
|
|
33
|
+
title('1. 自定义静态集合:map / item(标准 JSON Schema)');
|
|
34
|
+
const map = mm.dataAdmin('map', {
|
|
35
|
+
kind: 'static', key: 'map_id', version: 1,
|
|
36
|
+
schema: {
|
|
37
|
+
$schema: DRAFT, type: 'object',
|
|
38
|
+
properties: {
|
|
39
|
+
name: { type: 'string', maxLength: 20 },
|
|
40
|
+
safe: { type: 'boolean', default: true },
|
|
41
|
+
portals: {
|
|
42
|
+
type: 'array', default: [],
|
|
43
|
+
items: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: { to: { type: 'number' }, label: { type: 'string' } },
|
|
46
|
+
required: ['to', 'label'], additionalProperties: false
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
required: ['name'], additionalProperties: false
|
|
51
|
+
},
|
|
52
|
+
seed: [
|
|
53
|
+
{ map_id: 1001, name: '晨曦之城', safe: true, portals: [{ to: 1002, label: '前往幽暗森林' }] },
|
|
54
|
+
{ map_id: 1002, name: '幽暗森林', safe: false, portals: [{ to: 1001, label: '返回主城' }] }
|
|
55
|
+
]
|
|
56
|
+
});
|
|
57
|
+
const item = mm.dataAdmin('item', {
|
|
58
|
+
kind: 'static', key: 'item_id', version: 12,
|
|
59
|
+
schema: {
|
|
60
|
+
$schema: DRAFT, type: 'object',
|
|
61
|
+
properties: {
|
|
62
|
+
name: { type: 'string', minLength: 1 },
|
|
63
|
+
atk: { type: 'number', default: 0 },
|
|
64
|
+
price: { type: 'number', minimum: 0, default: 0 }
|
|
65
|
+
},
|
|
66
|
+
required: ['name'], additionalProperties: false
|
|
67
|
+
},
|
|
68
|
+
seed: [
|
|
69
|
+
{ item_id: 1001, name: '铁剑', atk: 12, price: 120 },
|
|
70
|
+
{ item_id: 3001, name: '小红药水', price: 20 },
|
|
71
|
+
{ item_id: 4001, name: '皮甲', price: 80 }
|
|
72
|
+
]
|
|
73
|
+
});
|
|
74
|
+
console.log('map.schema():', map.schema().$schema, '/', map.schema().type);
|
|
75
|
+
console.log('地图传送点:', map.get(1001).portals.map(function (p) { return p.label; }).join(' / '));
|
|
76
|
+
console.log('道具模板:', item.all().map(function (i) { return i.name; }).join('、'));
|
|
77
|
+
|
|
78
|
+
// ---- 2. 动态角色集合:标准 JSON Schema,含嵌套 object / array ----
|
|
79
|
+
title('2. DataManager 角色集合 + 标准 JSON Schema');
|
|
80
|
+
const role = mm.dataAdmin('role', {
|
|
81
|
+
key: 'role_id',
|
|
82
|
+
schema: {
|
|
83
|
+
$schema: DRAFT, type: 'object',
|
|
84
|
+
properties: {
|
|
85
|
+
name: { type: 'string', minLength: 1, maxLength: 12 },
|
|
86
|
+
level: { type: 'number', minimum: 1, maximum: 99, default: 1 },
|
|
87
|
+
hp: { type: 'number', minimum: 0, default: 100 },
|
|
88
|
+
mp: { type: 'number', minimum: 0, default: 0 },
|
|
89
|
+
gold: { type: 'number', minimum: 0, default: 0 },
|
|
90
|
+
exp: { type: 'number', minimum: 0, default: 0 },
|
|
91
|
+
map_id: { type: 'number' },
|
|
92
|
+
x: { type: 'number', default: 0 },
|
|
93
|
+
y: { type: 'number', default: 0 },
|
|
94
|
+
title: { type: 'string', enum: ['见习', '骑士', '勇者'], default: '见习' },
|
|
95
|
+
attrs: {
|
|
96
|
+
type: 'object',
|
|
97
|
+
properties: {
|
|
98
|
+
element: { type: 'string', enum: ['fire', 'water', 'earth'] },
|
|
99
|
+
power: { type: 'number', minimum: 0 }
|
|
100
|
+
},
|
|
101
|
+
required: ['element'], additionalProperties: false
|
|
102
|
+
},
|
|
103
|
+
tags: { type: 'array', items: { type: 'string' }, maxItems: 3, default: [] }
|
|
104
|
+
},
|
|
105
|
+
required: ['name'], additionalProperties: false
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
console.log('role 是 DataManager:', role instanceof mm.DataManager, ',约束已启用:', role.constrained);
|
|
109
|
+
console.log('schema() 是标准文档:', role.schema().type, '/ properties:', Object.keys(role.schema().properties).length);
|
|
110
|
+
console.log('字段:', role.info().fields.join(', '));
|
|
111
|
+
console.log('范围越界:', JSON.stringify(role.validate({ name: '测试', level: 1000 }).errors));
|
|
112
|
+
console.log('类型不符:', JSON.stringify(role.validate({ name: '测试', level: '高' }).errors));
|
|
113
|
+
console.log('必填为空:', JSON.stringify(role.validate({ name: '', level: 1 }).errors[0]));
|
|
114
|
+
console.log('枚举不符:', JSON.stringify(role.validate({ name: '测试', title: '魔王' }).errors));
|
|
115
|
+
console.log('未声明字段:', JSON.stringify(role.validate({ name: '测试', extra: 1 }).errors));
|
|
116
|
+
console.log('嵌套 object 未声明属性:', JSON.stringify(role.validate({ name: '测试', attrs: { element: 'fire', evil: 9 } }).errors));
|
|
117
|
+
console.log('数组元素类型:', JSON.stringify(role.validate({ name: '测试', tags: ['a', 2] }).errors));
|
|
118
|
+
try { mm.dataAdmin('bad_int', { schema: { $schema: DRAFT, type: 'object', properties: { lv: { type: 'integer' } } } }); }
|
|
119
|
+
catch (e) { console.log('int 类型在定义阶段被拒:', e.code, '-', e.message.split(';')[0]); }
|
|
120
|
+
console.log('以上规则由 mm_validator 执行;本次用到工具:', mm.validator().engine_tools.join(','),
|
|
121
|
+
'/ 累计调用:', mm.validator().calls);
|
|
122
|
+
try { role.add({ name: '测试', level: 1000, map_id: 1001 }); }
|
|
123
|
+
catch (e) { console.log('写入越界被拦截:', e.code, '-', e.message.split('; ')[0]); }
|
|
124
|
+
|
|
125
|
+
// ---- 3. 运行时声明关联 ----
|
|
126
|
+
title('3. 运行时声明关联(ref / hasMany / snapshot)');
|
|
127
|
+
role.ref('map_id', 'map', { as: 'map_info', select: ['map_id', 'name', 'safe'] });
|
|
128
|
+
const bag = mm.dataAdmin('bag', {
|
|
129
|
+
key: 'bag_id',
|
|
130
|
+
schema: {
|
|
131
|
+
$schema: DRAFT, type: 'object',
|
|
132
|
+
properties: {
|
|
133
|
+
role_id: { type: 'number' },
|
|
134
|
+
item_id: { type: 'number' },
|
|
135
|
+
quantity: { type: 'number', minimum: 1, default: 1 },
|
|
136
|
+
equipped: { type: 'boolean', default: false },
|
|
137
|
+
item_name: { type: 'string', default: '' },
|
|
138
|
+
cfg_ver: { type: 'number', default: 0 }
|
|
139
|
+
},
|
|
140
|
+
additionalProperties: false
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
bag.ref('item_id', 'item', { as: 'item_info', select: ['name', 'price'] });
|
|
144
|
+
bag.field('item_name', { type: 'string', default: '', snapshot: 'item.name' });
|
|
145
|
+
bag.field('cfg_ver', { type: 'number', default: 0 });
|
|
146
|
+
role.hasMany('items', { model: 'bag', via: 'role_id', local: 'role_id' });
|
|
147
|
+
|
|
148
|
+
// ---- 4. 写数据 ----
|
|
149
|
+
title('4. 写数据:角色 / 背包');
|
|
150
|
+
const hero = role.add({
|
|
151
|
+
name: '亚瑟', level: 10, hp: 280, mp: 40, map_id: 1001, x: 300, y: 400,
|
|
152
|
+
attrs: { element: 'fire', power: 12 }, tags: ['tank']
|
|
153
|
+
});
|
|
154
|
+
bag.addMany([
|
|
155
|
+
{ role_id: hero.id, item_id: 1001, quantity: 1, equipped: true },
|
|
156
|
+
{ role_id: hero.id, item_id: 3001, quantity: 20 },
|
|
157
|
+
{ role_id: hero.id, item_id: 4001, quantity: 1 }
|
|
158
|
+
]);
|
|
159
|
+
role.add({ name: '梅林', level: 9, hp: 160, map_id: 1002, attrs: { element: 'water' } });
|
|
160
|
+
console.log('创建角色 role_id=' + hero.id + ',背包写入 ' + bag.size + ' 条');
|
|
161
|
+
console.log('快照字段: item_name=' + bag.get(1).item_name + ',cfg_ver=' + bag.get(1).cfg_ver);
|
|
162
|
+
try { bag.add({ role_id: hero.id, item_id: 99999 }); }
|
|
163
|
+
catch (e) { console.log('悬空引用被拦截:', e.code, '-', e.message); }
|
|
164
|
+
|
|
165
|
+
// ---- 5. 一次读回完整角色档案 ----
|
|
166
|
+
title('5. 关联展开(with)');
|
|
167
|
+
role.store.calls.list = 0;
|
|
168
|
+
const full = role.get(hero.id, { with: ['map_info', 'items'] });
|
|
169
|
+
console.log('后端查询次数(本表 + 2 个关联):', role.store.calls.list);
|
|
170
|
+
console.log('角色:', full.name, 'Lv.' + full.level, '/ 所在:', full.map_info.name);
|
|
171
|
+
console.log('背包:', full.items.map(function (b) { return b.item_name + '×' + b.quantity; }).join('、'));
|
|
172
|
+
|
|
173
|
+
// ---- 6. 传送 / 打怪 / 换版 ----
|
|
174
|
+
title('6. 传送打怪结算 / 引用完整性 / 配置换版');
|
|
175
|
+
role.set(hero.id, { title: '骑士', map_id: 1002, x: 20, y: 800 });
|
|
176
|
+
role.patch(hero.id, { $inc: { gold: 250, exp: 120 } });
|
|
177
|
+
const moved = role.get(hero.id, { with: ['map_info'] });
|
|
178
|
+
console.log('称号=' + moved.title + ',到达=' + moved.map_info.name +
|
|
179
|
+
',金币=' + moved.gold + ',经验=' + moved.exp);
|
|
180
|
+
console.log('谁持有铁剑(1001):', item.reverse(1001).count, '条背包记录');
|
|
181
|
+
console.log('全注册表完整性:', JSON.stringify(mm.dataAdmin.registry().verify()));
|
|
182
|
+
item.reload([
|
|
183
|
+
{ item_id: 1001, name: '精铁剑', atk: 18, price: 200 },
|
|
184
|
+
{ item_id: 3001, name: '小红药水', price: 20 },
|
|
185
|
+
{ item_id: 4001, name: '皮甲', price: 80 }
|
|
186
|
+
], { version: 13 });
|
|
187
|
+
console.log('item 换版到 v' + item.version + ',新名=' + item.get(1001).name +
|
|
188
|
+
';旧背包快照仍为 ' + bag.get(1).item_name);
|
|
189
|
+
|
|
190
|
+
// ---- 7. 任意名字 / 独立实例池 ----
|
|
191
|
+
title('7. 任意集合名 / mm_admin 多池隔离');
|
|
192
|
+
const pet = mm.dataAdmin('宠物');
|
|
193
|
+
pet.add({ name: '小火龙', owner_id: hero.id, element: 'fire' });
|
|
194
|
+
mm.dataAdmin('battle_log').generate(5, {
|
|
195
|
+
role_id: '{{ ref:role.role_id }}', turns: '{{ random(3,12) }}', seq: '{{ $index }}'
|
|
196
|
+
});
|
|
197
|
+
const w2 = mm.createPool('game2');
|
|
198
|
+
w2.data('role').add({ name: '另一个世界的角色' });
|
|
199
|
+
console.log('集合清单:', mm.dataAdmin.registry().models().map(function (m) { return m.name; }).join(' '));
|
|
200
|
+
console.log('默认池 role 数=' + role.size + ',game2 池 role 数=' + w2.data('role').size);
|
|
201
|
+
console.log('宠物:', JSON.stringify(pet.all()), '/ 战斗日志:', mm.dataAdmin('battle_log').size, '条');
|
|
202
|
+
|
|
203
|
+
// ---- 8. 持久化 ----
|
|
204
|
+
title('8. 独立池持久化');
|
|
205
|
+
const dir = path.join(os.tmpdir(), 'mm_data_v4_demo');
|
|
206
|
+
const disk = mm.createPool('disk', { store: 'json', dir: dir });
|
|
207
|
+
disk.data('role').add({ name: '存档角色' });
|
|
208
|
+
disk.data('role').save();
|
|
209
|
+
const restored = mm.createPool('disk_restore', { store: 'json', dir: dir });
|
|
210
|
+
mm.dataAdmin.registry(restored).load();
|
|
211
|
+
console.log('落盘:', dir);
|
|
212
|
+
console.log('恢复:', restored.data('role').get(1).name);
|
|
213
|
+
|
|
214
|
+
// ---- 9. 工具面 ----
|
|
215
|
+
title('9. AI / 工作流工具面');
|
|
216
|
+
const out = await tool.main({
|
|
217
|
+
action: 'get', model: 'role', query: hero.id, with: ['map_info'], admin: mm.dataAdmin()
|
|
218
|
+
}, {});
|
|
219
|
+
console.log('tool.data:', out.obj.name, '/', out.obj.map_info.name);
|
|
220
|
+
const v = await tool.main({
|
|
221
|
+
action: 'validate', model: 'role', body: { name: 'x', level: 999 }, admin: mm.dataAdmin()
|
|
222
|
+
}, {});
|
|
223
|
+
console.log('tool.validate:', JSON.stringify(v.obj));
|
|
224
|
+
const fnTool = mm.getFunctionTool('data');
|
|
225
|
+
console.log('LLM 工具定义:', fnTool.type + '/' + fnTool.function.name,
|
|
226
|
+
'| parameters 字段:', Object.keys(fnTool.function.parameters.properties).join(','));
|
|
227
|
+
console.log('type 通道:', mm.getToolConfig('data').type, '(工作流 data,LLM 视图为 function)');
|
|
228
|
+
|
|
229
|
+
title('完成');
|
|
230
|
+
console.log('可选 RPG 示例模型:createPool("preset", { models: mm.EXAMPLE_MODEL_DIR }) 显式加载。');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
run().catch(function (e) {
|
|
234
|
+
console.error('演示失败: ' + (e.code || 'ERROR') + ' - ' + e.message);
|
|
235
|
+
console.error(e.stack);
|
|
236
|
+
process.exitCode = 1;
|
|
237
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "item",
|
|
3
|
+
"kind": "static",
|
|
4
|
+
"version": 1,
|
|
5
|
+
"description": "道具表(静态配置)——落盘为 data/item/000001.json",
|
|
6
|
+
"key": "item_id",
|
|
7
|
+
"store": {
|
|
8
|
+
"db": "file",
|
|
9
|
+
"table": "item"
|
|
10
|
+
},
|
|
11
|
+
"schema": {
|
|
12
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
13
|
+
"type": "object",
|
|
14
|
+
"properties": {
|
|
15
|
+
"item_id": {
|
|
16
|
+
"type": "number",
|
|
17
|
+
"primary": true
|
|
18
|
+
},
|
|
19
|
+
"name": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"minLength": 1
|
|
22
|
+
},
|
|
23
|
+
"price": {
|
|
24
|
+
"type": "number",
|
|
25
|
+
"default": 0,
|
|
26
|
+
"minimum": 0
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"required": [
|
|
30
|
+
"name"
|
|
31
|
+
],
|
|
32
|
+
"additionalProperties": false
|
|
33
|
+
},
|
|
34
|
+
"seed": [
|
|
35
|
+
{
|
|
36
|
+
"item_id": 1,
|
|
37
|
+
"name": "小药水",
|
|
38
|
+
"price": 20
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"item_id": 2,
|
|
42
|
+
"name": "铁剑",
|
|
43
|
+
"price": 150
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "map",
|
|
3
|
+
"kind": "static",
|
|
4
|
+
"version": 1,
|
|
5
|
+
"description": "地图表(静态配置)——落盘为 data/map/000001.json",
|
|
6
|
+
"key": "map_id",
|
|
7
|
+
"store": {
|
|
8
|
+
"db": "file",
|
|
9
|
+
"table": "map"
|
|
10
|
+
},
|
|
11
|
+
"schema": {
|
|
12
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
13
|
+
"type": "object",
|
|
14
|
+
"properties": {
|
|
15
|
+
"map_id": {
|
|
16
|
+
"type": "number",
|
|
17
|
+
"primary": true
|
|
18
|
+
},
|
|
19
|
+
"name": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"minLength": 1
|
|
22
|
+
},
|
|
23
|
+
"width": {
|
|
24
|
+
"type": "number",
|
|
25
|
+
"default": 100,
|
|
26
|
+
"minimum": 1
|
|
27
|
+
},
|
|
28
|
+
"height": {
|
|
29
|
+
"type": "number",
|
|
30
|
+
"default": 100,
|
|
31
|
+
"minimum": 1
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"required": [
|
|
35
|
+
"name"
|
|
36
|
+
],
|
|
37
|
+
"additionalProperties": false
|
|
38
|
+
},
|
|
39
|
+
"seed": [
|
|
40
|
+
{
|
|
41
|
+
"map_id": 1,
|
|
42
|
+
"name": "晨曦之城",
|
|
43
|
+
"width": 120,
|
|
44
|
+
"height": 120
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"map_id": 2,
|
|
48
|
+
"name": "幽暗森林",
|
|
49
|
+
"width": 200,
|
|
50
|
+
"height": 200
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "player",
|
|
3
|
+
"kind": "dynamic",
|
|
4
|
+
"version": 1,
|
|
5
|
+
"description": "玩家角色(动态数据)——mm_cache 缓存 + mm_mongodb 持久,不落 000001.json",
|
|
6
|
+
"key": "id",
|
|
7
|
+
"store": {
|
|
8
|
+
"db": "mongodb",
|
|
9
|
+
"table": "player",
|
|
10
|
+
"cache": true
|
|
11
|
+
},
|
|
12
|
+
"schema": {
|
|
13
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
14
|
+
"type": "object",
|
|
15
|
+
"properties": {
|
|
16
|
+
"id": {
|
|
17
|
+
"type": "number",
|
|
18
|
+
"primary": true,
|
|
19
|
+
"auto": true
|
|
20
|
+
},
|
|
21
|
+
"name": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"minLength": 1,
|
|
24
|
+
"maxLength": 12
|
|
25
|
+
},
|
|
26
|
+
"role_id": {
|
|
27
|
+
"type": "number",
|
|
28
|
+
"ref": {
|
|
29
|
+
"model": "role",
|
|
30
|
+
"as": "role",
|
|
31
|
+
"mode": "live"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"map_id": {
|
|
35
|
+
"type": "number",
|
|
36
|
+
"ref": {
|
|
37
|
+
"model": "map",
|
|
38
|
+
"as": "map",
|
|
39
|
+
"mode": "live"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"level": {
|
|
43
|
+
"type": "number",
|
|
44
|
+
"default": 1,
|
|
45
|
+
"minimum": 1,
|
|
46
|
+
"maximum": 99
|
|
47
|
+
},
|
|
48
|
+
"hp": {
|
|
49
|
+
"type": "number",
|
|
50
|
+
"default": 100,
|
|
51
|
+
"minimum": 0
|
|
52
|
+
},
|
|
53
|
+
"online": {
|
|
54
|
+
"type": "boolean",
|
|
55
|
+
"default": false
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"required": [
|
|
59
|
+
"name",
|
|
60
|
+
"role_id",
|
|
61
|
+
"map_id"
|
|
62
|
+
],
|
|
63
|
+
"additionalProperties": false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "role",
|
|
3
|
+
"kind": "static",
|
|
4
|
+
"version": 1,
|
|
5
|
+
"description": "角色模板(静态配置)——落盘为 data/role/000001.json",
|
|
6
|
+
"key": "role_id",
|
|
7
|
+
"store": {
|
|
8
|
+
"db": "file",
|
|
9
|
+
"table": "role"
|
|
10
|
+
},
|
|
11
|
+
"schema": {
|
|
12
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
13
|
+
"type": "object",
|
|
14
|
+
"properties": {
|
|
15
|
+
"role_id": {
|
|
16
|
+
"type": "number",
|
|
17
|
+
"primary": true,
|
|
18
|
+
"description": "角色模板ID"
|
|
19
|
+
},
|
|
20
|
+
"name": {
|
|
21
|
+
"type": "string",
|
|
22
|
+
"minLength": 1,
|
|
23
|
+
"maxLength": 12
|
|
24
|
+
},
|
|
25
|
+
"base_hp": {
|
|
26
|
+
"type": "number",
|
|
27
|
+
"default": 100,
|
|
28
|
+
"minimum": 1
|
|
29
|
+
},
|
|
30
|
+
"base_mp": {
|
|
31
|
+
"type": "number",
|
|
32
|
+
"default": 30,
|
|
33
|
+
"minimum": 0
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"required": [
|
|
37
|
+
"name"
|
|
38
|
+
],
|
|
39
|
+
"additionalProperties": false
|
|
40
|
+
},
|
|
41
|
+
"seed": [
|
|
42
|
+
{
|
|
43
|
+
"role_id": 1,
|
|
44
|
+
"name": "战士",
|
|
45
|
+
"base_hp": 180,
|
|
46
|
+
"base_mp": 20
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"role_id": 2,
|
|
50
|
+
"name": "法师",
|
|
51
|
+
"base_hp": 100,
|
|
52
|
+
"base_mp": 120
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"role_id": 3,
|
|
56
|
+
"name": "牧师",
|
|
57
|
+
"base_hp": 120,
|
|
58
|
+
"base_mp": 100
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "skill",
|
|
3
|
+
"kind": "static",
|
|
4
|
+
"version": 1,
|
|
5
|
+
"description": "技能表(静态配置)——落盘为 data/skill/000001.json",
|
|
6
|
+
"key": "skill_id",
|
|
7
|
+
"store": {
|
|
8
|
+
"db": "file",
|
|
9
|
+
"table": "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
|
+
"minLength": 1,
|
|
22
|
+
"maxLength": 16
|
|
23
|
+
},
|
|
24
|
+
"mp_cost": {
|
|
25
|
+
"type": "number",
|
|
26
|
+
"default": 0,
|
|
27
|
+
"minimum": 0
|
|
28
|
+
},
|
|
29
|
+
"power": {
|
|
30
|
+
"type": "number",
|
|
31
|
+
"default": 10,
|
|
32
|
+
"minimum": 0
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"required": [
|
|
36
|
+
"name"
|
|
37
|
+
],
|
|
38
|
+
"additionalProperties": false
|
|
39
|
+
},
|
|
40
|
+
"seed": [
|
|
41
|
+
{
|
|
42
|
+
"skill_id": 1,
|
|
43
|
+
"name": "重斩",
|
|
44
|
+
"mp_cost": 0,
|
|
45
|
+
"power": 25
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"skill_id": 2,
|
|
49
|
+
"name": "火球术",
|
|
50
|
+
"mp_cost": 12,
|
|
51
|
+
"power": 40
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"skill_id": 3,
|
|
55
|
+
"name": "治愈术",
|
|
56
|
+
"mp_cost": 8,
|
|
57
|
+
"power": 18
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 可选演示:显式加载示例 RPG 模型集
|
|
4
|
+
*
|
|
5
|
+
* 这 15 个 JSON 只是「使用者提供的一份集合定义」的样例,
|
|
6
|
+
* mm_datas 默认不会加载它们;换成自己的目录、自己的表即可。
|
|
7
|
+
* 每个模型的 schema 都是标准 JSON Schema(draft-07 子集),类型只用 number,不含 int/integer。
|
|
8
|
+
* 运行:node example/preset_demo.js
|
|
9
|
+
*/
|
|
10
|
+
const mm = require('../index.js');
|
|
11
|
+
|
|
12
|
+
// 独立命名空间,不污染默认池 reg('mm_datas')
|
|
13
|
+
const pool = mm.createPool('rpg_preset', { models: mm.EXAMPLE_MODEL_DIR });
|
|
14
|
+
const reg = mm.dataAdmin.registry(pool);
|
|
15
|
+
|
|
16
|
+
console.log('显式加载的示例集合:', reg.models().length, '个');
|
|
17
|
+
console.log(reg.models().map(function (m) { return m.name + '(' + m.kind + ')'; }).join(' '));
|
|
18
|
+
console.log('character.schema() 是标准文档:', pool.data('character').schema().$schema,
|
|
19
|
+
'/ type:', pool.data('character').schema().type);
|
|
20
|
+
|
|
21
|
+
const map = pool.data('map');
|
|
22
|
+
const town = map.get(1001);
|
|
23
|
+
console.log('\n地图 1001:', town.name, '传送点', town.portals.length, '个');
|
|
24
|
+
|
|
25
|
+
const role = pool.data('character');
|
|
26
|
+
const hero = role.add({
|
|
27
|
+
player_id: 1, name: '亚瑟', job_id: 1, level: 10,
|
|
28
|
+
hp: 280, mp: 40, gold: 0, map_id: 1001, x: 300, y: 400, online: true
|
|
29
|
+
});
|
|
30
|
+
const bag = pool.data('bag');
|
|
31
|
+
bag.addMany([
|
|
32
|
+
{ character_id: hero.id, item_id: 1001, quantity: 1, equipped: true },
|
|
33
|
+
{ character_id: hero.id, item_id: 3001, quantity: 20 }
|
|
34
|
+
]);
|
|
35
|
+
pool.data('character_skill').add({ character_id: hero.id, skill_id: 101, level: 3 });
|
|
36
|
+
|
|
37
|
+
const full = role.get(hero.id, { with: ['job', 'map', 'items', 'skills'] });
|
|
38
|
+
console.log('\n角色档案:', full.name, '/', full.job.name, '/ Lv.' + full.level, '/', full.map.name);
|
|
39
|
+
console.log('背包:', full.items.map(function (b) { return b.item_name + '×' + b.quantity; }).join('、'));
|
|
40
|
+
console.log('完整性:', JSON.stringify(reg.verify()));
|
|
41
|
+
|
|
42
|
+
// 同一个 API 换成任意集合:反查谁引用了某个静态道具
|
|
43
|
+
console.log('\n引用 item 3001 的背包记录:', pool.data('item').reverse(3001).count);
|
|
44
|
+
console.log('默认池不含示例集合:', reg.has('map'), '/', mm.dataAdmin.registry().has('map'));
|