mm_os 3.3.0 → 4.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 (380) hide show
  1. package/LICENSE +21 -201
  2. package/README.md +491 -99
  3. package/README_EN.md +498 -0
  4. package/adapter/adapter.js +431 -0
  5. package/adapter/custom_persistence.js +660 -0
  6. package/adapter/mqtt.js +273 -0
  7. package/adapter/socket.js +113 -0
  8. package/adapter/web.js +67 -0
  9. package/adapter/websocket.js +146 -0
  10. package/com/api/com.json +5 -0
  11. package/{core/com → com}/api/config.tpl.json +8 -8
  12. package/com/api/drive.js +708 -0
  13. package/com/api/index.js +198 -0
  14. package/com/api/oauth.js +200 -0
  15. package/com/api/script.tpl.js +32 -0
  16. package/com/cmd/README.md +11 -0
  17. package/com/cmd/com.json +5 -0
  18. package/com/cmd/config.tpl.json +122 -0
  19. package/com/cmd/drive.js +1548 -0
  20. package/com/cmd/index.js +1066 -0
  21. package/com/cmd/msg.json +48 -0
  22. package/com/cmd/nlp.js +525 -0
  23. package/com/cmd/script.tpl.js +32 -0
  24. package/com/db/com.json +5 -0
  25. package/com/db/drive.js +1999 -0
  26. package/com/db/index.js +242 -0
  27. package/com/event/com.json +5 -0
  28. package/{core/com → com}/event/config.tpl.json +8 -8
  29. package/com/event/drive.js +59 -0
  30. package/com/event/index.js +409 -0
  31. package/com/event/script.tpl.js +23 -0
  32. package/com/mqtt/com.json +5 -0
  33. package/{core/com → com}/mqtt/config.tpl.json +3 -5
  34. package/com/mqtt/drive.js +676 -0
  35. package/com/mqtt/index.js +822 -0
  36. package/com/mqtt/mm_mqtt.js +425 -0
  37. package/com/mqtt/script.tpl.js +723 -0
  38. package/com/nav/com.json +5 -0
  39. package/com/nav/config.tpl.json +84 -0
  40. package/com/nav/drive.js +702 -0
  41. package/com/nav/index.js +231 -0
  42. package/{core/com → com}/nav/tpl/admin_pc/page_config.vue +280 -280
  43. package/{core/com → com}/nav/tpl/admin_pc/page_config_form.vue +194 -194
  44. package/com/nav/tpl/admin_pc/page_form.vue +180 -0
  45. package/com/nav/tpl/admin_pc/page_view.vue +124 -0
  46. package/com/nav/tpl/dev_pc/page_default.vue +247 -0
  47. package/com/nav/tpl/dev_pc/page_type.vue +313 -0
  48. package/com/nav/tpl/home_pc/page_default.vue +234 -0
  49. package/com/nav/tpl/home_pc/page_form.vue +137 -0
  50. package/com/nav/tpl/home_pc/page_list.vue +234 -0
  51. package/com/nav/tpl/home_pc/page_nav.vue +221 -0
  52. package/com/nav/tpl/home_pc/page_type.vue +234 -0
  53. package/com/nav/tpl/home_pc/page_view.vue +125 -0
  54. package/com/nav/tpl/home_phone/page_channel.vue +234 -0
  55. package/com/nav/tpl/home_phone/page_default.vue +234 -0
  56. package/com/nav/tpl/home_phone/page_form.vue +137 -0
  57. package/com/nav/tpl/home_phone/page_nav.vue +237 -0
  58. package/com/nav/tpl/home_phone/page_type.vue +234 -0
  59. package/com/nav/tpl/home_phone/page_view.vue +125 -0
  60. package/com/nav/viewmodel.js +446 -0
  61. package/com/param/com.json +5 -0
  62. package/{core/com → com}/param/config.tpl.json +7 -1
  63. package/com/param/drive.js +502 -0
  64. package/com/param/index.js +155 -0
  65. package/com/param/script.tpl.js +12 -0
  66. package/com/pendant/com.json +5 -0
  67. package/{core/com/component → com/pendant}/config.tpl.json +15 -13
  68. package/com/pendant/drive.js +204 -0
  69. package/com/pendant/index.js +441 -0
  70. package/com/pendant/pendant.html +16 -0
  71. package/com/pendant/script.tpl.js +18 -0
  72. package/com/socket/com.json +5 -0
  73. package/com/socket/config.tpl.json +12 -0
  74. package/com/socket/drive.js +651 -0
  75. package/com/socket/index.js +351 -0
  76. package/com/socket/script.tpl.js +41 -0
  77. package/com/sql/com.json +5 -0
  78. package/{core/com → com}/sql/config.tpl.json +13 -9
  79. package/com/sql/drive.js +1259 -0
  80. package/com/sql/index.js +150 -0
  81. package/com/sql/script.tpl.js +47 -0
  82. package/com/static/com.json +5 -0
  83. package/{core/com → com}/static/config.tpl.json +10 -6
  84. package/com/static/drive.js +194 -0
  85. package/com/static/index.js +226 -0
  86. package/com/static/script.tpl.js +28 -0
  87. package/com/task/com.json +5 -0
  88. package/{core/com → com}/task/config.tpl.json +4 -6
  89. package/com/task/drive.js +405 -0
  90. package/com/task/index.js +148 -0
  91. package/com/task/script.tpl.js +37 -0
  92. package/com/template/com.json +5 -0
  93. package/com/template/config.tpl.json +16 -0
  94. package/com/template/drive.js +80 -0
  95. package/com/template/index.js +141 -0
  96. package/com.js +156 -0
  97. package/common/README.md +2 -0
  98. package/common/handler/msg/handler.json +22 -0
  99. package/common/handler/msg/index.js +23 -0
  100. package/common/handler/player/handler.json +22 -0
  101. package/common/handler/player/index.js +287 -0
  102. package/common/handler/user/handler.json +22 -0
  103. package/common/handler/user/index.js +23 -0
  104. package/common/middleware/web_after/index.js +29 -0
  105. package/common/middleware/web_after/middleware.json +9 -0
  106. package/common/middleware/web_base/index.js +113 -0
  107. package/common/middleware/web_base/middleware.json +19 -0
  108. package/common/middleware/web_before/index.js +33 -0
  109. package/common/middleware/web_before/middleware.json +9 -0
  110. package/common/middleware/web_cors/index.js +87 -0
  111. package/common/middleware/web_cors/middleware.json +24 -0
  112. package/common/middleware/web_error/index.js +119 -0
  113. package/common/middleware/web_error/middleware.json +18 -0
  114. package/common/middleware/web_ip/index.js +15 -0
  115. package/common/middleware/web_ip/middleware.json +14 -0
  116. package/common/middleware/web_logger/index.js +156 -0
  117. package/common/middleware/web_logger/middleware.json +14 -0
  118. package/common/middleware/web_main/index.js +24 -0
  119. package/common/middleware/web_main/middleware.json +9 -0
  120. package/common/middleware/web_static/index.js +73 -0
  121. package/common/middleware/web_static/middleware.json +54 -0
  122. package/common/middleware/web_waf/index.js +385 -0
  123. package/common/middleware/web_waf/middleware.json +13 -0
  124. package/common/model/msg/index.js +88 -0
  125. package/common/model/msg/model.json +401 -0
  126. package/common/model/player/index.js +63 -0
  127. package/common/model/player/model.json +185 -0
  128. package/common/model/user/index.js +11 -0
  129. package/common/model/user/model.json +219 -0
  130. package/core/app/config.tpl.json +67 -0
  131. package/core/app/index.js +632 -0
  132. package/core/app/script.tpl.js +52 -0
  133. package/core/channel/index.js +899 -0
  134. package/core/channel/matcher.js +585 -0
  135. package/core/com/config.tpl.json +16 -0
  136. package/core/com/index.js +74 -0
  137. package/core/com/script.tpl.js +5 -0
  138. package/core/component/component.js +42 -0
  139. package/core/component/config.tpl.json +63 -0
  140. package/core/component/index.js +273 -0
  141. package/core/component/script.tpl.js +19 -0
  142. package/core/controller/config.tpl.json +14 -0
  143. package/core/controller/index.js +373 -0
  144. package/core/controller/script.tpl.js +27 -0
  145. package/core/factory/config.tpl.json +14 -0
  146. package/core/factory/entity.js +275 -0
  147. package/core/factory/index.js +241 -0
  148. package/core/factory/script.tpl.js +16 -0
  149. package/core/game/bat/index.js +137 -0
  150. package/core/game/bat/world.js +622 -0
  151. package/core/game/config.tpl.json +16 -0
  152. package/core/game/entity_admin.js +230 -0
  153. package/core/game/index.js +186 -0
  154. package/core/handler/config.tpl.json +22 -0
  155. package/core/handler/index.js +181 -0
  156. package/core/handler/script.tpl.js +23 -0
  157. package/core/logic/config.tpl.json +14 -0
  158. package/core/logic/index.js +59 -0
  159. package/core/logic/script.tpl.js +19 -0
  160. package/core/middleware/config.tpl.json +16 -0
  161. package/core/middleware/index.js +125 -0
  162. package/core/middleware/script.tpl.js +37 -0
  163. package/core/mod/config.tpl.json +22 -0
  164. package/core/mod/index.js +130 -0
  165. package/core/mod/script.tpl.js +34 -0
  166. package/core/model/config.tpl.json +219 -0
  167. package/core/model/index.js +272 -0
  168. package/core/model/model.js +27 -0
  169. package/core/model/script.tpl.js +20 -0
  170. package/core/notifier/config.tpl.json +14 -0
  171. package/core/notifier/index.js +77 -0
  172. package/core/notifier/script.tpl.js +20 -0
  173. package/core/plugin/config.tpl.json +24 -0
  174. package/core/plugin/index.js +232 -0
  175. package/core/plugin/script.tpl.js +51 -0
  176. package/core/pusher/config.tpl.json +14 -0
  177. package/core/pusher/index.js +161 -0
  178. package/core/pusher/script.tpl.js +20 -0
  179. package/core/room/bat/index.js +170 -0
  180. package/core/room/bat/room.js +524 -0
  181. package/core/room/config.tpl.json +20 -0
  182. package/core/room/index.js +249 -0
  183. package/core/room/room.js +61 -0
  184. package/core/scene/config.tpl.json +14 -0
  185. package/core/scene/index.js +466 -0
  186. package/core/scene/loop.js +1255 -0
  187. package/core/scene/map.js +28 -0
  188. package/core/scene/script.tpl.js +22 -0
  189. package/core/sender/config.tpl.json +14 -0
  190. package/core/sender/index.js +79 -0
  191. package/core/sender/script.tpl.js +20 -0
  192. package/core/service/config.tpl.json +14 -0
  193. package/core/service/index.js +100 -0
  194. package/core/service/script.tpl.js +25 -0
  195. package/core/store/config.tpl.json +26 -0
  196. package/core/store/index.js +1755 -0
  197. package/core/store/script.tpl.js +22 -0
  198. package/core/store/sql.js +1464 -0
  199. package/core/system/config.tpl.json +18 -0
  200. package/core/system/index.js +312 -0
  201. package/core/system/script.tpl.js +77 -0
  202. package/core/view/config.tpl.json +14 -0
  203. package/core/view/index.js +91 -0
  204. package/core/view/script.tpl.js +20 -0
  205. package/core/zone/bat/index.js +725 -0
  206. package/core/zone/config.tpl.json +54 -0
  207. package/core/zone/index.js +614 -0
  208. package/core/zone/script.tpl.js +10 -0
  209. package/core/zone/zone_bat.js +136 -0
  210. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +24 -0
  211. package/index.js +17 -314
  212. package/os.js +57 -0
  213. package/package.json +60 -58
  214. package/server.js +598 -0
  215. package/README.en.md +0 -36
  216. package/conf.json +0 -3
  217. package/core/base/mqtt/index.js +0 -1107
  218. package/core/base/mqtt/lib.js +0 -40
  219. package/core/base/web/index.js +0 -243
  220. package/core/com/api/com.json +0 -4
  221. package/core/com/api/drive.js +0 -668
  222. package/core/com/api/index.js +0 -108
  223. package/core/com/api/oauth.js +0 -158
  224. package/core/com/api/script.js +0 -32
  225. package/core/com/app/README.md +0 -3
  226. package/core/com/app/com.json +0 -4
  227. package/core/com/app/config.tpl.json +0 -16
  228. package/core/com/app/drive.js +0 -309
  229. package/core/com/app/index.js +0 -211
  230. package/core/com/app/script.js +0 -155
  231. package/core/com/cmd/com.json +0 -4
  232. package/core/com/cmd/config.tpl.json +0 -66
  233. package/core/com/cmd/drive.js +0 -513
  234. package/core/com/cmd/index.js +0 -354
  235. package/core/com/cmd/old/5w2h.js +0 -54
  236. package/core/com/cmd/old/drive.js +0 -423
  237. package/core/com/cmd/script.js +0 -11
  238. package/core/com/component/README.md +0 -3
  239. package/core/com/component/com.json +0 -4
  240. package/core/com/component/component.html +0 -16
  241. package/core/com/component/drive.js +0 -197
  242. package/core/com/component/index.js +0 -312
  243. package/core/com/component/script.js +0 -18
  244. package/core/com/db/com.json +0 -4
  245. package/core/com/db/drive.js +0 -1160
  246. package/core/com/db/index.js +0 -176
  247. package/core/com/event/com.json +0 -4
  248. package/core/com/event/drive.js +0 -133
  249. package/core/com/event/index.js +0 -345
  250. package/core/com/event/script.js +0 -26
  251. package/core/com/eventer/com.js +0 -477
  252. package/core/com/eventer/com.json +0 -4
  253. package/core/com/middleware/com.js +0 -153
  254. package/core/com/middleware/com.json +0 -4
  255. package/core/com/middleware/config.tpl.json +0 -8
  256. package/core/com/middleware/script.js +0 -9
  257. package/core/com/mqtt/com.json +0 -4
  258. package/core/com/mqtt/drive.js +0 -600
  259. package/core/com/mqtt/index.js +0 -572
  260. package/core/com/mqtt/mm_mqtt.js +0 -330
  261. package/core/com/mqtt/script.js +0 -604
  262. package/core/com/msg/com.js +0 -296
  263. package/core/com/msg/com.json +0 -4
  264. package/core/com/nav/com.json +0 -4
  265. package/core/com/nav/config.tpl.json +0 -75
  266. package/core/com/nav/drive.js +0 -549
  267. package/core/com/nav/index.js +0 -182
  268. package/core/com/nav/tpl/admin_pc/page_form.vue +0 -180
  269. package/core/com/nav/tpl/admin_pc/page_view.vue +0 -124
  270. package/core/com/nav/tpl/dev_pc/page_default.vue +0 -247
  271. package/core/com/nav/tpl/dev_pc/page_type.vue +0 -313
  272. package/core/com/nav/tpl/home_pc/page_default.vue +0 -234
  273. package/core/com/nav/tpl/home_pc/page_form.vue +0 -137
  274. package/core/com/nav/tpl/home_pc/page_list.vue +0 -234
  275. package/core/com/nav/tpl/home_pc/page_nav.vue +0 -221
  276. package/core/com/nav/tpl/home_pc/page_type.vue +0 -234
  277. package/core/com/nav/tpl/home_pc/page_view.vue +0 -125
  278. package/core/com/nav/tpl/home_phone/page_channel.vue +0 -234
  279. package/core/com/nav/tpl/home_phone/page_default.vue +0 -234
  280. package/core/com/nav/tpl/home_phone/page_form.vue +0 -137
  281. package/core/com/nav/tpl/home_phone/page_nav.vue +0 -237
  282. package/core/com/nav/tpl/home_phone/page_type.vue +0 -234
  283. package/core/com/nav/tpl/home_phone/page_view.vue +0 -125
  284. package/core/com/nav/viewmodel.js +0 -296
  285. package/core/com/param/drive.js +0 -366
  286. package/core/com/param/index.js +0 -80
  287. package/core/com/param/script.js +0 -12
  288. package/core/com/param/test.js +0 -98
  289. package/core/com/plugin/README.md +0 -3
  290. package/core/com/plugin/com.json +0 -4
  291. package/core/com/plugin/config.tpl.json +0 -26
  292. package/core/com/plugin/drive.js +0 -536
  293. package/core/com/plugin/index.js +0 -259
  294. package/core/com/plugin/script.js +0 -213
  295. package/core/com/rpc/com.json +0 -4
  296. package/core/com/rpc/drive.js +0 -160
  297. package/core/com/rpc/index.js +0 -87
  298. package/core/com/rpc/rpc.js +0 -118
  299. package/core/com/socket/com.json +0 -4
  300. package/core/com/socket/config.tpl.json +0 -14
  301. package/core/com/socket/drive.js +0 -403
  302. package/core/com/socket/index.js +0 -62
  303. package/core/com/socket/script.js +0 -42
  304. package/core/com/sql/drive.js +0 -1087
  305. package/core/com/sql/index.js +0 -83
  306. package/core/com/sql/script.js +0 -48
  307. package/core/com/static/com.json +0 -4
  308. package/core/com/static/drive.js +0 -220
  309. package/core/com/static/index.js +0 -149
  310. package/core/com/static/script.js +0 -28
  311. package/core/com/task/com.json +0 -4
  312. package/core/com/task/drive.js +0 -403
  313. package/core/com/task/index.js +0 -110
  314. package/core/com/task/script.js +0 -37
  315. package/core/com/timer/com.js +0 -217
  316. package/core/com/timer/com.json +0 -4
  317. package/core/com/tpl/com.js +0 -19
  318. package/core/com/tpl/com.json +0 -4
  319. package/lib/actions.js +0 -50
  320. package/lib/base.js +0 -361
  321. package/lib/com.js +0 -29
  322. package/lib/ref.js +0 -121
  323. package/middleware/mqtt_base/index.js +0 -10
  324. package/middleware/mqtt_base/middleware.json +0 -10
  325. package/middleware/performance/index.js +0 -151
  326. package/middleware/performance/middleware.json +0 -16
  327. package/middleware/security_audit/index.js +0 -549
  328. package/middleware/security_audit/middleware.json +0 -48
  329. package/middleware/security_headers/index.js +0 -487
  330. package/middleware/security_headers/middleware.json +0 -45
  331. package/middleware/waf/index.js +0 -348
  332. package/middleware/waf/middleware.json +0 -10
  333. package/middleware/waf_ddos/index.js +0 -520
  334. package/middleware/waf_ddos/middleware.json +0 -38
  335. package/middleware/waf_ip/index.js +0 -379
  336. package/middleware/waf_ip/middleware.json +0 -49
  337. package/middleware/waf_xss/index.js +0 -269
  338. package/middleware/waf_xss/middleware.json +0 -18
  339. package/middleware/web_after/index.js +0 -33
  340. package/middleware/web_after/middleware.json +0 -9
  341. package/middleware/web_base/index.js +0 -90
  342. package/middleware/web_base/middleware.json +0 -9
  343. package/middleware/web_before/index.js +0 -27
  344. package/middleware/web_before/middleware.json +0 -9
  345. package/middleware/web_check/index.js +0 -28
  346. package/middleware/web_check/middleware.json +0 -9
  347. package/middleware/web_main/index.js +0 -28
  348. package/middleware/web_main/middleware.json +0 -9
  349. package/middleware/web_proxy/index.js +0 -37
  350. package/middleware/web_proxy/middleware.json +0 -9
  351. package/middleware/web_render/index.js +0 -87
  352. package/middleware/web_render/middleware.json +0 -9
  353. package/middleware/web_socket/index.js +0 -34
  354. package/middleware/web_socket/middleware.json +0 -9
  355. package/middleware/web_static/index.js +0 -115
  356. package/middleware/web_static/middleware.json +0 -9
  357. /package/{core/com → com}/api/README.md +0 -0
  358. /package/{core/com → com}/db/README.md +0 -0
  359. /package/{core/com → com}/event/README.md +0 -0
  360. /package/{core/com → com}/mqtt/README.md +0 -0
  361. /package/{core/com → com}/nav/README.md +0 -0
  362. /package/{core/com → com}/nav/tpl/admin_pc/page_channel.vue +0 -0
  363. /package/{core/com → com}/nav/tpl/admin_pc/page_default.vue +0 -0
  364. /package/{core/com → com}/nav/tpl/admin_pc/page_lang.vue +0 -0
  365. /package/{core/com → com}/nav/tpl/admin_pc/page_nav.vue +0 -0
  366. /package/{core/com → com}/nav/tpl/admin_pc/page_table.vue +0 -0
  367. /package/{core/com → com}/nav/tpl/admin_pc/page_type.vue +0 -0
  368. /package/{core/com → com}/nav/tpl/dev_pc/page_channel.vue +0 -0
  369. /package/{core/com → com}/nav/tpl/dev_pc/page_config.vue +0 -0
  370. /package/{core/com → com}/nav/tpl/dev_pc/page_form.vue +0 -0
  371. /package/{core/com → com}/nav/tpl/dev_pc/page_nav.vue +0 -0
  372. /package/{core/com → com}/nav/tpl/dev_pc/page_table.vue +0 -0
  373. /package/{core/com → com}/nav/tpl/home_pc/page_channel.vue +0 -0
  374. /package/{core/com → com}/nav/tpl/home_phone/page_list.vue +0 -0
  375. /package/{core/com → com}/param/README.md +0 -0
  376. /package/{core/com/cmd → com/pendant}/README.md +0 -0
  377. /package/{core/com → com}/socket/README.md +0 -0
  378. /package/{core/com → com}/sql/README.md +0 -0
  379. /package/{core/com → com}/static/README.md +0 -0
  380. /package/{core/com → com}/task/README.md +0 -0
@@ -0,0 +1,230 @@
1
+ const {
2
+ Drive
3
+ } = require('mm_machine');
4
+
5
+ /**
6
+ * 实体管理器
7
+ */
8
+ class EntityAdmin extends Drive {
9
+ /**
10
+ * 构造函数
11
+ * @param {object} config 配置对象
12
+ */
13
+ constructor(config) {
14
+ super(config);
15
+ this._entities = new Map();
16
+ this._componentIndex = new Map();
17
+ }
18
+ }
19
+
20
+ /**
21
+ * 初始化核心组件
22
+ * @param {Map} entities 实体集合
23
+ * @param {object} factory 实体工厂
24
+ * @param {object} eventer 事件总线
25
+ * @param {object} logger 日志记录器
26
+ */
27
+ EntityAdmin.prototype._initCore = async function (entities, factory, eventer, logger) {
28
+ if (!entities) {
29
+ this.log('error', '实体集合为空');
30
+ return;
31
+ }
32
+ if (logger) {
33
+ this.setLogger(logger);
34
+ }
35
+ if (eventer) {
36
+ this.getEventer = function () {
37
+ return eventer;
38
+ };
39
+ }
40
+ if (factory) {
41
+ this._factory = factory;
42
+ }
43
+ this._entities = entities;
44
+ };
45
+
46
+ /**
47
+ * 获取所有实体
48
+ * @returns {Iterator} 实体迭代器
49
+ */
50
+ EntityAdmin.prototype.getEntities = function () {
51
+ return this._entities.values();
52
+ };
53
+
54
+ /**
55
+ * 获取实体
56
+ * @param {string} id 实体ID
57
+ * @returns {object} 实体
58
+ */
59
+ EntityAdmin.prototype.getEntity = function (id) {
60
+ return this._entities.get(id);
61
+ };
62
+
63
+ /**
64
+ * 添加实体
65
+ * @param {object} entity 实体
66
+ */
67
+ EntityAdmin.prototype.addEntity = function (entity) {
68
+ this._entities.set(entity.getId(), entity);
69
+ this._updateComponentIndex(entity);
70
+ // 触发实体创建事件
71
+ this.emitEvent('entity_create', {
72
+ entity_id: entity.getId(),
73
+ entity_type: entity.type || 'unknown',
74
+ timestamp: Date.now()
75
+ });
76
+ };
77
+
78
+ /**
79
+ * 更新组件索引
80
+ * @param {object} entity 实体
81
+ * @returns {void}
82
+ */
83
+ EntityAdmin.prototype._updateComponentIndex = function (entity) {
84
+ for (let [compName] of entity._component) {
85
+ if (!this._componentIndex.has(compName)) {
86
+ this._componentIndex.set(compName, new Set());
87
+ }
88
+ this._componentIndex.get(compName).add(entity.getId());
89
+ }
90
+ };
91
+
92
+ /**
93
+ * 删除实体
94
+ * @param {string} id 实体ID
95
+ */
96
+ EntityAdmin.prototype.delEntity = function (id) {
97
+ let entity = this._entities.get(id);
98
+ if (entity) {
99
+ // 触发实体销毁事件
100
+ this.emitEvent('entity_destroy', {
101
+ entity_id: id,
102
+ entity_type: entity.type || 'unknown',
103
+ timestamp: Date.now()
104
+ });
105
+ this._delCompIndex(entity);
106
+ this._entities.delete(id);
107
+ }
108
+ };
109
+
110
+ /**
111
+ * 从组件索引中移除实体
112
+ * @param {object} entity 实体
113
+ * @returns {void}
114
+ */
115
+ EntityAdmin.prototype._delCompIndex = function (entity) {
116
+ for (let [compName] of entity._component) {
117
+ let index = this._componentIndex.get(compName);
118
+ if (index) {
119
+ index.delete(entity.getId());
120
+ if (index.size === 0) {
121
+ this._componentIndex.delete(compName);
122
+ }
123
+ }
124
+ }
125
+ };
126
+
127
+ /**
128
+ * 检查实体是否存在
129
+ * @param {string} id 实体ID
130
+ * @returns {boolean} 是否存在
131
+ */
132
+ EntityAdmin.prototype.hasEntity = function (id) {
133
+ return this._entities.has(id);
134
+ };
135
+
136
+ /**
137
+ * 创建实体
138
+ * @param {object} model 实体配置
139
+ * @returns {object} 实体
140
+ */
141
+ EntityAdmin.prototype.createEntity = function (model) {
142
+ var factory = this._factory[model.type];
143
+ if (!factory) {
144
+ this.log('error', `实体类型 ${model.type} 不存在`);
145
+ return null;
146
+ }
147
+ let entity = factory.create(model);
148
+ this.addEntity(entity);
149
+ return entity;
150
+ };
151
+
152
+ /**
153
+ * 删除实体组件
154
+ * @param {object} entity 实体
155
+ * @param {string} name 组件类型
156
+ */
157
+ EntityAdmin.prototype.delComponent = function (entity, name) {
158
+ entity.delComponent(name);
159
+ // 触发实体组件移除事件
160
+ this.emitEvent('entity_component_del', {
161
+ entity_id: entity.getId(),
162
+ component: name,
163
+ timestamp: Date.now()
164
+ });
165
+ };
166
+
167
+ /**
168
+ * 获取实体组件
169
+ * @param {object} entity 实体
170
+ * @param {string} name 组件类型
171
+ * @returns {object} 组件
172
+ */
173
+ EntityAdmin.prototype.getComponent = function (entity, name) {
174
+ return entity.getComponent(name);
175
+ };
176
+
177
+ /**
178
+ * 添加实体组件
179
+ * @param {object} entity 实体
180
+ * @param {string} name 组件类型
181
+ */
182
+ EntityAdmin.prototype.addComponent = function (entity, name) {
183
+ let component = this._component[name].create();
184
+ entity.addComponent(name, component);
185
+ // 触发实体组件添加事件
186
+ this.emitEvent('entity_component_add', {
187
+ entity_id: entity.getId(),
188
+ component: name,
189
+ timestamp: Date.now()
190
+ });
191
+ };
192
+
193
+ /**
194
+ * 查询包含特定组件的实体
195
+ * @param {Array} names 组件类型数组
196
+ * @returns {Array} 实体数组
197
+ */
198
+ EntityAdmin.prototype.getByComponents = function (names) {
199
+ if (!names || names.length === 0) {
200
+ return [];
201
+ }
202
+
203
+ let first_comp = names[0];
204
+ let candidates = this._componentIndex.get(first_comp);
205
+ if (!candidates) {
206
+ return [];
207
+ }
208
+
209
+ let result = [];
210
+ for (let entity_id of candidates) {
211
+ let entity = this._entities.get(entity_id);
212
+ if (entity) {
213
+ let has_all = true;
214
+ for (let i = 1; i < names.length; i++) {
215
+ if (!entity.hasComponent(names[i])) {
216
+ has_all = false;
217
+ break;
218
+ }
219
+ }
220
+ if (has_all) {
221
+ result.push(entity);
222
+ }
223
+ }
224
+ }
225
+ return result;
226
+ };
227
+
228
+ module.exports = {
229
+ EntityAdmin
230
+ };
@@ -0,0 +1,186 @@
1
+ const {
2
+ Drive,
3
+ Manager
4
+ } = require('mm_machine');
5
+ const {
6
+ System
7
+ } = require('../system/index.js');
8
+ const {
9
+ Logic
10
+ } = require('../logic/index.js');
11
+ const {
12
+ Factory
13
+ } = require('../factory/index.js');
14
+ const {
15
+ Component
16
+ } = require('../component/index.js');
17
+ const {
18
+ Scene
19
+ } = require('../scene/index.js');
20
+
21
+ /**
22
+ * 游戏
23
+ */
24
+ class Game extends Drive {
25
+ static config = {
26
+ name: 'default',
27
+ };
28
+
29
+ /**
30
+ * 构造函数
31
+ * @param {object} config 游戏配置
32
+ * @param {object} parent 父驱动
33
+ */
34
+ constructor(config, parent = null) {
35
+ super({...Game.config, ...config }, parent);
36
+ // // 游戏场景集合
37
+ // this.scene = {};
38
+ // 游戏系统集合
39
+ this.system = {};
40
+ // 游戏逻辑集合
41
+ this.logic = {};
42
+ // 游戏工厂集合
43
+ this.factory = {};
44
+ // 游戏组件集合
45
+ this.component = {};
46
+
47
+ // 管理器集合
48
+ this.manager = {};
49
+ }
50
+ }
51
+
52
+ // /**
53
+ // * 预设游戏配置
54
+ // */
55
+ // Game.prototype._preset = function () {
56
+ // this.file = null;
57
+ // this.sql = null;
58
+ // this.cache = null;
59
+ // };
60
+
61
+ /**
62
+ * 初始化游戏核心
63
+ * @param {object} server 游戏服务器
64
+ * @param {object} eventer 事件分发器
65
+ * @param {object} logger 日志记录器
66
+ */
67
+ Game.prototype._initCore = async function (server, eventer, logger) {
68
+ if (!server) {
69
+ throw new TypeError('游戏服务器不能为空');
70
+ }
71
+ else {
72
+ this.getServer = function () {
73
+ return server;
74
+ };
75
+ }
76
+ if (logger) {
77
+ this.setLogger(logger);
78
+ }
79
+ if (eventer) {
80
+ this.getEventer = function () {
81
+ return eventer;
82
+ };
83
+ }
84
+ // 初始化管理
85
+ await this._initManager();
86
+ // 加载资源
87
+ await this._loadSources();
88
+ // 初始化资源
89
+ await this._initSources();
90
+ };
91
+
92
+ /**
93
+ * 初始化管理器集合
94
+ * @returns {Promise<void>} 初始化完成
95
+ */
96
+ Game.prototype._initManager = async function () {
97
+ // 创建所有管理器实例
98
+ var component = this._createManager('component', '组件', Component);
99
+ var factory = this._createManager('factory', '工厂', Factory);
100
+ var logic = this._createManager('logic', '逻辑', Logic);
101
+ var system = this._createManager('system', '系统', System);
102
+ var scene = this._createManager('scene', '场景', Scene);
103
+
104
+ // 并行执行所有管理器的初始化操作
105
+ var initPromises = [
106
+ component.do('init'),
107
+ factory.do('init'),
108
+ logic.do('init'),
109
+ system.do('init'),
110
+ scene.do('init'),
111
+ ];
112
+
113
+ await Promise.all(initPromises);
114
+ };
115
+
116
+ /**
117
+ * 创建管理器实例
118
+ * @param {string} name 管理器名称
119
+ * @param {string} title 管理器标题
120
+ * @param {Function} cls 管理器类
121
+ * @returns {object} 管理器实例
122
+ */
123
+ Game.prototype._createManager = function (name, title, cls) {
124
+ let dir = this._getDir();
125
+ var manager = new Manager({
126
+ name: name,
127
+ title: title,
128
+ filename: name + '.json',
129
+ tpl_dir: `../${name}/`.fullname(__dirname),
130
+ base_dir: '',
131
+ dir: `./${name}`.fullname(dir)
132
+ }, this, this[name], cls);
133
+ this.manager[name] = manager;
134
+ return manager;
135
+ };
136
+
137
+ /**
138
+ * 加载资源
139
+ */
140
+ Game.prototype._loadSources = async function () {
141
+ // 加载无需依赖关系,所以可以同时加载
142
+ await Promise.all([
143
+ this.manager.component.runAll('load'),
144
+ this.manager.factory.runAll('load'),
145
+ this.manager.logic.runAll('load'),
146
+ this.manager.system.runAll('load'),
147
+ this.manager.scene.runAll('load')
148
+ ]);
149
+ };
150
+
151
+ /**
152
+ * 初始化资源
153
+ */
154
+ Game.prototype._initSources = async function () {
155
+ // 初始化顺序 scene -> system -> logic -> factory -> entity -> component ,需要依赖下级,所以自下而上初始化
156
+ await this.manager.component.runAll('init', this.getLogger());
157
+ await this.manager.factory.runAll('init', this.component, this.getEventer(), this.getLogger());
158
+ await this.manager.logic.runAll('init', this.getLogger());
159
+ await this.manager.system.runAll('init', this, this.getEventer(), this.getLogger());
160
+ await this.manager.scene.runAll('init', this, this.getEventer(), this.getLogger());
161
+ };
162
+
163
+ /**
164
+ * 启动核心
165
+ */
166
+ Game.prototype._startCore = async function () {
167
+ // 启动资源
168
+ await this._startSources();
169
+ };
170
+
171
+ /**
172
+ * 启动资源
173
+ */
174
+ Game.prototype._startSources = async function () {
175
+ // 并行启动游戏系统和场景
176
+ await Promise.all([
177
+ // 启动游戏系统
178
+ this.manager.system.runAll('start'),
179
+ // 启动游戏场景
180
+ this.manager.scene.runAll('start')
181
+ ]);
182
+ };
183
+
184
+ module.exports = {
185
+ Game
186
+ };
@@ -0,0 +1,22 @@
1
+ {
2
+ // 名称
3
+ "name": "${name}",
4
+ // 标题
5
+ "title": "${title || '示例处理器'}",
6
+ // 描述
7
+ "description": "${description || '介绍该处理器有什么用'}",
8
+ // 主文件
9
+ "main": "${main || 'index.js'}",
10
+ // 作用域,决定加载到什么区域
11
+ "scope": "${scope || 'server'}",
12
+ // 状态 0:禁用 1:启用
13
+ "state": 1,
14
+ // 排序,越小越靠前
15
+ "sort": 100,
16
+ // 依赖的领域服务
17
+ "services": [],
18
+ // 依赖的仓储
19
+ "stores": [],
20
+ // 依赖的富血模型
21
+ "models": []
22
+ }
@@ -0,0 +1,181 @@
1
+ const {
2
+ Drive
3
+ } = require('mm_machine');
4
+
5
+ /**
6
+ * 应用处理器基类(应用服务层)
7
+ * 负责处理请求,调用服务层,返回响应
8
+ */
9
+ class Handler extends Drive {
10
+ static config = {
11
+ name: 'default',
12
+ title: '处理器',
13
+ // 依赖的服务
14
+ services: [],
15
+ // 依赖的仓储
16
+ stores: [],
17
+ // 依赖的富血模型
18
+ models: [],
19
+ timeout: 30000,
20
+ perf_log: false, // 是否记录性能日志
21
+ };
22
+ /**
23
+ * 构造函数
24
+ * @param {object} config 配置参数
25
+ * @param {object} parent 父对象
26
+ */
27
+ constructor(config, parent) {
28
+ super({ ...Handler.config, ...config || {} }, parent);
29
+
30
+ // 仓储管理集合
31
+ this._store = {};
32
+ // 服务管理集合
33
+ this._service = {};
34
+ // 富血模型管理集合
35
+ this._model = {};
36
+ }
37
+ }
38
+
39
+ /**
40
+ * 预设
41
+ */
42
+ Handler.prototype._preset = function () {
43
+ /** === 基础设施 === */
44
+ // 文件管理器
45
+ this._filer = null;
46
+
47
+ // 消息推送器
48
+ this._sender = null;
49
+ // 广播器
50
+ this._pusher = null;
51
+ // 通知器
52
+ this._notifier = null;
53
+ };
54
+
55
+ /**
56
+ * 获取模板目录
57
+ * @returns {string} 模板目录
58
+ */
59
+ Handler.prototype.getTplDir = function () {
60
+ return __dirname;
61
+ };
62
+
63
+ /**
64
+ * 初始化依赖检查
65
+ */
66
+ Handler.prototype._checkDeps = function () {
67
+ let {
68
+ services,
69
+ stores,
70
+ models
71
+ } = this.config;
72
+
73
+ for (var i = 0; i < services.length; i++) {
74
+ var name = services[i];
75
+ if (!this._service[name]) {
76
+ this.log('error', `${name}服务(service)未找到`);
77
+ }
78
+ }
79
+
80
+ for (var i = 0; i < stores.length; i++) {
81
+ var name = stores[i];
82
+ if (!this._store[name]) {
83
+ this.log('error', `${name}仓储(store)未找到`);
84
+ }
85
+ }
86
+
87
+ for (var i = 0; i < models.length; i++) {
88
+ var name = models[i];
89
+ if (!this._model[name]) {
90
+ this.log('error', `${name}模型(model)未找到`);
91
+ }
92
+ }
93
+ };
94
+
95
+ /**
96
+ * 初始化核心
97
+ * @param {object} app 当前应用
98
+ * @param {object} eventer 事件总线
99
+ * @param {object} logger 日志管理器
100
+ */
101
+ Handler.prototype._initCore = async function (app, eventer, logger) {
102
+ // 初始化依赖项
103
+ if (logger) {
104
+ this.setLogger(logger);
105
+ }
106
+ if (eventer) {
107
+ this.getEventer = function () {
108
+ return eventer;
109
+ };
110
+ }
111
+
112
+ let {
113
+ store,
114
+ service,
115
+ model
116
+ } = app;
117
+ this._store = store;
118
+ this._service = service;
119
+ this._model = model;
120
+
121
+ // 检查依赖项
122
+ this._checkDeps();
123
+ };
124
+
125
+ /**
126
+ * 销毁处理器
127
+ */
128
+ Handler.prototype._destroy = async function () {
129
+ try {
130
+ // 清理资源
131
+ this._store = null;
132
+ this._service = null;
133
+ this._model = null;
134
+ this._db = null;
135
+ this._cache = null;
136
+ this._filer = null;
137
+
138
+ this._sender = null;
139
+ this._pusher = null;
140
+ this._notifier = null;
141
+
142
+ } catch (error) {
143
+ this.log('error', '销毁发生错误:', error);
144
+ }
145
+ };
146
+
147
+ /**
148
+ * 请求预处理方法
149
+ * @param {object} req 请求对象
150
+ * @param {object} res 响应对象
151
+ * @returns {Promise<void>}
152
+ */
153
+ Handler.prototype._before = async function (req, res) {
154
+ this.log('debug', '请求预处理', { req, res });
155
+ };
156
+
157
+ /**
158
+ * 请求主处理逻辑
159
+ * 核心业务逻辑应该在此方法中实现
160
+ * @param {object} req 请求对象
161
+ * @param {object} res 响应对象
162
+ * @returns {Promise<object>} 处理结果
163
+ */
164
+ Handler.prototype.main = async function (req, res) {
165
+ this.log('debug', '默认请求处理,子类应重写此方法', { req, res });
166
+ // 默认实现,子类必须重写
167
+ throw new Error('main 方法必须被重写');
168
+ };
169
+
170
+ /**
171
+ * 请求后处理方法
172
+ * @param {object} req 请求对象
173
+ * @param {object} res 响应对象
174
+ * @param {object} result 处理结果
175
+ * @returns {Promise<void>}
176
+ */
177
+ Handler.prototype._after = async function (req, res, result) {
178
+ this.log('debug', '请求后处理', { req, res, result });
179
+ };
180
+
181
+ exports.Handler = Handler;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 处理器(应用层)
3
+ */
4
+ module.exports = {
5
+ /**
6
+ * 初始化
7
+ * @param {object} app 应用实例
8
+ * @param {object} eventer 事件总线
9
+ */
10
+ async _init(app, eventer) {
11
+ // this.log('debug', `初始化!`);
12
+ // 可将监听写在这里
13
+ // eventer.on('');
14
+ },
15
+ /**
16
+ * 主要逻辑
17
+ * @param {...any} args 逻辑参数
18
+ */
19
+ async main(...args) {
20
+ this.log('debug', `主要逻辑`);
21
+ // 主要代码写在这
22
+ }
23
+ };
@@ -0,0 +1,14 @@
1
+ {
2
+ // 名称
3
+ "name": "${name}",
4
+ // 标题
5
+ "title": "${title || '战斗逻辑'}",
6
+ // 描述
7
+ "description": "${description || '定义战斗逻辑'}",
8
+ // 主文件
9
+ "main": "${main || 'index.js'}",
10
+ // 状态 0:禁用 1:启用
11
+ "state": 1,
12
+ // 排序,越小越靠前
13
+ "sort": 100
14
+ }
@@ -0,0 +1,59 @@
1
+ const { Drive } = require('mm_machine');
2
+
3
+ /**
4
+ * 游戏逻辑基类
5
+ * 负责处理游戏业务规则和玩法逻辑
6
+ * 负责初始化游戏逻辑的核心组件,如日志记录器、工厂等
7
+ * 确保在游戏逻辑开始前,所有必要的组件都已初始化并准备就绪
8
+ */
9
+ class Logic extends Drive {
10
+ static config = {
11
+ // 逻辑名称
12
+ name: 'default',
13
+ // 逻辑标题
14
+ title: '基础逻辑',
15
+ // 逻辑描述
16
+ description: '游戏逻辑基类,负责处理游戏业务规则和玩法逻辑',
17
+ // 入口脚本
18
+ script: './index.js',
19
+ // 状态 0:禁用 1:启用
20
+ state: 1,
21
+ // 排序,越小越靠前
22
+ sort: 0,
23
+ // 逻辑类型: core|module|system
24
+ type: 'system',
25
+ // 依赖的工厂组件
26
+ factorys: [],
27
+ };
28
+ /**
29
+ * 构造函数
30
+ * @param {object} config 配置参数
31
+ * @param {object} parent 父对象
32
+ */
33
+ constructor(config, parent) {
34
+ // 确保正确合并配置并传递给父类构造函数
35
+ super({ ...Logic.config, ...config || {} }, parent);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * 获取模板目录
41
+ * @returns {string} 模板目录
42
+ */
43
+ Logic.prototype.getTplDir = function() {
44
+ return __dirname;
45
+ };
46
+
47
+ /**
48
+ * 初始化核心
49
+ * @param {object} logger 日志记录器实例
50
+ * @returns {Promise<void>}
51
+ */
52
+ Logic.prototype._initCore = async function (logger) {
53
+ // 初始化依赖项
54
+ if (logger) {
55
+ this.setLogger(logger);
56
+ }
57
+ };
58
+
59
+ exports.Logic = Logic;