-
Notifications
You must be signed in to change notification settings - Fork 113
/
jobTask.js
66 lines (53 loc) · 1.15 KB
/
jobTask.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
var sleep=(t)=>new Promise((y)=>setTimeout(y,t));
var Player=require("./player.js");
class jobTask{
constructor(classid,jobs,user,playerspeed){
this.user=user;
this.rawjobs=jobs.concat([]);
this.jobs=jobs;
this.taskend=false;
this.clazzId=classid;
this.index=0;
this.currentPlayer=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);
}
}
getJobProgress(){
return {current:this.index,total:this.rawjobs.length};
}
getStatusInfo(){
if(!this.currentPlayer)return "播放器就绪中...\n\n";
return this.currentPlayer.getStatusInfo();
}
async doTick(){
let job=this.jobs.shift();
if(!job){this.taskend=true;return;}
if(!job.isPassed){
let player=new Player(this.clazzId,this.user,job,this.playerspeed);
this.currentPlayer=player;
await player.wait();
}
this.index++;
}
async eventLoop(){
while(!this.taskend){
try{
await this.doTick();
}catch(e){
console.log(e);
}
await sleep(1000);
}
}
}
module.exports=jobTask;