This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreProcess.js
126 lines (112 loc) · 3.91 KB
/
coreProcess.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const logger = require("./simpleLogger");
const fs = require('fs');
const config = require("./config");
const moment = require('moment');
const dbm = require("./dbManager");
const {spawn} = require('child_process');
let playerList = new Map();
let coreProcess;
let lastBackupTime;
let serverStatus;
let output = fs.createWriteStream('server.log');
function backup() {
if (moment().diff(lastBackupTime, 'days') >= config.backupCycle) {
logger.info("begin Backup");
sendCommand("say [Server]: begin backup");
//这里是备份的代码
logger.info("end Backup");
sendCommand("backup finish");
}
}
function spawnServerProcess() {
logger.info("starting game server");
if (config.autoBackup) {
logger.info("init the start backup time");
}
lastBackupTime = moment();
coreProcess = spawn(config.serverPath + 'bedrock_server.exe');
serverStatus = true;
coreProcess.on('error', (err) => {
processExit(err);
});
coreProcess.on('exit', (code) => {
processExit(code);
});
let buff;
coreProcess.stdout.on('data', (chunk) => {
let str = chunk.toString();
buff = buff + str;
if (/\n/g.test(str)) {
if (buff.indexOf("INFO] Player") !== -1) {
if (config.autoBackup) {
backup();
}
const playerName = buff.match(/(?<=connected: )(.*)(?=, xuid)/g)[0];
processRecord(playerName);
}
logger.serverInfo(buff);
buff = "";
}
});
//控制命令输入到服务器后台
process.stdin.pipe(coreProcess.stdin);
//输出服务器日常到文件
coreProcess.stdout.pipe(output);
}
function sendCommand(cmd) {
coreProcess.stdin.write(cmd);
}
function processRecord(playerId) {
const time = playerList.get(playerId);
if (time === undefined) {
logger.info(playerId + " enter the game");
playerList.set(playerId, moment());
} else {
logger.info(playerId + " left the game");
dbm.insertRecord(playerId, time.format('YYYY-MM-DD HH:MM:SS'), moment().diff(time) / 36000);
playerList.delete(playerId);
}
}
function processExit(msg) {
serverStatus = false;
logger.info("server stop " + msg);
logger.info("process extra data,please do not close console");
playerList.forEach(function (value, key) {
dbm.insertRecord(key, value.format('YYYY-MM-DD HH:MM:SS'), moment().diff(value) / 36000);
});
playerList.clear();
logger.info("extra data process finished please restart this server");
}
module.exports = {
coreProcess,
spawnServerProcess,
sendCommand,
playerList
};
// function importLog() {
// const str = fs.readFileSync("server.log", "utf-8");
// const recordArray = str.split('\n');
// recordArray.forEach(function (line) {
// if (line.indexOf("INFO] Player") !== -1) {
//
// const timePoint = line.match(/\[(.+?)\]/g)[0].slice(1, 20);
// const data = moment(timePoint);
// const playerName = line.match(/(?<=connected: )(.*)(?=, xuid)/g)[0];
// const op = line.indexOf("Player connected") !== -1;
//
// const time = playerList.get(playerName);
// if (op) {
// if (time === undefined) {//玩家不在列表
// playerList.set(playerName, data);
// }
// } else {//这是一条离线记录
// if (time !== undefined) {
// // console.log(data.diff(time)/36000);
// //console.log(playerName + " - " + time.toLocaleString() + "----" + data.toLocaleString() + " " + hour);
// dbm.insertRecord(playerName, time.format('YYYY-MM-DD HH:MM:SS'), data.diff(time) / 36000);
// playerList.delete(playerName);
// }
// }
// }
// });
// }