-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
135 lines (116 loc) · 4.16 KB
/
server.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
/**
* Quick guide to readline for the next steps on the project
*/
var rl = require('readline-sync')
// Asking the user for a word
// Showing the word
// console.log('The word you choose was: ' + word + '!\n')
// Aqui nós fazemos a importação das classes
const LinkedList = require('./Classes/LinkedList')
const Translator = require('./Classes/Translator')
// Aqui nós fazemos a instanciação do Translator
const translator = new Translator();
// A operação de dar load no dicionário é assincrona então esperamos
// Depois do load executamos a função main
translator.loadDictionary('./Database/dicionario.dat').then(() => {
main()
})
// Aqui começa o programa, chamando a função de traduzir a
function main() {
console.log('Howdy Sheriff! Are you ready to know how to speak english?')
console.log(" ___ ")
console.log(" __|___|__ ")
console.log(" ('o_o') ")
console.log(" _\\~-~/_ ______. ")
console.log(" //\\__/\\ \\ ~(_]---' ")
console.log(" / )O O( .\\/_) ")
console.log(" \\ \\ / \\_/ ")
console.log(" )/_| |_\\ ")
console.log(" \/\/ /(\\/)\ \\ ")
console.log(" /_/ \_\\ ")
console.log(" (_|| ||_) ")
console.log(" \\| |__| |/ ")
console.log(" | | | | ")
console.log(" | | | | ")
console.log(" |_| |_| ")
console.log(" /_\\ /_\\ ")
translateWord()
}
// Aqui fazemos a pergunta de qual palavra o usuário deseja traduzir
// Se ele da uma resposta que temos no .dat respondemos com as correspondentes
// Caso não, pedimos para ele inserir ou traduzir outra palavra
function translateWord() {
var word = rl.question('Type the word you wish to translate:\n')
translator.translateWord(word.toLowerCase())
.then(() => {
console.log('What you want to do next?')
console.log(`1 - Translate another word`)
console.log(`2 - Exit`)
const prompt = rl.question('')
switch (parseInt(prompt)) {
case 1:
translateWord()
break
default:
exit()
}
})
.catch(() => {
console.log('What you want to do next?')
console.log(`1 - Add definitions for '${word}'`)
console.log(`2 - Translate another word`)
console.log(`3 - Exit`)
const prompt = rl.question('')
switch (parseInt(prompt)) {
case 1:
addWordToDictionary(word)
break
case 2:
translateWord()
break
default:
exit()
}
})
}
// Adiciona a palavra pra o dicionário e pergunta oq fará depois
async function addWordToDictionary(word) {
const definitions = await setDefinitions(word)
translator.insertTranslation(word, definitions)
console.log('Word and definitions inserted to dictionary')
console.log('What you want to do next?')
console.log(`1 - Translate another word`)
console.log(`2 - Exit`)
const prompt = rl.question('')
switch (parseInt(prompt)) {
case 1:
translateWord()
break
default:
exit()
}
}
function exit() {
const prompt = rl.question('You want to save the new dictionary?(y/N)\n')
if (prompt.toLowerCase() === 'y') {
translator.saveDictionary()
}
}
// Aqui você insere as definições da palavra até não ter mais nenhuma para sugerir
function setDefinitions(word) {
return new Promise((resolve) => {
const definitions = new LinkedList()
let keep = false
do {
let definition = rl.question(`Type a definition for '${word}':\n`)
definitions.insertAtEnd(definition)
let more = rl.question('You want to add more definitions? (y/N)\n')
if (more.toLowerCase() === 'y') {
keep = true
} else {
keep = false
}
} while (keep)
resolve(definitions)
})
}