-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn.js
56 lines (44 loc) · 1.6 KB
/
learn.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
var keys = Object.keys || require('object-keys');
const random = require('random-item');
const preludes = [
"Here's a new word, ",
"Here is a new word for you, ",
"Check if this one is known to you, "
];
let learn = function (admin, words) {
this.db = admin.database();
this.words = words;
};
learn.prototype.handleRequest = function (app) {
let user = app.getUser();
let userId = user.email ? user.email : user.userId;
this.db.ref('/users/' + userId + '/learned_words').once('value', (snapshot) => {
let learnedWords = snapshot.val();
if (!learnedWords) {
learnedWords = [];
}
let allWords = keys(this.words);
let randomWord = random(allWords);
while (learnedWords.includes(randomWord) && learnedWords.length < allWords.length) {
randomWord = random(allWords);
}
learnedWords.push(randomWord);
this.db.ref('/users/' + userId + '/learned_words').set(learnedWords);
let randomPrelude = preludes[Math.floor(preludes.length * Math.random())];
app.ask(
app.buildRichResponse()
.addSimpleResponse(randomPrelude + randomWord)
.addBasicCard(
app.buildBasicCard()
.setTitle(randomWord)
.setSubtitle(this.words[randomWord].pos)
.setBodyText(this.words[randomWord].meaning)
)
.addSuggestions([
"Learn another word",
"Test learned words"
])
);
});
};
exports.learn = learn;