-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
98 lines (88 loc) · 2.96 KB
/
core.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
var sqlite3 = require('sqlite3').verbose();
require('dotenv').config()
var db = new sqlite3.Database(process.env.DATABASE_PATH);
function migrate_tables_if_necessary(db_table_name) {
db.serialize(function() {
db.run(`CREATE TABLE IF NOT EXISTS ` + db_table_name + ` (
id integer primary key AUTOINCREMENT,
type TEXT,
pinyin TEXT,
hanzi TEXT,
english TEXT,
correct integer,
thinking_time integer,
answer_response_time integer,
created_at integer
);
`);
});
}
async function perform_flashcards_loop(db_table_name, words, chinese) {
console.log("Flashcard loop started, to exit press CTRL-C");
console.log("\n")
while(true) {
await perform_flashcard(db_table_name, words, chinese);
console.log("\n")
}
}
async function perform_flashcard(db_table_name, words, chinese) {
return new Promise(resolve => {
migrate_tables_if_necessary(db_table_name);
const random = Math.floor(Math.random() * words.length);
var word = words[random];
console.log(word);
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var totalThinkingTime = 0;
var correctAnswer = false;
var timeThinkingForAnswerStart = new Date().getTime();
rl.question('What is this word in Chinese? ', function (name) {
// Calculate how long did you think
var timeThinkingForAnswerEnd = new Date().getTime();
totalThinkingTime = timeThinkingForAnswerEnd - timeThinkingForAnswerStart;
console.log(`Correct: 🍏`, chinese[word][0], chinese[word][1]);
var answerResponseTime = 0;
var answerResponseTimeStart = new Date().getTime();
rl.question("Did you guess correctly(input ' if yes)? ", function (name) {
if (name == "'") {
correctAnswer = true;
console.log("Answer: 💚 Correct")
} else {
console.log("Answer: 🔴 Incorrect")
}
// Calculate how long it took to answer
var answerResponseTimeEnd = new Date().getTime();
answerResponseTime = answerResponseTimeEnd - answerResponseTimeStart;
// Save to Database
db.run(`INSERT INTO ` + db_table_name + `(
type,
pinyin,
hanzi,
english,
correct,
thinking_time,
answer_response_time,
created_at
) VALUES($type, $pinyin, $hanzi, $english, $correct, $thinking_time, $answer_response_time, $created_at);
`,
{
$type: 'pinyin',
$pinyin: chinese[word][1],
$hanzi: chinese[word][0],
$english: word,
$correct: correctAnswer,
$thinking_time: totalThinkingTime,
$answer_response_time: answerResponseTime,
$created_at: new Date().getTime()
}
);
rl.close();
resolve();
});
});
});
}
module.exports.perform_flashcards_loop = perform_flashcards_loop;