-
Notifications
You must be signed in to change notification settings - Fork 2
/
commandHandler.js
51 lines (45 loc) · 1.58 KB
/
commandHandler.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
let utils = require('./utils');
function setNick(newNick, socket, clients) {
let currentNick = socket.nickName;
// change nickName of client
if (clients[newNick] == null) {
socket.nickName = newNick;
clients[newNick] = socket;
delete clients[currentNick];
utils.broadcast(currentNick + ' changed their nickname to ' + newNick + '\n', socket, clients);
socket.write('Nickname changed to ' + newNick + '\n');
}
else {
socket.write('Nickname ' + newNick + ' is already taken.\n');
}
return clients;
}
function getHelp(socket) {
let helpString = " \
Avaialable commands: \
setnick - Change/set your nickname \
";
socket.write(helpString + '\n');
}
module.exports = function commandHandler(commandString, socket, clients) {
command = commandString.split(' ');
if (command.length == 2) {
switch (command[0]) {
case 'setnick': setNick(command[1], socket, clients);
break;
case 'help': getHelp(socket);
break;
default: socket.write('Unknown command ' + command[0] + '\n');
break;
}
}
else if (command.length == 1) {
switch (command[0]) {
case 'help': getHelp(socket);
break;
default: socket.write('Unknown command ' + command[0] + '\n')
break;
}
}
return clients;
}