forked from hczhcz/telegram-kuso-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.resource.js
50 lines (38 loc) · 1.1 KB
/
hangman.resource.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
'use strict';
const fs = require('fs');
const readline = require('readline');
const config = require('./config');
const core = require('./hangman.core');
const dicts = {};
const load = (id, limit, onDone, onNotValid) => {
let dictInfo = null;
for (const i in config.hangmanDict) {
if (config.hangmanDict[i].id === id) {
dictInfo = config.hangmanDict[i];
break;
}
}
if (dictInfo) {
if (dicts[id + '_' + limit]) {
onDone(dictInfo, dicts[id + '_' + limit]);
} else {
const rl = readline.createInterface({
input: fs.createReadStream('hangman/' + id + '.dict'),
});
const dict = core.dictInit();
rl.on('line', (line) => {
if (!limit || dict.list.length < limit) {
core.dictAdd(dict, line);
}
}).on('close', () => {
dicts[id + '_' + limit] = dict;
onDone(dictInfo, dict);
});
}
} else {
onNotValid();
}
};
module.exports = {
load: load,
};