-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
89 lines (68 loc) · 2.26 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
/**
* Main server script for Discord mudlib. Most of the code is still messy from
* Node Knockout.
*
* This file is in charge of all the nitty gritty bits of loading all the
* JavaScript files required to get a World instance running, then opening a
* socket and redirecting input to a Player object within the world.
*
* Most of the file loading logistics lives in the World class, which handles
* determining where to pull files from based on configuration options and
* set rules.
*
* @author Michelle Steigerwalt <msteigerwalt.com>
* @copyright 2010-2012 Michelle Steigerwalt
*/
require('./engine');
var sys = require('util'),
fs = require('fs');
log_error = function (e) {
console.log("ERROR:".color('red'), e );
console.log(e.stack || console.trace());
}
exports.start = function(config) {
var world = new World(config);
on_error = log_error;
process.on('uncaughtException', log_error);
var net = require('net');
var server = net.createServer(function (stream) {
var enhanced = false;
stream.setEncoding('utf8');
var player = new Player();
/* Error handling, notifies admins of errors. */
stream.on('uncaughtException', function (message) {
player.send("ERROR: ".color('red')+message);
});
player.addEvent('output', function(message, style) {
if (!stream.writable) return;
var brk = (enhanced) ? "\\r\\n" : "\r\n";
var str = message.style(style);
stream.write(str.wordwrap(80)+brk);
});
player.addEvent('guiOutput', function(obj, handler) {
if (!enhanced) { return false; }
var data = {'data':obj, 'handler':handler};
var json = JSON.encode(data);
stream.write("<!-- \n"+json+"\n-->\n\\r\\n");
});
player.addEvent('quit', function() { stream.end(); });
player.ip = stream.remoteAddress;
player.world = world;
sys.puts(player.ip+" has connected.");
player.prompt(Prompts.login, "Please enter your name.");
stream.on('data', function(data) {
if (data.trim()==">>> GUI_ON <<<") { enhanced = true; return; }
else if (data.trim().match(/^>>>(.*?)<<<$/)) { return; }
player.onInput(data);
});
stream.on('end', function () {
player.disconnect();
});
});
return {
listen: function(port) {
console.log("Game engine listening on port", port);
server.listen(port);
}
};
};