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