-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
984 lines (877 loc) · 32.7 KB
/
main.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
#!/usr/bin/env node
console.log('===============================');
console.log(' ProPresenter Arena bridge ');
console.log('===============================');
console.log('\nUsage:\n');
console.log('Enable HTTP Api in Resolume Arena');
console.log('Enable Remote Conrol "Stage" in ProPresenter');
console.log('Setup config.json file according to the credentials in Arena and Propresenter');
console.log('Add "#pab" to the clip name in Resolume Arena which cointains "Text box" source');
console.log('')
console.log('\nExample modificators:\n');
console.log('-uc : UPPERCASE');
console.log('-lc : lovercase');
console.log('-cp : Caps Each Word');
console.log('')
console.log('\nExample manipulators:\n');
console.log('-fw : First word only');
console.log('-lw : Last word only');
console.log('-re : Replace ENTER char to SPACE');
console.log('-rd : Trim dot "."');
console.log('-rc : Trim comma ","');
console.log('-rv : Trim verse label "<sup>1</sup>" - works only if label is wrapped in <sup> tag');
console.log('')
console.log('\nExample box:\n');
console.log('-1,2..n : "1,2,.." means Slide first or second or nth text box only');
console.log('')
console.log('\nExample triggers:\nTriggers for "Zig-Zag" triggering.\n');
console.log('-a : "a" means trigger only on odd');
console.log('-b : "b" means trigger only on even');
console.log('-c : "c" means trigger only if all box are cleared aka nothing to show fallback');
console.log('')
console.log('Tags can be combined (order is not relevant):\n')
console.log('#pab-a-uc-fw')
console.log('#pab-fw-uc-a')
console.log('#pab-uc-a-fw')
console.log('\nAll works the same way.\n')
console.log('===============================\n');
//
//require('console')
//import os from 'os'
import fs from 'fs';
import fetch from 'node-fetch'
import WebSocket from 'ws';
import Gun from "gun";
import exp from 'constants';
//import { setgid } from 'process';
//import { clear } from 'console';
//
let config = readConfiguration()
//
const arena_path_clip_by_id = "/composition/clips/by-id";
const arena_path_layer_by_id = "/composition/layers/by-id";
//
//var config = {}
//
var arena_state = 'disconnected';
//
var arena_check_timeout;
var propresenter_check_timeout;
//
var arena = []
//cycle for odd/event
var cycle = false;
//const sup_array = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "⁾ "]
const sup_array = ["0", "1", "2", "3", "4", "5", "6", "7", "9", "0", ") "]
// read file everytime if needed to change conf without restart the app
function readConfiguration() {
var config_ = null;
try {
config_ = JSON.parse(fs.readFileSync('./config.json'))
} catch (e) {
try {
config_ = JSON.parse(fs.readFileSync('./config_default.json'))
} catch (e) {
console.error("No config file", e)
console.error("No config file")
console.error("No config file")
console.error("No config file")
console.error("No config file")
console.error("No config file")
console.error("No config file")
console.error("No config file")
//die
process.exit()
return undefined
}
}
console.log("CONFIG", config_)
return config_;
}
function propresenter_reconnect() {
clearTimeout(propresenter_check_timeout);
propresenter_check_timeout = setTimeout(function () {
return propresenter_connect();
}, 10000);
}
function propresenter_connect() {
//refresh config
if (config.propresenter.enabled !== true) {
console.warn("\n------\nProPresenter Module is disabled!\n------\n");
return;
}
console.log('ws://' + config.propresenter.host + ':' + config.propresenter.port + '/stagedisplay');
const ws = new WebSocket('ws://' + config.propresenter.host + ':' + config.propresenter.port + '/stagedisplay');
//
ws.on('open', function open() {
console.log('ProPresenter: Connection Established');
console.log('ProPresenter: Sending Password');
ws.send('{"acn":"ath","ptl":610,"pwd":"' + config.propresenter.pass + '"}');
});
//set error handle
ws.on('error', function (error) {
console.log("ProPresenter: Connection Error: " + error.toString());
});
//set close handle
ws.on('close', function close() {
console.log('ProPresenter: Connection Closed');
return propresenter_reconnect();
});
//setup message handle
ws.on('message', function message(data) {
data = data.toString()
console.log(data)
//check fv data before json parse to safe cpu
if (data.includes('"acn":"ath"')) {
data = JSON.parse(data);
console.log(data);
if (!data || !data.acn || !data.ath) {
console.error("\n\n\nPropresenter: Auth failed\n\n\n")
return propresenter_reconnect();
}
return;
}
//console.log(data);
//var data = JSON.parse(message.utf8Data);
if (data.includes('"acn":"cc"') && config.propresenter.presentation_request) {
console.log("\n\n\n\n\n\n\n\n\n\n\n", "CC event received", "\n\n\n\n\n\n\n\n\n\n\n")
//i do not know what data is uid from cc event so who cares and give me whole active presentation
return propresenter_presentation_request();
}
if (data.includes('"acn":"fv"')) {
//console.log({slide: data});
return propresenter_parse_slide(JSON.parse(data))
}
return;
});
}
async function propresenter_presentation_request(uuid = 'active', attempt = 0) {
if (!config.propresenter.presentation_request) {
console.info("ProPresenter: presentation_request not enabled");
return;
}
try {
const response = await fetch('http://' + config.propresenter.host + ':' + config.propresenter.port + '/v1/presentation/' + uuid + '?chunked=false');
//const response = await fetch('https://api.github.com/users/github');
if (!response.ok) {
console.log("ProPresenter: presentation_request not OK");
if (attempt < 2) {
return propresenter_presentation_request(uuid, attempt++);
}
return;
}
let data = await response.json();
if (!data || data == undefined) {
console.log("ProPresenter: presentation_request undefined response");
return;
}
//console.log("ProPresenter: presentation_request OK");
return propresenter_parse_presentation_data(data);
} catch (error) {
console.log("ProPresenter: presentation_request error", error);
return;
}
}
async function propresenter_parse_presentation_data(data) {
//console.log("ProPresenter: propresenter_parse_presentation_data", data)
//
if (data === undefined || data.presentation === undefined) {
console.log("ProPresenter: undefined data");
return;
}
return execute_pab_presentation(data.presentation)
}
function parse_first_word(words) {
//
let word = ''
let a = (words[0] !== undefined && words[0] !== '') ? words[0].replace(/^[\ \,\.\:\;\"\'\(\)\-\n]+|[\ \,\.\:\;\"\'\(\)\-\n]+$/gm, "") : ''
// feel fre to modify to your needs
if (a.length > 5) {
word = a;
} else {
let b = (words[1] !== undefined && words[1] !== '') ? words[1].replace(/^[\ \,\.\:\;\"\'\(\)\-\n]+|[\ \,\.\:\;\"\'\(\)\-\n]+$/gm, "") : ''
if (a == 'a' || a == 'A') {
word = b;
} else {
word = a + ' ' + b
}
}
// add spaces between words to enhance effect
word = word.replace(/^[\ ]+|[\ ]+$/gm, "").replace(/\ /, " ");
//
return word
}
function parse_last_word(words) {
//
let word = ''
let a = (words[words.length - 1] !== undefined && words[words.length - 1] !== '') ? words[words.length - 1].replace(/^[\ \,\.\:\;\"\'\(\)\-\n]+|[\ \,\.\:\;\"\'\(\)\-\n]+$/gm, "") : ''
//.replace(/^\n+|\n+$/gm, "")
// feel fre to modify to your needs is very dependant on local language
if (a.length > 5) {
word = a;
} else {
let b = (words[words.length - 2] !== undefined && words[words.length - 2] !== '') ? words[words.length - 2].replace(/^[\ \,\.\:\;\"\'\(\)\-\n]+|[\ \,\.\:\;\"\'\(\)\-\n]+$/gm, "") : ''
//
if (b == "to") {
let c = (words[words.length - 3] !== undefined && words[words.length - 3] !== '') ? words[words.length - 3].replace(/^[\ \,\.\:\;\"\'\(\)\-\n]+|[\ \,\.\:\;\"\'\(\)\-\n]+$/gm, "") : ''
word = c + ' ' + b + ' ' + a;
} else {
if (b == "ma" || b == "sa") {
word = a;
} else {
word = b + ' ' + a;
}
}
}
// add spaces between words to enhance effect
word = word.replace(/^[\ ]+|[\ ]+$/gm, "").replace(/\ /, " ");
//
return word
}
function parse_slide_segments(segments) {
//
let segments_ = []
//
for (var i = 0; i < segments.length; i++) {
// trim new lines
segments[i] = segments[i].replace(/\{break\}/g, "").replace(/(^\n+)|(\n+$)/g, "").trim()
// first word ana last word is little tricky because wo cant read the words and we dont know the context
// we use the litle trick to "join" common words with pre-words
//
let words = segments[i].split(' ')
//
let first_word = parse_first_word(words)
let last_word = parse_last_word(words)
//
// manipulaciu robim pri uploade, potrebujem uz len txt, fw, lw
segments_.push({
txt: segments[i],
fw: first_word,
lw: last_word
})
}
//
return segments_;
}
async function propresenter_parse_slide(data) {
//console.log("ProPresenter: Slide", data)
//
if (data.ary === undefined) {
console.log("ProPresenter: undefined data");
return;
}
// struct
let slide = {
current: {
txt: '',
segments: [
]
},
next: {
txt: '',
segments: [
]
}
}
for (var i = 0; i < data.ary.length; i++) {
//console.log(content[i]);
if (data.ary[i].acn !== 'cs' && data.ary[i].acn !== 'ns') {
//skip other stuff
continue;
}
//
let txt = ''
let split = []
switch (data.ary[i].acn) {
case 'cs':
if (data.ary[i].txt !== '') {
// optimalisation
txt = data.ary[i].txt.replace(/^\x82+|\x82+$/gm, "")
txt = txt.replace(/(^\r+)|(\r+$)/g, "").replace(/\n|\x0B|\x0C|\u0085|\u2028|\u2029/g, "\n")
//replace non-printable char
txt = txt.replace(/\u00a0/gm, " ");
// reverse order
//split = txt.split("\r").reverse()
// stadnard order
split = txt.split("\r")
txt = split.join("\r")
//
slide.current = parse_slide_segments([txt])[0]
//
if (split.length > 1) {
slide.current.segments = parse_slide_segments(split)
}
}
break;
case 'ns':
if (data.ary[i].txt !== '') {
// optimalisation
txt = data.ary[i].txt.trim().replace(/^\x82+|\x82+$/gm, "").replace(/(^\r+)|(\r+$)/g, "").replace(/\n|\x0B|\x0C|\u0085|\u2028|\u2029/g, "\n")
//replace non-printable char
txt = txt.replace(/\u00a0/gm, " ");
// reverse order
//split = txt.split("\r").reverse()
// standard order
split = txt.split("\r")
txt = split.join("\r")
//
slide.next = parse_slide_segments([txt])[0]
//
if (split.length > 1) {
slide.next.segments = parse_slide_segments(split)
}
}
break;
}
}
console.log(slide.current)
return execute_pab_slide(slide)
}
// gun connection
// https://github.com/filiphanes/gun-overlays/tree/public
var gan_allinone_last = '{}';
var gan_shown_last = false;
function gun_connect() {
console.log("GUN: Connect")
//
if (config.gun_overlays.enabled !== true) {
console.warn("\n\n------\n\nGUN Overlays Module is disabled!\n\n------\n\n");
return;
}
//
let gun = Gun([config.gun_overlays.peer]);
let overlay = gun.get(config.gun_overlays.service).get(config.gun_overlays.namespace);
let data = overlay.get('allinone').on(function (data, key) {
if (data !== gan_allinone_last) {
// we fire the vent just if last data chanted
gan_allinone_last = data
// we need to know if the event is ment for show or if it is clear event
data = JSON.parse(data)
if (data.shown || (data.shown !== gan_shown_last)) {
//if the last event shown is FALSE and is the same as last time wi do not trigger arena
gan_shown_last = data.shown
gun_overlays_parse_slide(data)
}
}
//console.log("Last\n", allinone_last)
//console.log("\n\n", key, "\n");
//console.log(JSON.parse(data));
});
}
async function gun_overlays_parse_slide(data) {
//console.log("GUN: Slide data ", data)
//
if (data === undefined) {
console.log("GUN: undefined data");
return;
}
//
//console.log("GUUUUUUUUUUUN\n", data)
let txt = ''
let split = []
//hack for now
if (data.shown) {
txt = data.line1 + "\r\n" + data.line2 + "\r\n" + data.line3 + "\r\n" + data.line4;
}
//console.log("GUN: Slide txt ", txt)
//translate data from gun_overlays to pab
// struct
let slide = {
current: {
txt: '',
segments: [
]
},
next: {
txt: '',
segments: [
]
}
}
// optimalisation
txt = txt.trim().replace(/^\x82+|\x82+$/gm, "").replace(/(^\r+)|(\r+$)/g, "").replace(/\n|\x0B|\x0C|\u0085|\u2028|\u2029/g, "\n")
//replace non-printable char
txt = txt.replace(/\u00a0/gm, " ");
//console.log(txt);
txt = txt.replaceAll(/<sup>/g, "@").replaceAll(/<\/sup>/g, "@");
// replace html tags
txt = txt.replace(/(<([^>]+)>)/ig, "");
// reverse order
//split = txt.split("\r").reverse()
// stadnard order
split = txt.split("\r")
txt = split.join("\r")
//console.log(split)
//return;
//
slide.current = parse_slide_segments([txt])[0]
//
if (split.length > 1) {
slide.current.segments = parse_slide_segments(split)
}
//console.log(slide.current);
return execute_pab_slide(slide)
}
function arena_reconnect() {
clearTimeout(arena_check_timeout)
arena_state = 'disconnected';
arena_check_timeout = setTimeout(function () {
return arena_connect();
}, 10000)
}
async function arena_connect() {
clearTimeout(arena_check_timeout)
try {
const response = await fetch('http://' + config.arena.host + ':' + config.arena.port + '/api/v1/composition');
//const response = await fetch('https://api.github.com/users/github');
if (!response.ok) {
console.log("Arena: Connection not OK");
return arena_reconnect();
}
console.log("Arena: Connection OK");
arena_state = 'connected';
return arena_determine_clips();
} catch (error) {
//console.log("Arena: Connection error", error);
return arena_reconnect();
}
}
async function arena_determine_clips() {
console.log("determine_arena");
//
// arena have http api so we dont know if is on or not we use check state before
if (arena_state != 'connected') {
return arena_connect()
}
// arena is connected
let data;
try {
const response = await fetch('http://' + config.arena.host + ':' + config.arena.port + '/api/v1/composition');
//const response = await fetch('https://api.github.com/users/github');
if (!response.ok) {
console.log("Arena: Connection not OK");
return arena_reconnect();
}
console.log("Arena: Connection OK");
arena_state = 'connected';
data = await response.json();
if (!data || data == undefined) {
console.log("Arena: No data");
return arena_reconnect();
}
//
} catch (error) {
console.log("Arena: Connection error", error);
return arena_reconnect();
}
// check layers
if (data.layers === undefined) {
console.log("No layers", data);
return arena_reconnect();
}
let layer
let clips
let clip
let clip_name_pab
//reset global
arena = []
clips = []
//
// layers from top to bottom
// top layers is more important
let layer_id = null
for (var i = data.layers.length - 1; i >= 0; i--) {
//travers all layers
layer_id = data.layers[i].id
layer = data.layers[i].clips;
clips = []
//console.log(clips);
for (var x = 0; x < layer.length; x++) {
//travers all clips in layer
if (layer[x].name == undefined || layer[x].name.value == undefined || layer[x].name.value == '' || !layer[x].name.value.includes("#pab")) {
//skip clip because don have pab tag
continue;
}
clip_name_pab = layer[x].name.value.match(/#pab\S*/g)[0]
//
clip = {
id: layer[x].id,
layer_id: layer_id,
name: layer[x].name.value,
params: {
box: (clip_name_pab.match(/.*\-(\d+).*/g)) ? clip_name_pab.match(/.*\-(\d+).*/)[1] : null,
rd: (clip_name_pab.match(/.*\-rd(?!\w).*/g)) ? true : false, //remove dots
rc: (clip_name_pab.match(/.*\-rc(?!\w).*/g)) ? true : false, //remove commas
re: (clip_name_pab.match(/.*\-re(?!\w).*/g)) ? true : false, //remove new lines
rv: (clip_name_pab.match(/.*\-rv(?!\w).*/g)) ? true : false, //remove verses
uc: (clip_name_pab.match(/.*\-uc(?!\w).*/g)) ? true : false,
lc: (clip_name_pab.match(/.*\-lc(?!\w).*/g)) ? true : false,
cp: (clip_name_pab.match(/.*\-cp(?!\w).*/g)) ? true : false,
//
pn: (clip_name_pab.match(/.*\-pn(?!\w).*/g)) ? true : false, //presentation name
pnc: (clip_name_pab.match(/.*\-pnc(?!\w).*/g)) ? true : false, //presentation name
fw: (clip_name_pab.match(/.*\-fw(?!\w).*/g)) ? true : false,
lw: (clip_name_pab.match(/.*\-lw(?!\w).*/g)) ? true : false,
//
a: (clip_name_pab.match(/.*\-a(?!\w).*/g)) ? true : false, //trigger on a cycle
b: (clip_name_pab.match(/.*\-b(?!\w).*/g)) ? true : false, //trigger on b cycle
c: (clip_name_pab.match(/.*\-c(?!\w).*/g)) ? true : false, //trigger on clear
ns: (clip_name_pab.match(/.*\-ns(?!\w).*/g)) ? true : false,
sg: (clip_name_pab.match(/.*\-sg(\d+)/g)) ? parseInt(clip_name_pab.match(/.*\-sg(\d+)/g)[0].match(/.*\-sg(\d+)/)[1], 10) : false, //size guard
}
}
// now we populate array with objects
clips.push(clip)
}
if (clips.length > 0) {
console.log(clips)
arena.push(clips)
}
}
}
async function arena_push_presentation_data(data) {
try {
const response = await fetch('http://' + config.propresenter.host + ':' + config.propresenter.port + '/v1/presentation/' + uuid + '?chunked=false');
//const response = await fetch('https://api.github.com/users/github');
if (!response.ok) {
console.log("ProPresenter: presentation_request not OK");
if (attempt < 2) {
return propresenter_presentation_request(uuid, attempt++);
}
return;
}
//console.log("ProPresenter: presentation_request OK");
return arena_push_presentation_data();
} catch (error) {
console.log("ProPresenter: get_presentation error", error);
return;
}
}
async function execute_pab_slide_triggers(clips, connect = true) {
if (arena_state != 'connected') {
console.error("Execute: Arena NOT connected")
return;
}
for (var i = 0; i < clips.length; i++) {
//
//console.log("execute_pab_slide_triggers", clips[i])
try {
var response = null;
if (connect) {
response = await fetch('http://' + config.arena.host + ':' + config.arena.port + '/api/v1' + arena_path_clip_by_id + '/' + clips[i].id + '/connect', { method: 'POST', body: '' });
} else {
console.log('CLEAR WHOLE LAYER?????');
// /composition/layers/by-id/{layer-id}/clear
response = await fetch('http://' + config.arena.host + ':' + config.arena.port + '/api/v1' + arena_path_layer_by_id + '/' + clips[i].layer_id + '/clear', { method: 'POST', body: '' });
}
if (!response) {
console.error("Response not defined")
}
//const response = await fetch('https://api.github.com/users/github');
if (!response.ok) {
console.error("Arena: Trigger failed");
continue;
//return arena_reconnect();
}
continue;
//
} catch (error) {
console.error("Arena: Connection error", error);
continue;
//return arena_reconnect();
}
}
}
async function arena_update_clip(id, text) {
//console.log("arena_update_clip", id)
let obj = {
method: 'PUT',
body: JSON.stringify({ "video": { "sourceparams": { "Text": text } } }),
headers: { 'Content-Type': 'application/json' }
}
try {
const response = await fetch('http://' + config.arena.host + ':' + config.arena.port + '/api/v1' + arena_path_clip_by_id + '/' + id + '', obj);
//const response = await fetch('https://api.github.com/users/github');
if (!response.ok) {
console.error("Arena: PUT failed", id, obj);
return;
//return arena_reconnect();
}
return;
//
} catch (error) {
console.error("Arena: Connection error", error);
return;
//return arena_reconnect();
}
}
async function execute_pab_presentation(presentation) {
if (arena_state != 'connected') {
console.error("Execute: Arena NOT connected")
return;
}
if (arena.length == 0) {
console.log("execute_pab_slide", "No clips")
return;
}
let clip
let layer
let text_for_clip = ''
let same_layer_triger_protect = false
let actual = presentation; //hack
//
for (var i = 0; i < arena.length; i++) {
//console.log('LAYER %d\n', i)
//layers
layer = arena[i]
// disable protection for triggering on same page
same_layer_triger_protect = false
//
for (var x = 0; x < layer.length; x++) {
//slide
//console.log('\n\tclip %d', x)
clip = layer[x]
//skipp all slide related clips
if (!clip.params.pn && !clip.params.pnc) {
continue;
}
//console.log(clip)
//default text
text_for_clip = actual.id.name
//
if (text_for_clip == undefined || text_for_clip == '') {
//just clear the clip
console.log("CLEAR clip")
await arena_update_clip(clip.id, '')
continue;
}
//perform manupulators
text_for_clip = perform_manipulation(text_for_clip, clip);
await arena_update_clip(clip.id, text_for_clip)
}
}
}
let execute_pab_slide_triggers_timeout = []
let arena_scheduled_clips_clear_timeout = null;
async function execute_pab_slide(slide) {
if (arena_state != 'connected') {
console.error("Execute: Arena NOT connected")
return;
}
//reverse cycle on each slide
cycle = !cycle;
//
//console.log('execute_pab_slide', slide);
//now we need populate all clips according to their params
if (arena.length == 0) {
console.log("execute_pab_slide", "No clips")
return;
}
//
let clip
let layer
let text_for_clip = ''
let arena_scheduled_clips = []
let arena_scheduled_clips_clear = []
let actual
let same_layer_triger_protect = false
let update_count = 0;
let clear_count = 0;
let specific = 0
//
for (var i = 0; i < arena.length; i++) {
//console.log('LAYER %d\n', i)
//layers
layer = arena[i]
// disable protection for triggering on same page
same_layer_triger_protect = false
//
for (var x = 0; x < layer.length; x++) {
//slide
//console.log('\n\tclip %d', x)
clip = layer[x]
if (clip.params.pn || clip.params.pnc) {
//cc clip is not mented for slide content
continue;
}
if (clip.params.c) {
//clear clips are just for trigger so schedule and skip
arena_scheduled_clips_clear.push(clip)
continue;
}
//
if ((clip.params.a && cycle == true) || (clip.params.b && cycle == false)) {
//chedule for trigger
if (same_layer_triger_protect == false) {
//console.warn("Arena: Trigger scheduled")
arena_scheduled_clips.push(clip)
//enable protection
same_layer_triger_protect = true;
} else {
console.warn("Arena: Trigger schedule PROTECTION SKIP. FIX THIS IN ARENA!!! Layer = '%d' Clip = '%s'", i, clip.name)
//skip whole clip
continue;
}
}
// determine cycle
if (clip.params.a && cycle == false || clip.params.b && cycle == true) {
// if clip is in for oposite cylcle skip update
continue;
} else {
if (clip.params.ns) {
actual = slide.next
} else {
actual = slide.current
}
}
if (actual.txt == undefined || actual.txt == '') {
//just clear the clip
console.log("CLEAR clip")
update_count++
clear_count++
await arena_update_clip(clip.id, '')
continue;
}
// check if the clip wants specific segment
if (clip.params.box) {
//console.log("WANTED Specific segment", clip.params.box, actual.segments)
specific = parseInt(clip.params.box, 10) - 1
// specificky clip nikdy nedostane nespecificky slide/box
//if ((actual.segments == undefined && specific == 0) || (actual.segments && actual.segments.length == 0 && specific == 0)) {
// console.log("SET Specific segment DEFAULT")
// actual = actual
//} else
if (actual.segments && actual.segments[specific]) {
console.log("SET Specific segment SPECIFIC")
actual = actual.segments[specific]
} else {
// box is wanted but not present, clear clip
console.log("SET Specific segment CLEAR")
update_count++
clear_count++
await arena_update_clip(clip.id, '')
continue;
}
} else {
// clip do not want specific segment we beed
}
//
//default text
text_for_clip = actual.txt
//
if (clip.params.fw) {
//first word only
text_for_clip = actual.fw
} else if (clip.params.lw) {
//last word only
text_for_clip = actual.lw
}
//perform manupulators
text_for_clip = perform_manipulation(text_for_clip, clip);
if (text_for_clip && clip.params.sg && text_for_clip.length > clip.params.sg) {
console.warn("\n\n!!!TEXT FOR CLIP OVERFLOW !!!\n\n")
}
if (text_for_clip == undefined) {
console.warn("UNDEFINED TEXT", actual)
update_count++
clear_count++
await arena_update_clip(clip.id, '')
continue;
}
//update clip
update_count++
await arena_update_clip(clip.id, text_for_clip)
}
//because this is the break point for triggers we need to hide clear clips
if (arena_scheduled_clips.length > 0) {
execute_pab_slide_triggers(arena_scheduled_clips_clear, false) //false means disconnect
}
execute_pab_slide_triggers_timeout[i] = setTimeout(function (arg_clips, arg_timeout_index) {
clearTimeout(execute_pab_slide_triggers_timeout[arg_timeout_index])
execute_pab_slide_triggers(arg_clips)
}, 5, arena_scheduled_clips, i)
console.log("Arena: Triggers count=%d", arena_scheduled_clips.length)
arena_scheduled_clips = []
}
if (clear_count == update_count) {
console.warn("CONNECT ALL CLEAR CLIPS")
//execute_pab_slide_triggers(arena_scheduled_clips_clear)
arena_scheduled_clips_clear_timeout = setTimeout(function (arg_clips) {
clearTimeout(arena_scheduled_clips_clear);
execute_pab_slide_triggers(arg_clips)
}, 15, arena_scheduled_clips_clear)
}
//free the stack
arena_scheduled_clips_clear = []
console.log("Arena: Uptates count=%d, Clears count=%d", update_count, clear_count)
//
//execute_pab_slide_triggers(arena_scheduled_clips)
//
console.log("\n\n")
}
//nesmie byt async
function perform_manipulation(text_for_clip, clip) {
if (text_for_clip == undefined || text_for_clip == '') {
return text_for_clip
}
//
text_for_clip = text_for_clip.normalize('NFC').trim()
//
if (clip.params.pnc) {
text_for_clip = text_for_clip.replace(/^([\d]+)/g, "").trim();
}
if (clip.params.uc) {
//uppercase
text_for_clip = text_for_clip.toUpperCase()
} else if (clip.params.lc) {
//lowercase
text_for_clip = text_for_clip.toLowerCase()
} else if (clip.params.cp) {
//caps
text_for_clip = text_for_clip.toLowerCase().replace(/(^\w{1})|(\s+\w{1})/g, letter => letter.toUpperCase())
}
if (clip.params.re) {
//replace enter with space
text_for_clip = text_for_clip.replace(/[\r\n]/g, " ")
}
if (clip.params.rd && clip.params.rc) {
//trim dot and comma
text_for_clip = text_for_clip.replace(/(^(\.|\,)+)|((\.|\,)+$)/g, "")
} else {
if (clip.params.rd) {
//trim just dot
text_for_clip = text_for_clip.replace(/(^\.+)|(\.+$)/g, "")
}
if (clip.params.rc) {
//trim just comma
text_for_clip = text_for_clip.replace(/(^\.+)|(\.+$)/g, "")
}
}
if (clip.params.rv) {
//remove all numbering
text_for_clip = text_for_clip.replaceAll(/\@.*?\@/g, "");
} else {
// verses
let match = text_for_clip.matchAll(/\@(.*?)\@/g);
for (const m of match) {
//console.log(m)
let n = '';
for (var i = 0; i < m[1].length; i++) {
n = n + sup_array[parseInt(m[1][i], 10)]
}
//let regex = new RegExp('.*\{\{\{\{' + m[1] + '\}\}\}\}.*', 'g');
let regex = new RegExp('\@' + m[1] + '\@', 'g');
//console.log(regex)
text_for_clip = text_for_clip.replaceAll(regex, n + sup_array[10]);
//console.log(m[1], n, text_for_clip);
}
}
return text_for_clip
}
arena_connect()
propresenter_connect()
gun_connect()
let run_forever = null
run_forever = setInterval(function () {
//console.log("running")
}, 10000)