forked from victordepablo/P4_Quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
114 lines (76 loc) · 2.53 KB
/
main.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
const readline = require('readline');
//const model = require('./model');
const {log, biglog, errorlog, colorize} = require("./out");
const cmds = require("./cmds");
const net = require("net");
net.createServer(socket => {
console.log("Se ha conectado un cliente desde " + socket.remoteAddress);
// Mensaje inicial
biglog (socket, 'CORE Quiz', 'green');
const rl = readline.createInterface({
input: socket,
output: socket,
prompt: colorize("quiz > ", 'blue'),
completer: (line) => {
const completions = 'h help add delete edit list test p play credits q quit'.split(' ');
const hits = completions.filter((c) => c.startsWith(line));
// show all completions if none found
return [hits.length ? hits : completions, line];
}
});
socket
.on("end", () => { rl.close(); })
.on("error", () => {rl.close(); });
rl.prompt();
rl.on('line', (line) => {
let args = line.split(" ");
let cmd =args[0].toLowerCase().trim();
switch (cmd) {
case '':
rl.prompt();
break;
case 'help':
case 'h':
cmds.helpCmd(socket, rl);
break;
case 'quit':
case 'q':
cmds.quitCmd(socket, rl);
break;
case 'add':
cmds.addCmd(socket, rl);
break;
case 'list':
cmds.listCmd(socket, rl);
break;
case 'show':
cmds.showCmd(socket, rl, args[1]);
break;
case 'test':
cmds.testCmd(socket, rl, args[1]);
break;
case 'play':
case 'p':
cmds.playCmd(socket, rl);
break;
case 'delete':
cmds.deleteCmd(socket, rl, args[1]);
break;
case 'edit':
cmds.editCmd(socket, rl, args[1]);
break;
case 'credits':
cmds.creditsCmd(socket, rl);
break;
default:
log(socket, `Comando desconocido: '${colorize(cmd, 'red')}'`);
log(socket, `Use ${colorize('help', 'green')}' para ver todos los comandos disponibles.`);
rl.prompt();
break;
}
})
.on('close', () => {
log(socket, 'Adios!');
});
})
.listen(3030);