mm_os
Version:
MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。
312 lines (279 loc) • 7.39 kB
JavaScript
const { Drive } = require('mm_machine');
/**
* 系统基类
* 负责处理包含特定组件组合的实体
* 实现业务逻辑和游戏规则
*/
class System extends Drive {
static config = {
// 系统名称
'name': 'default',
// 系统标题
'title': '系统',
// 系统描述
'description': '系统基类,负责处理实体和组件的业务逻辑',
// 系统执行优先级
'sort': 100,
// 依赖逻辑列表
'logics': [],
// 依赖工厂列表
'factorys': []
};
/**
* 构造函数
* @param {object} config 配置参数
* @param {object} parent 父对象
*/
constructor(config, parent) {
super({ ...System.config, ...config || {} }, parent);
// 消息通道
this._channel = null;
// 工厂
this._factory = {};
// 逻辑管理
this._logic = {};
// 实体管理
this._entityAdmin = null;
// 系统是否启用
this._enabled = true;
}
}
/**
* 预设
*/
System.prototype._preset = function () {
// 系统执行优先级
this._priority = this.config.sort || 100;
};
/**
* 获取模板目录
* @returns {string} 模板目录
*/
System.prototype.getTplDir = function () {
return __dirname;
};
/**
* 初始化系统核心
* @param {object} channel 消息通道
* @param {object} eventer 事件管理器
* @param {object} logger 日志管理器
*/
System.prototype._initCore = async function (channel, eventer, logger) {
if (logger) {
this.setLogger(logger);
}
if (eventer) {
this.getEventer = function () {
return eventer;
};
}
// 初始化依赖项
if (channel) {
this._channel = channel;
this._factory = channel.factory;
this._logic = channel.logic;
this._entityAdmin = channel.entityAdmin;
}
// 检测依赖
this._checkDeps();
};
/**
* 检测依赖
*/
System.prototype._checkDeps = function () {
let { logics, factorys } = this.config;
logics = logics || [];
factorys = factorys || [];
// 检查逻辑依赖
for (let logic of logics) {
if (!this._logic[logic]) {
throw new Error(`系统 ${this.config.name} 依赖逻辑 ${logic} 未注册`);
}
}
// 检查工厂依赖
for (let factory of factorys) {
if (!this._factory[factory]) {
throw new Error(`系统 ${this.config.name} 依赖工厂 ${factory} 未注册`);
}
}
};
/**
* 获取系统依赖的逻辑对象
* @param {string} name 逻辑名称
* @returns {object} 逻辑对象
*/
System.prototype.getLogic = function (name) {
return this._logic[name];
};
/**
* 获取系统依赖的工厂对象
* @param {string} name 工厂名称
* @returns {object} 工厂对象
*/
System.prototype.getFactory = function (name) {
return this._factory[name];
};
/**
* 获取系统实体对象
* @param {string} name 实体名称
* @returns {object} 实体对象
*/
System.prototype.getEntity = function (name) {
return this._entityAdmin.get(name);
};
/**
* 获取系统执行优先级
* @returns {number} 优先级数值
*/
System.prototype.getPriority = function () {
return this._priority;
};
/**
* 设置系统执行优先级
* @param {number} priority 优先级数值
*/
System.prototype.setPriority = function (priority) {
// 参数校验
if (typeof priority !== 'number') {
throw new TypeError('优先级必须是数字');
}
this._priority = priority;
};
/**
* 检查系统是否启用
* @returns {boolean} 是否启用
*/
System.prototype.isEnabled = function () {
return this.status === 'running' && this._enabled;
};
/**
* 启用系统
*/
System.prototype.enable = function () {
this._enabled = true;
// 触发系统启用事件
this.emitEvent('system_enable', {
system_name: this.config.name,
timestamp: Date.now()
});
};
/**
* 禁用系统
*/
System.prototype.disable = function () {
this._enabled = false;
// 触发系统禁用事件
this.emitEvent('system_disable', {
system_name: this.config.name,
timestamp: Date.now()
});
};
/**
* 系统更新方法
* 子类必须重写此方法来实现具体的业务逻辑
* @param {number} delta_time 时间增量(毫秒)
*/
System.prototype.update = function (delta_time) {
if (typeof delta_time !== 'number') {
throw new TypeError('时间增量必须是数字');
}
// 触发系统更新事件
this.emitEvent('system_update', {
system_name: this.config.name,
delta_time: delta_time,
timestamp: Date.now()
});
let entities = this._entityAdmin.getByComponents(this.config.components);
for (let entity of entities) {
this._processEntity(entity, delta_time);
}
};
/**
* 处理单个实体
* 子类必须重写此方法来实现具体的实体处理逻辑
* @param {object} entity 实体对象
* @param {number} delta_time 时间增量(毫秒)
*/
System.prototype._processEntity = function (entity, delta_time) {
// 参数校验
if (!entity || typeof entity !== 'object') {
throw new TypeError('实体对象必须是有效的对象');
}
if (typeof delta_time !== 'number') {
throw new TypeError('时间增量必须是数字');
}
// 默认实现:记录实体处理
this.log('info', `系统 ${this.config.name} 处理实体 ${entity.entity_id}`);
};
/**
* 获取系统处理的实体数量
* @returns {number} 实体数量
*/
System.prototype.getEntityCount = function () {
let entities = this._entityAdmin.getByComponents(this.config.components);
return entities.length;
};
/**
* 系统清理方法
* 子类可以重写此方法来实现系统清理逻辑
*/
System.prototype.cleanup = function () {
// 默认实现:记录系统清理
this.log('info', `系统 ${this.config.name} 清理完成`);
};
/**
* 获取系统状态信息
* @returns {object} 系统状态信息
*/
System.prototype.getStatus = function () {
return {
name: this.config.name,
title: this.config.title,
description: this.config.description,
enabled: this._enabled,
priority: this._priority,
entitys: this.getEntityCount()
};
};
/**
* 处理实体事件
* 子类可以重写此方法来实现事件处理逻辑
* @param {string} event_type 事件类型
* @param {object} event_data 事件数据
* @param {object} entity 实体对象
*/
System.prototype.handleEvent = function (event_type, event_data, entity) {
// 参数校验
if (typeof event_type !== 'string') {
throw new TypeError('事件类型必须是字符串');
}
if (typeof event_data !== 'object' || Array.isArray(event_data)) {
throw new TypeError('事件数据必须是对象');
}
if (!this.isEnabled()) {
return;
}
this.log('info', `系统 ${this.config.name} 处理事件 ${event_type} 实体 ${entity ? entity.entity_id : 'null'}`);
};
/**
* 系统预更新方法
* 在系统更新前调用,用于准备数据
* @param {number} delta_time 时间增量(毫秒)
*/
System.prototype.preUpdate = function (delta_time) {
if (typeof delta_time !== 'number') {
throw new TypeError('时间增量必须是数字');
}
};
/**
* 系统后更新方法
* 在系统更新后调用,用于清理数据
* @param {number} delta_time 时间增量(毫秒)
*/
System.prototype.postUpdate = function (delta_time) {
if (typeof delta_time !== 'number') {
throw new TypeError('时间增量必须是数字');
}
};
// 导出模块
exports.System = System;