mm_os
Version:
MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。
250 lines (219 loc) • 5.44 kB
JavaScript
const {
Drive
} = require('mm_machine');
const {
GameRoom
} = require('./room.js');
/**
* 房间管理器
* 专注房间创建、销毁、进入、离开等管理
*/
class Room extends Drive {
static config = {
// 房间名称
'name': '5v5_rank',
// 房间标题
'title': '5v5排位赛',
// 房间描述
'description': '5v5排位赛房间',
// 房间等级上限
'max_level': 1000,
// 房间最大人数
'max_players': 5,
// 游戏类型 rpg、card
'type': 'card',
// 游戏类型 排位赛rank、娱乐局casual
'mode': 'rank',
// 房间主程序
'main': '',
// 房间最大房间数
'max_rooms': 1000
};
/**
* 构造函数
* @param {object} config 配置参数
*/
constructor(config) {
super({
...Room.config,
...config
});
// 房间列表
this.list = new Map();
}
}
/**
* 获取模板目录
* @returns {string} 模板目录
*/
Room.prototype.getTplDir = function() {
return __dirname;
};
/**
* 初始化核心
* @param {object} zone 游戏世界分区
* @param {object} eventer 事件管理器
* @param {object} logger 日志管理器
*/
Room.prototype._initCore = async function (zone, eventer, logger) {
if (!zone) {
throw new TypeError('游戏世界分区不能为空');
}
// 游戏世界分区
this.getZone = function () {
return zone;
};
if (logger) {
this.setLogger(logger);
}
if (eventer) {
this.getEventer = function () {
return eventer;
};
}
};
/**
* 创建房间
* @param {object} config 房间配置
* @param {string} owner_id 房主ID
* @returns {object} 房间对象
*/
Room.prototype.create = async function (config, owner_id) {
if (typeof owner_id !== 'string') {
throw new TypeError('房主ID必须是字符串');
}
if (this.list.size >= this.config.max_rooms) {
throw new Error(`房间数量已达上限: ${this.config.max_rooms}`);
}
try {
var room = new GameRoom(config, this);
room.setOwner({ player_id: owner_id });
await room.do('init', this.getEventer(), this.getLogger());
room.enter(owner_id);
this.list.set(room.room_id, room);
this.emitEvent('room_create:after', {
room_id: room.room_id,
room_name: room.config.name,
owner_id: owner_id,
timestamp: Date.now()
});
this.log('info', `房间创建成功: ${room.room_id}, 房主: ${owner_id}`);
return room;
} catch (error) {
this.log('error', `创建房间失败: ${error.message}`);
throw error;
}
};
/**
* 获取房间
* @param {string} room_id 房间ID
* @returns {object} 房间对象
*/
Room.prototype.get = function (room_id) {
if (typeof room_id !== 'string') {
throw new TypeError('房间ID必须是字符串');
}
var room = this.list.get(room_id);
if (!room) {
throw new Error(`房间不存在: ${room_id}`);
}
return room;
};
/**
* 删除房间
* @param {string} room_id 房间ID
*/
Room.prototype.del = function (room_id) {
if (typeof room_id !== 'string') {
throw new TypeError('房间ID必须是字符串');
}
var room = this.list.get(room_id);
if (!room) {
throw new Error(`房间不存在: ${room_id}`);
}
this.emitEvent('room_deleted', {
room_id: room_id,
room_name: room.config.name,
timestamp: Date.now()
});
this.list.delete(room_id);
this.log('info', `房间删除成功: ${room_id}`);
};
/**
* 玩家进入房间
* @param {string} room_id 房间ID
* @param {string} player_id 玩家ID
*/
Room.prototype.enter = function (room_id, player_id) {
if (typeof room_id !== 'string') {
throw new TypeError('房间ID必须是字符串');
}
if (typeof player_id !== 'string') {
throw new TypeError('玩家ID必须是字符串');
}
try {
var room = this.get(room_id);
room.enter(player_id);
this.log('info', `玩家 ${player_id} 进入房间 ${room_id}`);
} catch (error) {
this.log('error', `玩家进入房间失败: ${error.message}`);
throw error;
}
};
/**
* 玩家退出房间
* @param {string} room_id 房间ID
* @param {string} player_id 玩家ID
*/
Room.prototype.leave = function (room_id, player_id) {
if (typeof room_id !== 'string') {
throw new TypeError('房间ID必须是字符串');
}
if (typeof player_id !== 'string') {
throw new TypeError('玩家ID必须是字符串');
}
try {
var room = this.get(room_id);
room.leave(player_id);
if (room.getPlayerNum() === 0) {
this.del(room_id);
}
this.log('info', `玩家 ${player_id} 退出房间 ${room_id}`);
} catch (error) {
this.log('error', `玩家退出房间失败: ${error.message}`);
throw error;
}
};
/**
* 获取房间列表
* @returns {Array} 房间列表
*/
Room.prototype.getList = function () {
var room_list = [];
this.list.forEach((room, room_id) => {
room_list.push({
room_id: room_id,
room_name: room.config.name,
owner_id: room.owner,
player_count: room.getPlayerNum(),
max_players: room.config.max_players,
state: room.state
});
});
return room_list;
};
/**
* 获取房间数量
* @returns {number} 房间数量
*/
Room.prototype.getCount = function () {
return this.list.size;
};
/**
* 销毁管理器
*/
Room.prototype.destroy = function () {
this.list.clear();
this.log('info', '房间管理器已销毁');
};
exports.Room = Room;