forked from RainySY/chaoxing-xuexitong-autoflush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
courseTask.js
97 lines (83 loc) · 2.14 KB
/
courseTask.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
var sleep = (t) => new Promise((y) => setTimeout(y, t));
var Net = require("./net.js");
var chapterTask = require("./chapterTask.js");
var Course = require("./course.js");
var { LiveContainer, LiveArea } = require("clui-live");
class courseTask {
constructor(courses, user, speed, autotest) {
this.taskend = false;
this.user = user;
this.speed = speed;
this.current = undefined;
this.courses = courses;
this.autotest = autotest;
setInterval(() => {
this.drawGUI();
}, 1000);
this.eventLoop();
}
async init() {
console.log("\n".repeat(30)); //清屏
this.gui = new LiveContainer();
this.gui_area = this.gui.createLiveArea();
}
async drawGUI() {
if (!this.current) return;
let nextcourse = this.courses[0];
let minheight = 30;
let lines = this.current.getGUI();
lines.unshift(
"当前课程:" +
(this.current_course ? this.current_course.title : "无") +
" 下一个课程:" +
(nextcourse ? nextcourse.title : "无") +
"\n"
);
if (lines.length < minheight)
for (let i = 0; i < minheight - lines.length; i++) lines.push("");
//for(let i in lines)
// this.gui_area.write(lines.join("\n"));
console.log(lines.join("\n"));
}
async doTick() {
let course = this.courses.shift();
if (!course) {
this.taskend = true;
return;
}
let cs = new Course(course.clazzId, course.courseId, this.user);
let cps = await cs.getChapters();
// console.log(course,cps);
let task = new chapterTask(
course.clazzId,
cps,
this.user,
this.speed,
this.autotest
);
this.current_course = course;
this.current = task;
await task.wait();
}
async wait(timeout) {
let tick = 0;
if (!timeout) timeout = 9999999;
while (true) {
if (this.taskend) return;
if (tick++ > timeout) return;
await sleep(500);
}
}
async eventLoop() {
await this.init();
while (!this.taskend) {
try {
await this.doTick();
} catch (e) {
console.log(e);
}
await sleep(1000);
}
}
}
module.exports = courseTask;