-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDailyTask.ts
More file actions
59 lines (52 loc) · 1.53 KB
/
DailyTask.ts
File metadata and controls
59 lines (52 loc) · 1.53 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
class DailyTask extends DefaultTask implements Task {
public hour: number
public minute: number
public canExecuteToday: () => boolean
constructor(
taskRaw: string,
func: (args) => any,
hour: number,
minute: number,
dayChecker: () => boolean
) {
super(taskRaw, func)
this.hour = hour;
this.minute = minute;
this.canExecuteToday = dayChecker;
}
/**
* @param context
* @param taskRaw
* @example dailyTask Bot_AskStatuses 16 30 workDay
*/
static parse(context: object, taskRaw: string): Task {
const regExp = new RegExp(`^(\\w+) (\\w+) (\\d{1,2}) (\\d{1,2}) (${Object.keys(Context.dayChecks).join("|")})$`);
const match = taskRaw.trim().match(regExp)
if (match) {
const [all, type, funcName, hour, minute, dayCheck] = match
return new DailyTask(taskRaw, context[funcName], +hour, +minute, Context.dayChecks[dayCheck])
}
return new InvalidTask(taskRaw)
}
isValid(): boolean {
return super.isValid() &&
(typeof this.hour == 'number') &&
(typeof this.minute == 'number') &&
(typeof this.canExecuteToday == 'function') &&
(this.hour >= 0 && this.hour <= 23) &&
(this.minute >= 0 && this.minute <= 59)
}
getTaskName(): string {
return `(${this.hour}:${this.minute}) "${this.func.name}"`
}
getScheduledTimestamp(): number {
if (!this.canExecuteToday()) {
return 0
}
const date = new Date();
date.setHours(this.hour);
date.setMinutes(this.minute);
date.setSeconds(0);
return +date;
}
}