This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messenger.js
411 lines (336 loc) · 8.93 KB
/
messenger.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
'use strict';
const readline = require('readline');
const Back = require('./back.js');
const backend = new Back();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> ',
});
let storage;
let currentLogin;
const commands = {
help() {
list(storage.commandsDescription);
},
hotkeys() {
list(storage.hotkeysDescription);
},
registration() {
registrationScreen();
},
login() {
loginScreen();
},
logout() {
logoutScreen();
},
account() {
accountScreen();
},
friends() {
friendsScreen();
},
messages() {
messageScreen();
},
news() {
newsScreen();
},
exit() {
rl.close();
},
};
async function init() {
await backend.createDB();
storage = await backend.getFrontStorage();
}
async function showMessage(text, color) {
console.log(
storage.colors[color] + storage.messages[text] + storage.colors.white
);
}
async function checkInput(input, arg1, arg2, res1, res2) {
const object = {
[arg1]: res1,
[arg2]: res2
};
for (const [key, value] of Object.entries(object)) {
if (input === key || input === key[0]) {
value();
return;
}
}
showMessage('invalidInput', 'red');
}
function list(object) {
for (const [key, value] of Object.entries(object)) {
console.log(
storage.colors.cyan + key + storage.colors.white + ': ' + value
);
}
}
function difference(s1, s2) {
return new Set([...s1].filter(v => !s2.has(v)));
}
function intersection(s1, s2) {
return new Set([...s1].filter(v => s2.has(v)));
}
function question(str) {
return new Promise(resolve => {
rl.question(storage.questions[str], resolve);
});
}
async function loginScreen() {
if (currentLogin) {
showMessage('loggedIn', 'red');
return;
}
const login = await question('login');
const password = await question('password');
const check = await backend.checkAccount(login, password);
if (check) {
showMessage('logSuccess', 'green');
currentLogin = login;
} else {
showMessage('logFail', 'red');
}
rl.prompt();
}
function logoutScreen() {
if (currentLogin) {
showMessage('logoutSuccess', 'green');
currentLogin = null;
return;
}
showMessage('logoutFail', 'red');
}
async function infoAdd() {
const additional = await question('addInfo');
if (additional === 'y' || additional === 'yes') {
const infoArray = [];
for (let i = 0; i < storage.registrationQuestions.length; i++) {
const answer = await new Promise(resolve => {
rl.question(storage.registrationQuestions[i], resolve);
});
infoArray.push(answer);
}
await backend.addInfo(currentLogin, infoArray);
showMessage('infoAddSuccess', 'green');
return;
}
const infoArray = ['', '', '', '', ''];
await backend.addInfo(currentLogin, infoArray);
showMessage('infoAddLater', 'cyan');
}
async function registrationScreen() {
if (currentLogin) {
showMessage('loggedIn', 'red');
return;
}
const login = await question('loginCreate');
const password = await question('passwordCreate');
const check = await backend.createAccount(login, password);
if (check) {
showMessage('regSuccess', 'green');
currentLogin = login;
await infoAdd();
} else {
showMessage('regFail', 'red');
}
rl.prompt();
}
async function changeInfo(num) {
const infoObject = await backend.getInfo(currentLogin);
const item = storage.profileInfo[num - 1];
infoObject[item] = await question('newInfo');
await backend.changeInfo(currentLogin, infoObject);
showMessage('infoUpdate', 'green');
}
async function changePassword() {
const currentPassword = await question('currentPass');
const result = await backend.checkAccount(currentLogin, currentPassword);
if (result) {
const newPassword = await question('newPass');
const newPasswordRepeat = await question('repeatPass');
if (newPassword === newPasswordRepeat) {
await backend.changePassword(currentLogin, newPassword);
showMessage('passChangeSuccess', 'green');
} else {
showMessage('passChangeFail', 'red');
}
return;
}
showMessage('passCheckFail', 'red');
}
async function editAccount() {
showMessage('whatChange', 'white');
for (let i = 0; i < storage.profileInfo.length; i++) {
console.log(`${i + 1} - ${storage.profileInfo[i]}`);
}
const input = parseInt(await question('options'));
if (!storage.profileInfo[input - 1]) {
showMessage('invalidInput', 'red');
return;
}
if (input < storage.profileInfo.length) {
await changeInfo(input);
return;
}
await changePassword();
}
async function showAccount() {
const infoObject = await backend.getInfo(currentLogin);
showMessage('infoCheck', 'white');
console.log('\u001b[0m\x1b[1A');
console.table(infoObject);
rl.prompt();
}
async function accountScreen() {
if (!currentLogin) {
showMessage('notLoggedIn', 'red');
return;
}
const info = await question('seeEdit');
await checkInput(info, 'see', 'edit', showAccount, editAccount);
rl.prompt();
}
async function friendsScreen() {
if (!currentLogin) {
showMessage('notLoggedIn', 'red');
return;
}
const friendList = await backend.getFriends(currentLogin);
console.log('\n\x1b[1AYour friends:');
for (let i = 0; i < friendList.length; i++) {
console.log(
storage.colors.yellow +
`${i + 1}. ` +
storage.colors.white +
friendList[i]
);
}
const add = await question('addFriend');
if (add === 'y' || add === 'yes') {
const newFriend = await question('username');
await backend.addFriend(currentLogin, newFriend);
}
rl.prompt();
}
async function writeMessage() {
const partner = await question('writeMess');
if (!partner) return;
const messObject = await backend.getMessages(currentLogin, partner);
if (messObject) {
const dialog = messObject
.join('\n')
.split(partner)
.join(storage.colors.blue + partner + storage.colors.white)
.split(currentLogin)
.join(storage.colors.magenta + currentLogin + storage.colors.white);
console.log(dialog);
} else {
showMessage('messagesNew', 'white');
}
const text = await question('newMess');
if (!text) return;
await backend.addMessage(currentLogin, partner, text);
console.log(
storage.colors.magenta + currentLogin +
storage.colors.white + ': ' + text
);
}
async function messageScreen() {
if (!currentLogin) {
showMessage('notLoggedIn', 'red');
return;
}
const friends = await backend.getFriends(currentLogin);
const dif1 = new Set(friends);
const chats = await backend.getChats(currentLogin);
const dif2 = new Set(chats);
const resArr = Array.from(difference(dif1, dif2));
console.log('\n\x1b[1AAvailable chats:');
for (let i = 0; i < chats.length; i++) {
console.log(storage.colors.yellow + '- ' +
storage.colors.white + chats[i]);
}
console.log('\n\x1b[1AFriends you have no chats with:');
for (let i = 0; i < resArr.length; i++) {
console.log(
storage.colors.yellow + '- ' + storage.colors.white + resArr[i]
);
}
await writeMessage();
rl.prompt();
}
async function newsScreen() {
if (!currentLogin) {
showMessage('notLoggedIn', 'red');
return;
}
const friends = await backend.getFriends(currentLogin);
const inter1 = new Set(friends);
const news = await backend.getNews();
const authors = [];
for (let i = 0; i < news.length; i++) {
const postAuthor = news[i].split(':')[0];
authors.push(postAuthor);
}
const inter2 = new Set(authors);
const resArr = Array.from(intersection(inter1, inter2));
console.log('\n\x1b[1ALatest news:');
for (let i = 0; i < news.length; i++) {
if (resArr.indexOf(authors[i].split(':')[0]) !== -1) {
console.log(
news[i]
.split(authors[i])
.join(storage.colors.yellow + authors[i] +
storage.colors.white)
);
}
}
const add = await question('postSmth');
if (add === 'y' || add === 'yes') {
const news = await question('newPost');
await backend.addNews(currentLogin, news);
console.log(
storage.colors.yellow + currentLogin +
storage.colors.white + ': ' + news
);
}
rl.prompt();
}
(async () => {
await init();
console.clear();
showMessage('greeting', 'white');
showMessage('helpComm', 'cyan');
rl.prompt();
})();
rl.on('line', line => {
line = line.trim();
const command = commands[line];
if (command) {
command();
} else {
showMessage('unknown', 'red');
}
rl.prompt();
})
.on('close', () => {
showMessage('bye', 'white');
process.exit(0);
})
.on('SIGINT', () => {});
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
for (const [item, value] of Object.entries(storage.buttons)) {
if (key.name === item && key.ctrl) {
console.log(`Ctrl + ${key.name} (${value})`);
commands[value]();
rl.prompt();
}
}
});