-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
486 lines (437 loc) · 16.5 KB
/
commands.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
"use strict";
const COMMAND_TYPES = {};
const COMMAND_CLASSES = {};
const DEFAULT_VIDEO_ID = "gLHeQsy8juU";
function arrayFilterDirect(arr, filter) {
let f = arr.filter(filter);
arr.length = 0;
for (let x of f) arr.push(x);
}
// parses time string (seconds / minutes:secons / hours:minutes:seconds)
let READ_TIME_CHECK_FUNCTION = function(v) {
if (isNaN(v*1)) {
let parts = (v+"").replace(/ /gi,"").split(":");
if (parts.length > 3) throw new Error("unknwon time format " + v);
let t = 0;
for (let i = 0; i < parts.length; i++) {
let x = parts[i]*1;
if (isNaN(x)) throw new Error("can't read " + parts[i] + " in " + v);
if (x < 0) throw new Error(parts[i] + " is negative (in " + v + ")");
if (x%1 != 0 && i != parts.length-1) throw new Error(parts[i] + " is not a integer (in " + v + ")");
if (x >= 60 && i != 0) throw new Error(parts[i] + " is greater or equal 60 (in " + v + ")");
t = t*60 + x;
}
return t;
} else {
if (v*1 < 0) throw new Error(v + " is negative!");
return v*1;
}
}
COMMAND_TYPES[COMMAND_TYPE_LABEL.name] = COMMAND_TYPE_LABEL;
COMMAND_CLASSES[COMMAND_TYPE_LABEL.name] = COMMAND_CLASS_LABEL;
initEVSCommandClasses();
initEVSCommandTypes();
// EduVideoScript Command Type add
function addCommand(name,parameters,commandBuilder,description) {
COMMAND_TYPES[name] = new EVSCommandType(name,parameters,commandBuilder,description);
}
// EduVideoScript create Parameter Info
function createParameter(id,checkFunction,defaultValue,description) {
return new EVSParameterInfo(id,checkFunction,defaultValue,description);
}
function initEVSCommandTypes() {
/*
addCommand(
"commandName",
[
createParameter("parameter1",...checkFunction..., "default value", "description parameter1"),
createParameter("parameter2",...checkFunction2..., "default value", "description parameter2")
],
(t,p)=>new Command....(t,p.parameter1,p.parameter2, ... ),
"description"
);
*/
addCommand(
"wait",
[
createParameter("time",EVSParameterInfo.CHECK_FUNCTION_CREATE.BETWEEN_INT(0,100000000),"1000","time in milliseconds")
],
(t,p)=>new COMMAND_CLASSES.Wait(t,p.time),
"waits for <time> milliseconds"
);
addCommand(
"log",
[
createParameter("value",EVSParameterInfo.CHECK_FUNCTION.PASS,"no message","debug value")
],
(t,p)=>new COMMAND_CLASSES.Log(t,p.value),
"runs console.log(value) for debuging"
);
addCommand(
"alert",
[
createParameter("text",EVSParameterInfo.CHECK_FUNCTION.PASS,"no message :(","text to be displayed")
],
(t,p)=>new COMMAND_CLASSES.Alert(t,p.text),
"Opens a javascript dialog box and waits until you click on ok."
);
addCommand(
"say",
[
createParameter("text",EVSParameterInfo.CHECK_FUNCTION.PASS,"no text","text to be read aloud"),
createParameter("voice",EVSParameterInfo.CHECK_FUNCTION.PASS,"US English Female","exact voice name (for example \"US English Male\""),//TODO check if voice exist
createParameter("pitch",EVSParameterInfo.CHECK_FUNCTION_CREATE.BETWEEN(0,2),"1","pitch (between 0 and 2)"),
createParameter("rate",EVSParameterInfo.CHECK_FUNCTION_CREATE.BETWEEN(0,1.5),"1","talking speed (between 0 and 1.5)"),
createParameter("volumne",EVSParameterInfo.CHECK_FUNCTION_CREATE.BETWEEN(0,1),"1","talking volumne (between 0 and 1)"),
],
(t,p)=>new COMMAND_CLASSES.Say(t,p.text,p.voice,p.pitch,p.rate,p.volumne),
"uses responivevoice to say the text"
);
addCommand(
"openYTPlayer",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
createParameter("width",EVSParameterInfo.CHECK_FUNCTION_CREATE.BETWEEN_INT(200,8912),"640","width of the youtube player"),
createParameter("height",EVSParameterInfo.CHECK_FUNCTION_CREATE.BETWEEN_INT(200,8912),"320","heigth of the youtube player"),
createParameter("videoId",EVSParameterInfo.CHECK_FUNCTION.PASS,DEFAULT_VIDEO_ID,"youtube video id"),
],
(t,p)=>new COMMAND_CLASSES.OpenYTP(t,p.playerId,p.width,p.height,p.videoId),
"opens a new youtube player"
);
addCommand(
"playYTVideo",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
],
(t,p)=>new COMMAND_CLASSES.PlayYTV(t,p.playerId),
"Plays the video in the youtube player with the playerId."
);
addCommand(
"pauseYTVideo",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
],
(t,p)=>new COMMAND_CLASSES.PauseYTV(t,p.playerId),
"Pauses the video in the youtube player with the playerId."
);
addCommand(
"seekYTVideo",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
createParameter("time",READ_TIME_CHECK_FUNCTION,"2:07.3","time, where to seek in seconds or minutes:secons or hours:minutes:secons"),
],
(t,p)=>new COMMAND_CLASSES.SeekYTV(t,p.playerId,p.time),
"Seeks video in the youtube player with the id playerId to the position."
);
addCommand(
"changeYTVideo",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
createParameter("videoId",EVSParameterInfo.CHECK_FUNCTION.PASS,DEFAULT_VIDEO_ID,"new youtube video id"),
],
(t,p)=>new COMMAND_CLASSES.ChangeYTV(t,p.playerId,p.videoId),
"Change to an other video in the youtube player."
);
addCommand(
"waitYTVideoTime",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
createParameter("time",READ_TIME_CHECK_FUNCTION,"1:23.456","time in video to wait for in seconds"),
],
(t,p)=>new COMMAND_CLASSES.WaitYTVT(t,p.playerId,p.time),
"Waits until the play time is lager or equal time."
);
addCommand(
"volumneYTPlayer",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
createParameter("volumne",EVSParameterInfo.CHECK_FUNCTION_CREATE.BETWEEN_INT(0,100),"100","volumne (between 0 and 100)"),
],
(t,p)=>new COMMAND_CLASSES.VolumneYTP(t,p.playerId,p.volumne),
"Changes the volumne of the youtube player."
);
addCommand(
"fullScreenYTPlayer",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
],
(t,p)=>new COMMAND_CLASSES.FullScreenYTP(t,p.playerId,true),
"Makes the youtube player full screen."
);
addCommand(
"exitFullScreenYTPlayer",
[
createParameter("playerId",EVSParameterInfo.CHECK_FUNCTION.PASS,"ytplayer","youtube player id"),
],
(t,p)=>new COMMAND_CLASSES.FullScreenYTP(t,p.playerId,false),
"Exit youtube player full screen."
);
}
function initEVSCommandClasses() {
COMMAND_CLASSES.Wait = class extends EVSCommand {
constructor(type,time) {
super(type);
this.time = time;
}
async execute(evsInstance) {
await sleep(this.time);
}
async stopMe() {
//Do nothing
}
prepareSkip(cmnds) {
// waiting can simply be skipped
};
}
COMMAND_CLASSES.Log = class extends EVSCommand {
constructor(type,value) {
super(type);
this.value = value;
}
async execute(evsInstance) {
console.log(this.value);
}
async stopMe() {
//Do nothing
}
prepareSkip(cmnds) {
// loging can simply be skipped
};
}
COMMAND_CLASSES.Alert = class extends EVSCommand {
constructor(type,text) {
super(type);
this.text = text;
}
async execute(evsInstance) {
alert(this.text);
}
async stopMe() {
//impossible to close alert with javascript
}
prepareSkip(cmnds) {
// alert can simply be skipped
};
}
COMMAND_CLASSES.Say = class extends EVSCommand {
constructor(type,text,voice,pitch,rate,volumne) {
super(type);
this.text = text;
this.voice = voice;
this.pitch = pitch;
this.rate = rate;
this.volumne = volumne;
}
async execute(evsInstance) {
await speakAsyncEnd(this.text,this.voice,{pitch:this.pitch,rate:this.rate,volumne:this.volumne});
}
async stopMe() {
stopVoice();
}
prepareSkip(cmnds) {
// talking can simply be skipped
};
}
COMMAND_CLASSES.OpenYTP = class extends EVSCommand {
constructor(type,playerId,width,height,videoId) {
super(type);
this.playerId = playerId;
this.width = width;
this.height = height;
this.videoId = videoId;
}
async execute(evsInstance) {
let current = this;
let varName = "ytp_" + this.playerId;
if (evsInstance.getVar(varName) === undefined) {
let player = await newYTP(this.playerId,this.width,this.height,this.videoId);
pauseYTV(player);
evsInstance.setVar(varName,player);
} else {
// TODO reset youtube player and change video
}
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
// OpenYTP can't simply be skipped
cmnds.push(this);
};
}
let PlayerClass = class extends EVSCommand {
constructor(type,playerId) {
super(type);
this.playerId = playerId;
}
async execute(evsInstance) {
let varName = "ytp_" + this.playerId;
if (evsInstance.getVar(varName) === undefined) return;
await this.executeP(evsInstance,evsInstance.getVar(varName));
}
async executeP(evsInstance,player) {
throw new Error("PlayerClass.executeP is abstract!");
}
}
COMMAND_CLASSES.PlayYTV = class extends PlayerClass {
constructor(type,playerId) {
super(type,playerId);
}
async executeP(evsInstance,player) {
await playYTV(player);
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
let current = this;
// remove previous stop and play commands of the same player
arrayFilterDirect(cmnds,function(c) {
if (c instanceof COMMAND_CLASSES.PlayYTV || c instanceof COMMAND_CLASSES.PauseYTV) return c.playerId != current.playerId;
return true;
});
// add play command
cmnds.push(this);
};
}
COMMAND_CLASSES.PauseYTV = class extends PlayerClass {
constructor(type,playerId) {
super(type,playerId);
}
async executeP(evsInstance,player) {
await pauseYTV(player);
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
let current = this;
// remove previous stop and play commands of the same player
arrayFilterDirect(cmnds,function(c) {
if (c instanceof COMMAND_CLASSES.PlayYTV || c instanceof COMMAND_CLASSES.PauseYTV) return c.playerId != current.playerId;
return true;
});
// add play command
cmnds.push(this);
};
}
COMMAND_CLASSES.SeekYTV = class extends PlayerClass {
constructor(type,playerId,time) {
super(type,playerId);
this.time = time;
}
async executeP(evsInstance,player) {
await seekYTV(player, this.time);
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
let current = this;
// remove previous seek commands of the same player
arrayFilterDirect(cmnds,function(c) {
if (c instanceof COMMAND_CLASSES.SeekYTV) return c.playerId != current.playerId;
return true;
});
// add seek command
cmnds.push(this);
};
}
COMMAND_CLASSES.ChangeYTV = class extends PlayerClass {
constructor(type,playerId,videoId) {
super(type,playerId);
this.videoId = videoId;
}
async executeP(evsInstance,player) {
await changeYTV(player, this.videoId);
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
let current = this;
let playing = false;
for (let c of cmnds) {
if (c instanceof COMMAND_CLASSES.PlayYTV) {
if (c.playerId == this.playerId) playing = true;
} else if (c instanceof COMMAND_CLASSES.PauseYTV) {
if (c.playerId == this.playerId) playing = false;
}
}
// remove previous play, pause and seek commands of the same player
arrayFilterDirect(cmnds,function(c) {
if (c instanceof COMMAND_CLASSES.PlayYTV || c instanceof COMMAND_CLASSES.PauseYTV || c instanceof COMMAND_CLASSES.SeekYTV) return c.playerId != current.playerId;
return true;
});
// change video id of openYTPlayer command
for (let c of cmnds) if (c instanceof COMMAND_CLASSES.OpenYTP) if (c.playerId == this.playerId) c.videoId = this.videoId;
if (playing) cmnds.push(COMMAND_TYPES.playYTVideo.build({"playerId":this.playerId}));
};
}
COMMAND_CLASSES.WaitYTVT = class extends PlayerClass {
constructor(type,playerId,time) {
super(type,playerId);
this.time = time;
}
async executeP(evsInstance,player) {
await waitForYTVTime(player, this.time);
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
let current = this;
// remove seeks
arrayFilterDirect(cmnds,function(c) {
if (c instanceof COMMAND_CLASSES.SeekYTV) return c.playerId != current.playerId;
return true;
});
// because it is known that the time has been reached, a seek is added
cmnds.push(COMMAND_TYPES.seekYTVideo.build({"playerId":this.playerId, "time":this.time}));
}
}
COMMAND_CLASSES.VolumneYTP = class extends PlayerClass {
constructor(type,playerId,volumne) {
super(type,playerId);
this.volumne = volumne;
}
async executeP(evsInstance,player) {
await volumneYTP(player, this.volumne);
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
let current = this;
// remove vol changes
arrayFilterDirect(cmnds,function(c) {
if (c instanceof COMMAND_CLASSES.VolumneYTP) return c.playerId != current.playerId;
return true;
});
}
}
COMMAND_CLASSES.FullScreenYTP = class extends PlayerClass {
constructor(type,playerId,full) {
super(type,playerId);
this.full = full;
}
async executeP(evsInstance,player) {
if (this.full) {
fullScreenYTP(player);
} else {
exitFullScreenYTP(player);
}
}
async stopMe() {
//TODO what do do?
}
prepareSkip(cmnds) {
let current = this;
// remove vol changes
arrayFilterDirect(cmnds,function(c) {
if (c instanceof COMMAND_CLASSES.FullScreenYTP) return c.playerId != current.playerId;
return true;
});
}
}
}