forked from RainySY/chaoxing-xuexitong-autoflush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coursepicker.js
135 lines (120 loc) · 3.54 KB
/
coursepicker.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var sleep = (t) => new Promise((y) => setTimeout(y, t));
var CourseList = require("./courselist.js");
let Table = require("tty-table");
let prompt = require("prompts");
let { LiveContainer, LiveArea } = require("clui-live");
class CoursePicker {
constructor(user) {
this.user = user;
this.list = [];
this.pickinfos = {};
this.gui = new LiveContainer();
this.gui_area = this.gui.createLiveArea();
}
async getAllCourses() {
this.list = await new CourseList(this.user).getList();
}
toggle(clazzid) {
if (!this.pickinfos[clazzid]) this.pickinfos[clazzid] = { picked: true };
this.pickinfos[clazzid].picked = !this.pickinfos[clazzid].picked;
}
printCoursesGUI() {
let gui = "";
let header = [
{ value: "序号" },
{ value: "课程ID" },
{ value: "标题" },
{
value: "选中",
color: "red",
width: 10,
formatter: function (val) {
if (val == "是") return this.style(val, "green", "bold");
else return this.style(val, "red");
},
},
];
let built = [];
for (let i in this.list) {
let info = this.pickinfos[this.list[i].clazzId] || { picked: true };
built.push([
i,
this.list[i].courseId,
this.list[i].title,
info.picked ? "是" : "否",
]);
}
let tb = new Table(header, built).render();
gui += tb + "\n\n";
gui += "请选择你要切换选中的课程序号,不填则直接开始刷课:\n";
this.gui_area.write(gui);
//console.log(gui);
}
async input() {
return await prompt({ type: "text", name: "input", message: "" });
}
async pick(config) {
await this.getAllCourses();
while (!config.pick && true) {
this.printCoursesGUI();
let chosen = await this.input();
if (!chosen.input || chosen.input.length <= 0) break;
if (!this.list[chosen.input]) break;
this.toggle(this.list[chosen.input].clazzId);
await sleep(200);
}
if (config.pick) {
if (config.pickinfos) {
this.pickinfos = config.pickinfos;
let header = [
{ value: "序号" },
{ value: "课程ID" },
{ value: "标题" },
{
value: "选中",
color: "red",
width: 10,
formatter: function (val) {
if (val == "是") return this.style(val, "green", "bold");
else return this.style(val, "red");
},
},
];
let built = [];
for (let i in this.list) {
let info = this.pickinfos[this.list[i].clazzId] || { picked: true };
built.push([
i,
this.list[i].courseId,
this.list[i].title,
info.picked ? "是" : "否",
]);
}
let tb = new Table(header, built).render();
this.gui_area.write(tb);
await sleep(1000);
} else {
const pickinfos = {};
for (const i of this.list) {
pickinfos[i.clazzId] = {
picked: true,
title: i.title,
};
}
config.pickinfos = pickinfos;
await config.write();
console.log(
"课程字段已生成请修改config.json中的pickinfos字段来通过部分课程并重启该程序"
);
process.exit(0);
}
}
let picks = [];
for (let i in this.list) {
let info = this.pickinfos[this.list[i].clazzId] || { picked: true };
if (info.picked) picks.push(this.list[i]);
}
return picks;
}
}
module.exports = CoursePicker;