-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (94 loc) · 3.74 KB
/
index.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
/*jshint esversion: 6 */
module.exports = () => {
console.log('Welcome to my Social Networking Kata!');
};
//Import the readline module to read and write input and output to console
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
console_out('Welcome to my Social Networking Kata!Type a name and then a command to start...');
//Import the classes from each js file
const User = require('./src/User');
const Message = require('./src/Message');
const alice = new User('Alice');
const bob = new User('Bob');
const charlie = new User('Charlie');
readline.on('line', function(line) {
var activeUser = line.split(" ")[0];
switch (activeUser) {
case 'alice':
activeUser = alice;
giveCommand();
break;
case 'bob':
activeUser = bob;
giveCommand();
break;
case 'charlie':
activeUser = charlie;
giveCommand();
break;
default:
console_out("That is not a valid command.");
}
function giveCommand() {
if (line.split(" ").length > 1) { //If Enter is hit readline reads the input and process it
var cmd = line.split(" ")[1];
var arg = line.substr(activeUser.name.length + 1 + cmd.length + 1, line.length);
chat_command(activeUser, cmd, arg);
} else {
var userMessages = activeUser.readMessages(activeUser);
for (let index = 0; index < userMessages.length; index++) {
const element = userMessages[index].text;
console_out(element + ' (1 thousand years ago)');
}
}
}
});
function chat_command(activeUser, cmd, arg) { //Parse the commands separating them from user and message
switch (cmd) {
case '->': //Command to write message, if found a message is created and published to timeline
const message = new Message(arg);
activeUser.publishMessage(message);
/* for (let index = 0; index < activeUser.timeline.messages.length; index++) {
const element = activeUser.timeline.messages[index].text;
} */
break;
case 'follows':
switch (arg) {
case 'alice':
arg = alice;
break;
case 'bob':
arg = bob;
break;
case 'charlie':
arg = charlie;
break;
default:
console_out("That is not a valid user.");
}
activeUser.subscribeTo(arg); //If the argument is a user then subscribe to it
for (let index = 0; index < activeUser.subscriptions.length; index++) {
const element = activeUser.subscriptions[index].name;
console_out(activeUser.name + ' has subscribed to ' + element);
}
break;
case 'wall': //Command to read an aggregated list of all messages from all subscibed users
for (let index = 0; index < activeUser.subscriptionsTimeLine().length; index++) {
const element = activeUser.subscriptionsTimeLine()[index].text;
const elementName = activeUser.subscriptionsTimeLine()[index].name;
console_out(elementName + ' - ' + element + ' (1 trillion years ago)');
}
break;
default:
console_out("That is not a valid command.");
}
}
function console_out(msg) { //Fix readline console.log issue that breaks a new line
process.stdout.clearLine();
process.stdout.cursorTo(0);
console.log(msg);
readline.prompt(true);
}