mm_os
Version:
MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。
900 lines (782 loc) • 21.6 kB
JavaScript
const {
Drive
} = require('mm_machine');
const {
RoomAdmin
} = require('../room/index.js');
const {
Matcher
} = require('./matcher.js');
/**
* 游戏频道 - 平行世界管理器
* 负责管理频道的创建、销毁、玩家隔离和频道间通信
*/
class Channel extends Drive {
static config = {
'name': 'default',
'title': '默认频道',
'min_level': 0,
'max_level': 1000,
'max_players': 5000,
'type': 'normal',
'load_threshold': 0.8,
'main': '',
'match': {
'wait_time': 120000,
'lv_range_init': 5,
'lv_range_max': 20,
'lv_expand_int': 10000,
'interval': 1000
}
};
/**
* 构造函数
* @param {object} config 配置参数
*/
constructor(config) {
super({ ...Channel.config, ...config });
// 游戏世界集合
this.worlds = new Map();
}
}
/**
* 预置
*/
Channel.prototype._preset = function () {
this.channel_id = 0;
this.status = 1;
this.stats = {
player: 0,
room: 0,
world: 0,
message: 0,
last_update: Date.now()
};
this._monitorTimer = null;
this.roomAdmin = null;
this.matcher = null;
// 主世界ID
this._main_world_id = '';
};
/**
* 获取模板目录
* @returns {string} 模板目录
*/
Channel.prototype.getTplDir = function () {
return __dirname;
};
/**
* 初始化核心
* @param {object} zone 分区对象
* @param {object} eventer 事件管理器
* @param {object} logger 日志管理器
*/
Channel.prototype._initCore = async function (zone, eventer, logger) {
if (!zone) {
throw new TypeError('分区对象不能为空');
}
else {
this.getZone = function () {
return zone;
};
}
if (logger) {
this.setLogger(logger);
}
if (eventer) {
this.getEventer = function () {
return eventer;
};
}
await this._initManager();
await this._loadSources();
};
/**
* 加载世界
* @returns {Promise<void>}
*/
Channel.prototype._loadWorlds = async function () {
// 通过事件方式加载已创建的世界数据,避免直接操作数据库
var worlds = await this.emitWait('world_load', {
zone_name: this.getZone().config.name,
channel_id: this.channel_id
});
if (worlds && worlds.length > 0) {
// 遍历世界列表,创建世界对象
for (var i = 0; i < worlds.length; i++) {
var info = worlds[i];
await this.createWorld(info, info.world_id);
}
this.log('info', `加载世界成功,共 ${worlds.length} 个`);
}
// 加载所有游戏世界
var promises = [];
for (var o of this.worlds.values()) {
promises.push(o.do('load'));
}
await Promise.all(promises);
};
/**
* 初始化房间管理器
*/
Channel.prototype._initRoomAdmin = async function () {
this.roomAdmin = new RoomAdmin();
await this.roomAdmin.do('init', this);
};
/**
* 初始化匹配器
*/
Channel.prototype._initMatcher = async function () {
this.matcher = new Matcher(this.config.match, this);
await this.matcher.do('init', this);
};
/**
* 初始化管理器
*/
Channel.prototype._initManager = async function () {
// var dir = this.getDir().dirname().dirname();
// var game = new Manager({
// name: 'game',
// title: '游戏',
// filename: 'game.json',
// tpl_dir: `../game/`.fullname(__dirname),
// base_dir: '',
// // base_dir: `../../common/${name}/`.fullname(__dirname),
// dir: `./game`.fullname(dir)
// }, this, this.game, Game);
// await game.do('init');
// this.manager.game = game;
};
/**
* 获取游戏管理器目录
* @returns {string} 游戏管理器目录
*/
Channel.prototype.getDir = function () {
return this.getZone().getDir();
};
/**
* 加载资源
*/
Channel.prototype._loadSources = async function () {
await this.manager.game.runAll('load');
};
/**
* 设置主世界ID
* @param {string} world_id 主世界ID
*/
Channel.prototype.setMainWorld = function (world_id) {
this._main_world_id = world_id;
};
/**
* 初始化资源
*/
Channel.prototype._initSources = async function () {
await this.manager.game.runAll('init', this, this.getEventer(), this.getLogger());
};
/**
* 加载游戏房间
* @returns {Promise<void>}
*/
Channel.prototype._loadRooms = async function () {
// 通过事件方式加载已创建的房间数据,避免直接操作数据库
var rooms = await this.emitWait('room_load', {
zone_name: this.getZone().config.name,
channel_id: this.channel_id
});
if (rooms && rooms.length > 0) {
// 遍历房间列表,创建房间对象
for (var i = 0; i < rooms.length; i++) {
var info = rooms[i];
var room = await this.roomAdmin.create(info, info.owner);
// 恢复房间原有ID
if (info.room_id) {
this.roomAdmin._rooms.delete(room.room_id);
room.room_id = info.room_id;
this.roomAdmin._rooms.set(room.room_id, room);
}
// 恢复房间状态
if (info.state) {
room.state = info.state;
}
}
this.log('info', `加载房间成功,共 ${rooms.length} 个`);
}
// 加载所有游戏房间
var promises = [];
for (var o of this.rooms.values()) {
promises.push(o.do('load'));
}
await Promise.all(promises);
};
/**
* 启动核心
*/
Channel.prototype._startCore = async function () {
await this._initSources();
await this._loadWorlds();
await this._initRoomAdmin();
await this._loadRooms();
await this._initMatcher();
await this.manager.game.runAll('start');
await this.matcher.do('start');
this._startMonitor();
await this._startWorlds();
};
/**
* 启动游戏世界
*/
Channel.prototype._startWorlds = async function () {
var promises = [];
for (let o of this.worlds.values()) {
promises.push(o.do('start'));
}
await Promise.all(promises);
};
/**
* 初始化默认房间
*/
Channel.prototype._initRooms = async function () {
var promises = [];
for (let o of this.rooms.values()) {
promises.push(o.do('init', this, this.getEventer(), this.getLogger()));
}
await Promise.all(promises);
};
/**
* 启动频道监控
*/
Channel.prototype._startMonitor = function () {
this._monitorTimer = setInterval(() => {
this._updateStatus();
}, 30000);
};
/**
* 更新频道状态
*/
Channel.prototype._updateStatus = function () {
var player = this.player_ids.size;
var max_players = this.config.max_players;
var load_ratio = player / max_players;
if (load_ratio >= 1) {
this.status = 3;
} else if (load_ratio >= this.config.load_threshold) {
this.status = 2;
} else {
this.status = 1;
}
this.stats.player = player;
this.stats.room = this.rooms.size;
this.stats.world = this.worlds.size;
this.stats.last_update = Date.now();
};
/**
* 玩家进入频道
* @param {string} player_id 玩家ID
* @returns {object} 世界对象
*/
Channel.prototype.enter = function (player_id) {
if (!player_id) {
throw new TypeError('玩家ID不能为空');
}
if (this.player_ids.has(player_id)) {
throw new Error('玩家已在当前频道中');
}
var player = this.getZone().getPlayer(player_id);
if (!player) {
throw new Error(`玩家 ${player_id} 不存在`);
}
var {
min_level,
max_level,
status,
max_players
} = this.config;
this._validateStatus(status);
this._validateQualify(player, min_level, max_level, max_players);
try {
this.player_ids.set(player_id, true);
this.emitEvent('player_enter_channel', {
channel_id: this.config.name,
player_id: player_id,
player_level: player.level,
timestamp: Date.now()
});
let world = this.enterMainWorld(player_id);
return world;
} catch (error) {
this.player_ids.delete(player_id);
throw error;
}
};
/**
* 验证频道状态
* @param {number} status 频道状态
*/
Channel.prototype._validateStatus = function (status) {
if (status === 0) {
throw new Error('频道已关闭,无法进入');
}
if (status === 2) {
throw new Error('频道维护中,暂时无法进入');
}
if (status === 3) {
throw new Error('频道爆满,请稍后再试');
}
};
/**
* 检查玩家是否在频道中
* @param {string} player_id 玩家ID
* @returns {boolean} 是否在频道中
*/
Channel.prototype._hasPlayer = function (player_id) {
return this.player_ids.has(player_id);
};
/**
* 验证玩家资格
* @param {object} player 玩家对象
* @param {number} min_level 最低等级
* @param {number} max_level 最高等级
* @param {number} max_players 最大玩家数
*/
Channel.prototype._validateQualify = function (player, min_level, max_level, max_players) {
if (player.level < min_level || player.level > max_level) {
var msg = `境界不符,无法进入此频道(要求等级:${min_level}-${max_level})`;
throw new Error(msg);
}
if (this.player_ids.size >= max_players) {
throw new Error('频道人数已达上限,无法进入');
}
if (this._hasPlayer(player.player_id)) {
throw new Error('玩家已在当前频道中');
}
};
/**
* 获取默认房间
* @param {string} player_id 玩家ID
* @returns {object} 房间对象
*/
Channel.prototype.getDefaultRoom = async function (player_id) {
var player = this.getPlayer(player_id);
var room_name = player.returning ? 'main_room' : 'cultivation_area';
var room = this.rooms.get(room_name);
if (!room) {
throw new Error(`默认房间 ${room_name} 不存在`);
}
return room;
};
/**
* 玩家退出频道
* @param {string} player_id 玩家ID
* @returns {boolean} 是否成功退出
*/
Channel.prototype.leave = async function (player_id) {
if (!player_id) {
throw new TypeError('玩家ID不能为空');
}
if (!this.player_ids.has(player_id)) {
throw new Error('玩家不在当前频道中');
}
this.player_ids.delete(player_id);
this.emitEvent('player_leave_channel', {
channel_id: this.config.name,
player_id: player_id,
timestamp: Date.now()
});
return true;
};
/**
* 强制踢出玩家
* @param {string} player_id 玩家ID
* @param {string} reason 踢出原因
* @returns {boolean} 是否成功踢出
*/
Channel.prototype.kickPlayer = async function (player_id, reason = '系统维护') {
if (!player_id) {
throw new TypeError('玩家ID不能为空');
}
if (!this.player_ids.has(player_id)) {
throw new Error('玩家不在当前频道中');
}
this.player_ids.delete(player_id);
this.emitEvent('player_kicked_channel', {
channel_id: this.config.name,
player_id: player_id,
reason: reason,
timestamp: Date.now()
});
return true;
};
/**
* 获取玩家对象
* @param {string} player_id 玩家ID
* @returns {object} 玩家对象
*/
Channel.prototype.getPlayer = function (player_id) {
if (!player_id) {
throw new TypeError('玩家ID不能为空');
}
return this.getZone().getPlayer(player_id);
};
/**
* 获取所有玩家对象
* @returns {Array} 玩家对象列表
*/
Channel.prototype.getAllPlayers = function () {
var players = [];
for (var player_id of this.player_ids.keys()) {
var player = this.getPlayer(player_id);
players.push(player);
}
return players;
};
/**
* 创建房间
* @param {string} name 房间名称
* @param {string} player_id 玩家ID
* @returns {object} 创建的房间对象
*/
Channel.prototype.createRoom = async function (name, player_id) {
if (!name) {
throw new TypeError('房间名称不能为空');
}
if (!player_id) {
throw new TypeError('玩家ID不能为空');
}
var player = this.getPlayer(player_id);
if (!player) {
throw new Error(`玩家 ${player_id} 不存在`);
}
let config = {
name,
'min_level': player.level,
'max_level': player.level + 100
};
var room = await this.roomAdmin.create(config, player_id);
return room;
};
/**
* 销毁房间
* @param {string} room_id 房间ID
* @returns {boolean} 是否成功销毁
*/
Channel.prototype.destroyRoom = async function (room_id) {
if (!room_id) {
throw new TypeError('房间ID不能为空');
}
this.roomAdmin.del(room_id);
return true;
};
/**
* 玩家进入房间
* @param {string} room_id 房间ID
* @param {string} player_id 玩家ID
*/
Channel.prototype.enterRoom = function (room_id, player_id) {
this.roomAdmin.enter(room_id, player_id);
};
/**
* 玩家退出房间
* @param {string} room_id 房间ID
* @param {string} player_id 玩家ID
*/
Channel.prototype.leaveRoom = function (room_id, player_id) {
this.roomAdmin.leave(room_id, player_id);
};
/**
* 获取房间列表
* @returns {Array} 房间列表
*/
Channel.prototype.getRoomList = function () {
return this.roomAdmin.getList();
};
/**
* 玩家开始实时匹配
* @param {string} player_id 玩家ID
* @param {object} options 匹配选项
* @returns {object} 匹配状态
*/
Channel.prototype.startQuickMatch = function (player_id, options = {}) {
if (typeof player_id !== 'string') {
throw new TypeError('玩家ID必须是字符串');
}
var mode = options.mode || 'rank';
var format = options.format || '5v5';
var team_members = options.team_members || [player_id];
var team_size = team_members.length;
if (team_size > this._getMaxTeamSize(format)) {
throw new Error(`队伍人数超过限制: ${team_size}`);
}
for (var i = 0; i < team_members.length; i++) {
if (this.matcher.has(team_members[i])) {
throw new Error(`玩家 ${team_members[i]} 已在匹配池中`);
}
}
this.matcher.join(player_id, {
mode: mode,
format: format,
team_size: team_size,
team_members: team_members
});
return this.matcher.getMatchStatus(player_id);
};
/**
* 玩家取消实时匹配
* @param {string} player_id 玩家ID
* @returns {boolean} 是否成功取消
*/
Channel.prototype.cancelQuickMatch = function (player_id) {
if (typeof player_id !== 'string') {
throw new TypeError('玩家ID必须是字符串');
}
return this.matcher.leave(player_id);
};
/**
* 获取实时匹配状态
* @param {string} player_id 玩家ID
* @returns {object|null} 匹配状态
*/
Channel.prototype.getQuickMatchStatus = function (player_id) {
return this.matcher.getMatchStatus(player_id);
};
/**
* 获取匹配统计信息
* @returns {object} 统计信息
*/
Channel.prototype.getMatchStats = function () {
return {
waiting_count: this.matcher.getTotalWaiting(),
room_count: this.roomAdmin.getCount()
};
};
/**
* 获取最大队伍人数
* @param {string} format 比赛格式
* @returns {number} 最大队伍人数
*/
Channel.prototype._getMaxTeamSize = function (format) {
var sizes = {
'1v1': 1,
'3v3': 3,
'5v5': 5
};
return sizes[format] || 5;
};
/**
* 广播频道消息
* @param {object} data 广播数据
* @param {string} data_type 数据类型
* @returns {boolean} 是否广播成功
*/
Channel.prototype.broadcast = async function (data, data_type) {
if (!data) {
throw new TypeError('数据不能为空');
}
if (!data_type || typeof data_type !== 'string') {
throw new TypeError('数据类型必须是有效字符串');
}
// 触发广播事件,由业务层处理具体的广播逻辑
this.emitEvent('channel_broadcast', {
channel_id: this.config.name,
data: data,
data_type: data_type,
timestamp: Date.now()
});
return true;
};
/**
* 发送频道消息
* @param {string} player_id 发送者ID
* @param {string} content 消息内容
* @returns {boolean} 是否发送成功
*/
Channel.prototype.sendMessage = async function (player_id, content) {
if (!player_id) {
throw new TypeError('发送者ID不能为空');
}
if (!content || typeof content !== 'string') {
throw new TypeError('消息内容必须是有效字符串');
}
if (!this.player_ids.has(player_id)) {
throw new Error('发送者不在当前频道中');
}
var player = this.getPlayer(player_id);
var msg = {
sender_id: player_id,
sender_name: player.name || player_id,
content: content,
msg_type: 'text',
msg_group: 'channel',
timestamp: Date.now(),
channel_id: this.config.name
};
// 触发消息发送事件,由业务层处理具体的发送逻辑
this.emitEvent('channel_message_send', msg);
return true;
};
/**
* 公共数据传输方法
* @param {string} sender_id 发送者ID
* @param {object} data 传输数据
* @param {string} data_type 数据类型
* @returns {object} 传输结果
*/
Channel.prototype.send = async function (sender_id, data, data_type) {
// 触发数据发送事件,由业务层处理具体的发送逻辑
this.emitEvent('channel_data_send', {
channel_id: this.config.name,
sender_id: sender_id,
data: data,
data_type: data_type,
timestamp: Date.now()
});
// 返回默认结果
return {
players: this.player_ids.size,
success: 0,
fail: 0,
failed_players: []
};
};
/**
* 获取频道统计信息
* @returns {object} 统计信息
*/
Channel.prototype.getStats = function () {
return {
...this.stats,
channel_name: this.config.name,
channel_status: this.status,
channel_type: this.config.type,
current_time: Date.now()
};
};
/**
* 切换频道状态
* @param {number} new_status 新状态
* @returns {boolean} 是否切换成功
*/
Channel.prototype.switchStatus = function (new_status) {
if (typeof new_status !== 'number' || new_status < 0 || new_status > 3) {
throw new TypeError('频道状态必须是0-3之间的数字');
}
var old_status = this.status;
this.status = new_status;
this.emitEvent('channel_status_changed', {
channel_id: this.config.name,
old_status: old_status,
new_status: new_status,
timestamp: Date.now()
});
this.log('info', `频道 ${this.channel_id} 状态切换: ${old_status} -> ${new_status}`);
return true;
};
/**
* 销毁频道
* @returns {boolean} 是否销毁成功
*/
Channel.prototype.destroy = async function () {
if (this._monitorTimer) {
clearInterval(this._monitorTimer);
}
for (var player_id of this.player_ids.keys()) {
await this.kickPlayer(player_id, '频道关闭');
}
// 保存所有房间数据
await this._saveRooms();
for (var world_name of this.worlds.keys()) {
await this.destroyWorld(world_name);
}
for (var room_name of this.rooms.keys()) {
await this.destroyRoom(room_name);
}
if (this.matcher) {
this.matcher.stop();
}
if (this.roomAdmin) {
this.roomAdmin.destroy();
}
this.emitEvent('channel_destroyed', {
channel_id: this.channel_id,
timestamp: Date.now()
});
return true;
};
/**
* 保存所有房间数据
* @returns {Promise<void>}
*/
Channel.prototype._saveRooms = async function () {
try {
var promises = [];
for (var room of this.roomAdmin._rooms.values()) {
promises.push(room.do('save'));
}
await Promise.all(promises);
this.log('info', `保存所有房间数据成功,共 ${this.roomAdmin._rooms.size} 个`);
} catch (error) {
this.log('error', `保存房间数据失败: `, error);
}
};
/**
* 获取频道玩家数量
* @returns {number} 玩家数量
*/
Channel.prototype.getPlayerNum = function () {
return this.player_ids.size;
};
/**
* 进入主世界
* @param {string} player_id 玩家ID
*/
Channel.prototype.enterMainWorld = function (player_id) {
this.enterWorld(player_id, this._main_world_id);
};
/**
* 进入世界
* @param {string} player_id 玩家ID
* @param {string} world_id 世界ID
* @returns {object} 世界对象
*/
Channel.prototype.enterWorld = function (player_id, world_id) {
let world = this.getWorldById(world_id);
if (!world) {
throw new Error(`世界 ${world_id} 不存在`);
}
world.enter(player_id);
return world;
};
/**
* 根据世界ID获取世界对象
* @param {string} world_id 世界ID
* @returns {object|null} 世界对象
*/
Channel.prototype.getWorldById = function (world_id) {
return this.worlds.get(world_id);
};
/**
* 根据世界名称获取世界对象
* @param {string} name 世界名称
* @returns {object|null} 世界对象
*/
Channel.prototype.getWorldByName = function (name) {
for (let world of this.worlds.values()) {
if (world.config.name === name) {
return world;
}
}
return null;
};
/**
* 创建游戏世界
* @param {object} config 世界配置
* @param {string} world_id 世界ID
* @returns {object} 世界对象
*/
Channel.prototype.createWorld = async function (config, world_id) {
// let game = this.game[config.type];
// if (!game) {
// throw new Error(`游戏类型 ${config.type} 不存在`);
// }
// return await game.createWorld(config, world_id);
};
exports.Channel = Channel;