-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
223 lines (157 loc) · 5.46 KB
/
server.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const process = require("process");
const path = require("path");
const https = require("https");
const fs = require("fs-extra");
const AdmZip = require("adm-zip");
const { spawn } = require("child_process");
// const cmd = "wineconsole --backend=curses /home/cormac/nw_server/mb_warband_dedicated.exe -r NW_Sample_Duel.txt -m Napoleonic Wars";
// exec(cmd, (error, stdout, stderr) => {
// if (error) {
// console.log(`error: ${error.message}`);
// return;
// }
// if (stderr) {
// console.log(`stderr: ${stderr}`);
// return;
// }
// console.log(`stdout: ${stdout}`);
// });
// set_map mp_french_farm
// scn_mp_french_farm.sco
module.exports = {
showConsoleOutput: false,
restartIntervalInHours: 24,
restartInterval: 0,
nw_server: {},
calculate_restart_interval() {
return this.restartIntervalInHours*60*60*1000;
},
/**
* gets the current date and time for time stamping
* @returns timestamp of current time
*/
get_time() {
const now = new Date();
return `[${now.getDate()}/${now.getMonth()+1}/${now.getFullYear()} ${now.getHours()}:${now.getMinutes()}]`;
},
/**
* starts the NW server process
*/
start_server() {
console.log(this.get_time(), 'Starting server');
const server_path = path.join(__dirname, "server_files", "mb_warband_dedicated.exe")
const config_file = "NW_server_config.txt"
this.nw_server = spawn("wineconsole", [
"--backend=curses", server_path,
// "-r", "NW_Sample_Duel.txt",
"-r", config_file,
"-m", "Napoleonic Wars"
], {
shell: true,
detached: true
});
let output = '';
this.nw_server.stdout.on("data", data => {
if (!this.showConsoleOutput) return;
if (output.length > 2000) {
output = output.split('\n');
for (let i = 0; i < output.length - 1; i++) {
console.log(output[i].trim());
}
output = output[output.length - 1];
}
output = output + data;
});
this.nw_server.stderr.on("data", data => {
console.log(`stderr: ${data}`);
});
this.nw_server.on('error', (error) => {
console.log(`error: ${error.message}`);
});
this.nw_server.on("close", code => {
console.log(this.get_time(), `server closed with code ${code}`);
this.start_server();
});
},
/**
* starts begins the restart routine to automatically restart the server
* @param {int} restartHour what hour of a 24 hour clock to restart
*/
start_automatic_restart(restartHour = 5) {
console.log(this.get_time(), 'automatic restart started');
let today = new Date();
let tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
tomorrow.setHours(restartHour, 0, 0);
let msToRestart = tomorrow.getTime() - today.getTime();
console.log(this.get_time(), 'restarting in', Math.floor(msToRestart/1000/60), "minutes");
setTimeout(() => {
this.kill_server();
console.log(this.get_time(), 'restarting in', Math.floor(this.restartInterval/1000/60), "minutes");
setInterval(() => {
this.kill_server();
console.log(this.get_time(), 'restarting in', Math.floor(this.restartInterval/1000/60), "minutes");
}, this.restartInterval);
}, msToRestart);
},
/**
* stops the NW server process
*/
kill_server() {
console.log(this.get_time(), 'attempting to close the server');
if (this.nw_server.pid) {
process.kill(-this.nw_server.pid); // note - before pid. This converts a pid to a group of pids for process kill() method.
}
},
install_server() {
// TODO promisify these commands
console.log(this.get_time(), "installing server");
const download_url = "https://download.taleworlds.com/mb_warband_napoleonic_wars_dedicated_1174_nw_1210.zip";
this.download_server(download_url);
},
download_server(url) {
console.log(this.get_time(), "downloading server");
console.log(this.get_time(), "download started");
const file = fs.createWriteStream(path.join(__dirname, "server_files", "nw_server.zip"));
const request = https.get(url, (res) => {
res.pipe(file);
res.on("end", () => {
console.log(this.get_time(), "download finished");
this.unzip_server();
})
})
request.on("error", (error) => {
console.log(`error: ${error}`);
})
},
unzip_server() {
console.log(this.get_time(), "unzipping server");
const zip = new AdmZip(path.join(__dirname, "server_files", "nw_server.zip"));
zip.extractAllTo(path.join(__dirname, "server_files/"), true);
console.log(this.get_time(), "moving files");
const src = path.join(__dirname, "server_files", "Mount&Blade Warband Napoleonic Wars Dedicated");
const dst = path.join(__dirname, "server_files");
fs.copySync(src, dst);
fs.rmSync(src, { recursive: true, froce: true });
fs.unlinkSync(path.join(dst, "nw_server.zip"))
console.log(this.get_time(), "finished moving files");
console.log(this.get_time(), "installation finished");
},
server_is_installed() {
try {
fs.statSync(path.join(__dirname, "server_files", "mb_warband_dedicated.exe"));
return true;
} catch (error) {
return false;
}
},
initalise() {
this.restartInterval = this.calculate_restart_interval();
if (!this.server_is_installed()) {
this.install_server();
} else {
this.start_server();
this.start_automatic_restart(5);
}
},
}