-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
47 lines (41 loc) · 1.03 KB
/
input.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
let connection; // Stores the active TCP connection object
const handleUserInput = (key) => {
if (key === '\u0003') { // \u0003 maps to ctrl+c input
process.exit();
}
// adding movement commands
if (key === 'w') {
connection.write("Move: up");
}
if (key === 'a') {
connection.write("Move: left");
}
if (key === 's') {
connection.write("Move: down");
}
if (key === 'd') {
connection.write("Move: right");
}
// Sending messages to the server
if (key === '1') {
connection.write("Say: I got this!!");
}
if (key === '2') {
connection.write("Say: You can't beat me!");
if (key === '3') {
connection.write("Say: BOOOM!");
}
if (key === '4') {
connection.write("Say: Better luck next time!");
}
};
const setupInput = (conn) => {
connection = conn; // save the connection object
const stdin = process.stdin;
stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdin.resume();
stdin.on('data', handleUserInput);
return stdin;
};
module.exports = { setupInput };