-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMUDServer.js
455 lines (417 loc) · 14.7 KB
/
MUDServer.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
const { parseColors } = require('./modules/Mud/Color.js');
const EventEmitter = require('events');
const fs = require('fs');
const net = require('net');
const path = require('path');
const { generateRandomString, gitPull } = require('./modules/Mud/Helpers.js');
/**
* Class representing a MUD (Multi-User Dungeon) server.
* @extends EventEmitter
*/
class MUDServer extends EventEmitter {
static BANS_LIST_PATH = path.join(__dirname, 'system', 'bans.json');
static CONFIG_PATH = path.join(__dirname, 'system', 'config.json');
static MODULE_ORDER_FILE = path.join(__dirname, 'system', 'module_order.txt');
static MUD_TITLE_PATH = path.join(__dirname, 'system', 'mudtitle.txt');
static MODULES_PATH = path.join(__dirname, 'modules');
banList = new Map();
commands = new Map();
modules = [];
players = new Map();
server = net.createServer();
/**
* Creates an instance of MUDServer.
*/
constructor() {
super();
this.events = {};
global.mudServer = this;
this.hotbooting = false;
this.loadConfig();
this.loadModules();
this.loadBanList();
this.loadTitle();
// Handle player disconnects
this.on('playerDisconnected', (player) => {
if (player.connectionStatus == null) return;
const playerName = player.username;
const wasLoggedIn = player.loggedIn;
this.players.delete(player.socket);
player.destroy();
if (wasLoggedIn) {
player.connectionStatus = null;
player.loggedIn = false;
player.save();
this.players.forEach(p => {
p.send(`Player ${playerName} has logged out.`);
});
}
});
// Handle incoming connections
this.server.on('connection', socket => {
if (this.isConnected(socket.remoteAddress) && !this.multiplay) {
socket.write('MUD doesn\'t allow multiplaying!');
socket.end();
socket.destroy();
return;
}
if (this.isBanned(socket)) {
console.log(`Banned address tried to connect: ${socket.remoteAddress}`);
socket.end();
socket.destroy();
return;
}
// Send MUD Title
socket.write(parseColors(this.mudTitle));
// Player connected, let modules know
this.emit('playerConnected', socket);
});
// Handle server errors
this.server.on('error', err => {
console.error('Server error:', err);
});
}
/**
* Ban a player.
* @param {Player} player - The player issuing the ban.
* @param {string[]} args - The arguments for the ban command.
*/
banPlayer(player, args) {
const [banPlayer] = args;
if (banPlayer !== undefined) {
if (banPlayer.toLowerCase() != player.username.toLowerCase()) {
const playerToBan = this.findPlayerByUsername(banPlayer);
if (playerToBan != null) {
const socket = playerToBan.socket;
this.banList.set(playerToBan.username, socket.remoteAddress);
this.players.forEach(p => {
if (p.username == player.username) return;
if (p.socket.remoteAddress == socket.remoteAddress) {
this.banList.set(p.username, p.socket.remoteAddress);
p.socket.end();
p.destroy();
}
});
socket.end();
player.destroy();
this.saveBansList();
player.send(`Player ${banPlayer} banned successfully.`);
} else {
player.send('You cannot ban yourself!');
}
} else {
player.send(`Player ${banPlayer} doesn't exist!`);
}
} else {
player.send(`A player to ban must be specified!`);
}
}
/**
* Add an event listener.
* @param {string} event - The event name.
* @param {Function} listener - The listener function.
*/
on(event, listener) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(listener);
}
/**
* Emit an event.
* @param {string} event - The event name.
* @param {...any} args - The arguments to pass to the event listeners.
* @returns {boolean} True if the event was handled, false otherwise.
*/
emit(event, ...args) {
if (!this.events[event] || this.events[event].length === 0) {
return false;
}
let eventHandled = false;
for (const listener of this.events[event]) {
if (eventHandled) {
break; // If an event has been handled, ignore subsequent events
}
if (listener(...args) === true) {
eventHandled = true;
}
}
return eventHandled;
}
/**
* Remove an event listener.
* @param {string} event - The event name.
* @param {Function} listener - The listener function to remove.
*/
off(event, listener) {
if (!this.events[event]) return;
this.events[event] = this.events[event].filter(l => l !== listener);
}
/**
* Find a player by their username.
* @param {string} username - The username of the player to find.
* @returns {Player|null} The player object if found, otherwise null.
*/
findPlayerByUsername(username) {
if (username != null && username != '') {
for (let [key, player] of this.players) {
if (player.username.toLowerCase() === username.toLowerCase()) {
return player;
}
}
}
return null;
}
/**
* Perform a hotboot, reloading modules and the title.
* @param {Player} player - The player issuing the hotboot command.
*/
hotBoot(player) {
global.mudServer.hotbooting = true;
global.mudServer.players.forEach(p => {
p.send('Performing hotboot...');
});
global.mudServer.emit('hotBootBefore', player);
global.mudServer.loadModules();
global.mudServer.loadTitle();
global.mudServer.players.forEach(p => {
p.send('Hotboot finished');
});
global.mudServer.emit('hotBootAfter', player);
global.mudServer.hotbooting = false;
}
/**
* Check if an address is banned.
* @param {Object} arg - The argument to check (player, socket, or address).
* @returns {boolean} True if the address is banned, false otherwise.
*/
isBanned(arg) {
return [...this.banList].some(([k, v]) => {
return arg?.username === k || arg?.socket?.remoteAddress === v || arg?.remoteAddress === v;
});
}
/**
* Check if an address is already connected.
* @param {string} address - The address to check.
* @returns {boolean} True if the address is connected, false otherwise.
*/
isConnected(address) {
return [...this.players].some(([k]) => {
return k.remoteAddress == address;
});
}
/**
* Kick a player.
* @param {Player} player - The player issuing the kick command.
* @param {string[]} args - The arguments for the kick command.
*/
kickPlayer(player, args) {
const [kickPlayer] = args;
if (kickPlayer !== undefined) {
const playerToKick = this.findPlayerByUsername(kickPlayer);
if (playerToKick != null) {
if (kickPlayer.toLowerCase() != player.username.toLowerCase()) {
const socket = player.socket;
kickPlayer.save();
this.players.forEach(p => {
if (p.username == kickPlayer.username) return;
if (p.socket.remoteAddress == socket.remoteAddress) {
p.save();
p.socket.end();
p.destroy();
}
});
socket.end();
player.destroy();
} else {
player.send(`You cannot kick yourself!`);
}
} else {
player.send(`Player ${kickPlayer} doesn't exist!`);
}
} else {
player.send(`A player to kick must be specified!`);
}
}
/**
* Check if a player is logged in.
* @param {Player} player - The player to check.
* @returns {boolean} True if the player is logged in, false otherwise.
*/
loggedIn(player) {
return [...this.players].some(([k, v]) => {
return player?.username?.toLowerCase() == v?.username?.toLowerCase() && v?.loggedIn;
});
}
/**
* Load the ban list from a JSON file.
*/
loadBanList() {
try {
const data = fs.readFileSync(MUDServer.BANS_LIST_PATH, 'utf-8');
const banListEntries = JSON.parse(data);
if (Array.isArray(banListEntries) && banListEntries.length > 0) {
this.banList = new Map(banListEntries);
}
console.log('Bans list loaded');
} catch (err) {
console.error('Error reading or parsing JSON file:', err);
}
}
/**
* Load the configuration from a JSON file.
*/
loadConfig() {
try {
const data = fs.readFileSync(MUDServer.CONFIG_PATH, 'utf-8');
const configData = JSON.parse(data);
Object.assign(this, configData);
} catch (err) {
console.error('Error reading or parsing JSON file:', err);
}
}
/**
* Load modules based on their order listed in the text file.
*/
loadModules() {
try {
this.modules = {};
const moduleNames = fs.readFileSync(MUDServer.MODULE_ORDER_FILE, 'utf-8').split('\n');
moduleNames.forEach(moduleName => {
moduleName = moduleName.trim();
const modulePath = path.join(MUDServer.MODULES_PATH, moduleName + '.js');
this.loadModule(modulePath);
});
for (const moduleId in this.modules) {
const module = this.modules[moduleId];
if (typeof module.load === 'function') {
module.load();
console.log(`Module ${module.name} loaded`);
}
}
console.log('Modules loaded based on order specified in the text file.');
} catch (error) {
console.error('Error loading modules based on order:', error);
}
}
/**
* Load a single module.
* @param {string} modulePath - The path to the module file.
*/
loadModule(modulePath) {
if (fs.existsSync(modulePath)) {
delete require.cache[require.resolve(modulePath)];
const module = require(modulePath);
this.modules[module.name === undefined ? generateRandomString(10) : module.name] = module;
if (typeof module.init === 'function') {
module.init(this);
console.log(`Module ${module.name} initialized.`);
}
} else {
console.error(`Module file '${modulePath}' not found.`);
}
}
/**
* Load the MUD title from a file.
*/
loadTitle() {
try {
const dataSync = fs.readFileSync(MUDServer.MUD_TITLE_PATH, 'utf8');
this.mudTitle = dataSync + '\n';
} catch (err) {
console.error('Error reading file synchronously:', err);
}
}
/**
* Check if a player's data file exists.
* @param {Player} player - The player to check.
* @returns {boolean} True if the player's data file exists, false otherwise.
*/
playerExist(player) {
const filePath = player.getFilePath();
try {
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}
}
/**
* Register a command handler.
* @param {Object} handler - The command handler to register.
*/
registerCommand(handler) {
this.commands.set(handler.command.toLowerCase(), handler);
}
/**
* Reload the configuration.
* @param {Player} player - The player issuing the reload command.
*/
reloadConfig(player) {
this.loadConfig();
player.send('Config reloaded!');
}
/**
* Reload the MUD title.
* @param {Player} player - The player issuing the reload command.
*/
reloadTitle(player) {
this.loadTitle();
player.send('Title reloaded!');
}
/**
* Save the ban list to a JSON file.
*/
saveBansList() {
try {
fs.writeFileSync(MUDServer.BANS_LIST_PATH, JSON.stringify(Array.from(this.banList.entries()), null, 2));
console.log('Bans list saved!');
} catch (err) {
console.error('Error writing bans file synchronously:', err);
}
}
/**
* Start the MUD server.
*/
start() {
this.server.listen(this.port, () => {
console.log(`${this.name} server listening on port ${this.port}`);
});
}
/**
* Unban a player.
* @param {Player} player - The player issuing the unban command.
* @param {string[]} args - The arguments for the unban command.
*/
unBan(player, args) {
const [usernameToUnBan] = args;
const bannedUser = Array.from(this.banList.keys()).find(key => key.toLowerCase() === usernameToUnBan.toLowerCase());
if (!bannedUser) {
player.send(`${usernameToUnBan} is not currently banned.`);
return;
}
const address = this.banList.get(bannedUser);
const otherUsers = Array.from(this.banList.entries())
.filter(([key, value]) => value.toLowerCase() === address)
.map(([key, value]) => key);
otherUsers.forEach(u => {
this.banList.delete(u);
});
this.banList.delete(bannedUser);
this.saveBansList();
player.send(`Player ${usernameToUnBan} and other address players unbanned.`);
}
async updateServer(player) {
const data = await gitPull().catch(err => {
player.send(err);
console.log(err);
return;
});
if (data) {
data.split('\n').forEach(line => {
player.send(line);
});
}
}
}
module.exports = MUDServer;