MCSManager/model/ServerModel.js

110 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-02-12 12:21:18 +08:00
/*
* @Author: Copyright(c) 2020 Suwings
* @Date: 2020-10-08 13:28:28
* @LastEditTime: 2021-02-12 12:20:18
* @Description:
*/
2020-10-08 11:59:48 +08:00
const ServerManager = require("../core/Process/ServerCenter");
const fs = require("fs");
const fsextra = require("fs-extra");
2021-02-12 12:21:18 +08:00
const pathm = require("path");
2017-11-13 12:26:31 +08:00
// 事实上Node.js 的缓存机制可以间接的到达单列模式的目的
var onlyServerManager = new ServerManager();
// module.exports.ServerManager = onlyServerManager;
function getServerDir(serverName) {
2020-10-08 11:59:48 +08:00
return "./server/server_core/" + serverName + "/";
2017-11-13 12:26:31 +08:00
}
module.exports.getServerDir = getServerDir;
module.exports.ServerManager = () => {
2020-10-08 11:59:48 +08:00
return onlyServerManager;
};
2017-11-13 12:26:31 +08:00
//后来维护添加的 优先创建根目录
module.exports.createServerDir = (serverName, cwd) => {
2020-10-08 11:59:48 +08:00
if (cwd == "" || cwd == "<默认标准位置>") cwd = getServerDir(serverName);
if (!fs.existsSync(cwd)) {
fsextra.mkdirsSync(cwd);
}
};
2017-11-13 12:26:31 +08:00
module.exports.createServer = (serverName, config) => {
2020-10-08 12:13:27 +08:00
if (config.cwd == "" || config.cwd == "<默认标准位置>") config.cwd = getServerDir(serverName);
2020-10-08 11:59:48 +08:00
if (!fs.existsSync(config.cwd)) {
fsextra.mkdirsSync(config.cwd);
}
2021-02-12 12:21:18 +08:00
// 关于 EULA 同意功能,在使用面板时就必须表示接受 Minecraft 相关的 EULA 与其他协议。
const eulaPath = pathm.join(config.cwd, "eula.txt");
if (!fs.existsSync(eulaPath)) {
fsextra.writeFileSync(eulaPath, "eula=true");
}
2020-10-08 11:59:48 +08:00
onlyServerManager.newMinecraftServer(serverName);
onlyServerManager.builderMinecraftServer(serverName, config);
onlyServerManager.saveMinecraftServer(serverName);
return true;
};
2017-11-13 12:26:31 +08:00
module.exports.deleteServer = (serverName) => {
2020-10-08 11:59:48 +08:00
return onlyServerManager.delMinecraftServer(serverName);
};
2017-11-13 12:26:31 +08:00
module.exports.startServer = (serverName) => {
2020-10-08 11:59:48 +08:00
if (onlyServerManager.isExist(serverName)) {
return onlyServerManager.startMinecraftServer(serverName);
}
return false;
};
2017-11-13 12:26:31 +08:00
module.exports.stopServer = (serverName) => {
2020-10-08 11:59:48 +08:00
if (onlyServerManager.isExist(serverName)) {
return onlyServerManager.stopMinecraftServer(serverName);
}
return false;
};
2017-11-13 12:26:31 +08:00
2020-03-04 11:59:47 +08:00
module.exports.restartServer = (serverName) => {
2020-10-08 11:59:48 +08:00
if (onlyServerManager.isExist(serverName)) {
return onlyServerManager.restartServer(serverName);
}
return false;
};
2020-03-04 11:59:47 +08:00
2017-11-13 12:26:31 +08:00
module.exports.sendCommand = (serverName, command) => {
2020-10-08 11:59:48 +08:00
if (onlyServerManager.isExist(serverName)) {
return onlyServerManager.sendMinecraftServer(serverName, command);
}
return false;
};
2017-11-13 12:26:31 +08:00
module.exports.builder = (serverName, config) => {
2020-10-08 11:59:48 +08:00
onlyServerManager.builderMinecraftServer(serverName, config);
onlyServerManager.saveMinecraftServer(serverName);
};
2018-08-19 10:14:21 +08:00
2018-08-19 10:17:24 +08:00
//服务端中心 用于判断所有运行服务端是否到期 | 每2个小时检查一次
2018-08-19 10:14:21 +08:00
//https://www.npmjs.com/package/node-schedule
const schedule = require("node-schedule");
2020-10-08 11:59:48 +08:00
schedule.scheduleJob("1 0 */2 * * *", function () {
let serverCollect = onlyServerManager.getServerObjects();
try {
for (let k in serverCollect) {
let server = serverCollect[k];
if (server && server.isRun()) {
let res = server.isDealLineDate();
if (res) {
2020-10-08 12:13:27 +08:00
MCSERVER.log("[时间期限] 服务端 [", server.dataModel.name, "]", "于现在过期,正在执行关闭程序.");
2020-10-08 11:59:48 +08:00
//先进行标准流程关闭服务端,如果 45 秒后未关闭,则强制性结束进程
2020-10-12 12:28:21 +08:00
server.stopServer();
2020-10-08 11:59:48 +08:00
setTimeout(() => {
server.kill();
}, 45 * 1000);
2018-08-19 10:14:21 +08:00
}
2020-10-08 11:59:48 +08:00
}
2018-08-19 10:14:21 +08:00
}
2020-10-08 11:59:48 +08:00
} catch (err) {
2020-10-08 12:13:27 +08:00
MCSERVER.error("[时间期限] 关闭服务端时出现异常,某个服务端可能未能正确关闭:", err);
2020-10-08 11:59:48 +08:00
}
});