-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
624 lines (546 loc) · 22.4 KB
/
code.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
let globalSettings = {};
let websocket = null;
let pluginUUID = null;
let gotGlobalSettings = false;
let devices;
let currentlyBusy = false;
function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, inInfo) {
pluginUUID = inPluginUUID;
devices = JSON.parse(inInfo).devices.reduce(function (map, obj) {
map[obj.id] = obj;
return map;
}, {});
// Open the web socket
websocket = new WebSocket("ws://localhost:" + inPort);
function registerPlugin(inPluginUUID) {
let json = {
"event": inRegisterEvent,
"uuid": inPluginUUID
};
websocket.send(JSON.stringify(json));
};
websocket.onopen = function () {
// WebSocket is connected, send message
registerPlugin(pluginUUID);
};
websocket.onmessage = function (evt) {
// Received message from Stream Deck
let jsonObj = JSON.parse(evt.data);
let event = jsonObj["event"];
let action = jsonObj["action"];
let context = jsonObj["context"];
let jsonPayload = jsonObj["payload"] || {};
let settings = jsonPayload["settings"] || {};
let device = jsonObj["device"];
if (event == "keyDown") {
let settings = jsonPayload["settings"];
let coordinates = jsonPayload["coordinates"];
let userDesiredState = jsonPayload["userDesiredState"];
//get correct variable for id
let actionType = action.replace("io.predictionbuttons.", "");
let actionObj = onKeyDownActionSet[actionType];
if (actionObj) {
actionObj.onKeyDown(context, settings, coordinates, userDesiredState, device);
}
} else if (event == "keyUp") {
let settings = jsonPayload["settings"];
let coordinates = jsonPayload["coordinates"];
let userDesiredState = jsonPayload["userDesiredState"];
if (action == "io.predictionbuttons.start") {
startAction.onKeyUp(context, settings, coordinates, userDesiredState);
}
} else if (event == "willAppear") {
settings = jsonPayload["settings"];
let coordinates = jsonPayload["coordinates"];
switch (action) {
case "io.predictionbuttons.start":
requestGlobalSettings(pluginUUID);
break;
default:
//get correct variable for id
let actionType = action.replace("io.predictionbuttons.", "");
let actionObj = willAppearActionSet[actionType];
if (actionObj) {
actionObj.onWillAppear(context, settings, coordinates, device);
}
break;
}
} else if (event == "sendToPlugin") {
if (jsonPayload.hasOwnProperty("outcomeValue")) {
settings.outcomeNumber = jsonPayload.outcomeValue;
setSettings(context, settings);
} else if (jsonPayload.hasOwnProperty("predictionTitle")) {
settings.predictionTitle = jsonPayload.predictionTitle;
settings.outcomes = jsonPayload.outcomes;
settings.duration = jsonPayload.duration;
settings.profileSwap = jsonPayload.profileSwap;
setSettings(context, settings);
} else if (jsonPayload.hasOwnProperty("show")) {
showError();
}
} else if (event == "didReceiveGlobalSettings") {
logToFile("didReceiveGlobalSettings");
gotGlobalSettings = true;
globalSettings = jsonPayload.settings;
}
};
websocket.onclose = function () {
// Websocket is closed
};
};
//Auth
function recentlyAuthorised() {
if (globalSettings.expires_in == null) {
return false;
}
let expiryDate = new Date(globalSettings.expires_in);
logToFile("176 - " + expiryDate.toTimeString() + " " + new Date(Date.now()) + " " + expiryDate.getTime() + " " + Date.now());
if (expiryDate.getTime() > Date.now()) {
return true;
} else {
return false;
}
}
function checkAuthAndContinue(actionName, context, defaultPng, action) {
if (currentlyBusy == true) {
logToFile(`203 - ${actionName} - Currently Busy`);
return;
} else if (gotGlobalSettings) {
logToFile(`202 - ${actionName} - Not busy, got globals`);
if (recentlyAuthorised() == true) {
logToFile(`211 - ${actionName} - recently Auth'd`);
action.call();
} else {
logToFile(`218 - ${actionName} - not recently Auth'd`);
if (!globalSettings.broadcasterAccessToken) {
setAuthState(context, false);
} else {
updateButtonBusyState(context, true, defaultPng);
fetch("https://id.twitch.tv/oauth2/validate", {
headers: {
Authorization: "Bearer " + globalSettings.broadcasterAccessToken,
"Client-Id": "dx2y2z4epfd3ycn9oho1dnucnd7ou5",
"Content-Type": "application/json"
}
}).then(async (response) => {
if (!response.ok) {
//Do reauth flow iwht refresh token
if (globalSettings.broadcasterRefreshToken) {
logToFile(`213 - ${actionName} - ${response.status} ${response.statusText}`);
let success = await refreshToken();
if (success == true) {
updateButtonBusyState(context, false, defaultPng);
//do continue
action.call();
} else {
updateButtonBusyState(context, false, defaultPng);
//alert
showError(context);
setAuthState(context, false);
}
} else {
updateButtonBusyState(context, false, defaultPng);
//show error
logToFile(`234 - ${actionName} - ${response.status} ${response.statusText}`);
setAuthState(context, false);
}
} else {
updateButtonBusyState(context, false, defaultPng);
setAuthState(context, true);
action.call();
}
}).catch((error) => {
updateButtonBusyState(context, false, defaultPng);
logToFile(`286 - ${actionName} - ${error}`);
setAuthState(context, false);
});
}
}
} else {
logToFile(`299 - ${actionName} - Something terrible has happened.`);
setAuthState(context, true);
}
}
async function refreshToken() {
let authd = false;
if (globalSettings.broadcasterRefreshToken) {
logToFile("Refreshing token with " + globalSettings.broadcasterRefreshToken + " " + globalSettings.lastUpdated);
let tokens = await refreshAccessToken(globalSettings.broadcasterRefreshToken);
if (tokens.accessToken && tokens.refreshToken && tokens.expires_in) {
authd = true;
logToFile("got new tokens - " + tokens.accessToken + " - " + tokens.refreshToken + " - " + tokens.expires_in);
globalSettings.broadcasterAccessToken = tokens.accessToken;
globalSettings.broadcasterRefreshToken = tokens.refreshToken;
globalSettings.expires_in = new Date(Date.now() + (tokens.expires_in * 1000)).toISOString();
globalSettings.lastUpdated = new Date(Date.now()).toISOString();
saveGlobalSettings(pluginUUID);
} else {
logToFile("broadcasterRefreshToken refresh failed - missing values");
authd = false;
}
} else {
logToFile("broadcasterRefreshToken not found");
authd = false;
}
setAuthState(pluginUUID, authd);
return authd;
}
//API Actions
//TODO could build this into the settings to save time/effort?
function generateOutcomes(settings) {
let outcomesObj = [];
if (!settings.outcomes) {
settings.outcomes = ["YES", "NO"];
}
settings.outcomes.forEach(outcome => {
outcomesObj.push({ "title": outcome });
});
return outcomesObj;
}
function createPrediction(context, settings, deviceId, outcomesObj) {
//continue to create;
fetch("https://api.twitch.tv/helix/predictions", {
body: JSON.stringify({
"broadcaster_id": globalSettings.broadcasterId,
"title": settings.predictionTitle ?? "Will I RIP?",
"outcomes": outcomesObj,
"prediction_window": settings.duration ?? 120
}),
headers: {
Authorization: "Bearer " + globalSettings.broadcasterAccessToken,
"Client-Id": "dx2y2z4epfd3ycn9oho1dnucnd7ou5",
"Content-Type": "application/json"
},
method: "POST"
}).then((response) => {
if (response.ok) {
response.json().then((body) => {
//get prediction id
globalSettings.activePredictionId = body.data[0].id;
//Store outcomes, not individual ID's
globalSettings.activeOutcomes = body.data[0].outcomes;
globalSettings.activePredictionState = "ACTIVE";
saveGlobalSettings(pluginUUID);
//transition to new profile screen
if (settings.profileSwap != false) {
loadCorrectProfile(pluginUUID, devices[deviceId]);
}
});
} else {
showError(context);
}
}).catch((reason) => {
showError(context);
logToFile(reason);
});
}
function fireOffPrediction(context, settings, deviceId) {
logToFile("111 " + globalSettings.expires_in + " " + globalSettings.lastUpdated);
fetch("https://api.twitch.tv/helix/predictions?" + new URLSearchParams({
"broadcaster_id": globalSettings.broadcasterId,
}), {
headers: {
Authorization: "Bearer " + globalSettings.broadcasterAccessToken,
"Client-Id": "dx2y2z4epfd3ycn9oho1dnucnd7ou5",
"Content-Type": "application/json"
}
}).then(response => {
if (!response.ok) {
logToFile("315 - " + response.status);
throw new Error(response.status);
} else {
if (!response.ok) {
logToFile("fireOffPrediction - Response not ok - " + response.status + " - " + response.statusText);
showError(context);
} else {
response.json().then((body) => {
let outcomesObj = generateOutcomes(settings);
if (body.data) {
let lastPredictionData = body.data;
let lastPrediction = lastPredictionData[0];
if (lastPrediction.status === "ACTIVE" || lastPrediction.status === "LOCKED") {
logToFile("145 - Prediction active - " + lastPrediction.status);
if (lastPrediction.id != globalSettings.activePredictionId) {
//resume remote started prediction
globalSettings.activePredictionId = lastPrediction.id;
globalSettings.activeOutcomes = lastPrediction.outcomes;
globalSettings.activePredictionState = lastPrediction.status;
}
saveGlobalSettings(pluginUUID);
if (settings.profileSwap != false) {
loadCorrectProfile(pluginUUID, devices[deviceId]);
}
} else {
logToFile("144 - No active Prediction");
createPrediction(context, settings, deviceId, outcomesObj);
}
} else {
logToFile("143 - body.data = " + body);
createPrediction(context, settings, deviceId, outcomesObj);
}
}).catch((e) => {
logToFile("142" + e);
showError(context);
});
}
}
}).catch((e) => {
logToFile("141" + e);
showError(context);
});
}
function doConfirmAction(outcomeNumber, context, settings, deviceId) {
if (outcomeNumber < globalSettings.activeOutcomes.length) {
fetch("https://api.twitch.tv/helix/predictions", {
method: "PATCH",
body: JSON.stringify({
"broadcaster_id": globalSettings.broadcasterId,
"id": globalSettings.activePredictionId,
"status": "RESOLVED",
"winning_outcome_id": globalSettings.activeOutcomes[outcomeNumber].id
}),
headers: {
Authorization: "Bearer " + globalSettings.broadcasterAccessToken,
"Client-Id": "dx2y2z4epfd3ycn9oho1dnucnd7ou5",
"Content-Type": "application/json"
}
}).then(response => {
if (!response.ok) {
throw new Error(response.status);
} else {
//clean up settings
globalSettings.activePredictionId = undefined;
globalSettings.activeOutcomes = undefined;
saveGlobalSettings(pluginUUID);
//go back to default profile
returnToProfile(pluginUUID, devices[deviceId]);
}
}
).catch((error) => {
logToFile(error);
showError(context);
});
}
}
function doCancelAction(context, deviceId) {
fetch("https://api.twitch.tv/helix/predictions", {
method: "PATCH",
body: JSON.stringify({
"broadcaster_id": globalSettings.broadcasterId,
"id": globalSettings.activePredictionId,
"status": "CANCELED"
}),
headers: {
Authorization: "Bearer " + globalSettings.broadcasterAccessToken,
"Client-Id": "dx2y2z4epfd3ycn9oho1dnucnd7ou5",
"Content-Type": "application/json"
}
}).then(response => {
if (!response.ok) {
throw new Error(response.status);
} else {
//clean up settings
globalSettings.activePredictionId = undefined;
globalSettings.activeOutcomes = undefined;
globalSettings.activePredictionState = undefined;
saveGlobalSettings(pluginUUID);
//go back to default profile
returnToProfile(pluginUUID, devices[deviceId]);
}
}
).catch((error) => {
logToFile(error);
showError(context);
});
}
function doLockAction(context) {
if (globalSettings.activePredictionState === "ACTIVE") {
fetch("https://api.twitch.tv/helix/predictions", {
method: "PATCH",
body: JSON.stringify({
"broadcaster_id": globalSettings.broadcasterId,
"id": globalSettings.activePredictionId,
"status": "LOCKED"
}),
headers: {
Authorization: "Bearer " + globalSettings.broadcasterAccessToken,
"Client-Id": "dx2y2z4epfd3ycn9oho1dnucnd7ou5",
"Content-Type": "application/json"
}
}).then(response => {
if (!response.ok) {
throw new Error(response.status);
} else {
response.json().then((_) => {
setLockState(context, true);
});
}
}
).catch((error) => {
logToFile(error);
showError(context);
});
} else {
showError(context);
}
}
//Action Tools
//TODO need to support other SKU's
function loadCorrectProfile(context, device) {
switch (device.type) {
case 3:
loadProfile(context, device, "PredictionUiMobile");
break;
case 2:
loadProfile(context, device, "PredictionUiXL");
break;
case 1:
loadProfile(context, device, "PredictionUiMini");
break;
case 0:
loadProfile(context, device, "PredictionUi");
break;
default:
logToFile("Device type not found! - " + device.type);
break;
}
}
function getOutcomeNumberFromCoords(coordinates, deviceId) {
let outcomeNumber = 1;
let deviceType = devices[deviceId].type;
if (deviceType === 1) {
//streamdeck mini
//USes custom buttons
} else if (deviceType === 2) {
//XL Layout
switch (coordinates.row) {
case 1:
outcomeNumber = coordinates.column - 3;
break;
case 2:
outcomeNumber = (coordinates.column - 3) + 4;
break
case 3:
outcomeNumber = (coordinates.column - 4) + 8;
break;
}
} else {
//normal layout should suffice?
outcomeNumber = (coordinates.row == 1 ? 0 : 5) + coordinates.column;
}
return outcomeNumber;
}
function updateButtonBusyState(context, busyUpdate, defaultPng) {
currentlyBusy = busyUpdate;
if (busyUpdate == true) {
setImage(context, "art/predictionicons_wait.png");
} else if (busyUpdate == false) {
setImage(context, defaultPng);
} else {
logToFile("busyUpdate not booly - " + busyUpdate);
}
}
//Actions
let startAction = {
type: "io.predictionbuttons.start",
onKeyDown: function (context, settings, coordinates, userDesiredState, deviceId) {
checkAuthAndContinue("startAction", context, "art/predictionicons_start.png", () => { fireOffPrediction(context, settings, deviceId); })
},
onKeyUp: function (context, settings, coordinates, userDesiredState) {
if (!globalSettings.broadcasterAccessToken) {
setAuthState(context, false);
} else {
setAuthState(context, true);
}
},
onWillAppear: function (context, settings, coordinates, deviceId) {
updateButtonBusyState(context, false);
}
}
let outcomeCustomAction = {
type: "io.predictionbuttons.confirmOutcomeCustom",
onKeyDown: function (context, settings, coordinates, userDesiredState, deviceId) {
checkAuthAndContinue("outcomeCustomAction", context, "art/predictionicons_outcome.png", () => {
doConfirmAction(settings.outcomeNumber || 0, context, settings, deviceId);
});
},
onWillAppear: function (context, settings, coordinates, deviceId) {
updateButtonBusyState(context, false);
let outcomeNumber = settings.outcomeNumber || 0;
//check auth state, set state false if failed
if (gotGlobalSettings && outcomeNumber < globalSettings.activeOutcomes.length) {
let adjustedTitle = globalSettings.activeOutcomes[outcomeNumber].title.replace(/ /g, "\n");
//set the label with outcome text
setOutcomeState(context, 0);
setTitle(context, adjustedTitle);
} else {
setOutcomeState(context, 1);
setTitle(context, "");
}
}
};
let outcomeAction = {
type: "io.predictionbuttons.confirmOutcome",
onKeyDown: function (context, settings, coordinates, userDesiredState, deviceId) {
checkAuthAndContinue("outcomeAction", context, "art/predictionicons_outcome.png", () => {
doConfirmAction(getOutcomeNumberFromCoords(coordinates, deviceId), context, settings, deviceId);
});
},
onWillAppear: function (context, settings, coordinates, deviceId) {
updateButtonBusyState(context, false);
let outcomeNumber = getOutcomeNumberFromCoords(coordinates, deviceId);
//check auth state, set state false if failed
if (gotGlobalSettings && outcomeNumber < globalSettings.activeOutcomes.length) {
let adjustedTitle = globalSettings.activeOutcomes[outcomeNumber].title.replace(/ /g, "\n");
//set the label with outcome text
setOutcomeState(context, 0);
setTitle(context, adjustedTitle);
} else {
setOutcomeState(context, 1);
setTitle(context, "");
}
}
};
let cancelAction = {
type: "io.predictionbuttons.cancel",
onKeyDown: function (context, settings, coordinates, userDesiredState, deviceId) {
checkAuthAndContinue("cancelAction", context, "art/predictionicons_cancel.png", () => {
doCancelAction(context, deviceId);
});
},
onWillAppear: function (context, settings, coordinates, deviceId) {
updateButtonBusyState(context, false);
}
};
let exitAction = {
type: "io.predictionbuttons.exit",
onKeyDown: function (context, settings, coordinates, userDesiredState, deviceId) {
returnToProfile(pluginUUID, devices[deviceId]);
}
};
let lockAction = {
type: "io.predictionbuttons.lock",
onKeyDown: function (context, settings, coordinates, userDesiredState, device) {
//Check for if already locked
if (globalSettings.activePredictionState === "ACTIVE") {
//update button state
checkAuthAndContinue("lockAction", context, globalSettings.activePredictionState === "ACTIVE" ? "art/predictionicons_unlocked.png" : "art/predictionicons_locked.png", () => {
doLockAction(context);
});
} else {
logToFile("567 - globalSettings.activePredictionState != ACTIVE - " + globalSettings.activePredictionState);
}
},
onWillAppear: function (context, settings, coordinates, deviceId) {
updateButtonBusyState(context, false);
let currentLockState = (globalSettings.activePredictionState === "ACTIVE" ? false : true);
setLockState(context, currentLockState);
}
}
//Action Sets
let willAppearActionSet = {
"lock": lockAction, "confirmoutcome": outcomeAction, "confirmoutcomecustom": outcomeCustomAction, "start": startAction, "cancel": cancelAction,
};
let onKeyDownActionSet = {
"lock": lockAction, "start": startAction, "cancel": cancelAction, "exit": exitAction, "confirmoutcomecustom": outcomeCustomAction, "confirmoutcome": outcomeAction
};