forked from johnkeates/gsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameprocess.js
259 lines (207 loc) · 6.79 KB
/
gameprocess.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var spawn = require('child_process').spawn;
var pty = require('pty.js');
var util = require("util");
var events = require("events");
var plugins = require("./plugins.js").plugins;
var merge = require("./utls.js").merge;
var download = require('download');
var usage = require('usage');
var pathlib = require('path');
var fs = require('fs');
var exec = require('child_process').exec;
var createUser = require("./create.js").createUser;
var deleteUser = require("./create.js").deleteUser;
var fixperms = require("./create.js").fixperms;
var getIPAddress = require("./utls.js").getIPAddress;
var async = require('async');
var OFF = 0; ON = 1, STARTING = 2, STOPPING = 3; CHANGING_GAMEMODE = 4;
function GameServer(config) {
this.status = OFF;
this.config = config;
this.joined = ["-Xmx", "-XX:PermSize=", "-Djline.terminal="];
this.plugin = plugins[this.config.plugin + '.js'];
this.variables = merge(this.joined, this.plugin.defaultvariables, this.config.variables);
this.exe = this.plugin.exe;
if ('gameport' in this.config && this.config.gameport != 0){
this.gameport = this.config.gameport
}else{
this.gameport = this.plugin.defaultPort;
}
if ('gamehost' in this.config && this.config.gamehost != ""){
this.gamehost = this.config.gamehost
}else{
this.gamehost = getIPAddress();
}
};
util.inherits(GameServer, events.EventEmitter);
GameServer.prototype.turnon = function(){
var self = this;
// Shouldn't happen, but does on a crash after restart
if (!this.status == OFF){
// console.log("Tried to turn on but status is already : " + self.status);
return;
}
this.ps = pty.spawn(this.exe, this.variables, {cwd: this.config.path});
this.setStatus(STARTING);
this.pid = this.ps.pid
this.ps.on('data', function(data){
output = data.toString();
self.emit("console", output);
if (self.status == STARTING){
if (output.indexOf(self.plugin.started_trigger) !=-1){
self.setStatus(ON);
console.log("Server started");
self.queryCheck = setInterval(self.query, 15000, self);
self.statCheck = setInterval(self.procStats, 10000, self);
self.usagestats = {};
self.emit('started');
}
};
});
this.ps.on('exit', function(){
if (self.status == STOPPING){
console.log("Process stopped");
self.setStatus(OFF);
self.emit('off');
return;
}
if (self.status == ON || self.status == STARTING){
console.log("Process died a horrible death");
self.setStatus(OFF);
self.emit('off');
self.emit('crash');
}
});
this.on('crash', function(){
console.log("Restarting after crash");
self.restart();
});
this.on('off', function clearup(){
clearInterval(self.queryCheck);
clearInterval(self.statCheck);
self.usagestats = {};
usage.clearHistory(self.pid);
self.pid = undefined;
});
}
GameServer.prototype.turnoff = function(){
var self = this;
clearTimeout(self.queryCheck);
if (!self.status == OFF){
self.setStatus(STOPPING);
self.kill();
}else{
self.emit('off');
}
}
GameServer.prototype.create = function(){
var config = this.config;
var self = this;
async.series([
function(callback) {
createUser(config.user, config.path, function cb(){callback(null);});
},
function(callback) {
self.plugin.install(self, function cb(){callback(null);});
},
function(callback) {
fixperms(self);
callback(null);
}
]);
}
GameServer.prototype.delete = function(){
deleteUser(this.config.user);
}
GameServer.prototype.setStatus = function(status){
this.status = status
this.emit('statuschange');
return this.status;
}
GameServer.prototype.query = function(self){
r = self.plugin.query(self);
self.emit('query');
return r;
}
GameServer.prototype.procStats = function(self){
usage.lookup(self.pid, {keepHistory: true}, function(err, result) {
// TODO : Return as % of os.totalmem() (optional)
// TODO : Return as % of ram max setting
self.usagestats = {"memory":result.memory, "cpu":Math.round(result.cpu)};
self.emit('processStats');
});
}
GameServer.prototype.lastquery = function(){
return {"motd":this.hostname, "numplayers":this.numplayers, "maxplayers":this.maxplayers, "lastquery":this.lastquerytime, "map":this.map, "players":this.players}
}
GameServer.prototype.configlist = function(){
return this.plugin.configlist(this);
}
GameServer.prototype.maplist = function(){
return this.plugin.maplist(this);
}
GameServer.prototype.addonlist = function(){
return this.plugin.addonlist(this);
}
GameServer.prototype.info = function(){
return {"query":this.lastquery(), "config":this.config, "status":this.status, "pid":this.pid, "process":this.usagestats, "variables":this.variables}
}
GameServer.prototype.restart = function(){
this.once('off', function (stream) {this.turnon()});
this.turnoff();
}
GameServer.prototype.kill = function(){
this.ps.kill();
}
GameServer.prototype.send = function(data){
console.log(data);
this.ps.write(data + '\n');
}
GameServer.prototype.console = function Console(){
}
GameServer.prototype.readfile = function readfile(f){
file = pathlib.join(this.config.path, pathlib.normalize(f));
return fs.readFileSync(file, "utf8");
}
GameServer.prototype.writefile = function writefile(f, contents){
file = pathlib.join(this.config.path, pathlib.normalize(f));
fs.writeFile(file, contents);
}
GameServer.prototype.downloadfile = function downloadfile(url, path){
path = pathlib.join(this.config.path, pathlib.normalize(path));
//TODO : Work out when to extract (zip etc...) , { extract: true }
download(url, path);
return 'ok';
}
GameServer.prototype.deletefile = function Console(){
}
GameServer.prototype.getgamemodes = function getgamemode(res){
managerlocation = pathlib.join(__dirname,"gamemodes",self.config.plugin,"gamemodemanager");
child = exec(managerlocation + ' getlist',
function (error, stdout, stderr) {
res.send(JSON.parse(stdout));
});
}
GameServer.prototype.installgamemode = function installgamemode(){
managerlocation = pathlib.join(__dirname,"gamemodes",self.config.plugin,"gamemodemanager");
if (self.status == ON){
self.turnoff();
console.log("HERE");
}
self.setStatus(CHANGING_GAMEMODE);
console.log(self.config.path)
installer = spawn(managerlocation, ["install", "craftbukkit", self.config.path], {cwd: self.config.path});
console.log(managerlocation);
installer.stdout.on('data', function(data){
if (data == "\r\n"){return}
console.log(data);
self.emit('console',data);
});
installer.on('exit', function(){
self.setStatus(OFF);
});
}
GameServer.prototype.removegamemode = function Console(){
self.ps = spawn(self.exe, [self.config.path], {cwd: self.config.path});
}
module.exports = GameServer;