Skip to content

Commit 10992d8

Browse files
committed
Initial commit again
1 parent 284a79a commit 10992d8

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

Diff for: config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var config = {};
2+
3+
4+
config.process = {};
5+
config.server = {};
6+
7+
config.process.listen_port = 8001;
8+
config.process.console_port = 8002;
9+
config.process.events_port = 8003;
10+
11+
// config.server.command_line = "java -Xmx {{Xmx}} -jar minecraft.jar";
12+
config.server.command_line = "./count.sh";
13+
config.server.path = "./";
14+
config.server.variables = {"Xmx":"512Mb"};
15+
config.server.port = 25565;
16+
config.server.plugin = "minecraft";
17+
18+
module.exports = config;

Diff for: count.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
for COUNT in $(seq 1 100); do
3+
echo $COUNT
4+
sleep 0.25
5+
done

Diff for: gameprocess.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var spawn = require('child_process').spawn;
2+
var config = require('./config');
3+
var util = require("util");
4+
var events = require("events");
5+
var plugins = require("./plugins.js").plugins;
6+
7+
var OFF = 0; ON = 1, STARTING = 2, STOPPING = 3;
8+
9+
function GameServer() {
10+
self = this;
11+
this.status = OFF;
12+
this.commandline = config.server.command_line;
13+
this.plugin = plugins[config.server.plugin + '.js'];
14+
};
15+
16+
util.inherits(GameServer, events.EventEmitter);
17+
18+
GameServer.prototype.turnon = function(){
19+
// Shouldn't happen, but does on a crash after restart
20+
if (!self.status == OFF){
21+
console.log("Tried to turn on but status is already : " + self.status);
22+
return
23+
}
24+
25+
this.ps = spawn(config.server.command_line);
26+
this.output = this.ps.stdout;
27+
self.status = STARTING;
28+
29+
this.output.on('data', function(data){
30+
output = data.toString();
31+
console.log(output);
32+
self.emit('console', output);
33+
});
34+
35+
36+
this.ps.on('exit', function(){
37+
if (self.status == STOPPING){
38+
console.log("Process stopped");
39+
self.status = OFF;1
40+
self.emit('off');
41+
return;
42+
}
43+
44+
if (self.status == ON || self.status == STARTING){
45+
console.log("Process died a horrible death");
46+
self.status = OFF;
47+
self.emit('crash');
48+
return;
49+
}
50+
51+
});
52+
53+
this.on('crash', function(){
54+
console.log("Restarting after crash");
55+
this.turnon();
56+
});
57+
}
58+
59+
GameServer.prototype.turnoff = function(){
60+
self.status = STOPPING;
61+
this.kill()
62+
}
63+
64+
GameServer.prototype.restart = function(){
65+
self.once('off', function (stream) {self.turnon()});
66+
this.turnoff();
67+
}
68+
69+
GameServer.prototype.kill = function(){
70+
this.ps.kill();
71+
}
72+
73+
GameServer.prototype.send = function(data){
74+
this.ps.write(data);
75+
}
76+
77+
module.exports = GameServer;

Diff for: gsd.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var WsServer = require('ws').Server;
2+
var config = require('./config');
3+
var gameserver = require('./gameprocess');
4+
5+
gameconsole = new WsServer({port: 8080});
6+
7+
gameconsole.broadcast = function(data) {
8+
for(var i in this.clients)
9+
this.clients[i].send(data);
10+
};
11+
12+
var potato = new gameserver();
13+
potato.turnon();
14+
15+
// Stream everything out to the console
16+
potato.on('data', function(data){
17+
gameconsole.broadcast(data.toString());
18+
});
19+
20+
21+
setTimeout(function(){
22+
potato.restart()
23+
}, 5 * 1000);

Diff for: plugins.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var fs = require('fs');
2+
3+
var plugins = {};
4+
fs.readdirSync("./plugins").forEach(function(file){
5+
path = "./plugins/" + file;
6+
if (fs.lstatSync(path).isFile()){
7+
console.log('Loading plugin ' + file);
8+
plugins[file] = require(path);
9+
}
10+
});
11+
12+
exports.plugins = plugins;

0 commit comments

Comments
 (0)