-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.nl
256 lines (225 loc) · 7.09 KB
/
server.nl
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
var config = require('./config'),
http = require('http'),
fs = require('fs'),
universe = require('./universe');
function plural(word, count) {
return parseInt(count, 10) == 1 ? word : word + 's';
}
var world = new universe.World(1);
var Nope = universe.Nope;
function setup(cb) {
count <- world.getRoomCount();
if (!count) {
id <- world.createRoom({vis: {desc: "You are at home."}});
_ <- world.setStartingRoom(id);
return "Made initial room.";
}
else {
return "World has " + count + " " + plural('room', count);
}
}
setup(function (err, msg) {
if (err) throw err;
console.log(msg);
});
var dirOpposites = {north: 'south', south: 'north', west: 'east', east: 'west', up: 'down', down: 'up'};
function execute(query, player, cb) {
switch (query.verb) {
case 'go':
{
var dir = query.arg.dir;
oldLoc <- player.getLoc();
newLoc <- world.lookupRoomExit(oldLoc, query.arg.dir);
if (!newLoc)
return Nope("Can't go that way.");
_ <- player.move(oldLoc, newLoc, query.arg.dir);
newRoom <- world.getRoom(newLoc);
return look(newRoom);
}
case 'dig':
{
var dir = query.arg.dir, backDir = dirOpposites[dir];
if (!dir || !backDir)
return Nope("That's not a direction.");
oldId <- player.getLoc();
_ <- world.addRoomExit(oldId, dir);
newId <- world.createRoom({});
_ <- world.setRoomExit(newId, backDir, oldId);
_ <- world.setRoomExit(oldId, dir, newId);
_ <- player.move(oldId, newId, dir);
room <- world.getRoom(newId);
var msg = look(room);
msg.prefix = 'Dug.';
return msg;
}
case 'desc':
{
var newDesc = query.arg.desc.slice(0, 140);
loc <- player.getLoc();
room <- world.getRoom(loc);
if (!room.vis)
room.vis = {};
room.vis.desc = newDesc;
_ <- world.updateRoom(loc, 'vis', room.vis);
var msg = look(room);
msg.prefix = 'Changed.'
return msg;
}
case 'look':
{
room <- player.getRoom();
return look(room);
}
default:
cb(null, "What?");
}
}
function look(room) {
var vis = {};
vis.msg = (room.vis && room.vis.desc) || "Can't see shit captain.";
if (room.exits)
vis.exits = Object.keys(room.exits);
if (room.players)
vis.msg += ' Players: ' + room.players;
return vis;
}
function doCommand(player, command, callback) {
if (typeof command != 'object')
return callback("Bad command.");
try {
execute(command, player, function (err, msg) {
if (err) {
if (err instanceof Nope)
return callback(err.message);
dumpError(err, command, player);
return callback('Game error.');
}
if (msg instanceof Nope)
return callback(msg.message);
if (typeof msg == 'string')
msg = {msg: msg};
callback(null, msg);
});
}
catch (e) {
dumpError(e, command, player);
callback('Server error.');
}
}
function dumpError(error, command, player) {
console.error((player ? 'Player #' + player.id : 'Unknown player') + " using command:");
console.error(JSON.stringify(command));
console.error("caused error:");
console.error(error.stack || error);
}
/* WEB STUFF */
function handler(req, resp) {
// TEMP debug
if (config.DEBUG) {
var name = req.url.slice(1);
if (name == '') {
resp.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
fs.readFile('index.html', function (err, html) {
if (err) throw err;
resp.end(html);
});
return;
}
else if (media.indexOf(name) >= 0) {
var mime = name.match(/\.js$/) ? 'application/javascript' : 'text/css';
resp.writeHead(200, {'Content-Type': mime});
var filename = req.url.slice(1);
if (filename == 'client.js')
filename = 'out/client.js';
fs.readFile(filename, function (err, data) {
if (err) throw err;
resp.end(data);
});
return;
}
else {
console.warn('No /' + name);
}
}
resp.writeHead(404);
resp.end('Not found');
}
var media = ['client.js', 'plain.css', 'jquery-1.7.1.min.js', 'sockjs-0.3.1.min.js', 'input.js'];
var server = http.createServer(handler);
server.listen(config.PORT);
var sockJs = require('sockjs').createServer({
sockjs_url: 'sockjs-0.3.1.min.js',
prefix: '/sock',
jsessionid: false,
});
server.on('upgrade', function (req, resp) {
resp.end();
});
sockJs.installHandlers(server);
var CLIENTS = {};
function Client(id) {
this.clientId = id;
this.commandResult = this.onCommandResult.bind(this);
};
Client.prototype.emit = function (name, arg) {
this.socket.write(JSON.stringify({e: name, a: arg}));
};
Client.prototype.onClose = function () {
if (CLIENTS[this.clientId] === this)
delete CLIENTS[this.clientId];
};
Client.prototype.onCommand = function (data) {
var command;
try {
command = JSON.parse(data);
}
catch (e) {}
if (!command)
return this.emit('error', 'Invalid command.');
doCommand(this.player, command, this.commandResult);
};
Client.prototype.onCommandResult = function (err, result) {
if (err)
this.emit('error', err);
else
this.emit('result', result);
};
function onLogin(id) {
if (!id.match(/^\d{1,20}$/))
return this.write(JSON.stringify({e: 'error', a: 'Bad id.'}));
var client = CLIENTS[id];
if (!client)
CLIENTS[id] = client = new Client(id);
if (client.socket) {
client.emit('error', 'Kicked out by another browser session.');
client.socket.close();
}
client.socket = this;
client.socket.on('close', client.onClose.bind(client));
if (!client.player) {
universe.Player.getOrCreate(id, world, function (err, player) {
if (err) {
console.error(err);
client.emit('error', "Couldn't enter world.");
}
else if (client.player) {
// Shouldn't happen due to .once()
client.emit('error', 'Login conflict.');
}
else {
client.player = player;
ready('Welcome, ' + player.name + '.');
}
});
}
else
ready('Welcome back, ' + client.player.name + '.');
function ready(welcome) {
client.emit('login', {msg: welcome});
client.socket.on('data', client.onCommand.bind(client));
}
}
sockJs.on('connection', function (socket) {
socket.once('data', onLogin.bind(socket));
});
// vi: set sw=4 ts=4 sts=4 ai et filetype=javascript: