-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-networking-server.js
More file actions
293 lines (252 loc) · 10.6 KB
/
test-networking-server.js
File metadata and controls
293 lines (252 loc) · 10.6 KB
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
/**
* Simple test networking server for multi-user functionality testing
* This avoids NestJS complexity and focuses on core MUD functionality
*/
const net = require('net');
class TestServer {
constructor(port = 3001) {
this.port = port;
this.server = null;
this.sessions = new Map();
this.players = new Map(); // Simple player storage
this.setupBasicCommands();
}
setupBasicCommands() {
this.commands = new Map();
this.commands.set('help', (session, args) => 'Available commands: help, who, say, whisper, chat, look, quit');
this.commands.set('who', (session, args) => {
const onlineUsers = Array.from(this.players.values()).map(p => p.username);
if (onlineUsers.length === 0) {
return 'No players are currently online.';
}
return `Online players: ${onlineUsers.join(', ')}`;
});
this.commands.set('say', (session, args) => {
if (args.length === 0) return 'What do you want to say?';
const message = args.join(' ');
const player = this.players.get(session.id);
if (!player) return 'You must be logged in to chat';
// Broadcast to all players
this.broadcastToAll(`${player.username} says: ${message}`, session.id);
return `You say: ${message}`;
});
this.commands.set('whisper', (session, args) => {
if (args.length < 2) return 'Usage: whisper <username> <message>';
const targetUsername = args[0];
const message = args.slice(1).join(' ');
const sender = this.players.get(session.id);
if (!sender) return 'You must be logged in to send private messages';
// Find target player
const targetPlayer = Array.from(this.players.values()).find(p => p.username === targetUsername);
if (!targetPlayer) return `Player '${targetUsername}' not found`;
// Send to target
const targetSession = targetPlayer.session;
if (targetSession && targetSession.socket) {
targetSession.socket.write(`\r\n${sender.username} whispers: ${message}\r\n${targetSession.prompt || '> '}`);
}
return `You whisper to ${targetUsername}: ${message}`;
});
this.commands.set('chat', (session, args) => {
if (args.length === 0) return 'What do you want to say globally?';
const message = args.join(' ');
const player = this.players.get(session.id);
if (!player) return 'You must be logged in to use global chat';
// Broadcast globally
this.broadcastToAll(`[Global] ${player.username}: ${message}`, session.id);
return `You say globally: ${message}`;
});
this.commands.set('look', (session, args) => {
return 'You are in a test room. This is a simple test implementation.';
});
this.commands.set('quit', (session, args) => {
session.socket.end('Goodbye!\r\n');
return null;
});
// Movement commands
const directions = ['north', 'south', 'east', 'west', 'up', 'down'];
for (const direction of directions) {
this.commands.set(direction, (session, args) => {
const player = this.players.get(session.id);
if (!player) return 'You must be logged in to move';
return `You attempt to move ${direction}...\r\nYou are still in the test area.`;
});
}
// Go command (alternative movement syntax)
this.commands.set('go', (session, args) => {
if (args.length === 0) return 'Which direction do you want to go?';
const direction = args[0].toLowerCase();
const player = this.players.get(session.id);
if (!player) return 'You must be logged in to move';
return `You attempt to move ${direction}...\r\nYou are still in the test area.`;
});
// Talk command for NPC interactions
this.commands.set('talk', (session, args) => {
if (args.length === 0) return 'Talk to whom?';
const npcName = args.join(' ');
const player = this.players.get(session.id);
if (!player) return 'You must be logged in to talk';
if (npcName.toLowerCase().includes('blacksmith')) {
return 'You don\'t see blacksmith here.';
}
return `${npcName} doesn't seem interested in talking.`;
});
// Respond command for dialogue continuation
this.commands.set('respond', (session, args) => {
const player = this.players.get(session.id);
if (!player) return 'You must be logged in to respond';
return 'No active dialogue to respond to.';
});
}
async start() {
return new Promise((resolve, reject) => {
this.server = net.createServer((socket) => {
this.handleConnection(socket);
});
this.server.listen(this.port, () => {
console.log(`Test server listening on port ${this.port}`);
resolve();
});
this.server.on('error', (err) => {
console.error('Server error:', err);
reject(err);
});
});
}
handleConnection(socket) {
const sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const session = {
id: sessionId,
socket,
authenticated: false,
username: null,
prompt: '> '
};
this.sessions.set(sessionId, session);
console.log(`New connection from ${socket.remoteAddress}:${socket.remotePort} (session: ${sessionId})`);
// Send welcome message and username prompt
socket.write('Welcome to the MUD Test Server!\r\n');
socket.write('What is your name? ');
let buffer = '';
socket.on('data', (data) => {
const chunk = data.toString();
buffer += chunk;
// Process complete lines
const lines = buffer.split('\n');
buffer = lines.pop(); // Keep incomplete line
lines.forEach(line => {
const input = line.trim();
if (input) {
this.handleInput(session, input);
}
});
});
socket.on('close', () => {
console.log(`Connection closed for session ${sessionId}`);
this.sessions.delete(sessionId);
if (session.username) {
this.players.delete(sessionId);
this.broadcastToOthers(sessionId, `${session.username} has left the game.`);
}
});
socket.on('error', (err) => {
console.error(`Socket error for session ${sessionId}:`, err);
});
}
handleInput(session, input) {
if (!session.authenticated || !session.username) {
// Handle username input
const username = input.trim();
if (username && username.length > 0) {
// Check if username is already taken
const existingUser = Array.from(this.players.values()).find(p => p.username === username);
if (existingUser) {
session.socket.write('Username already taken. Please choose another: ');
return;
}
session.username = username;
session.authenticated = true;
// Add to players
this.players.set(session.id, {
username,
session,
connectedAt: new Date()
});
// Send welcome message
session.socket.write(`\r\nWelcome, ${username}!\r\n`);
session.socket.write('Type "help" for available commands.\r\n');
session.socket.write(session.prompt);
// Notify other players
this.broadcastToOthers(session.id, `${username} has joined the game.`);
} else {
session.socket.write('Please enter a valid username: ');
}
} else {
// Handle commands
const parts = input.trim().split(' ');
const command = parts[0].toLowerCase();
const args = parts.slice(1);
const commandHandler = this.commands.get(command);
if (commandHandler) {
const response = commandHandler(session, args);
if (response) {
session.socket.write(response + '\r\n');
}
if (response !== null) { // null means disconnect
session.socket.write(session.prompt);
}
} else {
session.socket.write(`Unknown command: ${command}. Type 'help' for available commands.\r\n`);
session.socket.write(session.prompt);
}
}
}
broadcastToAll(message, excludeSessionId = null) {
for (const [sessionId, session] of this.sessions) {
if (sessionId !== excludeSessionId && session.authenticated && session.socket) {
session.socket.write(message + '\r\n');
}
}
}
broadcastToOthers(excludeSessionId, message) {
this.broadcastToAll(message, excludeSessionId);
}
async stop() {
return new Promise((resolve) => {
if (this.server) {
this.server.close(() => {
console.log('Test server stopped');
resolve();
});
// Close all connections
for (const [sessionId, session] of this.sessions) {
if (session.socket) {
session.socket.destroy();
}
}
this.sessions.clear();
this.players.clear();
} else {
resolve();
}
});
}
}
// Start server if run directly
if (require.main === module) {
const server = new TestServer();
server.start().then(() => {
console.log('Test networking server started successfully');
// Graceful shutdown
process.on('SIGINT', async () => {
console.log('\nShutting down...');
await server.stop();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\nShutting down...');
await server.stop();
process.exit(0);
});
}).catch(console.error);
}
module.exports = TestServer;