-
Notifications
You must be signed in to change notification settings - Fork 113
/
chapterTask.js
152 lines (125 loc) · 3.36 KB
/
chapterTask.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var sleep=(t)=>new Promise((y)=>setTimeout(y,t));
let Course=require("./course.js");
let jobTask=require("./jobTask.js")
function reduceTree(tree,lvl){
if(!lvl)lvl=0;
let ret=[];
for(let i in tree)
{
ret.push({id:tree[i].id,title:tree[i].title,courseid:tree[i].courseid,lvl});
if(tree[i].subchaps && tree[i].subchaps.length>0)
ret=ret.concat(reduceTree(tree[i].subchaps,lvl+1));
}
return ret;
}
class chapterTask{
constructor(classid,chapters,user,playerspeed){
let red=reduceTree(chapters);
this.rawchapters=red.concat([]);
this.chapters=red;
this.user=user;
this.taskend=false;
this.clazzId=classid;
this.current=undefined;
this.current_task=undefined;
this.playerspeed=playerspeed;
this.eventLoop();
}
async wait(timeout){
let tick=0;
if(!timeout)timeout=9999999;
while(true){
if(this.taskend)
return;
if(tick++>timeout)return;
await sleep(500);
}
}
getGUI(){
if(!this.current)return [];
let built=[];
//built.push("\n\n\n\n\n\n\n\n\n\n");
built.push("\n");
let focus=-1;
for(let i in this.rawchapters){
let iscurrent=this.rawchapters[i].id==this.current.id;
if(iscurrent)focus=i;
built.push(" ".repeat(this.rawchapters[i].lvl)+this.rawchapters[i].title+" "+(iscurrent?" <- ":"")+" "+(this.rawchapters[i].tipinfo || ""));
}
let offset=10;
let start=parseInt(focus)-offset>0?(parseInt(focus)-offset):0;
let end=parseInt(focus)+offset>built.length-1?(built.length-1):(parseInt(focus)+offset);
let rebuilt=[];
for(let i in built){
if(parseInt(i)>start && parseInt(i)<end)
rebuilt.push(built[i]);
}
rebuilt.unshift(this.getStatusInfo());
// this.refreshTipInfo();
return rebuilt;
}
refreshTipInfo(){
if(!this.current_task)return;
let jobprogress=this.current_task.getJobProgress();
let tipinfo=`(${jobprogress.current}/${jobprogress.total})`;
this.rawchapters.find((x)=>(x.id==this.current.id)).tipinfo=tipinfo;
return tipinfo;
}
getStatusInfo(){
if(!this.current || ! this.current_task)return "就绪中...";
return " 进行中的章节 -> "+this.current.title+"\n\n"+
this.current_task.getStatusInfo();
}
async studyChapter(chapter){
return this.user.net.post("mycourse/studentstudyAjax",{
courseId:chapter.courseid,
clazzid:this.clazzId,
chapterId:chapter.id,
cpi:0,
verificationcode:""
})
}
async doTick(){
let chapter=this.chapters.shift();
this.current=chapter;
if(!chapter){this.taskend=true;return;}
await this.studyChapter(chapter);
let course=new Course(this.clazzId,chapter.courseid,this.user);
let jobs=await course.getVideoJobs(chapter.id);
let task=new jobTask(this.clazzId,jobs,this.user,this.playerspeed);
this.current_task=task;
this.refreshTipInfo();
await task.wait();
this.refreshTipInfo();
}
async eventLoop(){
while(!this.taskend){
try{
await this.doTick();
}catch(e){
console.log(e);
}
await sleep(1000);
}
}
}
module.exports=chapterTask;
/*
var Net=require("./net.js");
let net=new Net("https://mooc1-1.chaoxing.com/");
net.setCookie(``);
let courselist=require("./courselist.js");
(async()=>{
let user={
userid:"94374572",
net
};
let classid="13132734";
let courseid="206211149";
let cs=new Course(classid,courseid,user);
let cps=await cs.getChapters();
console.log(cps);
//new chapterTask(classid,cps,user);
console.log(await (new courselist(user)).getList());
})();
*/