mm_os 4.3.6 → 5.0.0

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.
Files changed (234) hide show
  1. package/README.md +101 -504
  2. package/index.js +26 -19
  3. package/lib/loader.js +787 -0
  4. package/lib/os.js +369 -0
  5. package/package.json +55 -80
  6. package/LICENSE +0 -21
  7. package/README_EN.md +0 -505
  8. package/adapter/adapter.js +0 -484
  9. package/adapter/custom_persistence.js +0 -661
  10. package/adapter/mqtt.js +0 -254
  11. package/adapter/socket.js +0 -244
  12. package/adapter/web.js +0 -92
  13. package/adapter/websocket.js +0 -43
  14. package/ai.js +0 -130
  15. package/com/api/README.md +0 -3
  16. package/com/api/com.json +0 -5
  17. package/com/api/config.tpl.json +0 -41
  18. package/com/api/drive.js +0 -732
  19. package/com/api/index.js +0 -201
  20. package/com/api/oauth.js +0 -200
  21. package/com/api/script.tpl.js +0 -32
  22. package/com/cmd/README.md +0 -11
  23. package/com/cmd/com.json +0 -5
  24. package/com/cmd/config.tpl.json +0 -48
  25. package/com/cmd/drive.js +0 -176
  26. package/com/cmd/index.js +0 -287
  27. package/com/cmd/job/config.json +0 -66
  28. package/com/cmd/job/index.js +0 -241
  29. package/com/cmd/job/test.js +0 -188
  30. package/com/cmd/msg.json +0 -48
  31. package/com/cmd/script.tpl.js +0 -12
  32. package/com/db/README.md +0 -3
  33. package/com/db/com.json +0 -5
  34. package/com/db/drive.js +0 -2206
  35. package/com/db/index.js +0 -241
  36. package/com/event/README.md +0 -4
  37. package/com/event/com.json +0 -5
  38. package/com/event/config.tpl.json +0 -20
  39. package/com/event/drive.js +0 -61
  40. package/com/event/index.js +0 -438
  41. package/com/event/script.tpl.js +0 -23
  42. package/com/mqtt/README.md +0 -47
  43. package/com/mqtt/com.json +0 -5
  44. package/com/mqtt/config.tpl.json +0 -42
  45. package/com/mqtt/drive.js +0 -696
  46. package/com/mqtt/index.js +0 -1017
  47. package/com/mqtt/index.test.js +0 -503
  48. package/com/mqtt/mm_mqtt.js +0 -446
  49. package/com/mqtt/script.tpl.js +0 -719
  50. package/com/nav/README.md +0 -2
  51. package/com/nav/com.json +0 -5
  52. package/com/nav/config.tpl.json +0 -84
  53. package/com/nav/drive.js +0 -702
  54. package/com/nav/index.js +0 -228
  55. package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
  56. package/com/nav/tpl/admin_pc/page_config.vue +0 -280
  57. package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
  58. package/com/nav/tpl/admin_pc/page_default.vue +0 -247
  59. package/com/nav/tpl/admin_pc/page_form.vue +0 -180
  60. package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
  61. package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
  62. package/com/nav/tpl/admin_pc/page_table.vue +0 -296
  63. package/com/nav/tpl/admin_pc/page_type.vue +0 -321
  64. package/com/nav/tpl/admin_pc/page_view.vue +0 -124
  65. package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
  66. package/com/nav/tpl/dev_pc/page_config.vue +0 -253
  67. package/com/nav/tpl/dev_pc/page_default.vue +0 -247
  68. package/com/nav/tpl/dev_pc/page_form.vue +0 -162
  69. package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
  70. package/com/nav/tpl/dev_pc/page_table.vue +0 -247
  71. package/com/nav/tpl/dev_pc/page_type.vue +0 -313
  72. package/com/nav/tpl/home_pc/page_channel.vue +0 -234
  73. package/com/nav/tpl/home_pc/page_default.vue +0 -234
  74. package/com/nav/tpl/home_pc/page_form.vue +0 -137
  75. package/com/nav/tpl/home_pc/page_list.vue +0 -234
  76. package/com/nav/tpl/home_pc/page_nav.vue +0 -221
  77. package/com/nav/tpl/home_pc/page_type.vue +0 -234
  78. package/com/nav/tpl/home_pc/page_view.vue +0 -125
  79. package/com/nav/tpl/home_phone/page_channel.vue +0 -234
  80. package/com/nav/tpl/home_phone/page_default.vue +0 -234
  81. package/com/nav/tpl/home_phone/page_form.vue +0 -137
  82. package/com/nav/tpl/home_phone/page_list.vue +0 -234
  83. package/com/nav/tpl/home_phone/page_nav.vue +0 -237
  84. package/com/nav/tpl/home_phone/page_type.vue +0 -234
  85. package/com/nav/tpl/home_phone/page_view.vue +0 -125
  86. package/com/nav/viewmodel.js +0 -446
  87. package/com/param/README.md +0 -2
  88. package/com/param/com.json +0 -5
  89. package/com/param/config.tpl.json +0 -136
  90. package/com/param/drive.js +0 -502
  91. package/com/param/index.js +0 -158
  92. package/com/param/script.tpl.js +0 -12
  93. package/com/pendant/README.md +0 -3
  94. package/com/pendant/com.json +0 -5
  95. package/com/pendant/config.tpl.json +0 -32
  96. package/com/pendant/drive.js +0 -204
  97. package/com/pendant/index.js +0 -467
  98. package/com/pendant/pendant.html +0 -16
  99. package/com/pendant/script.tpl.js +0 -18
  100. package/com/socket/README.md +0 -47
  101. package/com/socket/com.json +0 -5
  102. package/com/socket/config.tpl.json +0 -12
  103. package/com/socket/drive.js +0 -679
  104. package/com/socket/index.js +0 -402
  105. package/com/socket/script.tpl.js +0 -41
  106. package/com/sql/README.md +0 -3
  107. package/com/sql/com.json +0 -5
  108. package/com/sql/config.tpl.json +0 -74
  109. package/com/sql/drive.js +0 -1283
  110. package/com/sql/index.js +0 -152
  111. package/com/sql/script.tpl.js +0 -47
  112. package/com/static/README.md +0 -2
  113. package/com/static/com.json +0 -5
  114. package/com/static/config.tpl.json +0 -24
  115. package/com/static/drive.js +0 -194
  116. package/com/static/index.js +0 -237
  117. package/com/static/script.tpl.js +0 -28
  118. package/com/task/README.md +0 -3
  119. package/com/task/com.json +0 -5
  120. package/com/task/config.tpl.json +0 -24
  121. package/com/task/drive.js +0 -428
  122. package/com/task/index.js +0 -159
  123. package/com/task/script.tpl.js +0 -40
  124. package/com/template/com.json +0 -5
  125. package/com/template/config.tpl.json +0 -16
  126. package/com/template/drive.js +0 -81
  127. package/com/template/index.js +0 -141
  128. package/com.js +0 -237
  129. package/common/README.md +0 -2
  130. package/common/middleware/web_after/index.js +0 -26
  131. package/common/middleware/web_after/middleware.json +0 -9
  132. package/common/middleware/web_base/index.js +0 -113
  133. package/common/middleware/web_base/middleware.json +0 -19
  134. package/common/middleware/web_before/index.js +0 -33
  135. package/common/middleware/web_before/middleware.json +0 -9
  136. package/common/middleware/web_cors/index.js +0 -87
  137. package/common/middleware/web_cors/middleware.json +0 -24
  138. package/common/middleware/web_error/index.js +0 -131
  139. package/common/middleware/web_error/middleware.json +0 -19
  140. package/common/middleware/web_ip/index.js +0 -15
  141. package/common/middleware/web_ip/middleware.json +0 -14
  142. package/common/middleware/web_logger/index.js +0 -156
  143. package/common/middleware/web_logger/middleware.json +0 -14
  144. package/common/middleware/web_main/index.js +0 -26
  145. package/common/middleware/web_main/middleware.json +0 -9
  146. package/common/middleware/web_static/index.js +0 -73
  147. package/common/middleware/web_static/middleware.json +0 -54
  148. package/common/middleware/web_waf/index.js +0 -385
  149. package/common/middleware/web_waf/middleware.json +0 -13
  150. package/core/app/config.tpl.json +0 -67
  151. package/core/app/index.js +0 -710
  152. package/core/app/script.tpl.js +0 -52
  153. package/core/channel/index.js +0 -899
  154. package/core/channel/matcher.js +0 -585
  155. package/core/com/config.tpl.json +0 -16
  156. package/core/com/index.js +0 -77
  157. package/core/com/script.tpl.js +0 -5
  158. package/core/component/component.js +0 -42
  159. package/core/component/config.tpl.json +0 -63
  160. package/core/component/index.js +0 -273
  161. package/core/component/script.tpl.js +0 -19
  162. package/core/controller/config.tpl.json +0 -14
  163. package/core/controller/index.js +0 -371
  164. package/core/controller/script.tpl.js +0 -27
  165. package/core/factory/config.tpl.json +0 -14
  166. package/core/factory/entity.js +0 -275
  167. package/core/factory/index.js +0 -241
  168. package/core/factory/script.tpl.js +0 -16
  169. package/core/game/bat/index.js +0 -137
  170. package/core/game/bat/world.js +0 -622
  171. package/core/game/config.tpl.json +0 -16
  172. package/core/game/entity_admin.js +0 -230
  173. package/core/game/index.js +0 -186
  174. package/core/handler/config.tpl.json +0 -22
  175. package/core/handler/index.js +0 -181
  176. package/core/handler/script.tpl.js +0 -23
  177. package/core/logic/config.tpl.json +0 -14
  178. package/core/logic/index.js +0 -59
  179. package/core/logic/script.tpl.js +0 -19
  180. package/core/middleware/config.tpl.json +0 -16
  181. package/core/middleware/index.js +0 -117
  182. package/core/middleware/script.tpl.js +0 -37
  183. package/core/mod/config.tpl.json +0 -22
  184. package/core/mod/index.js +0 -136
  185. package/core/mod/script.tpl.js +0 -34
  186. package/core/model/config.tpl.json +0 -219
  187. package/core/model/index.js +0 -272
  188. package/core/model/model.js +0 -27
  189. package/core/model/script.tpl.js +0 -20
  190. package/core/notifier/config.tpl.json +0 -14
  191. package/core/notifier/index.js +0 -77
  192. package/core/notifier/script.tpl.js +0 -20
  193. package/core/plugin/config.tpl.json +0 -36
  194. package/core/plugin/index.js +0 -297
  195. package/core/plugin/script.tpl.js +0 -51
  196. package/core/pusher/config.tpl.json +0 -14
  197. package/core/pusher/index.js +0 -158
  198. package/core/pusher/script.tpl.js +0 -20
  199. package/core/room/bat/index.js +0 -170
  200. package/core/room/bat/room.js +0 -524
  201. package/core/room/config.tpl.json +0 -20
  202. package/core/room/index.js +0 -249
  203. package/core/room/room.js +0 -61
  204. package/core/scene/config.tpl.json +0 -14
  205. package/core/scene/index.js +0 -467
  206. package/core/scene/loop.js +0 -1306
  207. package/core/scene/map.js +0 -28
  208. package/core/scene/script.tpl.js +0 -22
  209. package/core/sender/config.tpl.json +0 -14
  210. package/core/sender/index.js +0 -79
  211. package/core/sender/script.tpl.js +0 -20
  212. package/core/service/config.tpl.json +0 -14
  213. package/core/service/index.js +0 -100
  214. package/core/service/script.tpl.js +0 -25
  215. package/core/store/config.tpl.json +0 -26
  216. package/core/store/index.js +0 -1794
  217. package/core/store/script.tpl.js +0 -22
  218. package/core/store/sql.js +0 -1488
  219. package/core/system/config.tpl.json +0 -18
  220. package/core/system/index.js +0 -312
  221. package/core/system/script.tpl.js +0 -77
  222. package/core/view/config.tpl.json +0 -14
  223. package/core/view/index.js +0 -91
  224. package/core/view/script.tpl.js +0 -20
  225. package/core/zone/bat/index.js +0 -744
  226. package/core/zone/config.tpl.json +0 -54
  227. package/core/zone/index.js +0 -655
  228. package/core/zone/script.tpl.js +0 -10
  229. package/core/zone/zone_bat.js +0 -136
  230. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
  231. package/os.js +0 -53
  232. package/server.js +0 -595
  233. package/ulits/id_gen.js +0 -134
  234. package/ulits/word.js +0 -206
package/server.js DELETED
@@ -1,595 +0,0 @@
1
- require('mm_expand');
2
- const { Log } = require('mm_logs');
3
- const { cacheAdmin } = require('mm_cache');
4
- const { sqlAdmin } = require('mm_sql');
5
- const { Config } = require('mm_config');
6
- const { Drive, Manager } = require('mm_machine');
7
-
8
- const { Web } = require('./adapter/web.js');
9
- const { WebSocket } = require('./adapter/websocket.js');
10
- const { Socket } = require('./adapter/socket.js');
11
- const { Mqtt } = require('./adapter/mqtt.js');
12
-
13
- const { Middleware } = require('./core/middleware/index.js');
14
- const { Game } = require('./core/game/index.js');
15
- const { App } = require('./core/app/index.js');
16
- const { Mod } = require('./core/mod/index.js');
17
-
18
- const { Com } = require('./com.js');
19
-
20
- /**
21
- * 服务器类
22
- * 负责管理整个应用的服务端架构
23
- */
24
- class Server extends Drive {
25
- static config = {
26
- name: 'os',
27
- title: 'mm服务端系统',
28
- log: {
29
- level: 'debug',
30
- file: false,
31
- },
32
- sys: {
33
- // 服务端名称
34
- name: 'mm',
35
- // 服务端中文名
36
- title: '超级美眉',
37
- // 系统使用的语言
38
- lang: 'zh_CN',
39
- // 定时任务
40
- task: false,
41
- // 是否启用AI功能
42
- ai: false,
43
- },
44
- mqtt: {
45
- hostname: '127.0.0.1',
46
- port: 1883,
47
- state: true,
48
- auth_enabled: true,
49
- auth_users: [
50
- // {
51
- // clientid: 'server',
52
- // username: 'admin',
53
- // password: 'asd123'
54
- // }
55
- ],
56
- },
57
- mqtt_client: {
58
- state: true,
59
- hostname: '127.0.0.1',
60
- port: 1883,
61
- protocol: 'mqtt',
62
- client_id: 'server',
63
- username: 'admin',
64
- password: 'asd123',
65
- clean: false,
66
- },
67
- socket: {
68
- host: '0.0.0.0',
69
- port: 8200,
70
- state: false,
71
- },
72
- sql: {
73
- // mysql、sqlite
74
- way: 'mysql',
75
- },
76
- sqlite: {
77
- dir: './db/'.fullname(),
78
- user: 'root',
79
- password: '',
80
- database: 'mm',
81
- charset: 'utf8mb4',
82
- connect_timeout: 5000,
83
- connect_limit: 500,
84
- },
85
- mysql: {
86
- host: '127.0.0.1',
87
- port: 3306,
88
- user: 'root',
89
- password: 'Asd159357',
90
- database: '',
91
- charset: 'utf8mb4',
92
- timezone: '+08:00',
93
- connect_timeout: 5000,
94
- connect_limit: 500,
95
- },
96
- cache: {
97
- // 缓存方式memory、redis、mongodb
98
- way: 'redis',
99
- },
100
- // 缓存基础配置
101
- cachebase: {
102
- dir: './cache/'.fullname(),
103
- expire: 0,
104
- max: 0,
105
- persist: false, // 是否启用持久化
106
- save_interval: 60000, // 保存间隔(毫秒)
107
- },
108
- // 缓存配置
109
- redis: {
110
- // 服务器地址
111
- host: 'localhost',
112
- // 端口号
113
- port: 6379,
114
- // 密码
115
- password: 'asd159357',
116
- // 数据库
117
- database: 0,
118
- // 前缀
119
- prefix: 'mm_',
120
- // 连接超时时间(ms)
121
- connect_timeout: 5000,
122
- // 重试次数
123
- retry_count: 3,
124
- // 重试间隔(ms)
125
- retry_delay: 1000,
126
- },
127
- // 数据库配置
128
- mongodb: {
129
- // 服务器地址
130
- host: 'localhost',
131
- // 端口号
132
- port: 27017,
133
- // 数据库名
134
- database: 'mm',
135
- // 用户名
136
- user: '',
137
- // 密码
138
- password: '',
139
- // 连接选项 (MongoDB驱动7.x版本支持的参数)
140
- min_pool_size: 10,
141
- max_pool_size: 100,
142
- connect_timeout_ms: 10000,
143
- server_selection_timeout_ms: 5000,
144
- retry_writes: true,
145
- },
146
- web: {
147
- state: 1,
148
- // 访问地址
149
- host: '127.0.0.1',
150
- // 访问端口号
151
- port: 8000,
152
- // 是否启用websocket
153
- socket: false,
154
- // 是否启用压缩
155
- compress: false,
156
- // 是否启用事件
157
- event: true,
158
- // 是否启用事件
159
- log: true,
160
- // 跨域
161
- cos: {
162
- status: true,
163
- origin: '*',
164
- // "headers": "Content-Type,Content-Length,Authorization,Accept,X-Requested-With,x-auth-token,token,clientid,appid,apikey,user_id,x-forwarded-for,x-real-ip,User-Agent,cache-control,pragma,accept-encoding,connection,host"
165
- },
166
- // 代理转发
167
- proxy: {},
168
- // static 配置
169
- static: {
170
- // 是否开启静态文件处理
171
- state: 1,
172
- // 前端缓存时间,单位秒
173
- max_age: 7200,
174
- // 是否开启缓存
175
- cache: true,
176
- // 后端缓存时间,单位秒
177
- cache_age: 0,
178
- // 是否开启immutable缓存
179
- immutable: true,
180
- // 是否开启隐藏文件处理
181
- hidden: false,
182
- // 是否格式化文件
183
- format: true,
184
- // 是否指定文件扩展名
185
- extensions: false,
186
- // 是否开启brotli压缩
187
- brotli: false,
188
- // 是否开启gzip压缩
189
- gzip: false,
190
- // 静态文件根目录
191
- root: './static',
192
- // 编译vue文件,启动后会将vue转为js,可让前端通过 import xxx from './xxx.vue' 引入
193
- compile_vue: true,
194
- // 编译markdown文件,启动后会将markdown转为html
195
- compile_md: true,
196
- // 是否将files中的文件的js转换为amd格式
197
- convert_amd: false,
198
- // 指定路径文件需要转换
199
- src_path: '/src',
200
- // 需要转换的文件扩展名
201
- files: ['.js', '.vue', '.html'],
202
- // 是否启用文件修改监听(实时检测文件修改)
203
- watch: true,
204
- },
205
- },
206
- };
207
-
208
- /**
209
- * 构造函数
210
- * @param {object} config 配置参数
211
- * @param parent
212
- */
213
- constructor(config, parent) {
214
- super(config, parent);
215
- }
216
- }
217
-
218
- /**
219
- * 预置
220
- */
221
- Server.prototype._preset = function () {
222
- // Server不加载脚本文件,清除从配置继承的main与script_file
223
- this.config.main = '';
224
- this.script_file = '';
225
-
226
- /** == 管理层 == */
227
- // 管理器
228
- this.manager = {};
229
- // 通知器
230
- this.notifier = {};
231
- // 消息发送器
232
- this.sender = {};
233
- // 广播器
234
- this.pusher = {};
235
-
236
- /** == 应用层 == */
237
- // 适配器
238
- this.adapter = {};
239
- // 中间件
240
- this.middleware = {};
241
- // 应用
242
- this.app = {};
243
- // 模组,用于功能/玩法扩展
244
- this.mod = {};
245
- // 游戏
246
- this.game = {};
247
-
248
- /** == 基础设施 == */
249
- // 日志
250
- this.setLogger($.log || console);
251
- // 数据库
252
- this.sql = null;
253
- // 缓存
254
- this.cache = null;
255
- // 文件管理
256
- this.filer = null;
257
- // 初始化工具
258
- this._createTool();
259
- // 初始化基础设施
260
- this._createBase();
261
- // 创建适配器
262
- this._createAdapter();
263
- // 使用统一处理错误
264
- this._useError();
265
- };
266
-
267
- /**
268
- * 获取管理器
269
- * @returns {object} 管理器实例
270
- */
271
- Server.prototype.getManager = async function () {
272
- return this.manager;
273
- };
274
-
275
- /**
276
- * 加载核心模块
277
- */
278
- Server.prototype._loadCore = async function () {
279
- await this._initManager();
280
- let arr = [this._loadSources()];
281
- if (this.config.sys.ai) {
282
- var { initAI } = require('./ai.js'.fullname(__dirname));
283
- arr.push(initAI(this));
284
- }
285
- await Promise.all(arr);
286
- };
287
-
288
- /**
289
- * 创建管理器实例
290
- * @param {string} name 管理器名称
291
- * @param {string} title 管理器标题
292
- * @param {Function} cls 管理器类
293
- * @returns {object} 管理器实例
294
- */
295
- Server.prototype._createManager = function (name, title, cls) {
296
- var manager = new Manager(
297
- {
298
- name: name,
299
- title: title,
300
- filename: name + '.json',
301
- tpl_dir: `./core/${name}/`.fullname(__dirname),
302
- base_dir: '',
303
- dir: `./${name}`.fullname(),
304
- },
305
- this,
306
- this[name],
307
- cls,
308
- );
309
- // 设置到对应的管理器属性
310
- this.manager[name] = manager;
311
- return manager;
312
- };
313
-
314
- Server.prototype._initMiddlewareManager = async function () {
315
- var manager = new Manager(
316
- {
317
- name: 'middleware',
318
- title: '中间件',
319
- filename: 'middleware.json',
320
- tpl_dir: `./core/middleware/`.fullname(__dirname),
321
- base_dir: './common/middleware'.fullname(__dirname),
322
- dir: `/middleware`.fullname(),
323
- },
324
- this,
325
- this.middleware,
326
- Middleware,
327
- );
328
- this.manager.middleware = manager;
329
- return manager;
330
- };
331
-
332
- /**
333
- * 初始化管理器
334
- */
335
- Server.prototype._initManager = async function () {
336
- console.time(' [加载] _initManager');
337
- // 管理器配置列表
338
- var infos = [
339
- { name: 'game', title: '游戏', Module: Game },
340
- { name: 'app', title: '应用', Module: App },
341
- { name: 'mod', title: '模组', Module: Mod },
342
- ];
343
-
344
- // 管理器实例集合
345
- var promises = [];
346
-
347
- // 使用for循环初始化所有管理器
348
- for (var i = 0; i < infos.length; i++) {
349
- var config = infos[i];
350
- var manager = this._createManager(config.name, config.title, config.Module);
351
- promises.push(manager.do('load'));
352
- }
353
- // 初始化中间件管理器
354
- var midd = await this._initMiddlewareManager();
355
- promises.push(midd.do('load'));
356
- await Promise.all(promises);
357
- };
358
-
359
- /**
360
- * 加载核心模块
361
- */
362
- Server.prototype._loadSources = async function () {
363
- let manager = await this.getManager();
364
- await Promise.all([
365
- manager.game.runAll('load'),
366
- manager.app.runAll('load'),
367
- manager.mod.runAll('load'),
368
- manager.middleware.runAll('load'),
369
- ]);
370
- };
371
-
372
- /**
373
- * 初始化全局错误处理机制
374
- */
375
- Server.prototype._useError = function () {
376
- var _this = this;
377
-
378
- // 全局未捕获异常处理
379
- process.on('uncaughtException', (error) => {
380
- // ERR_STREAM_PREMATURE_CLOSE 是客户端断开连接导致的,无需记录
381
- if (error && error.code === 'ERR_STREAM_PREMATURE_CLOSE') {
382
- return;
383
- }
384
- _this.log('error', '全局未捕获异常:', error);
385
- });
386
-
387
- // 全局未处理Promise拒绝处理
388
- process.on('unhandledRejection', (reason) => {
389
- // ERR_STREAM_PREMATURE_CLOSE 同样忽略
390
- if (reason && reason.code === 'ERR_STREAM_PREMATURE_CLOSE') {
391
- return;
392
- }
393
- _this.log('error', '全局未处理Promise拒绝:', reason);
394
- });
395
- };
396
-
397
- /**
398
- * 初始化
399
- */
400
- Server.prototype._initCore = async function () {
401
- // 初始化管理器
402
- await this._initSources();
403
- };
404
-
405
- /**
406
- * 初始化核心模块
407
- */
408
- Server.prototype._initSources = async function () {
409
- let manager = await this.getManager();
410
- await Promise.all([
411
- manager.game.runAll('init', this, this.getEventer(), this.getLogger()),
412
- manager.app.runAll('init', this, this.getEventer(), this.getLogger()),
413
- manager.mod.runAll('init', this, this.getEventer(), this.getLogger()),
414
- manager.middleware.runWait(
415
- 'init',
416
- this.adapter,
417
- this.getEventer(),
418
- this.getLogger(),
419
- ),
420
- ]);
421
- };
422
-
423
- /**
424
- * 初始化基础设施
425
- */
426
- Server.prototype._createBase = function () {
427
- // 获取数据库配置
428
- var sql = { type: 'mysql', ...this.config.sql };
429
- if (this.config.mysql) {
430
- sql.mysql = this.config.mysql;
431
- }
432
- if (this.config.sqlite) {
433
- sql.sqlite = this.config.sqlite;
434
- }
435
- $.sqlAdmin = sqlAdmin;
436
- this.sql = sqlAdmin('sys', sql);
437
- $.sql = this.sql;
438
-
439
- // 获取缓存配置
440
- var cache = { type: 'memory', ...this.config.cache };
441
- if (this.config.cachebase) {
442
- cache.cachebase = this.config.cachebase;
443
- }
444
- if (this.config.redis) {
445
- cache = { ...cache, ...this.config.redis };
446
- }
447
- if (this.config.mongodb) {
448
- cache.mongodb = this.config.mongodb;
449
- }
450
- this.cache = cacheAdmin('sys', cache);
451
- $.cache = this.cache;
452
-
453
- this.filer = $.file;
454
- };
455
-
456
- /**
457
- * 初始化工具
458
- */
459
- Server.prototype._createTool = function () {
460
- $.log = new Log(this.config.log);
461
- this.setLogger($.log);
462
- $.config = new Config({
463
- file: '/config.json'.fullname(),
464
- });
465
- $.config.load();
466
- $.config.setOptions(this.config);
467
- $.config.saveAsync();
468
- $.com = new Com();
469
- $.com.loads();
470
-
471
- // 模板引擎
472
- const Http = require('mm_https').Http;
473
- $.Http = Http;
474
- };
475
-
476
- /**
477
- * 创建适配器
478
- */
479
- Server.prototype._createAdapter = async function () {
480
- let config = this.config;
481
- var web = new Web(config.web, this);
482
- let adapter = this.adapter;
483
- adapter.web = await web.init();
484
- if (config.web.socket) {
485
- adapter.ws = await new WebSocket(config.web, this).init(web);
486
- }
487
- if (config.mqtt && config.mqtt.state) {
488
- adapter.mqtt = await new Mqtt(config.mqtt, this).init();
489
- }
490
- if (config.socket && config.socket.state) {
491
- adapter.socket = await new Socket(config.socket || {}, this).init();
492
- }
493
- };
494
-
495
- /**
496
- * 启动适配器
497
- */
498
- Server.prototype._startAdapter = async function () {
499
- let adapter = this.adapter;
500
-
501
- let promises = [];
502
- for (let key in adapter) {
503
- if (adapter[key]) {
504
- promises.push(adapter[key].start());
505
- }
506
- }
507
- await Promise.all(promises);
508
- };
509
-
510
- /**
511
- * 启动基础设施
512
- */
513
- Server.prototype._startBase = async function () {
514
- await Promise.all([$.cache.connect(), $.sql.open()]);
515
- };
516
-
517
- /**
518
- * 启动
519
- */
520
- Server.prototype._startCore = async function () {
521
- await this._startBase();
522
- // 启动适配器
523
- await this._startAdapter();
524
- // 启动核心模块
525
- await this._startSources();
526
- };
527
-
528
- /**
529
- * 启动核心模块
530
- */
531
- Server.prototype._startSources = async function () {
532
- let manager = await this.getManager();
533
- await Promise.all([
534
- manager.game.runAll('start', this, this.getEventer(), this.getLogger()),
535
- manager.app.runAll('start', this, this.getEventer(), this.getLogger()),
536
- ]);
537
- };
538
-
539
- /**
540
- * 停止基础设施
541
- */
542
- Server.prototype._stopBase = async function () {
543
- await Promise.all([$.cache.close(), $.sql.close()]);
544
- };
545
-
546
- /**
547
- * 停止 - 私有方法
548
- */
549
- Server.prototype._stopCore = async function () {
550
- await this._stopBase();
551
- await this._stopSources();
552
- };
553
-
554
- /**
555
- * 停止核心模块
556
- */
557
- Server.prototype._stopSources = async function () {
558
- let manager = await this.getManager();
559
- await Promise.all([manager.game.runAll('stop'), manager.app.runAll('stop')]);
560
- };
561
-
562
- /**
563
- * 运行
564
- * @returns {object} 服务器实例
565
- */
566
- Server.prototype.run = async function () {
567
- // 加载
568
- console.time('[加载] 总计');
569
- await this.do('load');
570
- console.timeEnd('[加载] 总计');
571
-
572
- // 初始化
573
- console.time('[初始化] 总计');
574
- await this.do('init');
575
- console.timeEnd('[初始化] 总计');
576
-
577
- // 启动
578
- console.time('[启动] 总计');
579
- await this.do('start');
580
- console.timeEnd('[启动] 总计');
581
- return this;
582
- };
583
-
584
- /**
585
- * 使用中间件
586
- * @param {object} cs 中间件实例
587
- * @param {object} config 配置参数
588
- */
589
- Server.prototype.use = async function (cs, config) {
590
- if (cs && typeof cs.install === 'function') {
591
- await cs.install(this, config);
592
- }
593
- };
594
-
595
- exports.Server = Server;
package/ulits/id_gen.js DELETED
@@ -1,134 +0,0 @@
1
- /**
2
- * ID生成器
3
- * 基于雪花算法实现,生成唯一ID
4
- */
5
- class IdGen {
6
- /**
7
- * 构造函数
8
- * @param {object} config 配置参数
9
- * @param {number} config.worker_id 工作机器ID (0-31)
10
- * @param {number} config.dc_id 数据中心ID (0-31)
11
- * @param {number} config.sequence 序列号
12
- */
13
- constructor(config = {}) {
14
- this.worker_id = config.worker_id || 0;
15
- this.dc_id = config.dc_id || 0;
16
- this.sequence = config.sequence || 0;
17
-
18
- this.worker_id_bits = 5;
19
- this.dc_id_bits = 5;
20
- this.max_worker_id = -1 ^ (-1 << this.worker_id_bits);
21
- this.max_dc_id = -1 ^ (-1 << this.dc_id_bits);
22
- this.sequence_bits = 12;
23
-
24
- this.worker_id_shift = this.sequence_bits;
25
- this.dc_id_shift = this.sequence_bits + this.worker_id_bits;
26
- this.timestamp_left_shift = this.sequence_bits + this.worker_id_bits + this.dc_id_bits;
27
- this.sequence_mask = -1 ^ (-1 << this.sequence_bits);
28
-
29
- this.last_timestamp = -1;
30
-
31
- this.twepoch = 1288834974657;
32
-
33
- if (this.worker_id > this.max_worker_id || this.worker_id < 0) {
34
- throw new TypeError(`工作机器ID必须在0-${this.max_worker_id}之间`);
35
- }
36
- if (this.dc_id > this.max_dc_id || this.dc_id < 0) {
37
- throw new TypeError(`数据中心ID必须在0-${this.max_dc_id}之间`);
38
- }
39
- }
40
- }
41
-
42
- /**
43
- * 获取当前时间戳
44
- * @returns {number} 当前时间戳
45
- */
46
- IdGen.prototype._timeGen = function () {
47
- return Date.now();
48
- };
49
-
50
- /**
51
- * 等待下一毫秒
52
- * @param {number} last_timestamp 上次时间戳
53
- * @returns {number} 新的时间戳
54
- */
55
- IdGen.prototype._tilNextMillis = function (last_timestamp) {
56
- var timestamp = this._timeGen();
57
- while (timestamp <= last_timestamp) {
58
- timestamp = this._timeGen();
59
- }
60
- return timestamp;
61
- };
62
-
63
- /**
64
- * 生成下一个ID
65
- * @returns {string} 唯一ID
66
- */
67
- IdGen.prototype.nextId = function () {
68
- var timestamp = this._timeGen();
69
-
70
- if (timestamp < this.last_timestamp) {
71
- throw new Error('时钟回拨,拒绝生成ID');
72
- }
73
-
74
- if (this.last_timestamp === timestamp) {
75
- this.sequence = (this.sequence + 1) & this.sequence_mask;
76
- if (this.sequence === 0) {
77
- timestamp = this._tilNextMillis(this.last_timestamp);
78
- }
79
- } else {
80
- this.sequence = 0;
81
- }
82
-
83
- this.last_timestamp = timestamp;
84
-
85
- var id = (BigInt(timestamp - this.twepoch) << BigInt(this.timestamp_left_shift))
86
- | (BigInt(this.dc_id) << BigInt(this.dc_id_shift))
87
- | (BigInt(this.worker_id) << BigInt(this.worker_id_shift))
88
- | BigInt(this.sequence);
89
-
90
- return id.toString();
91
- };
92
-
93
- /**
94
- * 生成带前缀的ID
95
- * @param {string} prefix 前缀
96
- * @returns {string} 带前缀的唯一ID
97
- */
98
- IdGen.prototype.genId = function (prefix) {
99
- if (typeof prefix !== 'string') {
100
- throw new TypeError('前缀必须是字符串');
101
- }
102
- return prefix + '_' + this.nextId();
103
- };
104
-
105
- /**
106
- * 解析ID信息
107
- * @param {string} id ID字符串
108
- * @returns {object} ID信息对象
109
- */
110
- IdGen.prototype.parseId = function (id) {
111
- if (typeof id !== 'string') {
112
- throw new TypeError('ID必须是字符串');
113
- }
114
- var num = BigInt(id.replace(/^[a-zA-Z_]+_/, ''));
115
- var timestamp = Number((num >> 22n) + BigInt(this.twepoch));
116
- var dc_id = Number((num >> 17n) & 31n);
117
- var worker_id = Number((num >> 12n) & 31n);
118
- var sequence = Number(num & 4095n);
119
-
120
- return {
121
- timestamp: timestamp,
122
- datetime: new Date(timestamp).toISOString(),
123
- dc_id: dc_id,
124
- worker_id: worker_id,
125
- sequence: sequence
126
- };
127
- };
128
-
129
- var idGen = new IdGen();
130
-
131
- module.exports = {
132
- IdGen,
133
- idGen
134
- };