-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom-student.splice.js
More file actions
91 lines (80 loc) · 2.16 KB
/
random-student.splice.js
File metadata and controls
91 lines (80 loc) · 2.16 KB
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
const fs = require('fs');
const gradient = require('gradient-string');
const CFonts = require('cfonts');
const isResetEnabled = process.argv[2] === 'reset';
const logFile = __dirname + '/random-student.log';
// students gets re-assigned to studentsAll on reset.
let students = require('./students.js');
const studentsAll = [...students];
/**
* Read history log file, remove those students from avail students list.
* Then call, or reset history first.
*/
function readHistoryAndCall() {
// Read history log on each run.
fs.readFile(logFile, 'utf-8', (err, history) => {
if (err) throw err;
const called = history.trim().split('\n');
// Removed called students from available students list.
for (let i = 0; i < called.length; i++) {
let calledStudent = called[i];
students.forEach((stu, index) => {
if (calledStudent === stu) {
students.splice(index, 1);
}
});
}
// console.log(students);
// After all students have been called OR reset argument specified.
if (students.length === 0 || isResetEnabled) {
reset(callRandom);
} else {
callRandom();
}
});
}
/**
* Call random student from available students list.
* Using CFonts to output fancy text.
*/
function callRandom() {
const student = students[Math.floor(Math.random() * students.length)];
fs.appendFile(logFile, '\n' + student, (err) => {
if (err) throw err;
// console.log('appendFile: ' + student)
});
const clrs = [
"black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
// "blackBright",
"redBright",
"greenBright",
"yellowBright",
"blueBright",
"magentaBright",
"cyanBright",
"whiteBright"
];
CFonts.say(student, {
colors: [clrs[Math.floor(Math.random() * clrs.length)]]
});
}
/**
* Reset students array to original list
*/
function reset(cb) {
fs.writeFile(logFile, '', (err) => {
if (err) throw err;
console.log(gradient.instagram('All students called! Clearing called log!'));
students = studentsAll;
if (cb) cb();
});
}
//-------------------------------------------------
readHistoryAndCall();