mm_os 4.3.7 → 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 (232) 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 -84
  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 -704
  46. package/com/mqtt/index.js +0 -1052
  47. package/com/mqtt/index.test.js +0 -503
  48. package/com/mqtt/script.tpl.js +0 -719
  49. package/com/nav/README.md +0 -2
  50. package/com/nav/com.json +0 -5
  51. package/com/nav/config.tpl.json +0 -84
  52. package/com/nav/drive.js +0 -702
  53. package/com/nav/index.js +0 -228
  54. package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
  55. package/com/nav/tpl/admin_pc/page_config.vue +0 -280
  56. package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
  57. package/com/nav/tpl/admin_pc/page_default.vue +0 -247
  58. package/com/nav/tpl/admin_pc/page_form.vue +0 -180
  59. package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
  60. package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
  61. package/com/nav/tpl/admin_pc/page_table.vue +0 -296
  62. package/com/nav/tpl/admin_pc/page_type.vue +0 -321
  63. package/com/nav/tpl/admin_pc/page_view.vue +0 -124
  64. package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
  65. package/com/nav/tpl/dev_pc/page_config.vue +0 -253
  66. package/com/nav/tpl/dev_pc/page_default.vue +0 -247
  67. package/com/nav/tpl/dev_pc/page_form.vue +0 -162
  68. package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
  69. package/com/nav/tpl/dev_pc/page_table.vue +0 -247
  70. package/com/nav/tpl/dev_pc/page_type.vue +0 -313
  71. package/com/nav/tpl/home_pc/page_channel.vue +0 -234
  72. package/com/nav/tpl/home_pc/page_default.vue +0 -234
  73. package/com/nav/tpl/home_pc/page_form.vue +0 -137
  74. package/com/nav/tpl/home_pc/page_list.vue +0 -234
  75. package/com/nav/tpl/home_pc/page_nav.vue +0 -221
  76. package/com/nav/tpl/home_pc/page_type.vue +0 -234
  77. package/com/nav/tpl/home_pc/page_view.vue +0 -125
  78. package/com/nav/tpl/home_phone/page_channel.vue +0 -234
  79. package/com/nav/tpl/home_phone/page_default.vue +0 -234
  80. package/com/nav/tpl/home_phone/page_form.vue +0 -137
  81. package/com/nav/tpl/home_phone/page_list.vue +0 -234
  82. package/com/nav/tpl/home_phone/page_nav.vue +0 -237
  83. package/com/nav/tpl/home_phone/page_type.vue +0 -234
  84. package/com/nav/tpl/home_phone/page_view.vue +0 -125
  85. package/com/nav/viewmodel.js +0 -446
  86. package/com/param/README.md +0 -2
  87. package/com/param/com.json +0 -5
  88. package/com/param/config.tpl.json +0 -136
  89. package/com/param/drive.js +0 -502
  90. package/com/param/index.js +0 -158
  91. package/com/param/script.tpl.js +0 -12
  92. package/com/pendant/README.md +0 -3
  93. package/com/pendant/com.json +0 -5
  94. package/com/pendant/config.tpl.json +0 -32
  95. package/com/pendant/drive.js +0 -204
  96. package/com/pendant/index.js +0 -467
  97. package/com/pendant/pendant.html +0 -16
  98. package/com/pendant/script.tpl.js +0 -18
  99. package/com/socket/README.md +0 -47
  100. package/com/socket/com.json +0 -5
  101. package/com/socket/config.tpl.json +0 -12
  102. package/com/socket/drive.js +0 -679
  103. package/com/socket/index.js +0 -402
  104. package/com/socket/script.tpl.js +0 -41
  105. package/com/sql/README.md +0 -3
  106. package/com/sql/com.json +0 -5
  107. package/com/sql/config.tpl.json +0 -74
  108. package/com/sql/drive.js +0 -1328
  109. package/com/sql/index.js +0 -152
  110. package/com/sql/script.tpl.js +0 -47
  111. package/com/static/README.md +0 -2
  112. package/com/static/com.json +0 -5
  113. package/com/static/config.tpl.json +0 -24
  114. package/com/static/drive.js +0 -194
  115. package/com/static/index.js +0 -237
  116. package/com/static/script.tpl.js +0 -28
  117. package/com/task/README.md +0 -3
  118. package/com/task/com.json +0 -5
  119. package/com/task/config.tpl.json +0 -24
  120. package/com/task/drive.js +0 -428
  121. package/com/task/index.js +0 -159
  122. package/com/task/script.tpl.js +0 -40
  123. package/com/template/com.json +0 -5
  124. package/com/template/config.tpl.json +0 -16
  125. package/com/template/drive.js +0 -81
  126. package/com/template/index.js +0 -141
  127. package/com.js +0 -237
  128. package/common/README.md +0 -2
  129. package/common/middleware/web_after/index.js +0 -26
  130. package/common/middleware/web_after/middleware.json +0 -9
  131. package/common/middleware/web_base/index.js +0 -113
  132. package/common/middleware/web_base/middleware.json +0 -19
  133. package/common/middleware/web_before/index.js +0 -33
  134. package/common/middleware/web_before/middleware.json +0 -9
  135. package/common/middleware/web_cors/index.js +0 -87
  136. package/common/middleware/web_cors/middleware.json +0 -24
  137. package/common/middleware/web_error/index.js +0 -131
  138. package/common/middleware/web_error/middleware.json +0 -19
  139. package/common/middleware/web_ip/index.js +0 -15
  140. package/common/middleware/web_ip/middleware.json +0 -14
  141. package/common/middleware/web_logger/index.js +0 -156
  142. package/common/middleware/web_logger/middleware.json +0 -14
  143. package/common/middleware/web_main/index.js +0 -26
  144. package/common/middleware/web_main/middleware.json +0 -9
  145. package/common/middleware/web_static/index.js +0 -73
  146. package/common/middleware/web_static/middleware.json +0 -54
  147. package/common/middleware/web_waf/index.js +0 -395
  148. package/common/middleware/web_waf/middleware.json +0 -13
  149. package/core/app/config.tpl.json +0 -67
  150. package/core/app/index.js +0 -711
  151. package/core/app/script.tpl.js +0 -52
  152. package/core/channel/index.js +0 -899
  153. package/core/channel/matcher.js +0 -585
  154. package/core/com/config.tpl.json +0 -16
  155. package/core/com/index.js +0 -77
  156. package/core/com/script.tpl.js +0 -5
  157. package/core/component/component.js +0 -42
  158. package/core/component/config.tpl.json +0 -63
  159. package/core/component/index.js +0 -273
  160. package/core/component/script.tpl.js +0 -19
  161. package/core/controller/config.tpl.json +0 -14
  162. package/core/controller/index.js +0 -371
  163. package/core/controller/script.tpl.js +0 -27
  164. package/core/factory/config.tpl.json +0 -14
  165. package/core/factory/entity.js +0 -275
  166. package/core/factory/index.js +0 -241
  167. package/core/factory/script.tpl.js +0 -16
  168. package/core/game/bat/index.js +0 -137
  169. package/core/game/bat/world.js +0 -622
  170. package/core/game/config.tpl.json +0 -16
  171. package/core/game/entity_admin.js +0 -230
  172. package/core/game/index.js +0 -186
  173. package/core/handler/config.tpl.json +0 -22
  174. package/core/handler/index.js +0 -181
  175. package/core/handler/script.tpl.js +0 -23
  176. package/core/logic/config.tpl.json +0 -14
  177. package/core/logic/index.js +0 -59
  178. package/core/logic/script.tpl.js +0 -19
  179. package/core/middleware/config.tpl.json +0 -16
  180. package/core/middleware/index.js +0 -117
  181. package/core/middleware/script.tpl.js +0 -37
  182. package/core/mod/config.tpl.json +0 -22
  183. package/core/mod/index.js +0 -136
  184. package/core/mod/script.tpl.js +0 -34
  185. package/core/model/config.tpl.json +0 -219
  186. package/core/model/index.js +0 -272
  187. package/core/model/model.js +0 -27
  188. package/core/model/script.tpl.js +0 -20
  189. package/core/notifier/config.tpl.json +0 -14
  190. package/core/notifier/index.js +0 -77
  191. package/core/notifier/script.tpl.js +0 -20
  192. package/core/plugin/config.tpl.json +0 -36
  193. package/core/plugin/index.js +0 -267
  194. package/core/plugin/script.tpl.js +0 -51
  195. package/core/pusher/config.tpl.json +0 -14
  196. package/core/pusher/index.js +0 -158
  197. package/core/pusher/script.tpl.js +0 -20
  198. package/core/room/bat/index.js +0 -170
  199. package/core/room/bat/room.js +0 -524
  200. package/core/room/config.tpl.json +0 -20
  201. package/core/room/index.js +0 -249
  202. package/core/room/room.js +0 -61
  203. package/core/scene/config.tpl.json +0 -14
  204. package/core/scene/index.js +0 -467
  205. package/core/scene/loop.js +0 -1306
  206. package/core/scene/map.js +0 -28
  207. package/core/scene/script.tpl.js +0 -22
  208. package/core/sender/config.tpl.json +0 -14
  209. package/core/sender/index.js +0 -79
  210. package/core/sender/script.tpl.js +0 -20
  211. package/core/service/config.tpl.json +0 -14
  212. package/core/service/index.js +0 -100
  213. package/core/service/script.tpl.js +0 -25
  214. package/core/store/config.tpl.json +0 -26
  215. package/core/store/index.js +0 -1794
  216. package/core/store/script.tpl.js +0 -22
  217. package/core/system/config.tpl.json +0 -18
  218. package/core/system/index.js +0 -312
  219. package/core/system/script.tpl.js +0 -77
  220. package/core/view/config.tpl.json +0 -14
  221. package/core/view/index.js +0 -91
  222. package/core/view/script.tpl.js +0 -20
  223. package/core/zone/bat/index.js +0 -744
  224. package/core/zone/config.tpl.json +0 -54
  225. package/core/zone/index.js +0 -655
  226. package/core/zone/script.tpl.js +0 -10
  227. package/core/zone/zone_bat.js +0 -136
  228. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
  229. package/os.js +0 -53
  230. package/server.js +0 -602
  231. package/ulits/id_gen.js +0 -134
  232. package/ulits/word.js +0 -206
@@ -1,54 +0,0 @@
1
- {
2
- // 区域名称
3
- "name": "${name || 'zone'}",
4
- // 区域标题
5
- "title": "${title || '枫叶区'}",
6
- // 区域描述
7
- "description": "${description || '枫叶区是一个区域'}",
8
- // 区域主脚本 - 可自定义区域初始化,一般情况不需要定义
9
- "main": "${main || ''}",
10
- // 区域状态 - 1 正常 0 禁用
11
- "state": 1,
12
- // 区域排序 - 数字越小越靠前
13
- "sort": 100,
14
- // 区域等级要求 - 0 表示无等级要求
15
- "level": 0,
16
- // 频道数
17
- "channels": 20,
18
- // 自定义数据库 - 1 开启 0 关闭
19
- "diy_db": 0,
20
- // 自定义缓存 - 1 开启 0 关闭
21
- "diy_cache": 0,
22
- // sql 配置 - 可以没有,没有则使用默认数据库
23
- "db": {
24
- // 数据库方式
25
- "way": "mysql",
26
- "mysql": {
27
- // 数据库主机
28
- "host": "127.0.0.1",
29
- // 数据库端口
30
- "port": 3306,
31
- // 数据库用户名
32
- "username": "root",
33
- // 数据库密码
34
- "password": "your_db_password_here",
35
- // 数据库名称
36
- "database": "mm"
37
- }
38
- },
39
- // 缓存配置 - 可以没有,没有则使用默认缓存
40
- "cache": {
41
- // 缓存方式方式
42
- "way": "redis",
43
- "redis": {
44
- // 缓存主机
45
- "host": "127.0.0.1",
46
- // 缓存端口
47
- "port": 6379,
48
- // 缓存密码
49
- "password": "your_redis_password_here",
50
- // 缓存数据库
51
- "database": 0
52
- }
53
- }
54
- }
@@ -1,655 +0,0 @@
1
- const { cacheAdmin } = require('mm_cache');
2
- const { sqlAdmin } = require('mm_sql');
3
- const { Drive, Manager } = require('mm_machine');
4
- const { Channel } = require('../channel/index.js');
5
- const { Room } = require('../room/index.js');
6
- const { World } = require('../world/index.js');
7
-
8
- /**
9
- * 游戏分区
10
- */
11
- class Zone extends Drive {
12
- static config = {
13
- // 区域名称
14
- name: 'default',
15
- // 区域标题
16
- title: '枫叶区',
17
- // 区域描述
18
- description: '枫叶区是一个区域',
19
- // 区域主脚本 - 可自定义区域初始化,一般情况不需要定义
20
- main: '',
21
- // 应用域
22
- scope: '',
23
- // 区域状态 - 1 正常 0 禁用
24
- state: 1,
25
- // 区域排序 - 数字越小越靠前
26
- sort: 100,
27
- // 相关数据表
28
- table: {
29
- // 玩家表名
30
- player: 'game_player',
31
- },
32
- // 频道数
33
- channels: 20,
34
- // 自定义数据库 - 1 开启 0 关闭
35
- diy_sql: 0,
36
- // 自定义缓存 - 1 开启 0 关闭
37
- diy_cache: 0,
38
- // sql 配置 - 可以没有,没有则使用默认数据库
39
- sql: {
40
- // 数据库方式
41
- way: 'mysql',
42
- // 数据库主机
43
- host: '127.0.0.1',
44
- // 数据库端口
45
- port: 3306,
46
- // 数据库用户名
47
- username: 'root',
48
- // 数据库密码
49
- password: process.env.DB_PASSWORD || '',
50
- // 数据库名称
51
- database: 'mm',
52
- },
53
- // 缓存配置 - 可以没有,没有则使用默认缓存
54
- cache: {
55
- // 缓存方式方式
56
- way: 'redis',
57
- // 缓存主机
58
- host: '127.0.0.1',
59
- // 缓存端口
60
- port: 6379,
61
- // 缓存密码
62
- password: process.env.REDIS_PASSWORD || '',
63
- // 缓存数据库
64
- database: 0,
65
- },
66
- };
67
-
68
- /**
69
- * 构造函数
70
- * @param {object} config 配置参数
71
- * @param {string} config.name 区域名称
72
- * @param {string} config.title 区域标题
73
- * @param {string} config.description 区域描述
74
- * @param {string} config.main 区域主脚本
75
- * @param {string} config.scope 应用域
76
- * @param {number} config.state 区域状态
77
- * @param {number} config.sort 区域排序
78
- * @param {object} config.table 相关数据表
79
- * @param {number} config.channels 频道数
80
- * @param {number} config.diy_sql 自定义数据库
81
- * @param {number} config.diy_cache 自定义缓存
82
- * @param {object} config.sql sql 配置
83
- * @param {object} config.cache 缓存配置
84
- * @param {object} parent 父分区对象
85
- */
86
- constructor(config, parent) {
87
- super({ ...Zone.config, ...config }, parent);
88
-
89
- this.manager = {};
90
- // 游戏频道集合
91
- this.channel = {};
92
- // 游戏房间集合
93
- this.room = {};
94
- // 游戏世界集合
95
- this.world = {};
96
-
97
- // 玩家对象集合 - 分区统一管理所有玩家
98
- this.players = new Map();
99
- }
100
- }
101
-
102
- /**
103
- * 预设
104
- */
105
- Zone.prototype._preset = function () {
106
- // 分区专用数据库
107
- this.sql = null;
108
- // 分区专用缓存
109
- this.cache = null;
110
- };
111
-
112
- /**
113
- * 获取模板目录
114
- * @returns {string} 模板目录
115
- */
116
- Zone.prototype.getTplDir = function () {
117
- return __dirname;
118
- };
119
-
120
- /**
121
- * 初始化核心
122
- * @param {object} server 服务器实例
123
- * @param {object} eventer 事件总线
124
- * @param {object} logger 日志管理器
125
- * @private
126
- */
127
- Zone.prototype._initCore = async function (server, eventer, logger) {
128
- // 初始化依赖项
129
- if (logger) {
130
- this.setLogger(logger);
131
- }
132
- if (eventer) {
133
- this.getEventer = function () {
134
- return eventer;
135
- };
136
- }
137
- if (server) {
138
- this.getServer = function () {
139
- return server;
140
- };
141
- }
142
- // 初始化基础组件
143
- await this._initBase();
144
- // 初始化管理器
145
- await this._initManager();
146
- // 加载资源
147
- await this._loadSources();
148
- };
149
-
150
- /**
151
- * 初始化基础组件
152
- * @private
153
- */
154
- Zone.prototype._initBase = async function () {
155
- // 初始化数据库
156
- if (this.config.diy_sql) {
157
- this.sql = sqlAdmin(this.config.name, this.config.sql);
158
- await this.sql.open();
159
- } else {
160
- this.sql = this.getServer().sql;
161
- }
162
-
163
- // 初始化缓存
164
- if (this.config.diy_cache) {
165
- this.cache = cacheAdmin(this.config.name, this.config.cache);
166
- await this.cache.connect();
167
- } else {
168
- this.cache = this.getServer().cache;
169
- }
170
- };
171
-
172
- /**
173
- * 创建管理器实例
174
- * @param {string} name 管理器名称
175
- * @param {string} title 管理器标题
176
- * @param {Function} cls 管理器类
177
- * @returns {object} 管理器实例
178
- */
179
- Zone.prototype._createManager = function (name, title, cls) {
180
- let dir = this.getDir();
181
- var manager = new Manager(
182
- {
183
- name: name,
184
- title: title,
185
- filename: name + '.json',
186
- tpl_dir: `../${name}/`.fullname(__dirname),
187
- base_dir: '',
188
- dir: `./${name}`.fullname(dir),
189
- },
190
- this,
191
- this[name],
192
- cls,
193
- );
194
- this.manager[name] = manager;
195
- return manager;
196
- };
197
-
198
- /**
199
- * 初始化管理器集合
200
- * @returns {Promise<void>} 初始化完成
201
- */
202
- Zone.prototype._initManager = async function () {
203
- // 管理器配置列表
204
- var mgr_configs = [
205
- { name: 'channel', title: '频道', Module: Channel },
206
- { name: 'room', title: '房间', Module: Room },
207
- { name: 'world', title: '世界', Module: World },
208
- ];
209
-
210
- // 管理器实例集合
211
- var managers = [];
212
-
213
- // 使用for循环初始化所有管理器
214
- for (var i = 0; i < mgr_configs.length; i++) {
215
- var config = mgr_configs[i];
216
- var manager = this._createManager(config.name, config.title, config.Module);
217
- managers.push(manager);
218
- }
219
-
220
- // 并行执行所有管理器的初始化操作
221
- var promises = [];
222
- for (var j = 0; j < managers.length; j++) {
223
- promises.push(managers[j].do('init'));
224
- }
225
-
226
- await Promise.all(promises);
227
- };
228
-
229
- /**
230
- * 加载资源
231
- * @returns {Promise<void>} 加载完成
232
- */
233
- Zone.prototype._loadSources = async function () {
234
- // 加载无需依赖关系,所以可以同时加载,内部并行执行
235
- let loadPromises = [
236
- this.manager.channel.runAll('load'),
237
- this.manager.room.runAll('load'),
238
- this.manager.world.runAll('load'),
239
- ];
240
- await Promise.all(loadPromises);
241
- };
242
-
243
- /**
244
- * 初始化资源
245
- * @returns {Promise<void>} 初始化完成
246
- */
247
- Zone.prototype._initSources = async function () {
248
- // 初始化顺序 world -> channel -> room,需要依赖,所以根据依赖关系自下而上初始化,内部并行执行
249
- await this.manager.world.runAll(
250
- 'init',
251
- this,
252
- this.getEventer(),
253
- this.getLogger(),
254
- );
255
- await this.manager.channel.runAll(
256
- 'init',
257
- this,
258
- this.getEventer(),
259
- this.getLogger(),
260
- );
261
- await this.manager.room.runAll(
262
- 'init',
263
- this,
264
- this.getEventer(),
265
- this.getLogger(),
266
- );
267
- };
268
-
269
- /**
270
- * 启动资源
271
- */
272
- App.prototype._startSources = async function () {
273
- // 启动无顺序的管理器(保持并行执行)
274
- let promises = [
275
- this.manager.channel.runAll('start'),
276
- this.manager.room.runAll('start'),
277
- this.manager.world.runAll('start'),
278
- ];
279
-
280
- // 等待其他管理器启动完成
281
- await Promise.all(promises);
282
- };
283
-
284
- /**
285
- * 启动核心
286
- * @private
287
- */
288
- Zone.prototype._startCore = async function () {
289
- // 初始化资源
290
- await this._initSources();
291
-
292
- // 加载所有登录玩家
293
- await this._loadPlayers();
294
-
295
- // 启动资源
296
- await this._startSources();
297
- };
298
-
299
- /**
300
- * 获取玩家对象
301
- * @param {string} player_id 玩家ID
302
- * @returns {object} 玩家对象
303
- */
304
- Zone.prototype.getPlayer = function (player_id) {
305
- // 参数校验
306
- if (!player_id || typeof player_id !== 'string') {
307
- throw new TypeError('玩家ID必须是有效字符串');
308
- }
309
-
310
- var player = this.players.get(player_id);
311
- if (!player) {
312
- throw new Error(`玩家 ${player_id} 不存在`);
313
- }
314
-
315
- return player;
316
- };
317
-
318
- /**
319
- * 从分区移除玩家
320
- * @param {string} player_id 玩家ID
321
- * @returns {boolean} 是否移除成功
322
- */
323
- Zone.prototype.delPlayer = function (player_id) {
324
- // 参数校验
325
- if (!player_id || typeof player_id !== 'string') {
326
- throw new TypeError('玩家ID必须是有效字符串');
327
- }
328
-
329
- var player = this.players.get(player_id);
330
- if (!player) {
331
- throw new Error('玩家不在分区中');
332
- }
333
-
334
- // 如果玩家在某个频道中,先退出频道
335
- if (player.channel_id && this.channel.get(player.channel_id)) {
336
- this.channel.get(player.channel_id).leave(player_id);
337
- }
338
-
339
- // 从分区移除玩家
340
- this.players.delete(player_id);
341
-
342
- // 触发玩家离开分区事件
343
- this.emitEvent('player_leave_zone', {
344
- player_id: player_id,
345
- zone_name: this.config.name,
346
- timestamp: Date.now(),
347
- });
348
-
349
- return true;
350
- };
351
-
352
- /**
353
- * 加载所有登录玩家
354
- */
355
- Zone.prototype._loadPlayers = async function () {
356
- var players = await this.emitWait('load_players', {
357
- zone_name: this.config.name,
358
- zone: this,
359
- });
360
- if (players && players.length > 0) {
361
- for (var i = 0; i < players.length; i++) {
362
- var info = players[i];
363
- var player = this._model.player.create(info);
364
- this.players.set(player.player_id, player);
365
- }
366
- }
367
- };
368
-
369
- /**
370
- * 保存所有玩家数据
371
- * @returns {Promise<object>} 保存结果
372
- */
373
- Zone.prototype._savePlayers = async function () {
374
- var all_players = Array.from(this.players.values());
375
- if (all_players.length === 0) {
376
- return { count: 0, success: true };
377
- }
378
- await this.emitWait('save_players', {
379
- zone_name: this.config.name,
380
- zone: this,
381
- players: all_players,
382
- });
383
- this.log('info', `保存所有玩家数据 ${all_players.length} 个`);
384
- return { count: all_players.length, success: true };
385
- };
386
-
387
- /**
388
- * 保存脏数据玩家
389
- * @returns {Promise<object>} 保存结果
390
- */
391
- Zone.prototype._saveDirtyPlayers = async function () {
392
- var dirty_players = [];
393
- for (var player of this.players.values()) {
394
- if (player.isDirty && player.isDirty()) {
395
- dirty_players.push(player);
396
- }
397
- }
398
- if (dirty_players.length === 0) {
399
- return { count: 0, success: true };
400
- }
401
- await this.emitWait('save_players', {
402
- zone_name: this.config.name,
403
- zone: this,
404
- players: dirty_players,
405
- });
406
- for (var i = 0; i < dirty_players.length; i++) {
407
- dirty_players[i].isDirty(false);
408
- }
409
- this.log('debug', `保存脏数据玩家 ${dirty_players.length} 个`);
410
- return { count: dirty_players.length, success: true };
411
- };
412
-
413
- /**
414
- * 全分区数据发送
415
- * @param {object} data 发送数据
416
- * @param {string} msg_type 消息类型
417
- * @param {string} msg_group 消息范围
418
- * @param {string} sender_id 发送者ID
419
- * @returns {object} 发送结果
420
- */
421
- Zone.prototype.sendData = async function (
422
- data,
423
- msg_type = 'system',
424
- msg_group = 'world',
425
- sender_id = 'system',
426
- ) {
427
- // 参数校验
428
- if (!data || typeof data !== 'object') {
429
- throw new TypeError('发送数据必须是有效对象');
430
- }
431
-
432
- // 创建发送对象
433
- var msg = {
434
- data: data,
435
- msg_type: msg_type,
436
- msg_group: msg_group,
437
- sender_id: sender_id,
438
- timestamp: Date.now(),
439
- zone_name: this.config.name,
440
- msg_id: this._generateMsgId(),
441
- };
442
-
443
- // 触发全分区数据发送事件,由业务层处理具体的发送逻辑
444
- this.emitEvent('zone_data_send', {
445
- ...msg,
446
- players: this.players.size,
447
- });
448
-
449
- this.log('debug', `全分区数据发送事件已触发: 玩家数 ${this.players.size}`);
450
-
451
- // 返回默认结果
452
- return {
453
- players: this.players.size,
454
- success_count: 0,
455
- fail_count: 0,
456
- failed_players: [],
457
- };
458
- };
459
-
460
- /**
461
- * 向指定玩家发送数据
462
- * @param {string} player_id 玩家ID
463
- * @param {object} data 发送数据
464
- * @param {string} msg_type 消息类型
465
- * @param {string} msg_group 消息范围
466
- * @param {string} sender_id 发送者ID
467
- * @returns {boolean} 是否发送成功
468
- */
469
- Zone.prototype.sendToPlayer = async function (
470
- player_id,
471
- data,
472
- msg_type = 'text',
473
- msg_group = 'private',
474
- sender_id = 'system',
475
- ) {
476
- // 参数校验
477
- if (!player_id || typeof player_id !== 'string') {
478
- throw new TypeError('玩家ID必须是有效字符串');
479
- }
480
-
481
- if (!data || typeof data !== 'object') {
482
- throw new TypeError('发送数据必须是有效对象');
483
- }
484
-
485
- try {
486
- // 获取玩家对象
487
- var player = this.getPlayer(player_id);
488
- if (!player) {
489
- throw new Error(`玩家 ${player_id} 不存在`);
490
- }
491
-
492
- // 触发私聊数据发送事件,由业务层处理具体的发送逻辑
493
- this.emitEvent('private_send', {
494
- data: data,
495
- msg_type: msg_type,
496
- msg_group: msg_group,
497
- sender_id: sender_id,
498
- timestamp: Date.now(),
499
- player_id: player_id,
500
- zone_name: this.config.name,
501
- msg_id: this._generateMsgId(),
502
- });
503
-
504
- return true;
505
- } catch (error) {
506
- this.log('error', `向玩家 ${player_id} 发送数据失败: `, error);
507
- throw error;
508
- }
509
- };
510
-
511
- /**
512
- * 条件数据发送
513
- * @param {Function} func 筛选函数
514
- * @param {object} data 发送数据
515
- * @param {string} msg_type 消息类型
516
- * @param {string} msg_group 消息范围
517
- * @param {string} sender_id 发送者ID
518
- * @returns {object} 发送结果
519
- */
520
- Zone.prototype.sendWithCondition = async function (
521
- func,
522
- data,
523
- msg_type = 'system',
524
- msg_group = 'world',
525
- sender_id = 'system',
526
- ) {
527
- // 参数校验
528
- if (typeof func !== 'function') {
529
- throw new TypeError('筛选函数必须是有效函数');
530
- }
531
-
532
- if (!data || typeof data !== 'object') {
533
- throw new TypeError('发送数据必须是有效对象');
534
- }
535
-
536
- try {
537
- // 触发条件数据发送事件,由业务层处理具体的发送逻辑
538
- this.emitEvent('conditional_send', {
539
- data: data,
540
- msg_type: msg_type,
541
- msg_group: msg_group,
542
- sender_id: sender_id,
543
- timestamp: Date.now(),
544
- zone_name: this.config.name,
545
- msg_id: this._generateMsgId(),
546
- condition: func.toString(),
547
- players: this.players.size,
548
- });
549
-
550
- // 返回默认结果
551
- return {
552
- players: this.players.size,
553
- filtered_players: 0,
554
- success_count: 0,
555
- fail_count: 0,
556
- failed_players: [],
557
- };
558
- } catch (error) {
559
- this.log('error', `条件数据发送失败: `, error);
560
- throw error;
561
- }
562
- };
563
-
564
- /**
565
- * 获取分区统计信息
566
- * @returns {object} 分区统计信息
567
- */
568
- Zone.prototype.getStats = function () {
569
- var channel_stats = {};
570
- var players = 0;
571
-
572
- // 统计各频道玩家数量
573
- for (var channel_id in this.channel) {
574
- if (this.channel[channel_id] && this.channel[channel_id].getStats) {
575
- var stats = this.channel[channel_id].getStats();
576
- channel_stats[channel_id] = stats;
577
- players += stats.player || 0;
578
- }
579
- }
580
-
581
- return {
582
- zone_name: this.config.name,
583
- players: this.players.size,
584
- online: players,
585
- channels: this.channel.size,
586
- channel_stats: channel_stats,
587
- current_time: Date.now(),
588
- };
589
- };
590
-
591
- /**
592
- * 添加玩家到分区
593
- * @param {object} player 玩家对象
594
- * @returns {object} 玩家对象
595
- */
596
- Zone.prototype.addPlayer = function (player) {
597
- this.players.set(player.player_id, player);
598
- // 触发玩家加入分区事件
599
- this.emitEvent('player_enter_zone', {
600
- player_id: player.player_id,
601
- zone_name: this.config.name,
602
- timestamp: Date.now(),
603
- });
604
- return player;
605
- };
606
-
607
- /**
608
- * 获取频道对象
609
- * @param {string} channel_id 频道ID
610
- * @returns {object} 频道对象
611
- */
612
- Zone.prototype.getChannel = function (channel_id) {
613
- return this.channel.get(channel_id);
614
- };
615
-
616
- /**
617
- * 获取玩家所在频道
618
- * @param {string} player_id 玩家ID
619
- * @returns {object} 频道对象
620
- */
621
- Zone.prototype.getChannelByPlayerId = function (player_id) {
622
- var player = this.players.get(player_id);
623
- if (player) {
624
- return this.channel.get(player.channel_id);
625
- }
626
- return null;
627
- };
628
-
629
- /**
630
- * 销毁分区
631
- * @returns {Promise<void>}
632
- */
633
- Zone.prototype._destroy = async function () {
634
- await this._savePlayers();
635
- this.players.clear();
636
-
637
- // 关闭自定义数据库连接
638
- if (this.config.diy_sql && this.sql && this.sql.close) {
639
- try {
640
- await this.sql.close();
641
- } catch (err) {
642
- this.log('error', '关闭数据库连接失败', err);
643
- }
644
- }
645
- // 关闭自定义缓存连接
646
- if (this.config.diy_cache && this.cache && this.cache.close) {
647
- try {
648
- await this.cache.close();
649
- } catch (err) {
650
- this.log('error', '关闭缓存连接失败', err);
651
- }
652
- }
653
- };
654
-
655
- exports.Zone = Zone;
@@ -1,10 +0,0 @@
1
- /**
2
- * Zone 区域脚本 - 可自定义区域初始化
3
- */
4
- module.exports = {
5
- /**
6
- * 区域初始化
7
- */
8
- async _init() {
9
- }
10
- };