UNPKG

mm_os

Version:

MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。

241 lines (214 loc) 6.18 kB
const { Drive } = require('mm_machine'); const { Entity } = require('./entity.js'); /** * 实体管理器类 * 负责实体的创建、查询、更新和删除操作 * 管理实体与组件的关联关系 */ class Factory extends Drive { static config = { 'name': 'default', 'title': '工厂', 'description': '数据模型工厂类,负责创建和管理数据模型实例', 'main': '', 'components': [] }; /** * 构造函数 * @param {object} config 配置参数 */ constructor(config) { super({ ...Factory.config, ...config || {} }); // 组件注册表 this._component = {}; } } /** * 获取模板目录 * @returns {string} 模板目录 */ Factory.prototype.getTplDir = function () { return __dirname; }; /** * 初始化核心 * @param {object} component 组件注册表 * @param {object} eventer 事件管理器 * @param {object} logger 日志管理器 */ Factory.prototype._initCore = async function (component, eventer, logger) { // 初始化依赖项 if (logger) { this.setLogger(logger); } if (eventer) { this.getEventer = function () { return eventer; }; } if (component) { this._component = component; } this._checkDeps(); }; /** * 检查依赖项 */ Factory.prototype._checkDeps = function () { let { components } = this.config; components = components || []; for (let i = 0; i < components.length; i++) { let name = components[i]; if (!this._component[name]) { this.log('error', `组件 ${name} 不存在`); } } }; /** * 注册组件类型 * @param {string} name 组件类型 * @param {object} component 组件类 */ Factory.prototype.registerComponent = function (name, component) { // 参数校验 if (typeof name !== 'string') { throw new TypeError('组件类型必须是字符串'); } if (typeof component !== 'object') { throw new TypeError('组件类必须是对象'); } this._component[name] = component; }; /** * 注销组件类型 * @param {string} name 组件类型 * @returns {boolean} 是否注销成功 */ Factory.prototype.unregisterComponent = function (name) { // 参数校验 if (typeof name !== 'string') { throw new TypeError('组件类型必须是字符串'); } delete this._component[name]; return true; }; /** * 创建实体 * @param {object} model 实体模型 * @param {string} model.entity_id 实体ID - 可选,默认自动生成 * @param {string} model.player_id 玩家ID - 可选,默认空字符串 * @param {string} model.type 实体类型 - 可选,默认空字符串 * @returns {object} 实体对象 */ Factory.prototype.createEntity = function (model = {}) { let entity = new Entity(model.entity_id, model.player_id, model.type); let { components } = this.config; for (let i = 0; i < components.length; i++) { let name = components[i]; if (this._component[name]) { entity.addComponent(name, this._component[name].create(model[name])); } } // 触发实体创建事件 this.emitEvent('factory_entity_create', { factory_name: this.config.name, entity_id: entity.entity_id, entity_type: entity.type, timestamp: Date.now() }); return entity; }; /** * 设置实体模型 * @param {Entity} entity 实体对象 * @param {object} model 实体模型 */ Factory.prototype.setModel = function (entity, model) { // 初始化实体 entity.setModel(model); }; /** * 为实体添加组件 * @param {Entity} entity 实体对象 * @param {string} name 组件类型 * @param {object} component_data 组件数据 * @returns {boolean} 是否添加成功 */ Factory.prototype.addComponent = function (entity, name, component_data) { // 参数校验 if (typeof entity !== 'object' || !entity.isActive()) { throw new TypeError('实体必须是有效实体对象'); } if (typeof name !== 'string') { throw new TypeError('组件类型必须是字符串'); } if (typeof component_data !== 'object' || Array.isArray(component_data)) { throw new TypeError('组件数据必须是对象'); } let result = entity.addComponent(name, component_data); // 触发组件添加事件 this.emitEvent('factory_component_add', { factory_name: this.config.name, entity_id: entity.entity_id, component: name, timestamp: Date.now() }); return result; }; /** * 从实体移除组件 * @param {Entity} entity 实体对象 * @param {string} name 组件类型 * @returns {boolean} 是否移除成功 */ Factory.prototype.delComponent = function (entity, name) { // 参数校验 if (typeof entity !== 'object' || !entity.isActive()) { throw new TypeError('实体必须是有效实体对象'); } if (typeof name !== 'string') { throw new TypeError('组件类型必须是字符串'); } let result = entity.delComponent(name); // 触发组件移除事件 this.emitEvent('factory_component_del', { factory_name: this.config.name, entity_id: entity.entity_id, component: name, timestamp: Date.now() }); return result; }; /** * 创建模型 * @param {object} data 模型数据 * @returns {object} 模型对象 */ Factory.prototype.createModel = function (data) { if (typeof data !== 'object' || Array.isArray(data)) { throw new TypeError('模型数据必须是对象'); } let entity = new Entity(model.entity_id, model.player_id); // let { components } = this.config; return entity; }; /** * 创建实体 * @param {object} data 实体数据 * @returns {object} 实体对象 */ Factory.prototype.create = function (data) { if (typeof data !== 'object') { throw new TypeError('实体数据必须是对象'); } let entity = this.createEntity(data); this.addMethod(entity); return entity; }; /** * 为实体添加方法 * @param {Entity} entity 实体对象 */ Factory.prototype.addMethod = function (entity) { }; exports.Factory = Factory;